ThinkGeo.com    |     Documentation    |     Premium Support

Reproject a shape using Proj4Projection

Hello,


The method ConvertToExternalProjection sends an AccessViolationException when I try to reproject a shape.


Here is a test code which use a shape created from WKT. I my real situation, shapes are loaded from a shapefile.


 




Proj4Projection proj4 = new Proj4Projection(); 
proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(2039); 
proj4.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326); 

BaseShape shape = BaseShape.CreateShapeFromWellKnownData(@"MULTIPOLYGON(((173061.835356474 635942.881484032,173133.905875921 635925.559813499,173110.707210779 635846.375037193,173103.283638 635847.921614647,173090.911016703 635806.164017677,173073.280031204 635805.236070633,173062.453987598 635806.78264904,173041.111215591 635812.659644127,173025.336123466 635820.083216667,173061.835356474 635942.881484032)))"); 

BaseShape shapeGeo = proj4.ConvertToExternalProjection(shape); 



Do I have something wrong? Do I forgot something?


Thanks


Nicolas 



Nicolas,  
  
 You need to open the projection object before you use it. The code should be like this: 
  
 … 
 proj4.Open(); 
 BaseShape shapeGeo = proj4.ConvertToExternalProjection(shape); 
 proj4.Close(); 
 … 
  
 The method proj4.Open() does some initialization work for the projection, which is a bit expensive. If you want to convert more than one shapes, it’s better to open it before the first conversion and close it after the last conversion to improve the performance. 
  
 Ben.

Thanks a lot Ben. 
 It works.

My pleasure. Let us know for more queries.