Convert graphics to feature(shapefile)

6147
4
03-16-2014 11:21 PM
Labels (1)
SateeshKarri
New Contributor
Hi,
I am trying to find a way to create graphics and convert those graphics to features (shapefile). Users should be able to draw graphics like point/line/polygon on surface and all those graphics should be converted to a shapefile in a workspace folder. Any help is much appreciated. Thanks
0 Kudos
4 Replies
Cristian_Galindo
Occasional Contributor III
Hello fellow,

I don't know if I am going to be dissapear after this answer, so tell my family that I love them.......

I face a similar problem, but let me tell you that ArcGIS Runtime is quite limited....(I miss ArcObjects..sigh)...

I used DotSpatial library, to be more specific, DotSpatial.Data, you can download it from NuGet.

the process was to iterate over the graphics an use the Dotspatial to create each feature

            var layer = this.MyMap.Layers["MyLayer"] as ArcGISLocalFeatureLayer;

            FeatureTemplate lineLayerTemplate = null;

            if (layer.LayerInfo.Templates != null && layer.LayerInfo.Templates.Count > 0)
            {
                lineLayerTemplate = layer.LayerInfo.Templates.FirstOrDefault().Value;
            }

            var featureSet = new DotSpatial.Data.FeatureSet(DotSpatial.Topology.FeatureType.Line);

            if (lineLayerTemplate != null && lineLayerTemplate.PrototypeAttributes != null)
            {
                foreach (var prototypeAttribute in lineLayerTemplate.PrototypeAttributes)
                {
                    // the purpose of this is to create the columns that will apper in the shp file
                    featureSet.DataTable.Columns.Add(new DataColumn(prototypeAttribute.Key as String));
                }
            }


            foreach (var graphic in layer.Graphics)
            {
                var newPaths = new List<LineString>();

                foreach (var path in (graphic.Geometry as Polyline).Paths)
                {
                    var vertices = new List<Coordinate>();

                    for (int i = 0; i < path.Count; i++)
                    {
                        vertices.Add(new Coordinate(path.X,path.Y,path.Z,path.M));
                    }

                    // LineString is from DotSpatial...like a PolyLine in Runtime
                    var newPath = new LineString(vertices);
                    newPaths.Add(newPath);
                }

                // Multiline is from DotSpatial
                var geom = new MultiLineString(newPaths);

                var feature = featureSet.AddFeature(geom);

                feature.DataRow.BeginEdit();

                foreach (var attribute in graphic.Attributes)
                {
                    feature.DataRow[attribute.Key] = attribute.Value;
                }

                feature.DataRow.EndEdit();

            }

            featureSet.Projection = ProjectionInfo.FromEpsgCode(layer.SpatialReference.WKID);

            featureSet.SaveAs(@"C:\shptest.shp",true);


enjoy
0 Kudos
BKuiper
Occasional Contributor III
thanks for sharing. That is an interesting approach. We are currently investigating using to From JSON to Feature tool within the Conversion tools

arcpy.JSONToFeatures_conversion
0 Kudos
SateeshKarri
New Contributor
Thank you xtian79
I will work on it and keep you posted
Thanks for your time
Sateesh

Thank you kuiperfoliage for posting to this thread. I appreciate your time
Sateesh
0 Kudos
DharmaRajan
Occasional Contributor

Hi,

The below given snippet may be useful.


IMap pMap = null;
IMxDocument pDoc = null;

pDoc = clsGlobal.pApp.Document as IMxDocument;
pMap = pDoc.FocusMap;

IGraphicsContainer graphicsContainer ;
pMap = pDoc.FocusMap;
graphicsContainer = pMap.Layer[0] as IGraphicsContainer;//Assuming that layer[0] will be a graphic layer
graphicsContainer.Reset();
IElement element = graphicsContainer.Next();
while (element != null)
{
if (element is IPolygonElement)
{

IElement polyElm = element as IElement;

IGeometry geom = polyElm.Geometry;
//add the task to do
}
element = graphicsContainer.Next();
}

0 Kudos