Heya.
Like the subject asks... can I open a shape file... change the projection and save it in the new projection to disk?
Regards,
Brendan
Heya.
Like the subject asks... can I open a shape file... change the projection and save it in the new projection to disk?
Regards,
Brendan
Brendan,
This is an easy job for MapSuite component.
Following is the code, simple enough, enha?
Proj4Projection proj4Projection = new Proj4Projection();
// From Projection and To Projection set.
proj4Projection.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();
string sourceShapeFile = @"C:\temp\cntry02.shp";
string targetShapeFile = @"C:\temp\cntry02Google.shp";
ShapeFileFeatureLayer.SaveToProjection(sourceShapeFile, targetShapeFile, proj4Projection, OverwriteMode.DoNotOverwrite);
Thanks for your post, any more questions just let me know.
Thanks.
Yale
Welcome to the forum Yale. Nice to see new people from ThinkGeo answering questions:P
"ShapeFileFeatureLayer.SaveToProjection" was what i was missing. Much appreciated.
Welcome, Brendan.
Hope much more communications later.
Thanks.
Yale.
Ok, i've tried the conversion of a 390MB shape file on my laptop (3GB RAM)... I get an Out of Memory exception after the conversion saps up all of my available RAM.
I then tried to run the same code in a compiled app on our Win2008 server (64bit) with 8GB RAM... that should do the trick! No luck.. I get an exception "The FeatureSource is not in a transaction." Is this a 64bit related erorr?
It seems that the conversion loads the whole shape file into memory... then doubles that memory by converting the shape file in-memory... and then only writes. The result is that I don't think that I can convert this file any time soon:/ Is there a way to do this bit by bit?
I've added a timer that cleans up the memory "GC.Collect() " every few seconds and the RAM usage seems to be stable at around 670MB for the conversion app.
I'm not sure what's going on in the conversion logic, but it'll be a bit more lean if you add GC.Collect() after dumping large collections that are no longer used. The standard .Net memory management does not clear the memory nearly often enough when reading through large files.
Edit: The conversion has completed on my notebook. The GC.Collect() fixed the RAM issue.
Brendan,
Thanks for your sharing, Brendan.
I checked the code for the logic; it did will read all the data into memory and then start to do projection one by one. I think this will probably cause the out of memory problem when the shape file is a really large one.
I already add this to our issue tracking system, hopefully in next version, we can enhance it.
Thanks again!
Yale.
Hello,
First off, thanks for the initial post and the reply with the SaveToProjection method. I came looking for direction on solving a problem a different way (after trying many things) and this post was right on the first page and it turned out to be the quickest solution (part of a solution, actually) to something I've been working on for a few days!
However, the Overwrite mode parameter doesn't seem to have any effect. I obviously was able to get around this, but in a tool, I need this to work. If the file exists, whichever way I set that parameter, I always get the exception that the file already exists.
Thanks!
Kimberly
Kimberly,
Thanks for your post. We already know the problem of Override and we will fix it soon.
Currently, I suggest you just delete the files manually if you just want to override it.
Thanks for your post and bug report.
Yale.
Yale,
I’ve an issue here with ShapeFileFeatureLayer.SaveToProjection.
I need to Save a layer that is currently opened and displayed in an overlay; it’s opened in read/write mode.
With MS2, I was able to save it
With MS3, I’m getting an exception “file already opened in another thread”
How can I save an opened layer with MS3 ?
Patrick.
Patrick,
I think you have to close the layer before calling the SaveToProjection API.
private void button2_Click(object sender, EventArgs e)
{
Layer layer = winformsMap1.FindFeatureLayer("ShapeFileLayer");
if (layer.IsOpen)
{
layer.Close();
}
Proj4Projection proj4Projection = new Proj4Projection();
proj4Projection.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();
string targetShapeFile = @"C:\temp\cntry02Google.shp";
ShapeFileFeatureLayer.SaveToProjection(sourceShapeFile, targetShapeFile, proj4Projection, OverwriteMode.Overwrite);
}
Attachment contains a demo for this.
Let me know if you have any more questions.
Thanks.
Yale
1122-ThreadSaveToProjection.zip (14.7 KB)