ThinkGeo.com    |     Documentation    |     Premium Support

How to save a Layer to a Projection?

  In 2.x, there is a convenient way to save a Layer to a projection to avoid having to do projection on the fly. The layer has the function SaveToProjection.


Dim Layer As New Layer("...\MyLayer.shp")

Dim GeoToUTM17N As New GeodeticToUTM17N()


Layer.SaveToProjection("UTM17N", GeoToUTM17N , True)


 


    I could not find a such a simple API in 3.x. What would be the way to accomplish that task in 3.x? Are you planning on offering a simple API for that in the near future?


Thank you.


 


 


 



Adolfo, 
  
 Thanks for reminding. I have writen it down and we will discuss it. I believe we will add a similar API in the near future. 
  
 Now you can accomplish this with the following code. 
  
 
            string sourceShapeFile = @"C:\temp\Countries02.shp";
            string targetShapeFile = @"C:\temp\Countries02Test.shp";
 
            ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(sourceShapeFile);
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
            worldLayer.Open();
            Collection<Feature> allFeatures = worldLayer.QueryTools.GetAllFeatures(ReturningColumnsType.AllColumns);
            worldLayer.Close();
 
            ShapeFileFeatureSource.CloneShapeFileStructure(sourceShapeFile, targetShapeFile);
            ShapeFileFeatureSource targetFeatureSource = new ShapeFileFeatureSource(targetShapeFile, ShapeFileReadWriteMode.ReadWrite);
            targetFeatureSource.Open();
            proj4Projection.Open();
            targetFeatureSource.BeginTransaction();
            foreach (Feature feature in allFeatures)
            {
                Feature projectedFeature = proj4Projection.ConvertToExternalProjection(feature);
                targetFeatureSource.AddFeature(projectedFeature);
            }
            TransactionResult result = targetFeatureSource.CommitTransaction();
            proj4Projection.Close();
            targetFeatureSource.Close();
 
  
 Thanks, 
  
 Ben

Thank you, Ben. I used your code and I succesfully save a shapefile to UTM from an original shapefile in StatePlane. On my machine, on a 23 Meg shapefile, it took about 35 seconds so I guess this is decent performance.


 



Great! We are working on this and the API (static ShapeFileFeatureSource.SaveToProjection) will be available in the upcoming version. Keep letting us know your great ideas, Adolfo. :) 
  
 Ben.