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

Overlay geographic map over Google Maps

Topics: Italiano, SharpMap Project, SharpMap v0.9 / v1.x, WinForms Controls
May 25, 2011 at 3:37 PM
Edited May 25, 2011 at 3:54 PM


i'm new and I would love to know how I can overlap google/bing map on my territorial (geometric) map

I created my postgis DBMS from DXF file and try to do this:

var dataSourceParcels = new PostGIS(Properties.Settings.Default.PostGisConnectionString, "foglio31_plg", "the_geom", "gid");
var map = new SharpMap.Map();

var layParcels = new SharpMap.Layers.VectorLayer("parcels", dataSourceParcels);
layParcels.Style.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke);
layParcels.Style.Outline = System.Drawing.Pens.Gray;
layParcels.Style.EnableOutline = true;

TileLayer tileLayer = new TileLayer(new BingTileSource(BingRequest.UrlBing, "", BingMapType.Aerial), "TileLayer - Bing " + BingMapType.Aerial); 



but  the two maps does not overlap properly

someone can 'help me?

sorry for my english



May 26, 2011 at 8:26 AM
Edited May 26, 2011 at 1:59 PM

It sounds that you have mismatching coordinate systems.

If you use TileLayer, you almost certainly have the tiles in ESPG:3785 (Popular Vision/Web Mercator). If your vector data is not in that coordinate system, you need to transform it into that coordinate system.

You can do that on your PostGis table once

SELECT UpdateGeometrySRID('table', 'geom', 3785);
UPDATE "table" SET "geom" = ST_Transform("geom", 3875);

or you can setup on the fly coordiante transformation. Examples of how you can do that are in the WinFormSamples project.

Hth FObermaier

May 26, 2011 at 12:36 PM
Edited May 26, 2011 at 12:38 PM


1) UpdateSpatialSRID don't exist in PostGis this: updategeometrysrid? I dont know what is the 'table' ? and 'geom' is the geometry column?

about coordiante transformation on the fly I read this:

and i have read the trasformTest code in sharpmap project sources ...

2) I don't know how to set up source coordinate system for ESPG:3785 (better EPSG:32732) for my map...
 - CreateGeographicCoordinateSystem OR CreateProjectedCoordinateSystem ?
- i dont know How configure... do you can help me?

for destination I found this for bing/google geographic map... is ok?

CoordinateSystemFactory cFac = new CoordinateSystemFactory();

IGeographicCoordinateSystem target = cFac.CreateGeographicCoordinateSystem("WGS84", AngularUnit.Degrees,
                                                                                    new AxisInfo("Lon",
                                                                                    new AxisInfo("Lat",

var ctFac = new CoordinateTransformationFactory();
ICoordinateTransformation transform = ctFac.CreateFromCoordinateSystems(source, target);

//Apply transformation to a vectorlayer
(map.Layers[0] as VectorLayer).CoordinateTransformation = transform; 

3) map.Layers[0] is my layParcels as above ?

4) How I can find out exactly coordinate system in my post gis db?


sorry for my english

May 26, 2011 at 2:27 PM


  1. you are right, it should be UpdateGeometrySRID (I changed that above).
    table is the name of the table that has the geometry column. It is in single quotes since that function needs the name of the table as a string. If your table's name is all lowercase, you can omit the quotes.
    Same applies to geom, which is the name of the column in the table table that has the geometry stored in in.
    By looking in the documentation, UpdateGeometrySRID does not perform transformation. it only changes the SRID constraints. So maybe it is better to
    --Query geometry type
    SELECT GeometryType('geom') FROM "table" LIMIT 1;
    --Add new geometry column (assuming the above query returned 'POLYGON'
    SELECT AddGeometryColumn('table', 'geom3785', 3785, 'POLYGON', 2);
    -- Fill in the blanks
    UPDATE "table" set "geom3785" = ST_Transform("geom", 3785);
  2. You should setup coordinate transformation towards EPSG:3785, since you won't be able to transform the tiles.
    To use CoordinateSystemFactory is the way to go, but I'd choose the function that parses the wkt string (don't know the name out of the box). You can get the wkt from your postgis spatial_ref_sys table, it is in the srtext column.

Note you only need to do the one or the other, not both.

Hth FObermaier

May 26, 2011 at 2:49 PM
Edited May 26, 2011 at 2:50 PM


i run this

SELECT GeometryType(the_geom) FROM "parcel_plg" LIMIT 1;
SELECT AddGeometryColumn('parcel_plg', 'geom3785', 3785, 'MULTIPOLYGON', 2);

UPDATE "parcel_plg" set "geom3785" = ST_Transform("the_geom", 3785);

but the update statement retun me this error:

ERROR:  Input geometry has unknown (-1) SRID

********** Error **********

ERROR: Input geometry has unknown (-1) SRID
State SQL: XX000




May 26, 2011 at 3:07 PM

INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values (
   3785, 'EPSG', 3785, 
   '+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ', 
   'PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["Popular Visualisation CRS",DATUM["Popular_Visualisation_Datum",SPHEROID["Popular Visualisation Sphere",6378137,0,AUTHORITY["EPSG","7059"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6055"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4055"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],AUTHORITY["EPSG","3785"],AXIS["X",EAST],AXIS["Y",NORTH]]'

May 26, 2011 at 3:19 PM

sorry my spatial_ref_sys table contains the srid key...


ERROR:  duplicate key value violates unique constraint "spatial_ref_sys_pkey"
DETAIL:  Key (srid)=(3785) already exists.

********** Errore **********

ERROR: duplicate key value violates unique constraint "spatial_ref_sys_pkey"
Stato SQL: 23505
Dettaglio: Key (srid)=(3785) already exists.


maybe that my parcel_plg table not contain SRID? how I can do?

May 26, 2011 at 3:42 PM

you can look it up in the geometry_columns table

May 26, 2011 at 3:58 PM

i seen that srid column in the geometry_columns is set to -1

the parcel table derived from an dxf file imported in the db ...maybe that this dxf file is not georeferencied?

what i can do?


May 26, 2011 at 4:55 PM

dxf's are never referenced.

if you know the spatial reference system of the coordinates, you can simply apply it with UpdateGeometrySRID(...).

Otherwise, I think you are on your own

Cheers FObermaier

May 26, 2011 at 5:33 PM
Edited May 26, 2011 at 7:56 PM

ok i found my system coordinate 3068 and apply it

then run this UPDATE "parcel_plg" set "geom3785" = ST_Transform("the_geom", 3785);

and ok..

then i load the table with geom375 column

var dataSourceParcels = new PostGIS(Properties.Settings.Default.PostGisConnectionString, "parcel", "geom3785", "gid");

but dont show me the map and if I try to do this 

 var bbox = dataSourceText.GetExtents();
 dataSourceText.DefinitionQuery = string.Format("\"txt\" like '{0}'", "4043");
 var ds = new SharpMap.Data.FeatureDataSet();
 dataSourceText.ExecuteIntersectionQuery(bbox, ds);

I have this error: error: XX00 Operation on two geometries with differents SIDs

too much asked, and only your courtesy to answer...

May 30, 2011 at 12:21 PM

did you try map.ZoomToExtents()?

if it does not render the parcels layer, check if the coordinate transformation succeeds by dataSourceParcels.GetExtents();

For spatial operations to perform in postgis datasource and envelope need to be in the same spatial reference, that is do have the same SRID value.
ATM I'm not sure whether the bbox has an srid value at all. if not try setting it, and do file a bug.

Hth FObermaier