1
votes

I have a requirement to build a web application with geo-mapping that will display pins on a map and the data comes from my sql 2005 database. This intranet application will be hosted on our secure server and will not have access to any outside resources and cannot use any external api's.

After searching on the internet, I found that I can use leaflet.js and mapbox to handle this although I have to move all of the files locally. I also need to create a custom map with TileMill and then copy the tiles to my applications. I tried to create my custom map but I cannot create any layers because it is asking for a datasource to a csv file or sqllite and I need to pass the data from my sql 2005 database. Is it possible to create a custom map with tilemill and provide my own datasource? Am I going about this the right way? Is there a better way to create a geo-mapping application that has to be accessed locally? Thanks

2

2 Answers

1
votes

Rob, I am sure there is a better way to do this but this is the way I implemented it.

The solution I am about to give you should allow you to add markers to a Mapbox map (search "omnivore mapbox" for web application CSV import equivalent of TileMill add data source feature) that can be accessed through your web application and that gets its data from a CSV file generated from a GridView connected to a MS SQL database.

I had at the same problem, data is in SQL 2008 and I need to use it to create a map. Mapbox required CSV or POSTGRESQL to display pins but I did not have a POSTGRESQL server setup anywhere. Here is what I did: (note that I am using C# and creating a ASP NET web application)

  • Create an empty ASPX page, let's call it DataGridView.aspx
  • Add a GridView
  • Link that GridView to your SQL data source
  • Create a button, call it "Refresh CSV"
  • Double click on it to automatically create an event for that button.

In the code for the event for that button, use this piece of code:

protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GridView1.DataBind();

        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            if(k==0) // little trick to avoid comma at the end of the line
            {
                sb.Append(GridView1.Columns[k].HeaderText);
                continue;
            }

            //add separator 
            sb.Append(',' + GridView1.Columns[k].HeaderText);
        }
        //append new line 
        sb.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            for (int k = 0; k < GridView1.Columns.Count ; k++)
            {
                if (k == 0) // little trick to avoid comma at the end of the line
                {
                    sb.Append(GridView1.Rows[i].Cells[k].Text);
                    continue;
                }

                //add separator 
                sb.Append(','+GridView1.Rows[i].Cells[k].Text );
            }
            //append new line 
            sb.Append("\r\n");

        }

        string a= string.Empty;
        StreamWriter yourStream = File.CreateText(Server.MapPath("~/CSVExport.csv"));
        a = sb.ToString()+"\r\n"; //formatting text based on poeition
        yourStream.Write(a);                        
        yourStream.Close();
     } 

Don't forget to have these at the beginning of your DataGridView.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

On the root of your web application, create an empty file, name it "CSVExport.csv".

This is going to hold your GridView Data formatted as a CSV.

Now every time you click that button, it should update your CSVExport.csv file.

To check if everything displays correctly, launch TileMill and run (debug run locally) your website.

Go to [localhost:whateverthatportnumberis]/DataGridView.aspx

Click on the button. Open CSVExport.csv to check if your file has been updated (you should see your data in CSV file format.)

Now, without stopping your local website test, go ahead and use the link to the CSV file in TileMill [localhost:whateverthatportnumberis]/CSVExport.csv

If your CSVExport.csv file is formatted correctly, you should see the markers being displayed. This should answer your question on how to get your data from SQL to a CSV format that can be used by Mapbox. Your question becomes ambiguous here when you say: " Is there a better way to create a geo-mapping application that has to be accessed locally?". So I am going to stop here.

Again, the solution I just gave you should allow you to add markers to a Mapbox map (search "omnivore mapbox" for web application CSV import equivalent of TileMill add data source feature) that can be accessed through your web application and that gets its data from a CSV file generated from a GridView connected to a MS SQL database.

0
votes

One way is to create your custom map in TileMill, without the data in your SQL 2005 database, and to export it as a mbtiles file which you can afterwards host on mapbox.

Use this map as the baselayer in leaflet.js, and add your markers dynamically loaded via AJAX from a REST API accessing your SQL database.