Advertisement
Guest User

Untitled

a guest
Oct 16th, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Pipes;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml;
  8. using System.Xml.Linq;
  9. using Catfood.Shapefile;
  10.  
  11. namespace StatMap.Data
  12. {
  13.     // A shapefile consists of three files:
  14.     // filename.shp - the main file containing shapes.
  15.     // filename.shx - an index to the shapes in the main file.
  16.     // filename.dbf - database containing metadata for each shape.
  17.     public class EsriShapefileToVml
  18.     {
  19.         private readonly XNamespace _xmlnsOsgb = "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1";
  20.         private readonly XNamespace _xmlnsGml = "http://www.opengis.net/gml";
  21.         private readonly XNamespace _xmlnsXsi = "http://www.w3.org/2001/XMLSchema-instance";
  22.         private readonly XNamespace _xsiSchemaLocation =
  23.             "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd";
  24.  
  25.         private readonly string _shpPath;
  26.         private XDocument _gml;
  27.         private int _fid = 1;
  28.  
  29.         private XmlWriter _writer;
  30.  
  31.         public EsriShapefileToVml(string shpPath)
  32.         {
  33.             _shpPath = shpPath;
  34.             _gml = new XDocument();
  35. //            _writer = new XmlTextWriter(new StreamWriter(shpPath));
  36.         }
  37.  
  38.         public void ConvertESRIShapefile(Action<int> reportCallback)
  39.         {
  40.             using (
  41.                 _writer =
  42.                 XmlTextWriter.Create(
  43.                     new BufferedStream(new FileStream(@"D:\!SHPTOVML\TEST.xml", FileMode.Create,
  44.                                                       System.Security.AccessControl.FileSystemRights.Write,
  45.                                                       FileShare.None, 1024, FileOptions.SequentialScan)),
  46.                     new XmlWriterSettings {Encoding = Encoding.Unicode, Indent = true, CloseOutput = true}))
  47.             {
  48.                 using (Shapefile shapefile = new Shapefile(_shpPath))
  49.                 {
  50.                     AddHeader(shapefile);
  51.                    
  52.                     StringBuilder coordinates = new StringBuilder();
  53.                     XElement newElement = null;
  54.                     foreach (Shape shape in shapefile)
  55.                     {
  56.                         //ShowMetadata(shape);
  57.  
  58.                         XElement osgbFeatureCode = new XElement(_xmlnsOsgb + "featureCode");
  59.                         osgbFeatureCode.SetValue("4321... booom... featureCode");
  60.                         XElement osgbFeatureDescription = new XElement(_xmlnsOsgb + "featureDescription");
  61.                         osgbFeatureDescription.SetValue("Something in here");
  62.  
  63.                         XElement gmlCoordinates;
  64.                         switch (shape.Type)
  65.                         {
  66.                             case ShapeType.Null:
  67.                                 break;
  68.                             case ShapeType.Point:
  69.                                 ShapePoint point = shape as ShapePoint;
  70.                                 newElement = new XElement(_xmlnsOsgb + "pointMember");
  71.                                 XElement osgbVectorMapPoint = new XElement(_xmlnsOsgb + "VectorMapPoint");
  72.                                 osgbVectorMapPoint.SetAttributeValue("fid", string.Format("ID_{0}", _fid));
  73.                                 XElement osgbPoint = new XElement(_xmlnsOsgb + "point");
  74.                                 XElement gmlPoint = new XElement(_xmlnsGml + "Point");
  75.                                 gmlPoint.SetAttributeValue("srsName", "osgb:BNG");
  76.  
  77.                                 XElement osgbOrientation = new XElement(_xmlnsOsgb + "orientation");
  78.                                 osgbOrientation.SetValue(0);
  79.  
  80.                                 coordinates.AppendFormat("{0},{1}", point.Point.X, point.Point.Y);
  81.                                 gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
  82.                                 gmlCoordinates.SetValue(coordinates.ToString());
  83.  
  84.                                 gmlPoint.Add(coordinates);
  85.                                 osgbPoint.Add(gmlPoint);
  86.                                 osgbVectorMapPoint.Add(osgbFeatureCode);
  87.                                 osgbVectorMapPoint.Add(osgbFeatureDescription);
  88.                                 osgbVectorMapPoint.Add(osgbOrientation);
  89.                                 osgbVectorMapPoint.Add(osgbPoint);
  90.                                 newElement.Add(osgbVectorMapPoint);
  91.                                 break;
  92.                             case ShapeType.PolyLine:
  93.                                 ShapePolyLine polyline = shape as ShapePolyLine;
  94.                                 foreach (PointD p in polyline.Parts.SelectMany(part => part))
  95.                                 {
  96.                                     coordinates.AppendFormat("{0},{1} ", p.X, p.Y);
  97.                                 }
  98.  
  99.                                 newElement = new XElement(_xmlnsOsgb + "lineMember");
  100.                                 XElement osgbLine = new XElement(_xmlnsOsgb + "Line");
  101.                                 osgbLine.SetAttributeValue("fid", string.Format("ID_{0}", _fid));
  102.  
  103.                                 XElement osgbPolyline = new XElement(_xmlnsOsgb + "polyline");
  104.                                 XElement gmlLineString = new XElement(_xmlnsGml + "LineString");
  105.                                 gmlLineString.SetAttributeValue("srsName", "osgb:BNG");
  106.                                 gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
  107.                                 gmlCoordinates.SetValue(coordinates.ToString().Trim());
  108.  
  109.                                 gmlLineString.Add(gmlCoordinates);
  110.                                 osgbPolyline.Add(gmlLineString);
  111.                                 osgbLine.Add(osgbFeatureCode);
  112.                                 osgbLine.Add(osgbFeatureDescription);
  113.                                 osgbLine.Add(osgbPolyline);
  114.                                 newElement.Add(osgbLine);
  115.                                 break;
  116.                             case ShapeType.Polygon: // areaMember ???
  117.                                 ShapePolygon polygon = shape as ShapePolygon;
  118.                                 foreach (PointD p in polygon.Parts.SelectMany(part => part))
  119.                                 {
  120.                                     coordinates.AppendFormat("{0},{1} ", p.X, p.Y);
  121.                                 }
  122.  
  123.                                 newElement = new XElement(_xmlnsOsgb + "areaMember");
  124.  
  125.                                 gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
  126.                                 gmlCoordinates.SetValue(coordinates);
  127.  
  128.                                 XElement osgbArea = new XElement(_xmlnsOsgb + "Area");
  129.                                 osgbArea.SetAttributeValue("fid", string.Format("ID_{0}", _fid));
  130.                                 XElement osgbPolygon = new XElement(_xmlnsOsgb + "polygon");
  131.                                 XElement gmlPolygon = new XElement(_xmlnsGml + "Polygon");
  132.                                 gmlPolygon.SetAttributeValue("srsName", "osgb:BNG");
  133.                                 XElement gmlOuterBoundaryIs = new XElement(_xmlnsGml + "outerBoundaryIs");
  134.                                 XElement gmlLinearRing = new XElement(_xmlnsGml + "LinearRing");
  135.  
  136.                                 gmlLinearRing.Add(gmlCoordinates);
  137.                                 gmlOuterBoundaryIs.Add(gmlLinearRing);
  138.                                 gmlPolygon.Add(gmlOuterBoundaryIs);
  139.                                 osgbPolygon.Add(gmlPolygon);
  140.                                 osgbArea.Add(osgbFeatureCode);
  141.                                 osgbArea.Add(osgbFeatureDescription);
  142.                                 osgbArea.Add(osgbPolygon);
  143.                                 newElement.Add(osgbArea);
  144.                                 break;
  145.                             case ShapeType.MultiPoint:
  146.                                 ShapeMultiPoint multiPoint = shape as ShapeMultiPoint;
  147.  
  148.                                 break;
  149.                             case ShapeType.MultiPatch:
  150.                                 break;
  151.                             default:
  152.                                 throw new ArgumentOutOfRangeException();
  153.                         }
  154.  
  155.                         _gml.Element(_xmlnsOsgb + "FeatureCollection").Add(newElement);
  156.  
  157.                         _fid++;
  158.                         coordinates.Clear();
  159.                     }
  160.  
  161.                     _gml.WriteTo(_writer);
  162.                     _gml.Element(_xmlnsOsgb + "FeatureCollection").Descendants().Remove();
  163. //                    System.Windows.Forms.MessageBox.Show(
  164. //                    _gml.Element(_xmlnsOsgb + "FeatureCollection").Descendants().Count().ToString());
  165.                 }
  166.             }
  167.         }
  168.  
  169.         private static void ShowMetadata(Shape shape)
  170.         {
  171.             string[] metadataNames = shape.GetMetadataNames();
  172.             if (metadataNames != null)
  173.             {
  174.                 Console.WriteLine("Metadata:");
  175.                 foreach (string metadataName in metadataNames)
  176.                 {
  177.                     Console.WriteLine("{0}={1} ({2})", metadataName, shape.GetMetadata(metadataName),
  178.                                       shape.DataRecord.GetDataTypeName(
  179.                                           shape.DataRecord.GetOrdinal(metadataName)));
  180.                 }
  181.                 Console.WriteLine();
  182.             }
  183.         }
  184.  
  185.         private void AddHeader(Shapefile shapefile)
  186.         {
  187.             _gml.Declaration = new XDeclaration("1.0", "utf-8", null);
  188.  
  189.             XElement featureCollection = new XElement(_xmlnsOsgb + "FeatureCollection");
  190.             featureCollection.SetAttributeValue(XNamespace.Xmlns + "osgb",
  191.                                                 "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1");
  192.             featureCollection.SetAttributeValue(XNamespace.Xmlns + "gml", "http://www.opengis.net/gml");
  193.             featureCollection.SetAttributeValue(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance");
  194.             featureCollection.SetAttributeValue(XNamespace.Xmlns + "schemaLocation",
  195.                                                 "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd");
  196.             featureCollection.SetAttributeValue("fid", ""); // TODO: tu ustawiac
  197.  
  198.             XElement gmlDescription = new XElement(_xmlnsGml + "description");
  199.             gmlDescription.SetValue("Ordnance Survey, (c) Crown Copyright. All rights reserved, 2011-03-02");
  200.  
  201.             XElement gmlBoundedBy = new XElement(_xmlnsGml + "boundedBy");
  202.             XElement gmlBox = new XElement(_xmlnsGml + "Box");
  203.             gmlBox.SetAttributeValue("srsName", "osgb:BNG");
  204.  
  205.             string coordinates = string.Format("{0},{1} {2},{3}", shapefile.BoundingBox.Left,
  206.                                                shapefile.BoundingBox.Top, shapefile.BoundingBox.Right,
  207.                                                shapefile.BoundingBox.Bottom);
  208.             XElement gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
  209.             gmlCoordinates.SetValue(coordinates);
  210.  
  211.             XElement osgbTileExtent = new XElement(_xmlnsOsgb + "tileExtent");
  212.             XElement osgbRectangle = new XElement(_xmlnsOsgb + "Rectangle");
  213.             osgbRectangle.SetAttributeValue("srsName", "osgb:BNG");
  214.  
  215.             XElement osgbCreationDate = new XElement(_xmlnsOsgb + "creationDate");
  216.             osgbCreationDate.SetValue(DateTime.Today.ToString("yyyy-MM-dd"));
  217.  
  218.             gmlBox.Add(gmlCoordinates);
  219.             gmlBoundedBy.Add(gmlBox);
  220.  
  221.             osgbRectangle.Add(gmlCoordinates);
  222.             osgbTileExtent.Add(osgbRectangle);
  223.  
  224.             featureCollection.Add(gmlDescription);
  225.             featureCollection.Add(gmlBoundedBy);
  226.             featureCollection.Add(osgbTileExtent);
  227.             featureCollection.Add(osgbCreationDate);
  228.  
  229.             _gml.Add(featureCollection);
  230.         }
  231.  
  232.         public static string IndentXml(XDocument xml)
  233.         {
  234.             string xmlStr;
  235.             using (MemoryStream mst = new MemoryStream())
  236.             {
  237.                 XmlTextWriter writer = new XmlTextWriter(mst, Encoding.UTF8) { Formatting = Formatting.Indented };
  238.                 xml.WriteTo(writer);
  239.                 writer.Flush();
  240.  
  241.                 using (StreamReader sr = new StreamReader(mst))
  242.                 {
  243.                     sr.BaseStream.Seek(0, SeekOrigin.Begin);
  244.                     xmlStr = sr.ReadToEnd();
  245.                 }
  246.  
  247.                 writer.Close();
  248.             }
  249.             return xmlStr;
  250.         }
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement