DBroux
This was altered from existing forum thread:
private void SaveGraphics_Click(object sender, RoutedEventArgs e)
{
try
{
//Save the graphics layer
GraphicsLayer graphicsLayer = MyMap.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
GraphicCollection pGC = graphicsLayer.Graphics;
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = ".txt";
sfd.Filter = "Text Files|*.txt|All Files|*.*";
sfd.FilterIndex = 1;
sfd.ShowDialog();
using (Stream pStream = sfd.OpenFile())
{
StreamWriter pStreamWriter = new StreamWriter(pStream);
foreach (Graphic g in pGC)
{
if (g.Geometry is ESRI.ArcGIS.Client.Geometry.MapPoint)
{
//Create a row in a csv file
MapPoint pPoint = (MapPoint)g.Geometry;
pStreamWriter.WriteLine("Point, " + pPoint.X.ToString() + ", " + pPoint.Y.ToString());
}
if (g.Geometry is ESRI.ArcGIS.Client.Geometry.Polyline)
{
//Create a row in a csv file
ESRI.ArcGIS.Client.Geometry.Polyline PL = new ESRI.ArcGIS.Client.Geometry.Polyline();
PL = (ESRI.ArcGIS.Client.Geometry.Polyline)g.Geometry;
ESRI.ArcGIS.Client.Geometry.PointCollection PC = new ESRI.ArcGIS.Client.Geometry.PointCollection();
for (int i = 0; i < PL.Paths.Count; i++)
foreach (var mp in PL.Paths)
PC.Add(mp);
//ESRI.ArcGIS.Client.Geometry.Polyline pPolyline = (ESRI.ArcGIS.Client.Geometry.Polyline)g.Geometry;
//ESRI.ArcGIS.Client.Geometry.PointCollection[] pPC = pPolyline.Paths[0];
pStreamWriter.Write("Polyline, ");
foreach (MapPoint p in PC)
{
pStreamWriter.Write(p.X.ToString() + ", " + p.Y.ToString() + ",");
}
pStreamWriter.WriteLine();
}
if (g.Geometry is ESRI.ArcGIS.Client.Geometry.Polygon)
{
//Create a row in a csv file
ESRI.ArcGIS.Client.Geometry.Polygon PG = new ESRI.ArcGIS.Client.Geometry.Polygon();
PG = (ESRI.ArcGIS.Client.Geometry.Polygon)g.Geometry;
ESRI.ArcGIS.Client.Geometry.PointCollection PC = new ESRI.ArcGIS.Client.Geometry.PointCollection();
for (int i = 0; i < PG.Rings.Count; i++)
foreach (var mp in PG.Rings)
PC.Add(mp);
//ESRI.ArcGIS.Client.Geometry.Polygon pPolygon = (ESRI.ArcGIS.Client.Geometry.Polygon)g.Geometry;
//ESRI.ArcGIS.Client.Geometry.PointCollection[] pPC = pPolygon.Rings[0];
pStreamWriter.Write("Polygon, ");
foreach (MapPoint p in PC)
{
pStreamWriter.Write(p.X.ToString() + "," + p.Y.ToString() + " ");
}
pStreamWriter.WriteLine();
}
}
pStreamWriter.Close();
pStream.Close();
}
}
catch
{
MessageBox.Show(" Error saving file\n Contact GIS for assistance", "Error", MessageBoxButton.OK);
}
}
private void OpenGraphics_Click(object sender, RoutedEventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Filter = "Text Files|*.txt|All Files|*.*";
ofd.FilterIndex = 1;
ofd.ShowDialog();
//code to open the file
StreamReader pStreamReader = ofd.File.OpenText();
GraphicsLayer graphicsLayer = MyMap.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
Graphic g;
double X;
double Y;
//read in each row and then create the graphic
string Shape = pStreamReader.ReadLine();
while (Shape != null)
{
if (Shape.Contains("Point"))
{
string[] s = Shape.Split(',');
//create point graphic
X = System.Convert.ToDouble(s[1]);
Y = System.Convert.ToDouble(s[2]);
MapPoint point = new MapPoint(X, Y);
g = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = point,
//Symbol = DefaultMarkerSymbol,
Symbol = LayoutRoot.Resources["DefaultPointSymbol"] as SimpleMarkerSymbol,
};
graphicsLayer.Graphics.Add(g);
}
if (Shape.Contains("Polygon"))
{
string s = Shape.Remove(0, 10);
//string s2 = s.Replace(" ", "");
//string s3 = s2.Replace(",-", " -");
string s4 = s.TrimEnd(',');
//string s5 = "43589.006839111,175166.477333516 443942.241040781,175162.76146169 444885.157133937,175152.837597027 444878.470467523,173975.940672606 444786.990007445,174016.332324103 444595.920507684,174100.695344359 444472.40139778,174155.232636854 444389.013769269,174192.05080469 444249.601318449,174253.605471611 444097.723997518,174320.663736433 444018.592922196,174355.602642938 443817.821998268,174444.248791099 443654.861694276,174516.200746939 443580.473719686,174549.045169443 443582.007181197,175166.560010523 443589.006839111,175166.477333516";
//create polygon graphic
PointCollectionConverter pPointCollectionConverter = new PointCollectionConverter();
//get the point collection and add it to a polygon geometry
ESRI.ArcGIS.Client.Geometry.PointCollection pPCPolygon = new ESRI.ArcGIS.Client.Geometry.PointCollection();
pPCPolygon = (ESRI.ArcGIS.Client.Geometry.PointCollection)pPointCollectionConverter.ConvertFromString(s4);
ESRI.ArcGIS.Client.Geometry.Polygon polygon = new ESRI.ArcGIS.Client.Geometry.Polygon();
polygon.Rings.Add(pPCPolygon);
g = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = polygon,
//Symbol = DefaultFillSymbol,
Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as FillSymbol,
};
graphicsLayer.Graphics.Add(g);
}
if (Shape.Contains("Polyline"))
{
string s = Shape.Remove(0, 9);
string s2 = s.Replace(" ", "");
string s3 = s2.Replace(",-", " -");
string s4 = s3.TrimEnd(',');
//create polyline graphic
PointCollectionConverter pPointCollectionConverter = new PointCollectionConverter();
//get the point collection and add it to a polyline geometry
ESRI.ArcGIS.Client.Geometry.PointCollection pPCPolyline = new ESRI.ArcGIS.Client.Geometry.PointCollection();
pPCPolyline = (ESRI.ArcGIS.Client.Geometry.PointCollection)pPointCollectionConverter.ConvertFromString(s4);
ESRI.ArcGIS.Client.Geometry.Polyline polyline = new ESRI.ArcGIS.Client.Geometry.Polyline();
polyline.Paths.Add(pPCPolyline);
g = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = polyline,
//Symbol = DefaultLineSymbol,
Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as SimpleLineSymbol,
};
graphicsLayer.Graphics.Add(g);
}
Shape = pStreamReader.ReadLine();
}
}
catch
{
MessageBox.Show(" Error opening file\n Contact GIS for assistance", "Error", MessageBoxButton.OK);
}
}
Keith