示例#1
0
        public void TestPolygonContainsPoint()
        {
            LineairRing outer = new LineairRing(new GeoCoordinate(0, 0),
                new GeoCoordinate(5, 0), new GeoCoordinate(5, 5), new GeoCoordinate(0, 5), new GeoCoordinate(0, 0));
            LineairRing inner = new LineairRing(new GeoCoordinate(2, 2),
                new GeoCoordinate(4, 2), new GeoCoordinate(4, 4), new GeoCoordinate(2, 4),new GeoCoordinate(2, 2));

            Polygon polygon = new Polygon(outer, new LineairRing[] { inner });

            foreach (GeoCoordinate ringCoordinate in outer.Coordinates)
            {
                Assert.IsTrue(polygon.Contains(ringCoordinate));
            }
            foreach (GeoCoordinate ringCoordinate in inner.Coordinates)
            {
                Assert.IsFalse(polygon.Contains(ringCoordinate));
            }

            GeoCoordinate coordinate = new GeoCoordinate(1, 1);
            Assert.IsTrue(polygon.Contains(coordinate));
            coordinate = new GeoCoordinate(3, 3);
            Assert.IsFalse(polygon.Contains(coordinate));
            coordinate = new GeoCoordinate(-1, 1);
            Assert.IsFalse(polygon.Contains(coordinate));
        }
示例#2
0
        public void TestLineairRingContainsRing()
        {
            LineairRing outer = new LineairRing(new GeoCoordinate(0, 0),
                new GeoCoordinate(5, 0), new GeoCoordinate(5, 5), new GeoCoordinate(0, 5), new GeoCoordinate(0, 0));
            LineairRing inner = new LineairRing(new GeoCoordinate(1, 1),
                new GeoCoordinate(2, 1), new GeoCoordinate(2, 2), new GeoCoordinate(1, 2), new GeoCoordinate(1, 1));

            LineairRing test = new LineairRing(new GeoCoordinate(1, 3),
                new GeoCoordinate(2, 3), new GeoCoordinate(2, 4), new GeoCoordinate(1, 4), new GeoCoordinate(1, 3));
            Polygon polygon = new Polygon(outer, new LineairRing[] { inner });

            Assert.IsTrue(polygon.Contains(test));

            outer = new LineairRing(new GeoCoordinate(0, 0),
                new GeoCoordinate(5, 0), new GeoCoordinate(5, 5), new GeoCoordinate(0, 5), new GeoCoordinate(0, 0));
            inner = new LineairRing(new GeoCoordinate(1, 1),
                new GeoCoordinate(4, 1), new GeoCoordinate(4, 4), new GeoCoordinate(1, 4), new GeoCoordinate(1, 1));
            test = new LineairRing(new GeoCoordinate(2, 2),
                new GeoCoordinate(3, 2), new GeoCoordinate(3, 3), new GeoCoordinate(2, 3), new GeoCoordinate(2, 2));
            polygon = new Polygon(outer, new LineairRing[] { inner });

            Assert.IsFalse(polygon.Contains(test));
        }
        /// <summary>
        /// Groups the rings into polygons.
        /// </summary>
        /// <param name="rings"></param>
        /// <returns></returns>
        private Geometry GroupRings(List<KeyValuePair<bool, LineairRing>> rings)
        {
            Geometry geometry = null;
            bool[][] containsFlags = new bool[rings.Count][]; // means [x] contains [y]
            for (int x = 0; x < rings.Count; x++)
            {
                containsFlags[x] = new bool[rings.Count];
                for (int y = 0; y < x; y++)
                {
                    containsFlags[x][y] =
                        rings[x].Value.Contains(rings[y].Value);
                }
            }
            bool[] used = new bool[rings.Count];
            MultiPolygon multiPolygon = null;
            while (used.Contains(false))
            { // select a ring not contained by any other.
                LineairRing outer = null;
                int outerIdx = -1;
                for (int idx = 0; idx < rings.Count; idx++)
                {
                    if (!used[idx] && this.CheckUncontained(rings, containsFlags, used, idx))
                    { // this ring is not contained in any other used rings.
                        if (!rings[idx].Key)
                        {
                            OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Interpreter.SimpleGeometryInterpreter", TraceEventType.Error,
                                "Invalid multipolygon relation: an 'inner' ring was detected without an 'outer'.");
                        }
                        outerIdx = idx;
                        outer = rings[idx].Value;
                        used[idx] = true;
                        break;
                    }
                }
                if (outer != null)
                { // an outer ring was found, find inner rings.
                    List<LineairRing> inners = new List<LineairRing>();
                    // select all rings contained by inner but not by any others.
                    for (int idx = 0; idx < rings.Count; idx++)
                    {
                        if (!used[idx] && containsFlags[outerIdx][idx] &&
                            this.CheckUncontained(rings, containsFlags, used, idx))
                        {
                            inners.Add(rings[idx].Value);
                            used[idx] = true;
                        }
                    }

                    bool unused = !used.Contains(false);
                    if (multiPolygon == null &&
                        inners.Count == 0 &&
                        unused)
                    { // there is just one lineair ring.
                        geometry = outer;
                        break;
                    }
                    else if (multiPolygon == null &&
                        unused)
                    { // there is just one polygon.
                        Polygon polygon = new Polygon(
                            outer, inners);
                        geometry = polygon;
                        break;
                    }
                    else
                    { // there have to be other polygons.
                        {
                            multiPolygon = new MultiPolygon();
                            geometry = multiPolygon;
                        }
                        Polygon polygon = new Polygon(
                            outer, inners);
                        multiPolygon.Add(polygon);
                    }
                }
                else
                { // unused rings left but they cannot be designated as 'outer'.
                    OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Interpreter.SimpleGeometryInterpreter", TraceEventType.Error,
                        "Invalid multipolygon relation: Unassigned rings left.");
                    break;
                }
            }
            return geometry;
        }
示例#4
0
 /// <summary>
 /// Returns true if the given polygon is contained in this polygon.
 /// </summary>
 /// <returns></returns>
 public bool Contains(Polygon polygon)
 {
     return this.Contains(polygon);
 }
示例#5
0
        /// <summary>
        /// Translates a polygon.
        /// </summary>
        /// <param name="scene">The scene to add primitives to.</param>
        /// <param name="projection">The projection used to convert the objects.</param>
        /// <param name="polygon"></param>
        private void TranslatePolygon(Scene2D scene, IProjection projection, Polygon polygon)
        {
            polygon.Ring.Attributes = polygon.Attributes;
            this.TranslateLineairRing(scene, projection, polygon.Ring);
            //// build the rules.
            //List<MapCSSRuleProperties> rules =
            //    this.BuildRules(new MapCSSObject(polygon));

            //// validate what's there.
            //if (rules.Count == 0)
            //{
            //    return;
            //}
        }
        public void TestPolygonSerialization()
        {
            // polygon, no holes.
            var geometry = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                }));

            var serialized = geometry.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]}",
                serialized);

            // polygons, one hole.
            geometry = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                }),
                new LineairRing[]
                {
                    new LineairRing(
                        new GeoCoordinate[]
                        {
                            new GeoCoordinate(0.25, 0.25),
                            new GeoCoordinate(0.25, 0.75),
                            new GeoCoordinate(0.75, 0.75),
                            new GeoCoordinate(0.75, 0.25),
                            new GeoCoordinate(0.25, 0.25)
                        })
                });

            serialized = geometry.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]],[[0.25,0.25],[0.75,0.25],[0.75,0.75],[0.25,0.75],[0.25,0.25]]]}",
                serialized);
        }
示例#7
0
        /// <summary>
        /// Generates GeoJson for the given geometry.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="geometry"></param>
        /// <returns></returns>
        internal static void Write(JsonWriter writer, Polygon geometry)
        {
            if (writer == null) { throw new ArgumentNullException("writer"); }
            if (geometry == null) { throw new ArgumentNullException("geometry"); }

            writer.WriteStartObject();
            writer.WritePropertyName("type");
            writer.WriteValue("Polygon");
            writer.WritePropertyName("coordinates");
            writer.WriteStartArray();
            writer.WriteStartArray();
            foreach (var coordinate in geometry.Ring.Coordinates)
            {
                writer.WriteStartArray();
                writer.WriteValue(coordinate.Longitude);
                writer.WriteValue(coordinate.Latitude);
                writer.WriteEndArray();
            }
            writer.WriteEndArray();
            foreach(var hole in geometry.Holes)
            {
                writer.WriteStartArray();
                foreach (var coordinate in hole.Coordinates)
                {
                    writer.WriteStartArray();
                    writer.WriteValue(coordinate.Longitude);
                    writer.WriteValue(coordinate.Latitude);
                    writer.WriteEndArray();
                }
                writer.WriteEndArray();
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
        }
        public void TestMultiPolygonSerialization()
        {
            var geometry1 = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                }));
            var geometry2 = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 2),
                    new GeoCoordinate(2, 2),
                    new GeoCoordinate(2, 0),
                    new GeoCoordinate(0, 0)
                }));
            var geometry3 = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 3),
                    new GeoCoordinate(3, 3),
                    new GeoCoordinate(3, 0),
                    new GeoCoordinate(0, 0)
                }));
            var geometryCollection = new MultiPolygon(new Polygon[] { geometry1, geometry2, geometry3 });

            var serialized = geometryCollection.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]],[[[0.0,0.0],[2.0,0.0],[2.0,2.0],[0.0,2.0],[0.0,0.0]]],[[[0.0,0.0],[3.0,0.0],[3.0,3.0],[0.0,3.0],[0.0,0.0]]]]}",
                serialized);
        }
        public void TestGeometryCollectionSerialization()
        {
            var geometry1 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0)
                });
            var geometry2 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 2),
                    new GeoCoordinate(2, 2),
                    new GeoCoordinate(2, 0)
                });
            var geometry3 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 3),
                    new GeoCoordinate(3, 3),
                    new GeoCoordinate(3, 0)
                });
            var geometry4 = new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                });
            var geometry5 = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                }));
            var geometry6 = new MultiPolygon(geometry5, new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 2),
                    new GeoCoordinate(2, 2),
                    new GeoCoordinate(2, 0),
                    new GeoCoordinate(0, 0)
                })));
            var geometry7 = new Point(new GeoCoordinate(0, 1));
            var geometry8 = new MultiPoint(geometry7, new Point(new GeoCoordinate(0, 2)));
            var geometryCollection = new GeometryCollection(
                geometry1, geometry2, geometry3,
                geometry4, geometry5, geometry6,
                geometry7, geometry8);

            var serialized = geometryCollection.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]]},{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[2.0,0.0],[2.0,2.0],[0.0,2.0]]},{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[3.0,0.0],[3.0,3.0],[0.0,3.0]]},{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]},{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]},{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]],[[[0.0,0.0],[2.0,0.0],[2.0,2.0],[0.0,2.0],[0.0,0.0]]]]},{\"type\":\"Point\",\"coordinates\":[1.0,0.0]},{\"type\":\"MultiPoint\",\"coordinates\":[[1.0,0.0],[2.0,0.0]]}]}",
                serialized);
        }
示例#10
0
        public void TestFeatureSerialization()
        {
            // a feature with a point.
            var geometry = (Geometry)new Point(new GeoCoordinate(0, 1));
            var feature = new Feature(geometry);

            var serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.0,0.0]}}",
                serialized);

            feature = new Feature(geometry, new SimpleGeometryAttributeCollection(new GeometryAttribute[]
            {
                new GeometryAttribute()
                {
                    Key = "key1",
                    Value = "value1"
                }
            }));

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{\"key1\":\"value1\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.0,0.0]}}",
                serialized);

            // a feature with a linestring.
            geometry = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0)
                });
            feature = new Feature(geometry);

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]]}}",
                serialized);

            // a featurer with a linearring.
            geometry = new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                });
            feature = new Feature(geometry);

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]}}",
                serialized);

            // a featurer with a polygon.
            geometry = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                }));
            feature = new Feature(geometry);

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]}}",
                serialized);
        }
示例#11
0
        /// <summary>
        /// Adds a polygon.
        /// </summary>
        private void AddGeometry(Polygon geometry, int color, float width, bool fill)
        {
            if (geometry == null) { throw new ArgumentNullException(); }

            if(geometry.Holes != null &&
               geometry.Holes.Count() > 0)
            {
                OsmSharp.Logging.Log.TraceEvent("LayerPrimitive.AddGeometry", Logging.TraceEventType.Warning,
                    "Polygons with holes are not supported, only showing outerring.");
            }
            this.AddPolygon(geometry.Ring.Coordinates.ToArray(), color, width, fill);
        }
示例#12
0
 /// <summary>
 /// Returns true if the given polygon is contained in this polygon.
 /// </summary>
 /// <returns></returns>
 public bool Contains(Polygon polygon)
 {
     return(this.Contains(polygon));
 }