Example #1
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="envelope"></param>
 /// <returns></returns>
 public static IEnvelope GetEnvelopeExternal(IEnvelope envelope)
 {
     // Get envelope in external coordinates
     ICoordinate min = new Coordinate(envelope.MinX, envelope.MinY);
     ICoordinate max = new Coordinate(envelope.MaxX, envelope.MaxY);
     IEnvelope bounds = new Envelope(min.X, max.X, min.Y, max.Y);
     return bounds;
 }
 public static ICoordinate[] LocationCollectionToCoordinates(LocationCollection locations)
 {
     var coordinates = new Coordinate[locations.Count];
     for (var x = 0; x < locations.Count; x++)
     {
         coordinates[x] = (Coordinate)Convert(locations[x]);
     }
     return (ICoordinate[])coordinates;
 }
Example #3
0
 /// <summary>
 /// Converts an array of NTS coordinates into Backsight positions
 /// </summary>
 /// <param name="cos">The coordinates to convert</param>
 /// <returns>The corresponding positions</returns>
 IPosition[] GetPositionArray(NTS.Coordinate[] cos)
 {
     IPosition[] pts = new Position[cos.Length];
     for (int i = 0; i < cos.Length; i++)
     {
         NTS.Coordinate c = cos[i];
         pts[i] = new Position(c.X, c.Y);
     }
     return(pts);
 }
 /// <summary>
 /// Removes the common coordinate bits from a Geometry.
 /// The coordinates of the Geometry are changed.
 /// </summary>
 /// <param name="geom">The Geometry from which to remove the common coordinate bits.</param>
 /// <returns>The shifted Geometry.</returns>
 public IGeometry RemoveCommonBits(IGeometry geom)
 {
     if (commonCoord.X == 0.0 && commonCoord.Y == 0.0)
         return geom;
     ICoordinate invCoord = new Coordinate(commonCoord);
     invCoord.X = -invCoord.X;
     invCoord.Y = -invCoord.Y;
     Translater trans = new Translater(invCoord);            
     geom.Apply(trans);
     geom.GeometryChanged();
     return geom;
 }
Example #5
0
        public void IntersectTriangles()
        {
            ICoordinate[] v1 = new ICoordinate[7];
            v1[0] = new Coordinate(0,0);
            v1[1] = new Coordinate(5,10);
            v1[2] = new Coordinate(10,0);

            v1[3] = new Coordinate(8, 0);
            v1[4] = new Coordinate(5, 3);
            v1[5] = new Coordinate(4, 0);

            v1[6] = new Coordinate(0,0);


            IGeometry pol1 = new Polygon(new LinearRing(v1));

            ICoordinate[] v2 = new ICoordinate[5];
            v2[0] = new Coordinate(0, 0);
            v2[1] = new Coordinate(10, 0);
            v2[2] = new Coordinate(10, 1);
            v2[3] = new Coordinate(0, 1);
            v2[4] = new Coordinate(0, 0);

            IGeometry pol2 = new Polygon(new LinearRing(v2));

/*
            IGeometry g = pol1.Difference(pol2);
            Assert.AreEqual(6, g.Coordinates.Length);

            IGeometry g1 = pol1.Union(pol2);
            Assert.AreEqual(7, g1.Coordinates.Length);

*/
            var map = new Map();

            IGeometry gIntersection = pol1.Intersection(pol2);
            map.Layers.Add(new VectorLayer("g1", new DataTableFeatureProvider(new [] { gIntersection })));


/*
            map.Layers.Add(new VectorLayer("g", new DataTableFeatureProvider(new IGeometry[] { pol1 }) ));
            map.Layers.Add(new VectorLayer("g1", new DataTableFeatureProvider(new IGeometry[] { pol2 })));
*/

            MapTestHelper.ShowModal(map);
        }
        public static void Read(ref ICoordinate[] coordinates, JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("reader", "A valid JSON reader object is required.");
            }

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Array)
            {
                jreader.ReadToken(JsonTokenClass.Array);
                List <ICoordinate> list = new List <ICoordinate>();
                while (jreader.TokenClass == JsonTokenClass.Array)
                {
                    ICoordinate item = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate();
                    Read(ref item, jreader);
                    list.Add(item);
                }
                jreader.ReadToken(JsonTokenClass.EndArray);

                coordinates = list.ToArray();
            }
        }
        private static ILineString ReadLineString(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("reader", "A valid JSON reader object is required.");
            }

            ILineString line = null;

            if (jreader.TokenClass == JsonTokenClass.Array)
            {
                jreader.ReadToken(JsonTokenClass.Array);
                List <ICoordinate> list = new List <ICoordinate>();
                while (jreader.TokenClass == JsonTokenClass.Array)
                {
                    ICoordinate item = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate();
                    Read(ref item, jreader);
                    list.Add(item);
                }
                jreader.ReadToken(JsonTokenClass.EndArray);
                line = new GisSharpBlog.NetTopologySuite.Geometries.LineString(list.ToArray());
            }
            return(line);
        }
        private static IMultiPoint ReadMultiPoint(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("reader", "A valid JSON reader object is required.");
            }

            IMultiPoint points = null;

            if (jreader.TokenClass == JsonTokenClass.Array)
            {
                jreader.ReadToken(JsonTokenClass.Array);
                List <IPoint> list = new List <IPoint>();
                while (jreader.TokenClass == JsonTokenClass.Array)
                {
                    ICoordinate item = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate();
                    Read(ref item, jreader);
                    list.Add(new GisSharpBlog.NetTopologySuite.Geometries.Point(item));
                }
                jreader.ReadToken(JsonTokenClass.EndArray);
                points = new GisSharpBlog.NetTopologySuite.Geometries.MultiPoint(list.ToArray());
            }
            return(points);
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="line"></param>
 /// <returns></returns>
 private bool IsLineStringContainedInBoundary(ILineString line)
 {
     ICoordinateSequence seq = line.CoordinateSequence;
     ICoordinate p0 = new Coordinate();
     ICoordinate p1 = new Coordinate();
     for (int i = 0; i < seq.Count - 1; i++)
     {
         seq.GetCoordinate(i, p0);
         seq.GetCoordinate(i + 1, p1);
         if (!IsLineSegmentContainedInBoundary(p0, p1))
             return false;
     }
     return true;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="geom"></param>
 protected override void Visit(IGeometry geom)
 {
     if (!(geom is IPolygon))
         return;
     IEnvelope elementEnv = geom.EnvelopeInternal;
     if (! rectEnv.Intersects(elementEnv))
         return;
     // test each corner of rectangle for inclusion
     ICoordinate rectPt = new Coordinate();
     for (int i = 0; i < 4; i++) 
     {
         rectSeq.GetCoordinate(i, rectPt);
         if (!elementEnv.Contains(rectPt))
             continue;
         // check rect point in poly (rect is known not to touch polygon at this point)
         if (SimplePointInAreaLocator.ContainsPointInPolygon(rectPt, (IPolygon) geom)) 
         {
             containsPoint = true;
             return;
         }
     }
 }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="coordinates"></param>
            /// <param name="geom"></param>
            /// <returns></returns>
            public override ICoordinate[] Edit(ICoordinate[] coordinates, IGeometry geom)
            {
                if (coordinates.Length == 0) 
                    return null;

                ICoordinate[] reducedCoords = new ICoordinate[coordinates.Length];
                // copy coordinates and reduce
                for (int i = 0; i < coordinates.Length; i++) 
                {
                    ICoordinate coord = new Coordinate(coordinates[i]);
                    container.newPrecisionModel.MakePrecise( coord);
                    reducedCoords[i] = coord;
                }

                // remove repeated points, to simplify returned point as much as possible
                CoordinateList noRepeatedCoordList = new CoordinateList(reducedCoords, false);
                ICoordinate[] noRepeatedCoords = noRepeatedCoordList.ToCoordinateArray();

                /*
                * Check to see if the removal of repeated points
                * collapsed the coordinate List to an invalid length
                * for the type of the parent point.
                * It is not necessary to check for Point collapses, since the coordinate list can
                * never collapse to less than one point.
                * If the length is invalid, return the full-length coordinate array
                * first computed, or null if collapses are being removed.
                * (This may create an invalid point - the client must handle this.)
                */
                int minLength = 0;
                if (geom is ILineString) 
                    minLength = 2;
                if (geom is ILinearRing) 
                    minLength = 4;

                ICoordinate[] collapsedCoords = reducedCoords;
                if (container.removeCollapsed) 
                    collapsedCoords = null;

                // return null or orginal length coordinate array
                if (noRepeatedCoords.Length < minLength) 
                    return collapsedCoords;                

                // ok to return shorter coordinate array
                return noRepeatedCoords;
            }
Example #12
0
        public static FeatureDataRow LocatePolygon(SharpMap.Geometries.Point punto, SharpMap.Data.FeatureDataTable fdt)
        {
            FeatureDataRow fdr = null;

            if ((fdt as DataTable).Rows.Count == 1)
            {
                fdr = (FeatureDataRow)(fdt as DataTable).Rows[0];
            }
            else
            {
                GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory f = new GeometryFactory(new PrecisionModel());
                foreach (DataRow r in (fdt as DataTable).Rows)
                {
                    if ((r as  FeatureDataRow).Geometry.GetType() == typeof(SharpMap.Geometries.MultiPolygon))
                    {
                        // Doble cast: de Geometria a MultiPolygon, y de DataRow a FeatureDataRow.
                        SharpMap.Geometries.MultiPolygon SharpMultiPol = (SharpMap.Geometries.MultiPolygon)(r as  FeatureDataRow).Geometry;

                        foreach (SharpMap.Geometries.Polygon SharpPol in SharpMultiPol.Polygons)
                        {
                            //Contorno
                            int countVExt = SharpPol.ExteriorRing.Vertices.Count;
                            int i         = 0;
                            GisSharpBlog.NetTopologySuite.Geometries.Coordinate[] ListaCoordsExt = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate[countVExt];
                            foreach (SharpMap.Geometries.Point p in SharpPol.ExteriorRing.Vertices)
                            {
                                ListaCoordsExt[i++] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(p.X, p.Y);
                            }

                            //Huecos
                            int countPolInt = SharpPol.InteriorRings.Count;
                            int j           = 0;
                            GisSharpBlog.NetTopologySuite.Geometries.LinearRing[] ListaPolInt = new GisSharpBlog.NetTopologySuite.Geometries.LinearRing[countPolInt];
                            foreach (SharpMap.Geometries.LinearRing ring in SharpPol.InteriorRings)
                            {
                                int countVInt = ring.Vertices.Count;
                                int k         = 0;
                                GisSharpBlog.NetTopologySuite.Geometries.Coordinate[] ListaCoordsInt = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate[countVInt];
                                foreach (SharpMap.Geometries.Point p in ring.Vertices)
                                {
                                    ListaCoordsInt[k++] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(p.X, p.Y);
                                }
                                ListaPolInt[j++] = f.CreateLinearRing(ListaCoordsInt);
                            }

                            GisSharpBlog.NetTopologySuite.Geometries.Polygon NTSPol = f.CreatePolygon(f.CreateLinearRing(ListaCoordsExt), ListaPolInt);

                            if (NTSPol.Contains(new GisSharpBlog.NetTopologySuite.Geometries.Point(punto.X, punto.Y)) == true)
                            {
                                fdr = (FeatureDataRow)r;
                                break;
                            }
                        }
                        if (fdr != null)
                        {
                            break;
                        }
                    }
                }
            }
            return(fdr);
        }
        /// <summary>
        /// Create a new "split edge" with the section of points between
        /// (and including) the two intersections.
        /// The label for the new edge is the same as the label for the parent edge.
        /// </summary>
        /// <param name="ei0"></param>
        /// <param name="ei1"></param>
        public Edge CreateSplitEdge(EdgeIntersection ei0, EdgeIntersection ei1)
        {        
            int npts = ei1.SegmentIndex - ei0.SegmentIndex + 2;
            ICoordinate lastSegStartPt = edge.Points[ei1.SegmentIndex];
            // if the last intersection point is not equal to the its segment start pt,
            // add it to the points list as well.
            // (This check is needed because the distance metric is not totally reliable!)
            // The check for point equality is 2D only - Z values are ignored
            bool useIntPt1 = ei1.Distance > 0.0 || ! ei1.Coordinate.Equals2D(lastSegStartPt);
            if (! useIntPt1) 
                npts--;

            ICoordinate[] pts = new ICoordinate[npts];
            int ipt = 0;
            pts[ipt++] = new Coordinate(ei0.Coordinate);
            for (int i = ei0.SegmentIndex + 1; i <= ei1.SegmentIndex; i++) 
                pts[ipt++] = edge.Points[i];

            if (useIntPt1) 
                pts[ipt] = ei1.Coordinate;
            return new Edge(pts, new Label(edge.Label));
        }
Example #14
0
        public void IntersectPolygonWithLine()
        {
            ICoordinate[] coordinates = new ICoordinate[5];
            coordinates[0] = new Coordinate(0, 0);
            coordinates[1] = new Coordinate(10, 0);
            coordinates[2] = new Coordinate(10, 10);
            coordinates[3] = new Coordinate(0, 10);
            coordinates[4] = new Coordinate(0, 0);

            IGeometry pol2 = new Polygon(new LinearRing(coordinates));
            var line = new LineString(new[] { new Coordinate(5,5), new Coordinate(6,20),new Coordinate(7,5) });

            var result = pol2.Intersection(line);
        }