Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Pipes;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.Linq;
- using Catfood.Shapefile;
- namespace StatMap.Data
- {
- // A shapefile consists of three files:
- // filename.shp - the main file containing shapes.
- // filename.shx - an index to the shapes in the main file.
- // filename.dbf - database containing metadata for each shape.
- public class EsriShapefileToVml
- {
- private readonly XNamespace _xmlnsOsgb = "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1";
- private readonly XNamespace _xmlnsGml = "http://www.opengis.net/gml";
- private readonly XNamespace _xmlnsXsi = "http://www.w3.org/2001/XMLSchema-instance";
- private readonly XNamespace _xsiSchemaLocation =
- "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd";
- private readonly string _shpPath;
- private XDocument _gml;
- private int _fid = 1;
- private XmlWriter _writer;
- public EsriShapefileToVml(string shpPath)
- {
- _shpPath = shpPath;
- _gml = new XDocument();
- // _writer = new XmlTextWriter(new StreamWriter(shpPath));
- }
- public void ConvertESRIShapefile(Action<int> reportCallback)
- {
- using (
- _writer =
- XmlTextWriter.Create(
- new BufferedStream(new FileStream(@"D:\!SHPTOVML\TEST.xml", FileMode.Create,
- System.Security.AccessControl.FileSystemRights.Write,
- FileShare.None, 1024, FileOptions.SequentialScan)),
- new XmlWriterSettings {Encoding = Encoding.Unicode, Indent = true, CloseOutput = true}))
- {
- using (Shapefile shapefile = new Shapefile(_shpPath))
- {
- AddHeader(shapefile);
- StringBuilder coordinates = new StringBuilder();
- XElement newElement = null;
- foreach (Shape shape in shapefile)
- {
- //ShowMetadata(shape);
- XElement osgbFeatureCode = new XElement(_xmlnsOsgb + "featureCode");
- osgbFeatureCode.SetValue("4321... booom... featureCode");
- XElement osgbFeatureDescription = new XElement(_xmlnsOsgb + "featureDescription");
- osgbFeatureDescription.SetValue("Something in here");
- XElement gmlCoordinates;
- switch (shape.Type)
- {
- case ShapeType.Null:
- break;
- case ShapeType.Point:
- ShapePoint point = shape as ShapePoint;
- newElement = new XElement(_xmlnsOsgb + "pointMember");
- XElement osgbVectorMapPoint = new XElement(_xmlnsOsgb + "VectorMapPoint");
- osgbVectorMapPoint.SetAttributeValue("fid", string.Format("ID_{0}", _fid));
- XElement osgbPoint = new XElement(_xmlnsOsgb + "point");
- XElement gmlPoint = new XElement(_xmlnsGml + "Point");
- gmlPoint.SetAttributeValue("srsName", "osgb:BNG");
- XElement osgbOrientation = new XElement(_xmlnsOsgb + "orientation");
- osgbOrientation.SetValue(0);
- coordinates.AppendFormat("{0},{1}", point.Point.X, point.Point.Y);
- gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
- gmlCoordinates.SetValue(coordinates.ToString());
- gmlPoint.Add(coordinates);
- osgbPoint.Add(gmlPoint);
- osgbVectorMapPoint.Add(osgbFeatureCode);
- osgbVectorMapPoint.Add(osgbFeatureDescription);
- osgbVectorMapPoint.Add(osgbOrientation);
- osgbVectorMapPoint.Add(osgbPoint);
- newElement.Add(osgbVectorMapPoint);
- break;
- case ShapeType.PolyLine:
- ShapePolyLine polyline = shape as ShapePolyLine;
- foreach (PointD p in polyline.Parts.SelectMany(part => part))
- {
- coordinates.AppendFormat("{0},{1} ", p.X, p.Y);
- }
- newElement = new XElement(_xmlnsOsgb + "lineMember");
- XElement osgbLine = new XElement(_xmlnsOsgb + "Line");
- osgbLine.SetAttributeValue("fid", string.Format("ID_{0}", _fid));
- XElement osgbPolyline = new XElement(_xmlnsOsgb + "polyline");
- XElement gmlLineString = new XElement(_xmlnsGml + "LineString");
- gmlLineString.SetAttributeValue("srsName", "osgb:BNG");
- gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
- gmlCoordinates.SetValue(coordinates.ToString().Trim());
- gmlLineString.Add(gmlCoordinates);
- osgbPolyline.Add(gmlLineString);
- osgbLine.Add(osgbFeatureCode);
- osgbLine.Add(osgbFeatureDescription);
- osgbLine.Add(osgbPolyline);
- newElement.Add(osgbLine);
- break;
- case ShapeType.Polygon: // areaMember ???
- ShapePolygon polygon = shape as ShapePolygon;
- foreach (PointD p in polygon.Parts.SelectMany(part => part))
- {
- coordinates.AppendFormat("{0},{1} ", p.X, p.Y);
- }
- newElement = new XElement(_xmlnsOsgb + "areaMember");
- gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
- gmlCoordinates.SetValue(coordinates);
- XElement osgbArea = new XElement(_xmlnsOsgb + "Area");
- osgbArea.SetAttributeValue("fid", string.Format("ID_{0}", _fid));
- XElement osgbPolygon = new XElement(_xmlnsOsgb + "polygon");
- XElement gmlPolygon = new XElement(_xmlnsGml + "Polygon");
- gmlPolygon.SetAttributeValue("srsName", "osgb:BNG");
- XElement gmlOuterBoundaryIs = new XElement(_xmlnsGml + "outerBoundaryIs");
- XElement gmlLinearRing = new XElement(_xmlnsGml + "LinearRing");
- gmlLinearRing.Add(gmlCoordinates);
- gmlOuterBoundaryIs.Add(gmlLinearRing);
- gmlPolygon.Add(gmlOuterBoundaryIs);
- osgbPolygon.Add(gmlPolygon);
- osgbArea.Add(osgbFeatureCode);
- osgbArea.Add(osgbFeatureDescription);
- osgbArea.Add(osgbPolygon);
- newElement.Add(osgbArea);
- break;
- case ShapeType.MultiPoint:
- ShapeMultiPoint multiPoint = shape as ShapeMultiPoint;
- break;
- case ShapeType.MultiPatch:
- break;
- default:
- throw new ArgumentOutOfRangeException();
- }
- _gml.Element(_xmlnsOsgb + "FeatureCollection").Add(newElement);
- _fid++;
- coordinates.Clear();
- }
- _gml.WriteTo(_writer);
- _gml.Element(_xmlnsOsgb + "FeatureCollection").Descendants().Remove();
- // System.Windows.Forms.MessageBox.Show(
- // _gml.Element(_xmlnsOsgb + "FeatureCollection").Descendants().Count().ToString());
- }
- }
- }
- private static void ShowMetadata(Shape shape)
- {
- string[] metadataNames = shape.GetMetadataNames();
- if (metadataNames != null)
- {
- Console.WriteLine("Metadata:");
- foreach (string metadataName in metadataNames)
- {
- Console.WriteLine("{0}={1} ({2})", metadataName, shape.GetMetadata(metadataName),
- shape.DataRecord.GetDataTypeName(
- shape.DataRecord.GetOrdinal(metadataName)));
- }
- Console.WriteLine();
- }
- }
- private void AddHeader(Shapefile shapefile)
- {
- _gml.Declaration = new XDeclaration("1.0", "utf-8", null);
- XElement featureCollection = new XElement(_xmlnsOsgb + "FeatureCollection");
- featureCollection.SetAttributeValue(XNamespace.Xmlns + "osgb",
- "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1");
- featureCollection.SetAttributeValue(XNamespace.Xmlns + "gml", "http://www.opengis.net/gml");
- featureCollection.SetAttributeValue(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance");
- featureCollection.SetAttributeValue(XNamespace.Xmlns + "schemaLocation",
- "http://namespaces.ordnancesurvey.co.uk/cmd/local/v1.1 http://www.ordnancesurvey.co.uk/oswebsite/xml/cmdschema/local/V1.1/CMDFeatures.xsd");
- featureCollection.SetAttributeValue("fid", ""); // TODO: tu ustawiac
- XElement gmlDescription = new XElement(_xmlnsGml + "description");
- gmlDescription.SetValue("Ordnance Survey, (c) Crown Copyright. All rights reserved, 2011-03-02");
- XElement gmlBoundedBy = new XElement(_xmlnsGml + "boundedBy");
- XElement gmlBox = new XElement(_xmlnsGml + "Box");
- gmlBox.SetAttributeValue("srsName", "osgb:BNG");
- string coordinates = string.Format("{0},{1} {2},{3}", shapefile.BoundingBox.Left,
- shapefile.BoundingBox.Top, shapefile.BoundingBox.Right,
- shapefile.BoundingBox.Bottom);
- XElement gmlCoordinates = new XElement(_xmlnsGml + "coordinates");
- gmlCoordinates.SetValue(coordinates);
- XElement osgbTileExtent = new XElement(_xmlnsOsgb + "tileExtent");
- XElement osgbRectangle = new XElement(_xmlnsOsgb + "Rectangle");
- osgbRectangle.SetAttributeValue("srsName", "osgb:BNG");
- XElement osgbCreationDate = new XElement(_xmlnsOsgb + "creationDate");
- osgbCreationDate.SetValue(DateTime.Today.ToString("yyyy-MM-dd"));
- gmlBox.Add(gmlCoordinates);
- gmlBoundedBy.Add(gmlBox);
- osgbRectangle.Add(gmlCoordinates);
- osgbTileExtent.Add(osgbRectangle);
- featureCollection.Add(gmlDescription);
- featureCollection.Add(gmlBoundedBy);
- featureCollection.Add(osgbTileExtent);
- featureCollection.Add(osgbCreationDate);
- _gml.Add(featureCollection);
- }
- public static string IndentXml(XDocument xml)
- {
- string xmlStr;
- using (MemoryStream mst = new MemoryStream())
- {
- XmlTextWriter writer = new XmlTextWriter(mst, Encoding.UTF8) { Formatting = Formatting.Indented };
- xml.WriteTo(writer);
- writer.Flush();
- using (StreamReader sr = new StreamReader(mst))
- {
- sr.BaseStream.Seek(0, SeekOrigin.Begin);
- xmlStr = sr.ReadToEnd();
- }
- writer.Close();
- }
- return xmlStr;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement