Contour and heatmap

Topics: Data Access, SharpMap v0.9 / v1.x
Dec 16, 2013 at 1:20 PM
Hi,

I am using SharpMap from svn trunk (is it the latest ?).

I have downloaded some contour data using this tutorial, with some OSM data (using GeoFabrik.

I would like to display contours data : both lines and "HeatLayer".

How can I achieve this ?

Thanks.
Coordinator
Dec 17, 2013 at 6:49 AM
The latest stable code is in Branches/v1.0.

To add the contour lines you need a SharpMap.Data.Postgis provider (SharpMap.Extensions) and a SharpMap.Layers.VectorLayer.
To add the heat layer, you need a provider to puntal data and the SharpMap.Layers.HeatLayer (separate project). How to set that up is shown in the ExampleCodeSnipplets project and one of the TileLayer samples in the WinformSamples project has an example, too
Marked as answer by maitredede on 12/18/2013 at 4:44 AM
Dec 17, 2013 at 8:58 AM
Hi,

Thanks for your answers : I have my roads with height contours.

Related code :
 private static void AddRoads(Map map, string layer_name)
        {
            string pgcs = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myGis"].ConnectionString;
            Npgsql.NpgsqlConnectionStringBuilder csb = new Npgsql.NpgsqlConnectionStringBuilder(pgcs);
            var pgsql_lines = new SharpMap.Data.Providers.PostGIS(csb.ConnectionString, "planet_osm_line", "way", "osm_id");

            VectorLayer local_osm_line = new VectorLayer(layer_name);
            pgsql_lines.DefinitionQuery = "highway IS NOT NULL";
            local_osm_line.DataSource = pgsql_lines;
            local_osm_line.SmoothingMode = SmoothingMode.AntiAlias;
            var cls = new SharpMap.Rendering.Symbolizer.CachedLineSymbolizer();
            cls.LineSymbolizeHandlers.Add(new SharpMap.Rendering.Symbolizer.PlainLineSymbolizeHandler
            {
                Line = new Pen(System.Drawing.Color.Blue, 5),
            });
            cls.LineSymbolizeHandlers.Add(new SharpMap.Rendering.Symbolizer.PlainLineSymbolizeHandler
            {
                Line = new System.Drawing.Pen(System.Drawing.Color.Green, 3)
            });
            local_osm_line.Style.LineSymbolizer = cls;
            map.Layers.Add(local_osm_line);
        }

 private static void AddContours(Map map, string layer_name)
        {
            string pgcs = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myGis"].ConnectionString;
            Npgsql.NpgsqlConnectionStringBuilder csb = new Npgsql.NpgsqlConnectionStringBuilder(pgcs);
            csb.Database = "aster_gdem";
            var pgsql_heighmap = new SharpMap.Data.Providers.PostGIS(csb.ConnectionString, "contours", "geometry", "gid");
            SharpMap.Layers.VectorLayer local_osm_height = new VectorLayer("local_osm_height");
            local_osm_height.DataSource = pgsql_heighmap;
            local_osm_height.SmoothingMode = SmoothingMode.AntiAlias;

            var cls = new SharpMap.Rendering.Symbolizer.CachedLineSymbolizer();
            cls.LineSymbolizeHandlers.Add(new SharpMap.Rendering.Symbolizer.PlainLineSymbolizeHandler
            {
                Line = new Pen(System.Drawing.Color.Brown, 1),
            });
            local_osm_height.Style.LineSymbolizer = cls;

            var ctfac = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            local_osm_height.CoordinateTransformation =
                ctfac.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84,
                                                  ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator); 
            
            map.Layers.Add(local_osm_height);
        }
However, when I have a strange behavior : if I call these methods in this order :
            AddRoads(map, "local_osm_line");
            AddContours(map, "local_osm_height");
it works. But in this order :
            AddContours(map, "local_osm_height");
            AddRoads(map, "local_osm_line");
I have an error :
<ServiceException code="InvalidCRS">CRS not supported</ServiceException>
I am using nuget version of SharpMap (1.0.4.1) SharpMap.Extensions (1.0.4) SharpMap.Web (1.0.4), I have looked at SharpMap.Demo.Wms
Dec 17, 2013 at 9:06 AM
Then I have tried to use heatmap :
private static void AddHeatmap(Map map, string layer_name)
        {
            string pgcs = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myGis"].ConnectionString;
            Npgsql.NpgsqlConnectionStringBuilder csb = new Npgsql.NpgsqlConnectionStringBuilder(pgcs);
            csb.Database = "aster_gdem";
            var pgsql_heighmap = new SharpMap.Data.Providers.PostGIS(csb.ConnectionString, "contours", "geometry", "gid");

            var layer = new SharpMap.Layers.HeatLayer(pgsql_heighmap, "height", 1 / 4800f) { LayerName = layer_name };

            var ctfac = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            layer.CoordinateTransformation =
                ctfac.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84,
                                                  ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);

            layer.ZoomMin = 0;
            layer.ZoomMax = 0.9; //map.GetExtents().Width;

            map.Layers.Add(layer);
        }
If I omit ZoomMin and ZoomMax, the layer try to calculate zoom by requesting the extent of available data. It goes timeout (11G of data, even with an index).

Then I have an exception : "Destination image is smaller than requested dimentions", when code tries to call "ImageBlender.BlendImages".

The HeatMap code is taken from SVN Branche/1.0 (stable).
Coordinator
Dec 17, 2013 at 10:45 AM
Edited Dec 17, 2013 at 10:46 AM
Do you want a HeatMap or a shaded relief? I assume the later. Use GDAL Raster layer for that.
SharpMap's HeatLayer is a layer to visualize gravity of incidents.
Image
Dec 17, 2013 at 11:10 AM
You are right : shaded relief.

I have already imported data using gdal into postgis to have contours. Can't I take these data into gdal's layer ?
Coordinator
Dec 17, 2013 at 12:48 PM
If I understand the documentation you refer to correctly, you have constructed vector data from the DEM encoded in your ASTER_GDEMv2 file/directory.
To get the shaded relief, you need to transform the vector data to raster again. I suppose that is very expensive and not implemented in sharpmap.

Depending on your PostGIS server, you might be able to import the DEM to a postgis raster. However, even then you'll need GdalRasterLayer.
Dec 17, 2013 at 1:03 PM
Edited Dec 17, 2013 at 1:03 PM
From the tuto, I have downloaded a bunch of zip (containing *dem.tif files), and the script converted them to shapefiles (gdal_contour) and to postgis (shp2pgsql).

So if I understand you, I have to use the *dem.tif files as datasource for the gdal layer ? How can I achieve this ?

Thanks :)
Coordinator
Dec 17, 2013 at 2:45 PM
GdalRasterLayer takes the filename as constructor argument.