ThinkGeo.com    |     Documentation    |     Premium Support

Save the InMemoryFeatureLayer as Shape File

Hi,


InMemoryFeatureLayer provides for us different abilities like Highlighting, Moving, Shortest line between features,adding polygon/multiline/kine/...  to it and ... .


My question is , What's the way to save the InMemoryFeatureLayer as shape file.I tried to return all rows and columns of InMemoryFeatureLayer (Like the way that we convert TabFile to ShapeFile) and then save them as shape file, but I couldn't.


 


Do you have any sample,... for this issue?


 


Regards,


Ben



Ben,


Thanks for your post.
 
I think the saving data from InMemoryFeatureLayer to shape file is very similar to saving data from TabFile to shape file. Both of them need following steps:
1) Create a target empty shape file.( ShapeFileFeatureLayer.CreateShapeFile)
 
2) Get data from source like TabFile or InMemoryFeatureLayer.( inMemoryLayer.QueryTools.GetAllFeatures)
 
3) Save those data into the empty shape file.
inMemoryLayer.Open();
inMemoryLayer.EditTools.BeginTransaction();
// Add features.
inMemoryLayer.Close();
TransactionResult result = inMemoryLayer.EditTools.CommitTransaction();
 
Any more questions just let me know.
 
Thanks.
 
Yale

I used the below code to create a InMemoryFeatureLayer: 
wpfMap1.MapUnit = GeographyUnit.Meter;

wpfMap1.CurrentExtent = new RectangleShape(0, 100, 100, 0);
wpfMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.StandardColors.White);

InMemoryFeatureLayer inMemoryLayer = new InMemoryFeatureLayer();
inMemoryLayer.InternalFeatures.Add("Polygon", new Feature(BaseShape.CreateShapeFromWellKnownData("POLYGON((10 60,40 70,30 85, 10 60))")));
inMemoryLayer.InternalFeatures.Add("Multipoint", new Feature(BaseShape.CreateShapeFromWellKnownData("MULTIPOINT(10 20, 30 20,40 20, 10 30, 30 30, 40 30)")));
inMemoryLayer.InternalFeatures.Add("Line", new Feature(BaseShape.CreateShapeFromWellKnownData("LINESTRING(60 60, 70 70,75 60, 80 70, 85 60,95 80)")));
inMemoryLayer.InternalFeatures.Add("Rectangle", new Feature(new RectangleShape(65, 30, 95, 15)));
inMemoryLayer.InternalFeatures.Add("Ellipse", new Feature(new EllipseShape(new PointShape(50, 50), 10, 10)));

inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.FillSolidBrush.Color = GeoColor.FromArgb(100, GeoColor.StandardColors.RoyalBlue);
inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.Color = GeoColor.StandardColors.Blue;
inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.FromArgb(200, GeoColor.StandardColors.Red), 5);
inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.SymbolPen = new GeoPen(GeoColor.FromArgb(255, GeoColor.StandardColors.Green), 8);
inMemoryLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

LayerOverlay staticOverlay = new LayerOverlay();
staticOverlay.Layers.Add("InMemoryFeatureLayer", inMemoryLayer);
wpfMap1.Overlays.Add("InMemoryOverlay", staticOverlay);

wpfMap1.Refresh(); 

 Now, when I want to save it as ShapeFileFeatureLayer : 
ShapeFileType.Polygon, targetShapeFilePath, dbfColumns, System.Text.Encoding.UTF8, OverwriteMode.Overwrite);
 So ,I need the ShapeFileType and columns ,therefore I opened the InmemeoryFeature Layer and try to read all of its columns : 
Collection<FeatureSourceColumn> allColumns = inMemoryLayer.QueryTools.GetColumns();
 But it does not return for me any columns' Info. 


And what's the type of new shape file?



Ben,


Thanks for your post and code; it let me understand the problem quickly.
 
Yes, when you create your inmemoryFeatureLayer, you did not add any column information, so when you query out, it returns nothing. So, it depends on whether you want to store any information into dbf when you save it to shape file.
 
Following is a simple code snippet for you to add feature columns and add columns information when you create features:

Collection<FeatureSourceColumn> fatureSourceColumns = new Collection<FeatureSourceColumn>();
fatureSourceColumns.Add(new FeatureSourceColumn("Name","string",50));
fatureSourceColumns.Add(new FeatureSourceColumn("Area","double",10));
InMemoryFeatureLayer inMemoryLayer = new InMemoryFeatureLayer(fatureSourceColumns,new Feature[]{});
 
Feature feature1 = new Feature(BaseShape.CreateShapeFromWellKnownData("POLYGON((10 60,40 70,30 85, 10 60))"));
feature1.ColumnValues.Add("Name", "CHINA");
feature1.ColumnValues.Add("Area", "100000000");

 
About the feature type, we make this standards according to OGC specification, one shape file can only contains one shape file type. You can choose it from the list of the enumeration.
 
Any more questions just feel free to let me know.
 
Thanks.
 
Yale

cool, It works perfectly!

Ben, 
  
 Thanks for letting me know your status. 
  
 Any more questions just feel free to let me know. 
  
 Thanks. 
  
 Yale