Select to view content in your preferred language

saving a graphics layer

3353
5
02-02-2011 08:09 AM
MarkBaird
Esri Regular Contributor
I'm working on an application which uses a graphics layer to generate markup.  I'd like to be able to save the markup into a file so it can be reloaded at a later date.  Storing the markup in a feature class in a database is not an option.

Does anyone know a way of serializing / deserializing a graphics layer or even individual graphics to save / load from a file?
5 Replies
KevinDeege
Esri Contributor
You could try using Isolated Storage:

private void SaveGraphics_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Save the graphics layer
                GraphicsLayer graphicsLayer = MyMap.Layers["StatesGraphicsLayer"] as GraphicsLayer;
                GraphicCollection pGC = graphicsLayer.Graphics;

                //Isolated Storage -- Write Graphics
                IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
               
                using (Stream pStream = isf.OpenFile(_ISFileName, FileMode.Create, FileAccess.Write))
                {
                    StreamWriter pStreamWriter = new StreamWriter(pStream);
                    foreach (Graphic g in pGC)
                    {
                        if (g.Geometry is ESRI.ArcGIS.Client.Geometry.MapPoint)
                        {
                            MapPoint pPoint = (MapPoint)g.Geometry;
                            pStreamWriter.WriteLine("Point, " + pPoint.X.ToString() + ", " + pPoint.Y.ToString());
                        }

                        if (g.Geometry is ESRI.ArcGIS.Client.Geometry.Polyline)
                        {
                            ESRI.ArcGIS.Client.Geometry.Polyline pPolyline = (ESRI.ArcGIS.Client.Geometry.Polyline)g.Geometry;
                            ESRI.ArcGIS.Client.Geometry.PointCollection[] pPC = pPolyline.Paths.ToArray();
                            pStreamWriter.Write("Polyline, ");

                            foreach (MapPoint p in pPC[0])
                            {
                                pStreamWriter.Write(p.X.ToString() + ", " + p.Y.ToString() + ",");
                            }
                            pStreamWriter.WriteLine();
                        }

                        if (g.Geometry is ESRI.ArcGIS.Client.Geometry.Polygon)
                        {
                           
                            ESRI.ArcGIS.Client.Geometry.Polygon pPolygon = (ESRI.ArcGIS.Client.Geometry.Polygon)g.Geometry;
                            ESRI.ArcGIS.Client.Geometry.PointCollection[] pPC = pPolygon.Rings.ToArray();
                            pStreamWriter.Write("Polygon, ");

                            foreach (MapPoint p in pPC[0])
                            {
                                pStreamWriter.Write(p.X.ToString() + ", " + p.Y.ToString() + ",");
                            }
                            pStreamWriter.WriteLine();
                        }
                    }
                    pStreamWriter.Close();
                    pStream.Close();
                }
            }
            catch
            {
                MessageBox.Show("Error", "Error", MessageBoxButton.OK);
            }
        }
0 Kudos
MarkBaird
Esri Regular Contributor
Hi Kevin,

Thanks for the reply.  I was kind of hoping that I wouldn't need to dive into the components of the geometry, but your code gives me the technique of doing this so that helps loads.  I was hoping I might have missed a geometry.toJson/fromJson method or something similar.  I'm probably going to create an XML structure to store this.

Out of interest do you have a sample line of code which takes MapPoints and puts them back into the Ring of a Polygon for example.  If not I'll soon work it out 🙂

Thanks
0 Kudos
DominiqueBroux
Esri Frequent Contributor
There is an example of GraphicsLayer serialization here : http://forums.arcgis.com/threads/8774-save-layer-to-xml-file but there are some limitations : for example the symbols are not serialized.
0 Kudos
MarkBaird
Esri Regular Contributor
That serialisation example was just the answer!

I've adapted my graphics layer to use the attributes to store the symbology and it works really well.  Thanks again!
0 Kudos
AdrianMarsden
Honored Contributor
sorry please ignore - didn't notice forum I was posting in
0 Kudos