private void gatherDim4(NetTopologySuite.Geometries.Geometry geometry, Dictionary <Coordinate, Double> map)
 {
     if (geometry is Point)
     {
         gatherDim4(((Point)geometry).CoordinateSequence, map);
     }
     else if (geometry is LineString)
     {
         gatherDim4(((LineString)geometry).CoordinateSequence, map);
     }
     else if (geometry is Polygon)
     {
         Polygon polygon = (Polygon)geometry;
         gatherDim4(polygon.ExteriorRing.CoordinateSequence, map);
         for (int i = 0; i < polygon.NumInteriorRings; i++)
         {
             gatherDim4(polygon.GetInteriorRingN(i).CoordinateSequence, map);
         }
     }
     else
     {
         for (int i = 0; i < geometry.NumGeometries; i++)
         {
             gatherDim4(geometry.GetGeometryN(i), map);
         }
     }
 }
        /**
         * Making a Polygon valid may Creates
         * <ul>
         * <li>an Empty Polygon if input has no valid coordinate</li>
         * <li>a Point if input has only one valid coordinate</li>
         * <li>a LineString if input has only a valid segment</li>
         * <li>a Polygon in most cases</li>
         * <li>a MultiPolygon if input has a self-intersection</li>
         * <li>a GeometryCollection if input has degenerate parts (ex. degenerate
         * holes)</li>
         * </ul>
         *
         * @param polygon the Polygon to make valid
         * @return a valid Geometry which may be of any type if the source geometry
         * is not valid.
         */
        private NetTopologySuite.Geometries.Geometry makePolygonValid(Polygon polygon)
        {
            //This first step analyze linear components and Create degenerate geometries
            //of dimension 0 or 1 if they do not form valid LinearRings
            //If degenerate geometries are found, it may produce a GeometryCollection with
            //heterogeneous dimension
            NetTopologySuite.Geometries.Geometry        geom = makePolygonComponentsValid(polygon);
            List <NetTopologySuite.Geometries.Geometry> list = new();

            for (int i = 0; i < geom.NumGeometries; i++)
            {
                NetTopologySuite.Geometries.Geometry component = geom.GetGeometryN(i);
                if (component is Polygon)
                {
                    NetTopologySuite.Geometries.Geometry nodedPolygon = nodePolygon((Polygon)component);
                    for (int j = 0; j < nodedPolygon.NumGeometries; j++)
                    {
                        list.Add(nodedPolygon.GetGeometryN(j));
                    }
                }
                else
                {
                    list.Add(component);
                }
            }
            return(polygon.Factory.BuildGeometry(list));
        }
 // Reursively remove geometries with a dimension less than dimension parameter
 private void removeLowerDimension(NetTopologySuite.Geometries.Geometry geometry, List <NetTopologySuite.Geometries.Geometry> result, Dimension dimension)
 {
     for (int i = 0; i < geometry.NumGeometries; i++)
     {
         NetTopologySuite.Geometries.Geometry g = geometry.GetGeometryN(i);
         if (g is GeometryCollection)
         {
             removeLowerDimension(g, result, dimension);
         }
         else if (g.Dimension >= dimension)
         {
             result.Add(g);
         }
     }
 }
 /**
  * Decompose a geometry recursively into simple components.
  *
  * @param geometry input geometry
  * @param list a list of simple components (Point, LineString or Polygon)
  */
 private static void decompose(NetTopologySuite.Geometries.Geometry geometry, ICollection <NetTopologySuite.Geometries.Geometry> list)
 {
     for (int i = 0; i < geometry.NumGeometries; i++)
     {
         NetTopologySuite.Geometries.Geometry component = geometry.GetGeometryN(i);
         if (component is GeometryCollection)
         {
             decompose(component, list);
         }
         else
         {
             list.Add(component);
         }
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        ///     Converts an NTS LineString to a Microsoft.Spatial GeogaphyLineString.
        /// </summary>
        /// <param name="lineString">The NTS LineString.</param>
        /// <returns></returns>
        public static GeographyLineString ToGeographyLineString(this Geometry lineString)
        {
            if (lineString == null)
            {
                return(null);
            }

            Debug.Assert(lineString.GeometryType == "LineString");
            var builder  = SpatialBuilder.Create();
            var pipeLine = builder.GeographyPipeline;

            pipeLine.SetCoordinateSystem(CoordinateSystem.DefaultGeography);
            pipeLine.BeginGeography(SpatialType.LineString);

            var numPionts = lineString.NumPoints;

            for (var n = 0; n < numPionts; n++)
            {
                var pointN   = lineString.GetGeometryN(n + 1);
                var lat      = pointN.Coordinate.Y;
                var lon      = pointN.Coordinate.X;
                var alt      = pointN.Coordinate.Z;
                var m        = pointN.Length;
                var position = new GeographyPosition(lat, lon, alt, m);
                if (n == 0)
                {
                    pipeLine.BeginFigure(position);
                }
                else
                {
                    pipeLine.LineTo(position);
                }
            }

            pipeLine.EndFigure();
            pipeLine.EndGeography();
            return((GeographyLineString)builder.ConstructedGeography);
        }