Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
How to create a xml map
posted

 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

  • 3255
    posted

    Hi Darak,

    • To export a map to xml you have to use map's elements geodetic coordinates. For SurfaceElement and PathElement this is the Polylines property and for SumbolElement this is the SymbolOrigin property . Try the following code:

    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;
     }
    }

    • To import it back from xml use the following code:

    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