ThinkGeo.com    |     Documentation    |     Premium Support

Projecting layers back to original projection

All my layers are going from GetEpsgParametersString(4326) to GetEpsgParametersString(3857). How do I project these back to the original?

Hi Dan,

I am confused about your question, if you want to project them back, you can just remove the assigned projection code.

Regards,

Ethan

Sorry! I thought I posted more info.

I looped through each FeatureLayer and reversed the Internal and External projections, and changed my MapUnits back to DecimalDegree as well. It seemed to project back properly, but I can’t zoom-in fully after, and the mouse to world coordinates is giving me strange numbers.

Here’s the code for projecting TO (in Map_Load) :

string mainShapeFilePath = @"../../AppData/Merge_Countries.shp";

Map.MapUnit = GeographyUnit.Meter;

Proj4Projection proj4Projection = new Proj4Projection();
proj4Projection.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(3857);

ShapeFileFeatureLayer.BuildIndexFile(mainShapeFilePath, BuildIndexMode.DoNotRebuild);
ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(mainShapeFilePath);
worldLayer.FeatureSource.Projection = proj4Projection;
worldOverlay.Layers.Add("World Layer", worldLayer);

graticuleAdornmentLayer.DrawingMarginInPixel = 50;
graticuleAdornmentLayer.Name = "Grid Layer";
graticuleAdornmentLayer.FeatureSource.Projection = proj4Projection;
gridOverlay.Layers.Add(graticuleAdornmentLayer);

Map.Overlays.Add("World Overlay", worldOverlay);
Map.Overlays.Add("Grid Overlay", gridOverlay);

Here’s the code for projecting BACK to the original:

Map.MapUnit = GeographyUnit.DecimalDegree;

proj4Projection.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(3857);
proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
proj4Projection.Open();
foreach (LayerOverlay overlay in Map.Overlays)
{
    foreach (FeatureLayer layer in overlay.Layers)
    {
        layer.FeatureSource.Projection = proj4Projection;
    }
}

graticuleAdornmentLayer.FeatureSource.Projection = proj4Projection;

Map.CurrentExtent = worldOverlay.Layers[0].GetBoundingBox();

Map.Refresh();

Attached is the main shape file I’m using:
testShapefile.zip (2.5 MB)

Here’s a screenshot after I project it the first time:

This is correct. Now here’s what it is when I project it back:

No matter where I pan or zoom to, it looks the same.

Hi Dan,

Thanks for your code and screen shots.

The problem is you don’t understand how our projection works.

When we set projection to a layer, it’s only works when the layer get rendered by map.

That means your data keep it’s original projection, in your sample that means your shape file always is 4326.

When you set new projection to the layer, it will cover the original one. So that’s why you met this issue.

The shape file is still be 4326, but you assign a new 3857->4326 to it, your projection thought it’s 3857 now. Of course it cannot get a valid result.

The solution is

layer.FeatureSource.Projection = null;

Or:

proj4Projection.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);

Please notice, the InternalProjectionParametersString should be always keep the same with original value to the data source.

Regards,

Ethan

Thanks! Setting it to null threw a bunch of errors, but your second suggestion seems to work great. Thanks again.

Hi Dan,

I am glad to hear that works.

Regards,

Ethan