This project has moved and is read-only. For the latest updates, please go here.

Load map from db

Topics: Algorithms
Mar 15, 2012 at 11:07 AM

Dear All!

I am using Sharpmap v0.9. I have a shp file and loaded the Postgis database, but i cant display it on mapbox control. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;

namespace PMISMaps
    public static class clsPostgis2
        public static string PostGisConnectionString = "Server=localhost;Port=5432;Database=postgis_sample;uid=postgres;password=123456;";

        public static SharpMap.Map InitializeMap(float angle)
            //Initialize a new map of size 'imagesize'
            SharpMap.Map map = new SharpMap.Map();            
            SharpMap.Layers.VectorLayer layHuyen_camlam = new SharpMap.Layers.VectorLayer("huyen_camlam");                        
            layHuyen_camlam.DataSource = new SharpMap.Data.Providers.PostGIS(PostGisConnectionString, "huyen_camlam", "gid");
            //Set fill-style to green
            layHuyen_camlam.Style.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
            //Set the polygons to have a black outline
            layHuyen_camlam.Style.Outline = System.Drawing.Pens.Black;
            layHuyen_camlam.Style.EnableOutline = true;         

            //Add the layers to the map object.
            //The order we add them in are the order they are drawn, so we add the rivers last to put them on top

            //limit the zoom to 360 degrees width
            map.MaximumZoom = 360;
            map.BackColor = System.Drawing.Color.LightBlue;

            map.ZoomToExtents(); // = 360;
            map.Center = new SharpMap.Geometries.Point(0, 0);

            Matrix mat = new Matrix();
            mat.RotateAt(angle, map.WorldToImage(map.Center));
            map.MapTransform = mat;

            return map;


here shp file:

Help me, thanks.

Mar 15, 2012 at 11:20 AM
Edited Mar 15, 2012 at 11:21 AM

how to convert this shp to postgis? if you use shp2pgsql.exe of postgis. Please try this code:

layHuyen_camlam.DataSource = new SharpMap.Data.Providers.PostGIS(PostGisConnectionString, "the_geom", "gid");
Mar 15, 2012 at 11:51 AM

i using shp2pgsql.exe of postgis.

this is screenshot:

and this is the result:

Mar 16, 2012 at 2:22 AM

Link to screeenshot not work.

Try add geometry column when new a postgis datasouce. Example:

layHuyen_camlam.DataSource = new SharpMap.Data.Providers.PostGIS(PostGisConnectionString, "huyen_camlam","the_geom", "gid");
Mar 16, 2012 at 5:14 AM

I tried add geometry column when new a postgis datasouce:

layHuyen_camlam.DataSource = new SharpMap.Data.Providers.PostGIS(PostGisConnectionString, "huyen_camlam", "the_geom", "gid");


table after the import shp file:

CREATE TABLE huyen_camlam
  gid serial NOT NULL,
  elevation smallint,
  the_geom geometry,
  CONSTRAINT huyen_camlam_pkey PRIMARY KEY (gid ),
  CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
  CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'MULTILINESTRING'::text OR the_geom IS NULL),
  CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326)
ALTER TABLE huyen_camlam
  OWNER TO postgres;
screenshot when i using shp2pgsql.exe of postgis

I dont know why it can not display the map.

Mar 16, 2012 at 6:27 AM

wow, there are a bug in your code . Please remove line;

 map.Center = new SharpMap.Geometries.Point(0, 0);

after line:

B/c: after you zoom to your map , you set center to 0,0. You can not see your map
Mar 16, 2012 at 9:06 AM

i removed the line:

map.Center = new SharpMap.Geometries.Point(0, 0);

but still can not display the map. Why?
Mar 17, 2012 at 3:45 AM

I solved the problem. Thanks.

Aug 11, 2013 at 1:54 AM
I have the same problem
Aug 26, 2014 at 5:35 PM
As you solved your problem please share it for others.