I want to create a map,and transfer it into a xml document like MapSpatialData.xml
but i do not know how to do it ,thanks
Hi Darak,
private void ExportButton_Click(object sender, RoutedEventArgs e){ var saveDialog = new SaveFileDialog { DefaultExt = "xml", Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 };
if (saveDialog.ShowDialog() == true) { var map = ExportMapData(xamMap.Layers[0].Elements);
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), map);
using (var stream = saveDialog.OpenFile()) { doc.Save(stream); } }}
private XElement ExportMapData(IEnumerable<MapElement> elements){ var sb = new StringBuilder(); var map = new XElement("Map");
foreach (var element in elements) { if (element is SurfaceElement) { ExportSurfaceElement((SurfaceElement)element, sb); } else if (element is SymbolElement) { ExportSymbolElement((SymbolElement)element, sb); } else // PathElement { ExportPathElement((PathElement)element, sb); } map.Add(new XElement("SpatialElement", new XAttribute("Name", element.Name ?? ""), new XAttribute("Caption", element.Caption ?? ""), new XAttribute("SpatialValue", sb.ToString())));
sb.Clear(); }
return map;}
private void ExportPathElement(PathElement element, StringBuilder sb){ if (element.Polylines.Count > 0) { sb.Append("MULTILINESTRING "); sb.Append("(");
ExportPolylines(element.Polylines, sb);
sb.Append(")"); } else { sb.Append("EMPTY"); }}
private void ExportSymbolElement(SymbolElement element, StringBuilder sb){ sb.Append("POINT "); sb.Append("(");
var unprojectedPoint = xamMap.MapProjection.UnprojectFromMap(element.SymbolOrigin); sb.Append(unprojectedPoint.X.ToString(NumberFormatInfo.InvariantInfo)).Append(" ").Append(unprojectedPoint.Y.ToString(NumberFormatInfo.InvariantInfo));
sb.Append(")");}
private void ExportSurfaceElement(SurfaceElement element, StringBuilder points){ if (element.Polylines.Count > 0) { points.Append("MULTIPOLYGON "); points.Append("(");
ExportPolylines(element.Polylines, points);
points.Append(")"); } else { points.Append("EMPTY"); }}
private void ExportPolylines(IEnumerable<MapPolyline> polylines, StringBuilder sb){ foreach (var polyline in polylines) { sb.Append("(");
foreach (var point in polyline) { var unprojectedPoint = xamMap.MapProjection.UnprojectFromMap(point); sb.Append(unprojectedPoint.X.ToString(NumberFormatInfo.InvariantInfo)).Append(" ").Append(unprojectedPoint.Y.ToString(NumberFormatInfo.InvariantInfo)).Append(", "); }
if (sb.Length > 0) { sb.Length -= 2; }
sb.Append(")"); sb.Append(", "); }
if (sb.Length > 0) { sb.Length -= 2; }}
private void ImportButton_Click(object sender, RoutedEventArgs e){ var openDialog = new OpenFileDialog { Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 };
if (openDialog.ShowDialog() == true) { using (var stream = openDialog.File.OpenRead()) { var doc = XDocument.Load(stream);
ImportSpatialData(doc); } }}
private void ImportSpatialData(XDocument doc){ var spatialData = from element in doc.Root.Elements() select new Dictionary<string, string>{ { "Name", element.Attribute("Name").Value }, { "Caption", element.Attribute("Caption").Value }, { "SpatialValue", element.Attribute("SpatialValue").Value }};
var layer = xamMap.Layers[0];
layer.Reader = new SqlShapeReader { DataMapping = new DataMapping { new DataMappingPair("Name", "Name"), new DataMappingPair("Caption", "Caption"), new DataMappingPair("Data", "SpatialValue"), }, DataSource = spatialData };
layer.ImportAsync(); }
internal class SpatialData{ public string Name { get; set; } public string Caption { get; set; } public string SpatialValue { get; set; }
public SpatialData(string name, string caption, string spatialValue) { Name = name; Caption = caption; SpatialValue = spatialValue; }}
Regards,
Ivan Kotev
thank you for you answer,but could you tell me how to draw a map ,or use what tool to do it?
Please take a look at this page: http://help.infragistics.com/NetAdvantage/DV/2010.3/CLR4.0/?page=xamWebMap_Creating_and_Editing_Custom_Shapefiles.html. It contains information how to create and editi custom shapefiles.