Hi ThinkGeo Support,
I am pretty new to Map Suite 4.0 Geometry and would like to have some sample codes for saving a created polygon or any other shapes with the API. Could you please give me some pointer where to find it. Thanks.
Franklin
Hi ThinkGeo Support,
I am pretty new to Map Suite 4.0 Geometry and would like to have some sample codes for saving a created polygon or any other shapes with the API. Could you please give me some pointer where to find it. Thanks.
Franklin
Franklin,
Thanks for your post and questions.
I think it depends on how you want to save the target polygon, for example, if you want to save it into a well-known text(wkt) string or well-known byte(wkb), try use the following API:
string wkt = polygon.GetWellKnownText();
byte[] wkb = polygon.GetWellKnownBinary();
Also, we can use the APIs Edit tools to save the target polygon into Shape or SQL server, following is the code snippet you can take a reference.
ShapeFileFeatureLayer shapeFileFetureLayer = new ShapeFileFeatureLayer(shapeFile, ShapeFileReadWriteMode.ReadWrite);
try
{
shapeFileFetureLayer.Open();
shapeFileFetureLayer.EditTools.BeginTransaction();
shapeFileFetureLayer.EditTools.Add(polygonShape);
TransactionResult result = shapeFileFetureLayer.EditTools.CommitTransaction();
}
finally
{
shapeFileFetureLayer.Close();
}
Any more questions please feel free to let me know.
Thanks.
Yale
Hi Yale,
Thanks alot. If i want to save the polygon in text or byte under certain name and then later want to retrieve and display it again, what would be the code like?
Franklin
Franklin,
Try following code snippet, hope it helps:
string wkt = polygon.GetWellKnownText();
PolygonShape newPolygon1 = (PolygonShape)BaseShape.CreateShapeFromWellKnownData(wkt);
byte[] wkb = polygon.GetWellKnownBinary();
PolygonShape newPolygon2 = (PolygonShape)BaseShape.CreateShapeFromWellKnownData(wkb);
Any more questions please feel free to let me know.
Thanks.
Yale
Hi Yale,
Say, I have created a polygon using the sample application and now I want to save it. In which event handler should i use the above text or binary save method? I have same question in the scenario of having completed editing a existing polygon.
Either the text or binary save method is not persistent when the current session is over and the application is restarted.
How would you make the saved polygons reappear in the exact locations on the map when the application is restarted?
I find it difficult to find sample code for MapSuite Desktop Edition 4.0 GeoAPI for persisting zone shapes(created or updated) in relation to their positions on a map. Can you point me to some useful sample applications in this respect? Thanks a lot.
Franklin
Hi Yale,
Have added the following code to save a created polygon zone:
void trackOverlay_TrackEnded(object sender, TrackEndedTrackInteractiveOverlayEventArgs e)
{
PolygonShape polygonShape = (PolygonShape)e.TrackShape;
ShapeFileFeatureLayer shapeFileFetureLayer = new ShapeFileFeatureLayer(@"C:\testOnload\testLoad.shp", ShapeFileReadWriteMode.ReadWrite);
try
{
shapeFileFetureLayer.Open();
shapeFileFetureLayer.EditTools.BeginTransaction();
shapeFileFetureLayer.EditTools.Add(polygonShape);
TransactionResult result = shapeFileFetureLayer.EditTools.CommitTransaction();
}
finally
{
shapeFileFetureLayer.Close();
}
}
However, break point confirms that the runtime gets stuck at shapeFileFetureLayer.Open(); due to "File cannot be found exception", although the file is created in the directory whose path is clearly shown above. Can you shed some light on this or there is better way to do this?
My purpose is to save the polygon position at the current session and reload it when the application is restarted.
The code above only shows the "save" part.
Thanks.
Franklin
Franklin,
Could you add the following statement before create the ShapeFileFeturLayer to make sure the file exists in the disk.
Of course, you may need to change the Encoding or add some dbf columns if you want.
ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Polyline,@"C:\testOnload\testLoad.shp",new DbfColumn[]{}, Encoding.UTF8, OverwriteMode.DoNotOverwrite);
Any more questions please feel free to let me know.
Thanks.
Yale
Yale,
It works fine for the 1st time when the shp file is not generated and the shape of the polygon can be saved. If the application runs the second time
the execution gets stuck at ShapeFileFeatureLayer.CreateShapeFile(…) so i think one way to workaround this is to prompt the user to enter a new file name into the CreateShapeFile before it is executed. Can you give me some sample code to do the save thing with SQL database approach?
The file save approach seems generating a bunch of files and may be not efficient if i want to load all kinds of zone shapes onto the map at their respective locations.
Thanks a lot for your enlightening.
Franklin
Franklin
Franklin,
Thanks for your post and feedback.
As you have already noticed, the shape file is always accompanied with a group of files like shx , idx, ids and dbf, each file has its own particular functionality and I do not think the performance will be worsed with the existing of these files.
Following code snippet shows you how to do the saving stuff with the SQL Server 2008, it is exactly the same with the Shape File source.
MsSql2008FeatureLayer msSqlFeatureLayer;
try
{
msSqlFeatureLayer.Open();
msSqlFeatureLayer.EditTools.BeginTransaction();
msSqlFeatureLayer.EditTools.Add(polygonShape);
TransactionResult result = msSqlFeatureLayer.EditTools.CommitTransaction();
}
finally
{
msSqlFeatureLayer.Close();
}
Any more questions please feel free to let me know.
Thanks.
Yale
Yale,
I am trying to find a unified method by which the shape of a polygon can be saved by
4.0 PolygonShape object and the data can be loaded by 2.25.5.0 Polygon object.
In order to do so, I need to ensure or clarify the mapping relationships as follows.
Could you help me find the missing links there? Thanks a lot.
MapSuite 2.25.5.0 MapSuite 4.0
Terminology Terminology
BaseAreaShape AreaBaseShape
Polygon PolygonShape
Point PointShape
MultiPoint MultiPointShape
StraightRectangle RectangleShape
AreaSymbol ?
AreaMapShape ?
PointShape ?
Ellipse EllipseShape
MapLengthUnits ?
LineShape LineShape
textSymbol ?
Hi Yale,
No problem i can save a newly created polygon into a shp file and load it into the map. Afer loading the polygon shape into the map, the portion of the map beneath the polygon becomes opaque. Is there any way to make the map portion still visible?
The code is as follows:
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"C:\testOnload\testLoad.shp");
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay layerOverlay = new LayerOverlay();
layerOverlay.Layers.Add(worldLayer);
winformsMap1.Overlays.Add(layerOverlay);
winformsMap1.Refresh();
Thanks.
Franklin
franklin,
You can simply modify the 'alpha' value that is part of your AreaStyle. Here we have an alpha = 100:
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.SimpleColors.Transparent, GeoColor.FromArgb(100, GeoColor.SimpleColors.Green));
Yale,
Thanks, it works. I am not sure how to add column "NAME" value after the following:
dbfColumns.Add(new DbfColumn("NAME", DbfColumnType.String, 10, 0));
Once the name value is saved, how to make it appear in the displayed polygon?
I really appreciate your help. Thanks a lot.
Franklin
Franklin,
I am glad it’s working with you.
If you want to display column value as label, you can set TextStyle, for example
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.City1(“NAME”);
Thanks
James
HI James,
Thanks for answering the second question, which I am not able to try out in DbfColumn context yet. My first question asked was: how to add a specific name into the DbfColumn "Name". Not sure about the MapSuite 4.0 DesktopEdition statement syntax in that context.
Eventually I want to show the named polygon in a map via ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"C:\testOnload\testLoad.shp");
I know how to save and show an un-named polygon but not sure how to save and show the saved name together with the saved polygon. Then I run into the problem of how to add "Smith" to the DbfColumn "Name" and how to show the saved "Smith" polygon. Could you please enlighten me in the above context? Thanks a lot.
Franklin
Franklin,
Hope I did not make any mistake in understanding. You have to add the value to the feature before adding it to the shape file in transaction process like as following way.
editFeature.ColumnValues.Add("Name", "Yale");
Any more questions please feel free to let me know.
Thanks.
Yang
Yale,
Thanks, but before using this, need to instantiate Feature. “Feature editFeature=new Feature()” doesn’t work. What will be the right way of assigning editFeature to a valid reference?
Thanks.
Franklin
Yale,
To give you the full picture,
Here is the code in which the Feature statement is added:
void trackOverlay_TrackEnded(object sender, TrackEndedTrackInteractiveOverlayEventArgs e)
{
PolygonShape polygonShape = (PolygonShape)e.TrackShape;
Collection<DbfColumn> dbfColumns = new Collection<DbfColumn>();
dbfColumns.Add(new DbfColumn("POLYID", DbfColumnType.String, 10, 0));
dbfColumns.Add(new DbfColumn("NAME", DbfColumnType.String, 10, 0));
Feature editFeature = new Feature((PolygonShape)e.TrackShape); // I am not sure how to do this properly.
editFeature.ColumnValues.Add("Name", "Yale"); // as suggested
ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Polygon, @"C:\testOnload\testLoad.shp", dbfColumns, Encoding.UTF8, OverwriteMode.DoNotOverwrite);
ShapeFileFeatureLayer shapeFileFetureLayer = new ShapeFileFeatureLayer(@"C:\testOnload\testLoad.shp", ShapeFileReadWriteMode.ReadWrite);
try
{
shapeFileFetureLayer.Open();
shapeFileFetureLayer.EditTools.BeginTransaction();
shapeFileFetureLayer.EditTools.Add(polygonShape);
TransactionResult result = shapeFileFetureLayer.EditTools.CommitTransaction();
}
finally
{
shapeFileFetureLayer.Close();
}
}
The code below is used to display the saved shape.After adding the two lines of "editFeature…" statements, the name of the saved shape is still not
displayed except for the shape.
private void label1_Click(object sender, EventArgs e)
{
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"C:\testOnload\testLoad.shp");
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.SimpleColors.Transparent, GeoColor.FromArgb(100, GeoColor.SimpleColors.Black));
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay layerOverlay = new LayerOverlay();
// Add the shapefile layer to the layer overlay
layerOverlay.Layers.Add(worldLayer);
winformsMap1.Overlays.Add(layerOverlay);
winformsMap1.Refresh();
}
Does this part of the code require any changes too?
I saw that in MapSuite 3.xx, the VB code for adding the DbfColumn value used "Dictionary" class, but it is no longer there for 4.0
The instantiation of Feature in terms of editFeature may have something to do with DbfColumn but not sure how to make the connection.
Thanks a lot for your help.
Franklin
Franklin,
Thanks for your post and sample code.
I think you forget to set the TextStyle for the layer, just take a try to add it as following:
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.City1("Name");
Any more questions please feel free to let me know.
Thanks.
Yale
Hi Yale,
Tried adding the TextStyle statement above or below the worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle statement above. However the entered name is not shown. Any suggestion to make it work?
Thanks for the timely response.
Franklin