Load map from db

Topics: Algorithms
Mar 15, 2012 at 10: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: http://www.mediafire.com/?n0lvh7b5drrw435

Help me, thanks.

Mar 15, 2012 at 10:20 AM
Edited Mar 15, 2012 at 10: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 10:51 AM

i using shp2pgsql.exe of postgis.

this is screenshot: http://img338.imageshack.us/img338/8176/errorcx.jpg

and this is the result: http://img17.imageshack.us/img17/8357/error2oa.jpg

Mar 16, 2012 at 1: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 4: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 5: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 8: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 2:45 AM

I solved the problem. Thanks.

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