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); }
/// <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); } }
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); }
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>() }; }
public static string ToXML(Polygon[] polygons) { StringBuilder text = new StringBuilder(); XmlWriter writer = XmlWriter.Create(text); ToXML(polygons, writer); writer.Flush(); return text.ToString(); }
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)); }
public static void ToXML(Polygon[] polygons, XmlWriter writer) { writer.WriteStartElement("Polygons"); foreach (Polygon polygon in polygons) { ToXML(polygon, writer); } writer.WriteEndElement(); }
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); }
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); }
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(); }
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); }
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); }
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; }
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); }
public InMemoryGISPolygonFeature(LinearRing outerShell) { Shape = new Polygon(outerShell); }
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); }
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); }
/// <summary> /// /// </summary> public CoversTest() : base() { polygon1 = new Polygon(new LinearRing(array1)); polygon2 = new Polygon(new LinearRing(array2)); }
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); }
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()); }
public InMemoryGISPolygonFeature(LinearRing outerShell, LinearRing[] innerHoles) { Shape = new Polygon(outerShell, innerHoles); }
public InMemoryGISMultiPolygonFeature(Polygon[] polygons) { Shape = new MultiPolygon(polygons); }
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(); }
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); }
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; }
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); }
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); }
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); }