NtsGeometryConverter.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:20k
源码类别:

GIS编程

开发平台:

C#

  1. // Copyright 2006 - Diego Guidi
  2. //
  3. // This file is part of NtsProvider.
  4. // NtsProvider is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. // 
  9. // SharpMap is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU Lesser General Public License for more details.
  13. // You should have received a copy of the GNU Lesser General Public License
  14. // along with NtsProvider; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using System.Diagnostics;
  20. using System.Text;
  21. namespace SharpMap.Converters.NTS
  22. {
  23.     /// <summary>
  24.     /// Provides static methods that performs conversions 
  25.     /// between geometric elements provides by SharpMap and NetTopologySuite libraries.
  26.     /// </summary>
  27.     public static class GeometryConverter
  28.     {
  29.                 
  30.         /// <summary>
  31.         /// Converts any <see cref="SharpMap.Geometries.Geometry"/> array to the correspondant 
  32.         /// <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> array.
  33.         /// </summary>
  34.         /// <param name="geometries"></param>
  35.         /// <param name="factory"></param>
  36.         /// <returns></returns>
  37.         public static GisSharpBlog.NetTopologySuite.Geometries.Geometry[] ToNTSGeometry(SharpMap.Geometries.Geometry[] geometries,
  38.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  39.         {            
  40.             GisSharpBlog.NetTopologySuite.Geometries.Geometry[] converted = new GisSharpBlog.NetTopologySuite.Geometries.Geometry[geometries.Length];
  41.             int index = 0;
  42.             foreach (SharpMap.Geometries.Geometry geometry in geometries)
  43.                 converted[index++] = GeometryConverter.ToNTSGeometry(geometry, factory);
  44.             if((geometries.Length != converted.Length))
  45.                 throw new ApplicationException("Conversion error");
  46.             return converted;
  47.         }
  48.         /// <summary>
  49.         /// Converts any <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> to the correspondant 
  50.         /// <see cref="SharpMap.Geometries.Geometry"/>.
  51.         /// </summary>
  52.         /// <param name="geometry"></param>
  53.         /// <returns></returns>
  54.         public static GisSharpBlog.NetTopologySuite.Geometries.Geometry ToNTSGeometry(SharpMap.Geometries.Geometry geometry,
  55.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  56.         {
  57.             if (geometry == null)
  58.                 throw new NullReferenceException("geometry");
  59.             if (geometry.GetType() == typeof(SharpMap.Geometries.Point))
  60.                 return ToNTSPoint(geometry as SharpMap.Geometries.Point, factory);
  61.             else if (geometry.GetType() == typeof(SharpMap.Geometries.LineString))
  62.                 return ToNTSLineString(geometry as SharpMap.Geometries.LineString, factory);
  63.             else if (geometry.GetType() == typeof(SharpMap.Geometries.Polygon))
  64.                 return ToNTSPolygon(geometry as SharpMap.Geometries.Polygon, factory);
  65.             else if (geometry.GetType() == typeof(SharpMap.Geometries.MultiPoint))
  66.                 return ToNTSMultiPoint(geometry as SharpMap.Geometries.MultiPoint, factory);
  67.             else if (geometry.GetType() == typeof(SharpMap.Geometries.MultiLineString))
  68.                 return ToNTSMultiLineString(geometry as SharpMap.Geometries.MultiLineString, factory);
  69.             else if (geometry.GetType() == typeof(SharpMap.Geometries.MultiPolygon))
  70.                 return ToNTSMultiPolygon(geometry as SharpMap.Geometries.MultiPolygon, factory);
  71.             else if (geometry.GetType() == typeof(SharpMap.Geometries.GeometryCollection))
  72.                 return ToNTSGeometryCollection(geometry as SharpMap.Geometries.GeometryCollection, factory);
  73.             else throw new NotSupportedException("Type " + geometry.GetType().FullName + " not supported");
  74.         }
  75.         /// <summary>
  76.         /// Converts any <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> array to the correspondant 
  77.         /// <see cref="SharpMap.Geometries.Geometry"/> array.
  78.         /// </summary>
  79.         /// <param name="geometries"></param>
  80.         /// <returns></returns>
  81.         public static SharpMap.Geometries.Geometry[] ToSharpMapGeometry(GisSharpBlog.NetTopologySuite.Geometries.Geometry[] geometries)
  82.         {
  83.             SharpMap.Geometries.Geometry[] converted = new SharpMap.Geometries.Geometry[geometries.Length];
  84.             int index = 0;
  85.             foreach (GisSharpBlog.NetTopologySuite.Geometries.Geometry geometry in geometries)
  86.                 converted[index++] = GeometryConverter.ToSharpMapGeometry(geometry);
  87.             if ((geometries.Length != converted.Length))
  88.                 throw new ApplicationException("Conversion error");
  89.             return converted;
  90.                
  91.         }
  92.         
  93.         /// <summary>
  94.         /// Converts any <see cref="SharpMap.Geometries.Geometry"/> to the correspondant 
  95.         /// <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/>.
  96.         /// </summary>
  97.         /// <param name="geometry"></param>
  98.         /// <returns></returns>
  99.         public static SharpMap.Geometries.Geometry ToSharpMapGeometry(GisSharpBlog.NetTopologySuite.Geometries.Geometry geometry)
  100.         {
  101.             if (geometry == null)
  102.                 throw new NullReferenceException("geometry");
  103.             if(geometry.GetType() ==  typeof(GisSharpBlog.NetTopologySuite.Geometries.Point))
  104.                 return ToSharpMapPoint(geometry as GisSharpBlog.NetTopologySuite.Geometries.Point);
  105.             else if(geometry.GetType() ==  typeof(GisSharpBlog.NetTopologySuite.Geometries.LineString))
  106.                 return ToSharpMapLineString(geometry as GisSharpBlog.NetTopologySuite.Geometries.LineString);
  107.             else if (geometry.GetType() == typeof(GisSharpBlog.NetTopologySuite.Geometries.Polygon))
  108.                 return ToSharpMapPolygon(geometry as GisSharpBlog.NetTopologySuite.Geometries.Polygon);
  109.             else if (geometry.GetType() == typeof(GisSharpBlog.NetTopologySuite.Geometries.MultiPoint))
  110.                 return ToSharpMapMultiPoint(geometry as GisSharpBlog.NetTopologySuite.Geometries.MultiPoint);
  111.             else if (geometry.GetType() == typeof(GisSharpBlog.NetTopologySuite.Geometries.MultiLineString))
  112.                 return ToSharpMapMultiLineString(geometry as GisSharpBlog.NetTopologySuite.Geometries.MultiLineString);
  113.             else if (geometry.GetType() == typeof(GisSharpBlog.NetTopologySuite.Geometries.MultiPolygon))
  114.                 return ToSharpMapMultiPolygon(geometry as GisSharpBlog.NetTopologySuite.Geometries.MultiPolygon);
  115.             else if (geometry.GetType() == typeof(GisSharpBlog.NetTopologySuite.Geometries.GeometryCollection))
  116.                 return ToSharpMapGeometryCollection(geometry as GisSharpBlog.NetTopologySuite.Geometries.GeometryCollection);
  117.             
  118.             else throw new NotSupportedException("Type " + geometry.GetType().FullName + " not supported");           
  119.         }
  120.         /// <summary>
  121.         /// Converts the <see cref="GisSharpBlog.NetTopologySuite.Geometries.Envelope"/> instance <paramref name="envelope"/>
  122.         /// into a correspondant <see cref="SharpMap.Geometries.BoundingBox"/>.
  123.         /// </summary>
  124.         /// <param name="envelope"></param>
  125.         /// <returns></returns>
  126.         public static SharpMap.Geometries.BoundingBox ToSharpMapBoundingBox(GisSharpBlog.NetTopologySuite.Geometries.Envelope envelope)
  127.         {
  128.             return new SharpMap.Geometries.BoundingBox(envelope.MinX, envelope.MinY, envelope.MaxX, envelope.MaxY);           
  129.         }
  130.         /// <summary>
  131.         /// Converts the <see cref="GisSharpBlog.NetTopologySuite.Geometries.Envelope"/> instance <paramref name="envelope"/>
  132.         /// into a correspondant <see cref="SharpMap.Geometries.Geometry"/>.
  133.         /// </summary>
  134.         /// <param name="envelope"></param>
  135.         /// <returns></returns>
  136.         public static SharpMap.Geometries.Geometry ToSharpMapGeometry(GisSharpBlog.NetTopologySuite.Geometries.Envelope envelope)
  137.         {
  138.             return ToSharpMapGeometry(new SharpMap.Geometries.BoundingBox(envelope.MinX, envelope.MinY, envelope.MaxX, envelope.MaxY));
  139.         }
  140.         /// <summary>
  141.         /// Converts the <see cref="SharpMap.Geometries.BoundingBox"/> instance <paramref name="boundingBox"/>
  142.         /// into a correspondant <see cref="SharpMap.Geometries.Polygon"/>.
  143.         /// </summary>
  144.         /// <param name="boundingBox"></param>
  145.         /// <returns></returns>
  146.         public static SharpMap.Geometries.Geometry ToSharpMapGeometry(SharpMap.Geometries.BoundingBox boundingBox)
  147.         {
  148.             Collection<SharpMap.Geometries.Point> vertices = new Collection<SharpMap.Geometries.Point>();
  149.             vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Min.Y));
  150.             vertices.Add(new SharpMap.Geometries.Point(boundingBox.Max.X, boundingBox.Min.Y));
  151.             vertices.Add(new SharpMap.Geometries.Point(boundingBox.Max.X, boundingBox.Max.Y));
  152.             vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Max.Y));
  153.             vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Min.Y));
  154.             SharpMap.Geometries.LinearRing exterior = new SharpMap.Geometries.LinearRing(vertices);
  155.             return new SharpMap.Geometries.Polygon(exterior);
  156.         }
  157.         /// <summary>
  158.         /// Converts the <see cref="SharpMap.Geometries.BoundingBox"/> instance <paramref name="boundingBox"/>
  159.         /// into a correspondant <see cref="GisSharpBlog.NetTopologySuite.Geometries.Envelope"/>.
  160.         /// </summary>
  161.         /// <param name="boundingBox"></param>
  162.         /// <returns></returns>
  163.         public static GisSharpBlog.NetTopologySuite.Geometries.Envelope ToNTSEnvelope(SharpMap.Geometries.BoundingBox boundingBox)
  164.         {            
  165.             return new GisSharpBlog.NetTopologySuite.Geometries.Envelope(boundingBox.Min.X, boundingBox.Max.X, boundingBox.Min.Y, boundingBox.Max.Y);
  166.         }
  167.         #region Internal NTS Converters     
  168.    
  169.         internal static GisSharpBlog.NetTopologySuite.Geometries.Coordinate ToNTSCoordinate(SharpMap.Geometries.Point point, 
  170.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  171.         {            
  172.             return new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(point.X, point.Y);               
  173.         }
  174.         
  175.         internal static GisSharpBlog.NetTopologySuite.Geometries.Point ToNTSPoint(SharpMap.Geometries.Point point,
  176.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  177.         {
  178.             return factory.CreatePoint(ToNTSCoordinate(point, factory));            
  179.         }
  180.         internal static GisSharpBlog.NetTopologySuite.Geometries.LineString ToNTSLineString(SharpMap.Geometries.LineString lineString,
  181.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  182.         {
  183.             GisSharpBlog.NetTopologySuite.Geometries.Coordinate[] coordinates = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate[lineString.NumPoints];
  184.             int index = 0;
  185.             foreach (SharpMap.Geometries.Point point in lineString.Vertices)
  186.                 coordinates[index++] = ToNTSCoordinate(point, factory);
  187.             return factory.CreateLineString(coordinates);
  188.         }
  189.         internal static GisSharpBlog.NetTopologySuite.Geometries.LinearRing ToNTSLinearRing(SharpMap.Geometries.LinearRing linearRing,
  190.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  191.         {
  192.             GisSharpBlog.NetTopologySuite.Geometries.Coordinate[] coordinates = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate[linearRing.NumPoints];
  193.             int index = 0;
  194.             foreach (SharpMap.Geometries.Point point in linearRing.Vertices)
  195.                 coordinates[index++] = ToNTSCoordinate(point, factory);
  196.             return factory.CreateLinearRing(coordinates);
  197.         }
  198.         internal static GisSharpBlog.NetTopologySuite.Geometries.Polygon ToNTSPolygon(SharpMap.Geometries.Polygon polygon,
  199.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  200.         {
  201.             GisSharpBlog.NetTopologySuite.Geometries.LinearRing shell = ToNTSLinearRing(polygon.ExteriorRing, factory);
  202.             GisSharpBlog.NetTopologySuite.Geometries.LinearRing[] holes = new GisSharpBlog.NetTopologySuite.Geometries.LinearRing[polygon.InteriorRings.Count];
  203.             int index = 0;
  204.             foreach (SharpMap.Geometries.LinearRing hole in polygon.InteriorRings)
  205.                 holes[index++] = ToNTSLinearRing(hole, factory);
  206.             return factory.CreatePolygon(shell, holes);
  207.         }
  208.         internal static GisSharpBlog.NetTopologySuite.Geometries.MultiPoint ToNTSMultiPoint(SharpMap.Geometries.MultiPoint multiPoint,
  209.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  210.         {
  211.             GisSharpBlog.NetTopologySuite.Geometries.Point[] points = new GisSharpBlog.NetTopologySuite.Geometries.Point[multiPoint.Points.Count];
  212.             int index = 0;
  213.             foreach (SharpMap.Geometries.Point point in multiPoint.Points)
  214.                 points[index++] = ToNTSPoint(point, factory);
  215.             return factory.CreateMultiPoint(points);
  216.         }
  217.         internal static GisSharpBlog.NetTopologySuite.Geometries.MultiLineString ToNTSMultiLineString(SharpMap.Geometries.MultiLineString multiLineString,
  218.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  219.         {
  220.             GisSharpBlog.NetTopologySuite.Geometries.LineString[] lstrings = new GisSharpBlog.NetTopologySuite.Geometries.LineString[multiLineString.LineStrings.Count];
  221.             int index = 0;
  222.             foreach (SharpMap.Geometries.LineString lstring in multiLineString.LineStrings)
  223.                 lstrings[index++] = ToNTSLineString(lstring, factory);
  224.             return factory.CreateMultiLineString(lstrings);
  225.         }
  226.         internal static GisSharpBlog.NetTopologySuite.Geometries.MultiPolygon ToNTSMultiPolygon(SharpMap.Geometries.MultiPolygon multiPolygon,
  227.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  228.         {
  229.             GisSharpBlog.NetTopologySuite.Geometries.Polygon[] polygons = new GisSharpBlog.NetTopologySuite.Geometries.Polygon[multiPolygon.Polygons.Count];
  230.             int index = 0;
  231.             foreach (SharpMap.Geometries.Polygon polygon in multiPolygon.Polygons)
  232.                 polygons[index++] = ToNTSPolygon(polygon, factory);
  233.             return factory.CreateMultiPolygon(polygons);
  234.         }
  235.         internal static GisSharpBlog.NetTopologySuite.Geometries.GeometryCollection ToNTSGeometryCollection(SharpMap.Geometries.GeometryCollection geometryCollection,
  236.             GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory)
  237.         {
  238.             GisSharpBlog.NetTopologySuite.Geometries.Geometry[] geometries = new GisSharpBlog.NetTopologySuite.Geometries.Geometry[geometryCollection.Collection.Count];
  239.             int index = 0;
  240.             foreach (SharpMap.Geometries.Geometry geometry in geometryCollection.Collection)
  241.                 geometries[index++] = ToNTSGeometry(geometry, factory);
  242.             return factory.CreateGeometryCollection(geometries);
  243.         }
  244.         #endregion
  245.         #region Internal SharpMap Converters
  246.         internal static SharpMap.Geometries.Point ToSharpMapPoint(GisSharpBlog.NetTopologySuite.Geometries.Coordinate coordinate)
  247.         {            
  248.             return new SharpMap.Geometries.Point(coordinate.X, coordinate.Y);
  249.         }
  250.         internal static SharpMap.Geometries.Point ToSharpMapPoint(GisSharpBlog.NetTopologySuite.Geometries.Point point)
  251.         {            
  252.             Debug.Assert(point.Coordinates.Length == 1);
  253.             return ToSharpMapPoint(point.Coordinate);
  254.         }
  255.         internal static SharpMap.Geometries.LineString ToSharpMapLineString(GisSharpBlog.NetTopologySuite.Geometries.LineString lineString)
  256.         {
  257.             Collection<SharpMap.Geometries.Point> vertices = new Collection<SharpMap.Geometries.Point>();
  258.             foreach (GisSharpBlog.NetTopologySuite.Geometries.Coordinate coordinate in lineString.Coordinates)
  259.                 vertices.Add(ToSharpMapPoint(coordinate));            
  260.             return new SharpMap.Geometries.LineString(vertices);
  261.         }
  262.         internal static SharpMap.Geometries.LinearRing ToSharpMapLinearRing(GisSharpBlog.NetTopologySuite.Geometries.LinearRing lineString)
  263.         {
  264.             Collection<SharpMap.Geometries.Point> vertices = new Collection<SharpMap.Geometries.Point>();
  265.             foreach (GisSharpBlog.NetTopologySuite.Geometries.Coordinate coordinate in lineString.Coordinates)
  266.                 vertices.Add(ToSharpMapPoint(coordinate));
  267.             return new SharpMap.Geometries.LinearRing(vertices);
  268.         }
  269.         internal static SharpMap.Geometries.Polygon ToSharpMapPolygon(GisSharpBlog.NetTopologySuite.Geometries.Polygon polygon)
  270.         {
  271.             SharpMap.Geometries.LinearRing exteriorRing = ToSharpMapLinearRing((GisSharpBlog.NetTopologySuite.Geometries.LinearRing)polygon.ExteriorRing);
  272.             Collection<SharpMap.Geometries.LinearRing> interiorRings = new Collection<SharpMap.Geometries.LinearRing>();
  273.             foreach (GisSharpBlog.NetTopologySuite.Geometries.LineString interiorRing in polygon.InteriorRings)
  274.                 interiorRings.Add(ToSharpMapLinearRing((GisSharpBlog.NetTopologySuite.Geometries.LinearRing)interiorRing));
  275.             return new SharpMap.Geometries.Polygon(exteriorRing, interiorRings);
  276.         }
  277.         internal static SharpMap.Geometries.MultiPoint ToSharpMapMultiPoint(GisSharpBlog.NetTopologySuite.Geometries.MultiPoint multiPoint)
  278.         {
  279.             SharpMap.Geometries.MultiPoint collection = new SharpMap.Geometries.MultiPoint();
  280.             foreach(GisSharpBlog.NetTopologySuite.Geometries.Point point in multiPoint.Geometries)
  281.                 collection.Points.Add(ToSharpMapPoint(point));
  282.             return collection;
  283.         }
  284.         internal static SharpMap.Geometries.MultiLineString ToSharpMapMultiLineString(GisSharpBlog.NetTopologySuite.Geometries.MultiLineString multiLineString)
  285.         {
  286.             SharpMap.Geometries.MultiLineString collection = new SharpMap.Geometries.MultiLineString();
  287.             foreach (GisSharpBlog.NetTopologySuite.Geometries.LineString lineString in multiLineString.Geometries)
  288.                 collection.LineStrings.Add(ToSharpMapLineString(lineString));
  289.             return collection;
  290.         }
  291.         internal static SharpMap.Geometries.MultiPolygon ToSharpMapMultiPolygon(GisSharpBlog.NetTopologySuite.Geometries.MultiPolygon multiPolygon)
  292.         {
  293.             SharpMap.Geometries.MultiPolygon collection = new SharpMap.Geometries.MultiPolygon();
  294.             foreach (GisSharpBlog.NetTopologySuite.Geometries.Polygon polygon in multiPolygon.Geometries)
  295.                 collection.Polygons.Add(ToSharpMapPolygon(polygon));
  296.             return collection;
  297.         }
  298.         internal static SharpMap.Geometries.GeometryCollection ToSharpMapGeometryCollection(GisSharpBlog.NetTopologySuite.Geometries.GeometryCollection geometryCollection)
  299.         {
  300.             SharpMap.Geometries.GeometryCollection collection = new SharpMap.Geometries.GeometryCollection();
  301.             foreach (GisSharpBlog.NetTopologySuite.Geometries.Geometry geometry in geometryCollection.Geometries)
  302.                 collection.Collection.Add(ToSharpMapGeometry(geometry));
  303.             return collection;
  304.         }
  305.         #endregion
  306.     }
  307. }