ThinkGeo.com    |     Documentation    |     Premium Support

Taking data from Database and mapping the data on a shape file on map

Salaam Alike;

I am developing a web application in asp.net MVC. I want to take data from database and map the data on a shape file on map. These data are related to provinces. The data retrieved from the Database will be divided into 5 ranges from the biggest to lowest. Five color will be used to the map the rages on shape file on map( one color for one range) and change the color of provinces.

I know how to use shape file and how to map the data of the .dbf file of shape file on map. But how can I take data from database and map it on shape file to show the coverage?

Hi,

I think your target is like this:

You have a shape file for render shapes on map, and you render their colors by the data of dbf file before, now you want to use the data from database instead of the data from dbf right? If I misunderstand you please let me know.

I think there are some ways for your problem:

  1. Create an InmemoryFeatureLayer, then get all features from your shape file and save them into your InmemoryFeaturelayer. At the same time, you can query from your database, and save the information into the feature’s column value in the InmemoryFeaturelayer. You can directly render them by ClassBreakStyle. This the simplest solution.

  2. Import the shape file into database, then use our database classes for example Sql2008FeatureLayer directly render it.

  3. Get data from database, then save them into a new shape file.

Wish they’re helpful.

Regards,

Don

Thank you Don for the response.
The scenario you noted above is right.
From performance point of view which one is better between the first and second options?

Hi RuWatSIP,

That’s decided with where is your database, if your database need get data via network, the first one should get better performance, because it query less data via network.

If your database is in local machine or LAN, I think the result should get close, but you should want to test by a small data and calculate their speed for make sure it.

Regards,

Don

Salaam Alike dear Don,

I went through your guide and chose the first option. I can render map with shape file in view but how to re-render the map through Jquery/Javascript to query data from database. My code samples are as below. Please guide me through the scenario.

// Controller Start
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThinkGeo.MapSuite;
using ThinkGeo.MapSuite.Drawing;
using ThinkGeo.MapSuite.Layers;
using ThinkGeo.MapSuite.Mvc;
using ThinkGeo.MapSuite.Styles;
using ThinkGeo.MapSuite.Portable;
using ThinkGeo.MapSuite.Serialize;
using ThinkGeo.MapSuite.Shapes;
using GIS_Test.Models;

namespace GIS_Test.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{

        return View();
    }
    [HttpPost]
    public Map LoadMap(FormCollection formdata)
    {
        // variable (formdata) will used later as database query condition
        WashDb_Entities db = new WashDb_Entities();
        // The variable (data) retrieves data from database and has three columns (ProvinceID, Province, Budget)

// The column (ProvinceId) could be used to join database data with shape file.
var data = db.GetProvinceBudget().ToList();
Map map = new Map(“Map1”,
new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage),
new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage));
map.MapBackground = new GeoSolidBrush(GeoColor.StandardColors.LightBlue);
map.CurrentExtent = new RectangleShape(6643042.96358, 4644924.705353, 8355232.396930, 3421932.252960);
map.MapUnit = GeographyUnit.DecimalDegree;

        GoogleOverlay googleoverlay = new GoogleOverlay();
        googleoverlay.JavaScriptLibraryUri = new Uri("https://maps.googleapis.com/maps/api/js?key=AIzaSyCmUdrvN5g-MInWQHMBP9nloldwCvjrs1Y&callback=myMap");
        googleoverlay.GoogleMapType = GoogleMapType.Hybrid;
        googleoverlay.IsBaseOverlay = true;
        map.CustomOverlays.Add(googleoverlay);
        InMemoryFeatureLayer provincelayer = new InMemoryFeatureLayer();
        // The shape file has the columns ProvinceID, and Prov_Name. The data could be join based on the column ProvinceID 
        provincelayer.InternalFeatures.Add("Province", new Feature(Server.MapPath("~/Layer/Province635563247814151837.SHP")));
        provincelayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(100, 212, 220, 184), GeoColor.FromArgb(255, 132, 132, 154), 1);
        provincelayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
        LayerOverlay MappedData = new LayerOverlay();
        MappedData.Layers.Clear();
        ClassBreakStyle classBreakStyle = new ClassBreakStyle();
        provincelayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(classBreakStyle);
        Proj4Projection proj4 = new Proj4Projection();
        proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
        proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();
        provincelayer.FeatureSource.Projection = proj4;
        MappedData.Layers.Add(provincelayer);
        map.CustomOverlays.Add(MappedData);
        return map;
    }
}

}
// Controller End

// View Start
@{
ViewBag.Title = “Home Page”;
}
@using ThinkGeo.MapSuite;
@using ThinkGeo.MapSuite.Drawing;
@using ThinkGeo.MapSuite.Layers;
@using ThinkGeo.MapSuite.Mvc;
@using ThinkGeo.MapSuite.Styles;
@using ThinkGeo.MapSuite.Shapes;
@using System.Configuration;


@{
Html.ThinkGeo().Map(“Map1”, new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 510)
.MapBackground(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF")))
.CurrentExtent(6643042.96358, 4644924.705353, 8355232.396930, 3421932.252960)
.MapUnit(GeographyUnit.Meter)
.MapTools(tools =>
{
tools.OverlaySwitcherMapTool().Enabled(true);
tools.MouseCoordinateMapTool().Enabled(true);
})
.CustomOverlays(overlays =>
{
overlays.GoogleOverlay(“Google Map”)
.JavaScriptLibraryUri(new Uri(“https://maps.googleapis.com/maps/api/js?key=AIzaSyCmUdrvN5g-MInWQHMBP9nloldwCvjrs1Y&callback=myMap”))
.GoogleMapType(GoogleMapType.Normal);
                    FeatureLayer layer = new ShapeFileFeatureLayer(Server.MapPath("~/Layer/Province635563247814151837.SHP"));
                    layer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(100, 212, 220, 184), GeoColor.FromArgb(255, 132, 132, 154), 1);
                    layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
                    Proj4Projection proj4 = new Proj4Projection();
                    proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
                    proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();
                    layer.FeatureSource.Projection = proj4;
                    overlays.LayerOverlay("Shape Overlay", false, TileType.SingleTile)
                        .Layer(layer);
                })
                .Render();
        }

@section Scripts{

<script type="text/javascript">
    $("body").on("submit", "#GISSearch", function () {
        $.ajax({
            type: 'POST',
            url: '/@ViewContext.RouteData.Values["Controller"].ToString()/GetGISMap',
            data: $("#GISSearch").serialize()
        })
        .done(function (data) {
            // Code to redraw map
        })
        .fail(function () {
            alert("Error");
        });
        return false;
    });
}

// I removed some HTML codes since they were rendered by browser and were not readable.
// View End

Hi RuWatSIP,

I think you misunderstand the option 1, it’s not modify the style in client side, but also need in server side.

So it looks your requirement is modified to dynamic change the style right?

Here I found a topic about it, and it contains a sample I think should be helpful if you want to dynamic change the style.

Please let me know if that’s helpful or not.

Regards,

Don

Hi Don,
There are 2 issues.

1: In (http://samples.thinkgeo.com/MvcEdition/HowDoISamples/), the samples which are worked in View, work for me too but the samples which are worked in controller do not work for me, noting happens when I run them.
as I worked this sample(http://samples.thinkgeo.com/MvcEdition/HowDoISamples/BackgroundMaps/DisplayASimpleMap/1,AllSamples), but noting happens. Neither it gives an error nor the map is shown in view.

2: I have installed MapSuiteWebForMvc-Standar as guided here (http://wiki.thinkgeo.com/wiki/map_suite_web_for_mvc_quick_start_guide#installing_thinkgeo_mvc_nuget_packages).
when referencing, the following codes give error
(using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.MvcEdition; )
when I edit it like this (using ThinkGeo.MapSuite;
using ThinkGeo.MapSuite.Mvc; ) then there is no error.

Do I have installed the wrong package?
Can the second issue be the reason for the first issue?

Hi RuWatSIP,

The namespaces are for our V9:
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.MvcEdition;

and the namespaces are for our V10:
using ThinkGeo.MapSuite;
using ThinkGeo.MapSuite.Mvc;

And the sample you found http://samples.thinkgeo.com/MvcEdition/HowDoISamples is for our V9.

Please download our V10 sample here: https://github.com/thinkgeo

And you can see we reset the namespaces, the common namespaces should be:
using ThinkGeo.MapSuite;
using ThinkGeo.MapSuite.Mvc;
using ThinkGeo.MapSuite.Drawing;
using ThinkGeo.MapSuite.Layers;
using ThinkGeo.MapSuite.Shapes;
using ThinkGeo.MapSuite.Styles;

The sample I referenced in the post is for old version, so you can just remove its original references, then add the new V10 packages from Nuget, and replace the “using” code. It should works well for V10, please modify the .net framework version to 4.5.

If you found any problem when build it, please download V10 sample to compare.

Wish that’s helpful.

Regards,

Don