Setting MapSuite FeatureLayer projection during runtime

When working with ThinkGeo’s MapSuite, there may be times when you need to set a FeatureLayer’s projection dynamically at runtime.  Say for example, you loaded a Shapefile whose projection is WGS84 or Spatial Reference System Identifier (SRID) 4326 onto the map currently using OpenStreetMap (OSM) as the base map.  OSM’s SRID is 3857.

If the Shapefile had an associated projection file, you would have used the code similar to the following in order to create a projection for the ShapeFile’s FeatureSource:

 var file = Path.GetFileNameWithoutExtension(filename);
 var prjFile = Path.Combine(Path.GetDirectoryName(filename), file + ".prj");
 var proj4 = new Proj4Projection();
 string prjFileText = File.ReadAllText(prjFile); //get the text of the .prj file.
 proj4.InternalProjectionParametersString = Proj4Projection.ConvertPrjToProj4(prjFileText); //set the internal projection. 

Now, if you know the SRID of the current basemap, and let us assume in this case, it is 3857 for OSM, this is also the best time to set it as follows:

var proj4 = new Proj4Projection();
proj4.ExternalProjectionParametersString= Proj4Projection.GetEpsgParametersString(3587)

before assigning the projection object to the FeatureSource as follows:

shapeFileLayer.FeatureSource.Projection = proj4;

If you added the layer to the map first before setting the external projection parameters of its feature source…. so you did something like this:

overlay.Layers.Add(layerName, layer);
var featureLayer = layer as FeatureLayer;
 if (featureLayer != null)

where in EstablishLayerProjection, you are basically doing something like this

var projection = featureLayer.FeatureSource.Projection as Proj4Projection;
if (projection != null)
   projection.ExternalProjectionParametersString = CurrentProjection.ExternalProjectionParametersString;

you will run into problems.  So it is best to establish both the projection’s internal and external parameters before assigning it to a layer.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s