ThinkGeo.com    |     Documentation    |     Premium Support

Create new features and add into shapefiles?

 


Edgar,


I have a point shapfile and a polygon shapfile. The shapefiles have columns of ID and Label. Then I load them into a map control.


Would you show me some sample code that can do:


(1) Create a point feature (say PointShape)  based on screen point (world location); Assign values to ID and Label columns; Save the feature into the point shapefile.


(2) Create a polygon feature based on two PointShape  points; Assign values to ID and Label columns; Save the feature into the point shapefile.


So I can reload the new created features into map control next time.


Thank you and have a nice weekend,


Mengbo



 


 



Hi Mengbo,


Here is the code,



//======================== Add point feature ==================================================================
            PointShape currentPoint = new PointShape(-100, 34);// e.WorldLocation;

            ShapeFileFeatureLayer pointLayer = new ShapeFileFeatureLayer("your point shape file", ShapeFileReadWriteMode.ReadWrite);
            pointLayer.Open();

            Collection<FeatureSourceColumn> columns = pointLayer.FeatureSource.GetColumns();

            pointLayer.FeatureSource.BeginTransaction();
            Feature newFeature = new Feature(currentPoint);

            newFeature.ColumnValues.Add("LabelColumn", "New place");

            pointLayer.FeatureSource.AddFeature(newFeature);

            pointLayer.FeatureSource.CommitTransaction();
            pointLayer.FeatureSource.Close();

            winformsMap1.Refresh();

            //======================== Add polygon feature ==================================================================
            PointShape point1 = new PointShape(-100, 34);
            PointShape point2 = new PointShape(-101, 35);
            double minx = Math.Min(point1.X,point1.X);
            double miny = Math.Min(point1.Y,point1.Y);
            double maxx = Math.Max(point1.X,point1.X);
            double maxy = Math.Max(point1.Y,point1.Y);
            RectangleShape rectanglePolygon = new RectangleShape(minx, maxy, maxx, miny);
            PolygonShape currentPolygon = rectanglePolygon.ToPolygon();

            ShapeFileFeatureLayer polygonLayer = new ShapeFileFeatureLayer("your polygon shape file", ShapeFileReadWriteMode.ReadWrite);
            polygonLayer.Open();

            Collection<FeatureSourceColumn> polygonColumns = polygonLayer.FeatureSource.GetColumns();

            polygonLayer.FeatureSource.BeginTransaction();
            Feature newPolygonFeature = new Feature(currentPolygon);
            newPolygonFeature.ColumnValues.Add("LabelColumn", "New place");
            polygonLayer.FeatureSource.AddFeature(newPolygonFeature);

            polygonLayer.FeatureSource.CommitTransaction();
            polygonLayer.FeatureSource.Close();

            winformsMap1.Refresh();


Hope it helps,


Edgar



Edgar,


Your sample code is very helpful. My evaluation moves well. So far verything looks postive.


Thank you very much,


Mengbo


 



You’re welcome, if you have any questions please let us know. 
  
 Regards, 
 Edgar