Ejemplo n.º 1
0
        public void SplitPolygonVerticalAtInvalidLocationThrowsException()
        {
            //create a polygon
            int callCount = 0;
            try
            {
                var polygon = new Polygon(new LinearRing(new[]{new Coordinate(0, 0), 
                                           new Coordinate(10, 10), 
                                           new Coordinate(20, 0),
                                           new Coordinate(0, 0)}));
                var polygonHalfs = GeometryHelper.SplitGeometryVerticalAt(polygon, 30.01);
                Assert.AreEqual(
                    new[] { new Coordinate(0, 0), new Coordinate(10, 10), new Coordinate(10, 0), new Coordinate(0, 0) },
                    polygonHalfs.First.Coordinates);

                Assert.AreEqual(
                    new[] { new Coordinate(10, 10), new Coordinate(20, 0), new Coordinate(10, 0), new Coordinate(10, 10) },
                    polygonHalfs.Second.Coordinates);
            }
            catch (Exception ex)
            {
                callCount++;
                Assert.IsTrue(ex is ArgumentOutOfRangeException);
                //can't use expected message since the message is only known at runtime (the decimal sep)
                string expectedMessage = string.Format("Splitpoint at x {0:00.00} not within polygon. \r\nParameter name: splitPointX", 30.01);
                Assert.AreEqual(expectedMessage, ex.Message);
            }
            //make sure the exception was thrown once
            Assert.AreEqual(1,callCount);
            
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Draws this object on the specified display
        /// </summary>
        /// <param name="display">The display to draw to</param>
        /// <param name="style">The drawing style</param>
        public virtual void Render(ISpatialDisplay display, IDrawStyle style)
        {
            if (m_Geometry is NTS.Polygon)
            {
                NTS.Polygon      p     = (NTS.Polygon)m_Geometry;
                NTS.LineString   edge  = p.ExteriorRing;
                NTS.LineString[] holes = p.InteriorRings;

                IPosition[][] outlines = new IPosition[1 + holes.Length][];
                outlines[0] = GetPositionArray(edge.Coordinates);
                for (int i = 0; i < holes.Length; i++)
                {
                    outlines[i + 1] = GetPositionArray(holes[i].Coordinates);
                }

                foreach (IPosition[] pa in outlines)
                {
                    style.Render(display, pa);
                }

                if (style is HighlightStyle)
                {
                    style.Render(display, outlines);
                }
            }
            else
            {
                style.Render(display, PositionArray);
            }
        }
Ejemplo n.º 3
0
        public void GetHashCodeShouldBeComputedLazyAndShouldBeVeryFast()
        {
            var geometryCount = 1000000;
            var geometries = new IGeometry[geometryCount];

            for (int i = 0; i < geometryCount; i++)
            {
                geometries[i] = new Polygon(new LinearRing(new[] { new Coordinate(1.0, 2.0), new Coordinate(2.0, 3.0), new Coordinate(3.0, 4.0), new Coordinate(1.0, 2.0) }));
            }

            var polygon = new Polygon(new LinearRing(new[] { new Coordinate(1.0, 2.0), new Coordinate(2.0, 3.0), new Coordinate(3.0, 4.0), new Coordinate(1.0, 2.0) }));

            // computes hash code every call
            var t0 = DateTime.Now;
            for (int i = 0; i < geometryCount; i++)
            {
                geometries[i].GetHashCode();
            }
            var t1 = DateTime.Now;

            var dt1 = t1 - t0;

            // computes hash code only first time (lazy)
            t0 = DateTime.Now;
            for (int i = 0; i < geometryCount; i++)
            {
                polygon.GetHashCode();
            }
            t1 = DateTime.Now;

            var dt2 = t1 - t0;

            Assert.IsTrue(dt2.TotalMilliseconds < 15 * dt1.TotalMilliseconds);
        }
Ejemplo n.º 4
0
 public ResultadoMapa ListLugares(DataContract.PointDC tl, DataContract.PointDC br, byte zoom, FiltroMapa filtro)
 {
     if (zoom >= 8)
     {
         PersistenceManager persistence = new PersistenceManager();
         ICriteria criteria = persistence.CreateCriteria<Lugar>();
         List<Coordinate> coordenadas = new List<Coordinate>();
         coordenadas.Add(new Coordinate(tl.Longitud, tl.Latitud));
         coordenadas.Add(new Coordinate(br.Longitud, tl.Latitud));
         coordenadas.Add(new Coordinate(br.Longitud, br.Latitud));
         coordenadas.Add(new Coordinate(tl.Longitud, br.Latitud));
         coordenadas.Add(new Coordinate(tl.Longitud, tl.Latitud));
         Polygon p = new Polygon(new LinearRing(coordenadas.ToArray()));
         criteria.SetProjection(Projections.ProjectionList()
             .Add(Projections.Property<Lugar>(l => l.ID), "ID")
             .Add(Projections.Property<Lugar>(l => l.Posicion), "Point")
             .Add(Projections.Property<Lugar>(l => l.Nombre), "Nombre")
             );
         criteria.Add(SpatialRestrictions.Within("Posicion", p));
         criteria.SetResultTransformer(Transformers.AliasToBean<ItemLugar>());
         criteria.AddOrder(new Order(Projections.Property<Lugar>(l => l.Nombre), true));
         var list = criteria.List<ItemLugar>().ToList();
         HttpContext.Current.Session["Colegios"] = list;
         return new ResultadoMapa() { Items = list };
     }
     return new ResultadoMapa() { Items = new List<ItemLugar>() };
 }
Ejemplo n.º 5
0
        public static string ToXML(Polygon[] polygons)
        {
            StringBuilder text = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(text);

            ToXML(polygons, writer);
            writer.Flush();

            return text.ToString();
        }
Ejemplo n.º 6
0
        internal static Geometries.Polygon ToSharpMapPolygon(Polygon polygon)
        {
            Geometries.LinearRing exteriorRing = ToSharpMapLinearRing((LinearRing)polygon.ExteriorRing);
            Collection <Geometries.LinearRing> interiorRings = new Collection <Geometries.LinearRing>();

            foreach (LineString interiorRing in polygon.InteriorRings)
            {
                interiorRings.Add(ToSharpMapLinearRing((LinearRing)interiorRing));
            }
            return(new Geometries.Polygon(exteriorRing, interiorRings));
        }
Ejemplo n.º 7
0
        public static void ToXML(Polygon[] polygons, XmlWriter writer)
        {
            writer.WriteStartElement("Polygons");

            foreach (Polygon polygon in polygons)
            {
                ToXML(polygon, writer);
            }

            writer.WriteEndElement();
        }
Ejemplo n.º 8
0
        internal static MultiPolygon ToNTSMultiPolygon(Geometries.MultiPolygon multiPolygon,
                                                       GeometryFactory factory)
        {
            Polygon[] polygons = new Polygon[multiPolygon.Polygons.Count];
            int       index    = 0;

            foreach (Geometries.Polygon polygon in multiPolygon.Polygons)
            {
                polygons[index++] = ToNTSPolygon(polygon, factory);
            }
            return(factory.CreateMultiPolygon(polygons) as MultiPolygon);
        }
Ejemplo n.º 9
0
        private static IMultiPolygon ReadMultiPolygon(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("reader", "A valid JSON reader object is required.");
            }

            IMultiPolygon areas = null;

            if (jreader.TokenClass == JsonTokenClass.Array)
            {
                jreader.ReadToken(JsonTokenClass.Array);
                List <IPolygon> polygons = new List <IPolygon>();
                while (jreader.TokenClass == JsonTokenClass.Array)
                {
                    jreader.ReadToken(JsonTokenClass.Array);

                    //Read the outer shell
                    ILinearRing shell = null;
                    if (jreader.TokenClass == JsonTokenClass.Array)
                    {
                        ICoordinate[] coordinates = new ICoordinate[] { };
                        Read(ref coordinates, jreader);
                        shell = new GisSharpBlog.NetTopologySuite.Geometries.LinearRing(coordinates);
                    }

                    //Read all the holes
                    List <ILinearRing> list = new List <ILinearRing>();
                    while (jreader.TokenClass == JsonTokenClass.Array)
                    {
                        ICoordinate[] coordinates = new ICoordinate[] { };
                        Read(ref coordinates, jreader);
                        ILinearRing hole = new GisSharpBlog.NetTopologySuite.Geometries.LinearRing(coordinates);
                        list.Add(hole);
                    }

                    jreader.ReadToken(JsonTokenClass.EndArray);

                    //An outer shell was found so a polygon can be created
                    if (shell != null)
                    {
                        IPolygon area = new GisSharpBlog.NetTopologySuite.Geometries.Polygon(shell, list.ToArray());
                        polygons.Add(area);
                    }
                }
                jreader.ReadToken(JsonTokenClass.EndArray);

                areas = new GisSharpBlog.NetTopologySuite.Geometries.MultiPolygon(polygons.ToArray());
            }
            return(areas);
        }
Ejemplo n.º 10
0
        public static void ToXML(Polygon polygon, XmlWriter writer)
        {
            writer.WriteStartElement("Polygon");

            writer.WriteStartElement("Shell");
            ToXML(polygon.Shell, writer);
            writer.WriteEndElement();

            writer.WriteStartElement("Holes");
            ToXML(polygon.Holes, writer);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
Ejemplo n.º 11
0
        public void SplitPolygonVerticalAt()
        {
            //create a polygon
            var polygon = new Polygon(new LinearRing(new[]{new Coordinate(0, 0), 
                                           new Coordinate(10, 10), 
                                           new Coordinate(20, 0),
                                           new Coordinate(0, 0)}));
            var polygonHalfs = GeometryHelper.SplitGeometryVerticalAt(polygon, 10);
            Assert.AreEqual(
                new[] {new Coordinate(0, 0), new Coordinate(10, 10), new Coordinate(10, 0), new Coordinate(0, 0)},
                polygonHalfs.First.Coordinates);

            Assert.AreEqual(
                new[] { new Coordinate(10, 10), new Coordinate(20, 0), new Coordinate(10, 0), new Coordinate(10, 10) },
                polygonHalfs.Second.Coordinates);
        }
Ejemplo n.º 12
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);

*/
            Map 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.Show(map);
        }
Ejemplo n.º 13
0
 internal static MultiPolygon ToNTSMultiPolygon(Geometries.MultiPolygon multiPolygon,
                                                GeometryFactory factory)
 {
     Polygon[] polygons = new Polygon[multiPolygon.Polygons.Count];
     int index = 0;
     foreach (Geometries.Polygon polygon in multiPolygon.Polygons)
         polygons[index++] = ToNTSPolygon(polygon, factory);
     return factory.CreateMultiPolygon(polygons) as MultiPolygon;
 }
Ejemplo n.º 14
0
 internal static Geometries.Polygon ToSharpMapPolygon(Polygon polygon)
 {
     Geometries.LinearRing exteriorRing = ToSharpMapLinearRing((LinearRing) polygon.ExteriorRing);
     Collection<Geometries.LinearRing> interiorRings = new Collection<Geometries.LinearRing>();
     foreach (LineString interiorRing in polygon.InteriorRings)
         interiorRings.Add(ToSharpMapLinearRing((LinearRing) interiorRing));
     return new Geometries.Polygon(exteriorRing, interiorRings);
 }
Ejemplo n.º 15
0
 public InMemoryGISPolygonFeature(LinearRing outerShell)
 {
     Shape = new Polygon(outerShell);
 }
Ejemplo n.º 16
0
        public static void Write(Polygon area, TextWriter writer)
        {
            if (area == null)
                return;
            if (writer == null)
                throw new ArgumentNullException("writer", "A valid text writer object is required.");

            JsonTextWriter jwriter = new JsonTextWriter(writer);
            Write(area, jwriter);
        }
Ejemplo n.º 17
0
        private static IGeometry GetRightHalfGeometry(IGeometry geometry, double splitPointX)
        {
            double maxXValue = geometry.EnvelopeInternal.MaxX + 1;
            double minYValue = geometry.EnvelopeInternal.MinY - 1;
            double maxYValue = geometry.EnvelopeInternal.MaxY + 1;

            var coordinatesRight = new[]
                                       {
                                           new Coordinate(splitPointX, maxYValue),
                                           new Coordinate(splitPointX, minYValue),
                                           new Coordinate(maxXValue, minYValue),
                                           new Coordinate(maxXValue, maxYValue),
                                           new Coordinate(splitPointX, maxYValue)
                                       };
            //
            var leftRectangle = new Polygon(new LinearRing(coordinatesRight));
            return geometry.Intersection(leftRectangle);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 
        /// </summary>
        public CoversTest() : base()
        {            
            polygon1 = new Polygon(new LinearRing(array1));
            polygon2 = new Polygon(new LinearRing(array2));

        }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
0
        public void CompareUsingHashcode()
        {
            var polygon = new Polygon(new LinearRing(new[] { new Coordinate(1.0, 2.0), new Coordinate(2.0, 3.0), new Coordinate(3.0, 4.0), new Coordinate(1.0, 2.0) }));

            Assert.AreEqual(495991521, polygon.GetHashCode());
        }
Ejemplo n.º 21
0
 public InMemoryGISPolygonFeature(LinearRing outerShell, LinearRing[] innerHoles)
 {
     Shape = new Polygon(outerShell, innerHoles);
 }
 public InMemoryGISMultiPolygonFeature(Polygon[] polygons)
 {
     Shape = new MultiPolygon(polygons);
 }
Ejemplo n.º 23
0
        public static void Write(Polygon area, JsonTextWriter jwriter)
        {
            if (area == null)
                return;
            if (jwriter == null)
                throw new ArgumentNullException("jwriter", "A valid JSON writer object is required.");

            jwriter.WriteStartObject();

                jwriter.WriteMember("type");
                jwriter.WriteString("Polygon");

                jwriter.WriteMember("coordinates");
                jwriter.WriteStartArray();

                    //Write the exterior boundary or shell
                    Write(area.Shell.Coordinates, jwriter);

                    //Write all the holes
                    foreach (LineString hole in area.Holes)
                    {
                        Write(hole.Coordinates, jwriter);
                    }

                jwriter.WriteEndArray();

            jwriter.WriteEndObject();
        }
Ejemplo n.º 24
0
 public void Difference()
 {
     var polygon =
         new Polygon(
             new LinearRing(new[]
                                {
                                    new Coordinate(0, 0), new Coordinate(5, 0), new Coordinate(5, 5),
                                    new Coordinate(0, 5), new Coordinate(0, 0)
                                }));
     var other =
         new Polygon(
             new LinearRing(new[]
                                {
                                    new Coordinate(0, 2), new Coordinate(8, 2), new Coordinate(1, 1),
                                    new Coordinate(0, 1), new Coordinate(0, 2)
                                }));
     
     var error = polygon.Difference(other);
 }
Ejemplo n.º 25
0
        private static Polygon ReadPolygon(JsonTextReader jreader)
        {
            if (jreader == null)
                throw new ArgumentNullException("reader", "A valid JSON reader object is required.");

            Polygon area = null;
            if (jreader.TokenClass == JsonTokenClass.Array)
            {
                jreader.ReadToken(JsonTokenClass.Array);

                //Read the outer shell
                LinearRing shell = null;
                if (jreader.TokenClass == JsonTokenClass.Array)
                {
                    Coordinate[] coordinates = new Coordinate[] { };
                    Read(ref coordinates, jreader);
                    shell = new LinearRing(coordinates);
                }

                //Read all the holes
                List<LinearRing> list = new List<LinearRing>();
                while (jreader.TokenClass == JsonTokenClass.Array)
                {
                    Coordinate[] coordinates = new Coordinate[] { };
                    Read(ref coordinates, jreader);
                    LinearRing hole = new LinearRing(coordinates);
                    list.Add(hole);
                }

                jreader.ReadToken(JsonTokenClass.EndArray);

                //An outer shell was found so a polygon can be created
                if (shell != null)
                {
                    if (list.Count > 0)
                        area = new Polygon(shell, list.ToArray());
                    else
                        area = new Polygon(shell);
                }
            }
            return area;
        }
Ejemplo n.º 26
0
 public void NormalizePolygonWithEndPointAsStartPoint()
 {
     var polygonWithRedundantPoints = new Polygon(
         new LinearRing(new[]
                            {
                                new Coordinate(1, 0), new Coordinate(0, 0), new Coordinate(0,1),
                                new Coordinate(2, 1), new Coordinate(2, 0), new Coordinate(1, 0)
                            }));
     var expectedPolygon = new Polygon(
         new LinearRing(new[]
                            {
                                new Coordinate(0, 0), new Coordinate(0, 1),
                                new Coordinate(2, 1), new Coordinate(2, 0),new Coordinate(0,0)
                            }));
     
     var actualPolygon = GeometryHelper.NormalizeGeometry(polygonWithRedundantPoints);
     Assert.AreEqual(expectedPolygon.Coordinates.Length, actualPolygon.Coordinates.Length);
     Assert.AreEqual(expectedPolygon, actualPolygon);
 }
Ejemplo n.º 27
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);
        }
Ejemplo n.º 28
0
 public void TestSimplifyBadPoly()
 {
     var geom = new Polygon(new LinearRing(new ICoordinate[] 
     {
         new Coordinate(1, 1), 
         new Coordinate(1, 1),
         new Coordinate(1, 1), 
         new Coordinate(1, 1),
         new Coordinate(1, 1)
     }));
     Debug.WriteLine("Bad polygon: " + geom);
     var simple = DouglasPeuckerSimplifier.Simplify(geom, 0.1);
     Debug.WriteLine("Simple bad polygon: " + simple);
     Assert.AreEqual(geom.GetType(), simple.GetType());
     Assert.AreNotEqual(geom, simple, "Simplify didn't do anything to this invalid polygon.");
     // This happens with JTS 1.9.0, 1.8.0 still returns GeometryCollection.Empty
     Assert.AreEqual(geom.GetType(), Polygon.Empty); 
 }