Example #1
0
 /// <summary>
 /// Returns true if the given ring is contained in this polygon.
 /// </summary>
 /// <returns></returns>
 public bool Contains(LineairRing lineairRing)
 {
     // check if all points are inside this polygon.
     foreach (var coordinate in lineairRing.Coordinates)
     {
         if (!this.Contains(coordinate))
         { // a coordinate ouside of this ring can never be part of a contained inner ring.
             return(false);
         }
     }
     // check if none of the points of this ring are inside the other ring.
     foreach (var coordinate in this.Ring.Coordinates)
     {
         if (lineairRing.Contains(coordinate))
         { // a coordinate ouside of this ring can never be part of a contained inner ring.
             return(false);
         }
     }
     foreach (var hole in this.Holes)
     {
         foreach (var coordinate in hole.Coordinates)
         {
             if (lineairRing.Contains(coordinate))
             { // a coordinate ouside of this ring can never be part of a contained inner ring.
                 return(false);
             }
         }
     }
     return(true);
 }
Example #2
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));
        }
Example #3
0
        public void TestLineairRingContainsRing()
        {
            LineairRing inner = new LineairRing(new GeoCoordinate(1, 1),
                new GeoCoordinate(1, 0.2), new GeoCoordinate(0.2, 1), new GeoCoordinate(1, 1));
            LineairRing outer = new LineairRing(new GeoCoordinate(0, 0),
                new GeoCoordinate(2, 0), new GeoCoordinate(2, 2), new GeoCoordinate(0, 2), new GeoCoordinate(0, 0));

            Assert.IsTrue(outer.Contains(inner));
        }
        public void TestGetMetaData()
        {
            var ring = new LineairRing(new GeoCoordinate(0, -1), new GeoCoordinate(1, 1),
                new GeoCoordinate(-1, 1), new GeoCoordinate(0, -1));

            var source = (new OsmGeo[0]).ToOsmStreamSource();
            source.Meta.Add("source", "enumeration");
            var filter = new OsmStreamFilterPoly(ring);
            filter.RegisterSource(source);

            var meta = filter.GetAllMeta();

            Assert.IsTrue(meta.ContainsKeyValue("source", "enumeration"));
            Assert.IsTrue(meta.ContainsKeyValue("poly", OsmSharp.Geo.Streams.GeoJson.GeoJsonConverter.ToGeoJson(ring)));
        }
        public void TestNodeAfterWay()
        {
            var ring = new LineairRing(new GeoCoordinate(0, -1), new GeoCoordinate(1, 1),
                new GeoCoordinate(-1, 1), new GeoCoordinate(0, -1));

            var filter = new OsmStreamFilterPoly(ring);
            filter.RegisterSource(new OsmGeo[] {
                Way.Create(1, 1, 2),
                Node.Create(2, 10, 10)
            });
            Assert.Catch<OsmStreamNotSortedException>(() =>
            {
                var list = new List<OsmGeo>(
                   filter);
            });
        }
        public void TestOneNode()
        {
            var ring = new LineairRing(new GeoCoordinate(0, -1), new GeoCoordinate(1, 1),
                new GeoCoordinate(-1, 1), new GeoCoordinate(0, -1));

            var filter = new OsmStreamFilterPoly(ring);
            filter.RegisterSource(new OsmGeo[] {
                Node.Create(1, 0, 0),
                Node.Create(2, 10, 10)
            });
            var list = new List<OsmGeo>(
               filter);

            Assert.AreEqual(1, list.Count);
            Assert.AreEqual(1, list[0].Id);
        }
Example #7
0
 public bool Contains(LineairRing lineairRing)
 {
     foreach (GeoCoordinate coordinate in lineairRing.Coordinates)
     {
         if (!this.Contains(coordinate))
         {
             return(false);
         }
     }
     foreach (GeoCoordinate coordinate in this.Coordinates)
     {
         if (lineairRing.Contains(coordinate))
         {
             return(false);
         }
     }
     return(true);
 }
Example #8
0
        public void TestLineairRingContainsPoint()
        {
            LineairRing ring = new LineairRing(new GeoCoordinate(0, 0),
                new GeoCoordinate(3, 0), new GeoCoordinate(0, 3), new GeoCoordinate(0, 0));

            foreach (GeoCoordinate ringCoordinate in ring.Coordinates)
            {
                Assert.IsTrue(ring.Contains(ringCoordinate));
            }

            GeoCoordinate coordinate = new GeoCoordinate(1, 1);
            Assert.IsTrue(ring.Contains(coordinate));
            coordinate = new GeoCoordinate(2, 2);
            Assert.IsFalse(ring.Contains(coordinate));
            coordinate = new GeoCoordinate(-1, 1);
            Assert.IsFalse(ring.Contains(coordinate));
            coordinate = new GeoCoordinate(0, 1);
            Assert.IsTrue(ring.Contains(coordinate));
            coordinate = new GeoCoordinate(1, 0);
            Assert.IsTrue(ring.Contains(coordinate));
        }
        /// <summary>
        /// Tessellates the given LineairRings.
        /// </summary>
        /// <param name="ring"></param>
        /// <returns>A list of coordinates grouped per three.</returns>
        public GeoCoordinate[] Tessellate(LineairRing ring)
        {
            // TODO: yes i know this can be more efficient; proof of concept!
            // TODO: yes i know we know the number of triangles beforehand.
            // TODO: yes i know we can create a strip instead of duplicating coordinates!!

            List<GeoCoordinate> triangles = new List<GeoCoordinate>();
            if (ring.Coordinates.Count < 3)
            {
                throw new ArgumentOutOfRangeException("Invalid ring detected, only 1 or 2 vertices.");
            }
            LineairRing workRing = new LineairRing(ring.Coordinates);
            while (workRing.Coordinates.Count > 3)
            { // cut an ear.
                int earIdx = 0;
                while(!workRing.IsEar(earIdx))
                {
                    earIdx++;
                }

                // ear should be found, cut it!
                GeoCoordinate[] neighbours = workRing.GetNeigbours(earIdx);
                triangles.Add(neighbours[0]);
                triangles.Add(neighbours[1]);
                triangles.Add(workRing.Coordinates[earIdx]);

                // remove ear and update workring.
                List<GeoCoordinate> ringCoordinates = workRing.Coordinates;
                ringCoordinates.RemoveAt(earIdx);
                workRing = new LineairRing(ringCoordinates);
            }
            if (ring.Coordinates.Count == 3)
            { // this ring is already a triangle.
                triangles.Add(ring.Coordinates[0]);
                triangles.Add(ring.Coordinates[1]);
                triangles.Add(ring.Coordinates[2]);
            }
            return triangles.ToArray();
        }
Example #10
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));
        }
Example #11
0
 /// <summary>
 /// Creates a new polygon.
 /// </summary>
 /// <param name="outline">The outline of the polygon.</param>
 public Polygon(LineairRing outline)
 {
     this.Holes = new List <LineairRing>();
     this.Ring  = outline;
 }
        public void TestOneNodeAndOneRelation()
        {
            var ring = new LineairRing(new GeoCoordinate(0, -1), new GeoCoordinate(1, 1),
                new GeoCoordinate(-1, 1), new GeoCoordinate(0, -1));

            var filter = new OsmStreamFilterPoly(ring);
            filter.RegisterSource(new OsmGeo[] {
                Node.Create(1, 0, 0),
                Relation.Create(1, new RelationMember()
                {
                    MemberId = 1,
                    MemberType = OsmGeoType.Node,
                    MemberRole = string.Empty
                })
            });
            var list = new List<OsmGeo>(
               filter);

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual(1, list[0].Id);
            Assert.AreEqual(OsmGeoType.Node, list[0].Type);
            Assert.AreEqual(1, list[1].Id);
            Assert.AreEqual(OsmGeoType.Relation, list[1].Type);
        }
        public void TestWayAfterRelation()
        {
            var ring = new LineairRing(new GeoCoordinate(0, -1), new GeoCoordinate(1, 1),
                new GeoCoordinate(-1, 1), new GeoCoordinate(0, -1));

            var filter = new OsmStreamFilterPoly(ring);
            filter.RegisterSource(new OsmGeo[] {
                Relation.Create(1, new RelationMember()
                {
                    MemberId = 1,
                    MemberType = OsmGeoType.Node,
                    MemberRole = string.Empty
                }),
                Way.Create(1, 1, 2)
            });
            Assert.Catch<OsmStreamNotSortedException>(() =>
            {
                var list = new List<OsmGeo>(
                   filter);
            });
        }
Example #14
0
        /// <summary>
        /// Adds a ring.
        /// </summary>
        private void AddGeometry(LineairRing geometry, int color, float width, bool fill)
        {
            if (geometry == null) { throw new ArgumentNullException(); }

            this.AddPolygon(geometry.Coordinates.ToArray(), color, width, fill);
        }
Example #15
0
        /// <summary>
        /// Translates a lineair ring.
        /// </summary>
        /// <param name="scene">The scene to add primitives to.</param>
        /// <param name="projection">The projection used to convert the objects.</param>
        /// <param name="lineairRing"></param>
        private void TranslateLineairRing(Scene2D scene, IProjection projection, LineairRing lineairRing)
        {
            // build the rules.
            List<MapCSSRuleProperties> rules =
                this.BuildRules(new MapCSSObject(lineairRing));

            // validate what's there.
            if (rules.Count == 0)
            {
                return;
            }

            // get x/y.
            double[] x = null, y = null;
            if (lineairRing.Coordinates != null &&
                lineairRing.Coordinates.Count > 0)
            { // pre-calculate x/y.
                x = new double[lineairRing.Coordinates.Count];
                y = new double[lineairRing.Coordinates.Count];
                for (int idx = 0; idx < lineairRing.Coordinates.Count; idx++)
                {
                    x[idx] = projection.LongitudeToX(
                        lineairRing.Coordinates[idx].Longitude);
                    y[idx] = projection.LatitudeToY(
                        lineairRing.Coordinates[idx].Latitude);
                }

                // simplify.
                if (x.Length > 2)
                {
                    double[][] simplified = SimplifyCurve.Simplify(new double[][] { x, y }, 0.0001);
                    x = simplified[0];
                    y = simplified[1];
                }
            }
            // add the z-index.
            foreach (var rule in rules)
            {
                float minZoom = (float)projection.ToZoomFactor(rule.MinZoom);
                float maxZoom = (float)projection.ToZoomFactor(rule.MaxZoom);

                int zIndex;
                if (!rule.TryGetProperty<int>("zIndex", out zIndex))
                {
                    zIndex = 0;
                }

                // interpret the results.
                if (x != null)
                { // there is a valid interpretation of this way.
                    int color;
                    int fillColor;
                    if (rule.TryGetProperty("fillColor", out fillColor))
                    { // render as an area.
                        float fillOpacity;
                        if(rule.TryGetProperty("fillOpacity", out fillOpacity))
                        {
                            SimpleColor simpleFillColor = new SimpleColor() { Value = fillColor };
                            fillColor = SimpleColor.FromArgb((int)(255 * fillOpacity),
                                simpleFillColor.R, simpleFillColor.G, simpleFillColor.B).Value;
                        }
                        uint? pointsId = scene.AddPoints(x, y);
                        if (pointsId.HasValue)
                        {
                            scene.AddStylePolygon(pointsId.Value, this.CalculateSceneLayer(OffsetArea, zIndex), minZoom, maxZoom, fillColor, 1, true);
                            if (rule.TryGetProperty("color", out color))
                            {
                                scene.AddStylePolygon(pointsId.Value, this.CalculateSceneLayer(OffsetCasing, zIndex), minZoom, maxZoom, color, 1, false);
                            }
                        }
                    }
                }
            }
        }
Example #16
0
 /// <summary>
 /// Creates a new polygon.
 /// </summary>
 public Polygon(LineairRing outline, IEnumerable<LineairRing> holes)
 {
     this.Holes = holes;
     this.Ring = outline;
 }
        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);
        }
        /// <summary>
        /// Creates a new lineair ring from the given way and updates the assigned flags array.
        /// </summary>
        /// <param name="ways"></param>
        /// <param name="way"></param>
        /// <param name="assignedFlags"></param>
        /// <param name="ring"></param>
        /// <returns></returns>
        private bool AssignRing(List<KeyValuePair<bool, CompleteWay>> ways, int way, bool[] assignedFlags, out LineairRing ring)
        {
            List<GeoCoordinate> coordinates = null;
            assignedFlags[way] = true;
            if (ways[way].Value.IsClosed())
            { // the was is closed.
                // TODO: validate geometry: this should be a non-intersecting way.
                coordinates = ways[way].Value.GetCoordinates();
            }
            else
            { // the way is open.
                bool roleFlag = ways[way].Key;

                // complete the ring.
                List<CompleteNode> nodes = new List<CompleteNode>(ways[way].Value.Nodes);
                if (this.CompleteRing(ways, assignedFlags, nodes, roleFlag))
                { // the ring was completed!
                    coordinates = new List<GeoCoordinate>(nodes.Count);
                    foreach (CompleteNode node in nodes)
                    {
                        coordinates.Add(node.Coordinate);
                    }
                }
                else
                { // oeps, assignment failed: backtrack again!
                    assignedFlags[way] = false;
                    ring = null;
                    return false;
                }
            }
            ring = new LineairRing(coordinates);
            return true;
        }
Example #19
0
        /// <summary>
        /// Builds the geometry from the given coordinates.
        /// </summary>
        /// <param name="coordinates"></param>
        /// <returns></returns>
        internal static Polygon BuildPolygon(List<object> coordinates)
        {
            if (coordinates == null) { throw new ArgumentNullException(); }
            if (coordinates.Count >= 1)
            {
                var polygonCoordinates = new List<List<GeoCoordinate>>();
                foreach (List<object> coordinates1 in coordinates)
                {
                    var lineStringCoordinates = new List<GeoCoordinate>();
                    for (int idx = 0; idx < coordinates1.Count; idx++)
                    {
                        var pointCoordinate = coordinates1[idx] as List<object>;
                        if (pointCoordinate != null &&
                            pointCoordinate.Count == 2 &&
                            pointCoordinate[0] is double &&
                            pointCoordinate[1] is double)
                        {
                            lineStringCoordinates.Add(new Math.Geo.GeoCoordinate(
                                (double)pointCoordinate[1], (double)pointCoordinate[0]));
                        }
                    }
                    polygonCoordinates.Add(lineStringCoordinates);
                }

                var outer = new LineairRing(polygonCoordinates[0]);
                var holes = new List<LineairRing>();
                for (int idx = 1; idx < polygonCoordinates.Count; idx++)
                {
                    holes.Add(new LineairRing(polygonCoordinates[idx]));
                }
                return new Polygon(outer, holes);
            }
            throw new Exception("Invalid coordinate collection.");
        }
        public void TestLineairRingSerialization()
        {
            var 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)
                });

            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);
        }
Example #21
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, LineairRing 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.Coordinates)
            {
                writer.WriteStartArray();
                writer.WriteValue(coordinate.Longitude);
                writer.WriteValue(coordinate.Latitude);
                writer.WriteEndArray();
            }
            writer.WriteEndArray();
            writer.WriteEndArray();
            writer.WriteEndObject();
        }
Example #22
0
 public Polygon(LineairRing outline, IEnumerable <LineairRing> holes)
 {
     this.Holes = holes;
     this.Ring  = outline;
 }
Example #23
0
 public Polygon(LineairRing outline)
 {
     this.Holes = (IEnumerable <LineairRing>) new List <LineairRing>();
     this.Ring  = outline;
 }
Example #24
0
 /// <summary>
 /// Returns true if the given ring is contained in this ring.
 /// </summary>
 /// <param name="lineairRing"></param>
 /// <returns></returns>
 public bool Contains(LineairRing lineairRing)
 {
     // check if all points are inside this ring.
     foreach (var coordinate in lineairRing.Coordinates)
     {
         if (!this.Contains(coordinate))
         { // a coordinate ouside of this ring can never be part of a contained inner ring.
             return false;
         }
     }
     // check if none of the points of this ring are inside the other ring.
     foreach (var coordinate in this.Coordinates)
     {
         if (lineairRing.Contains(coordinate))
         { // a coordinate ouside of this ring can never be part of a contained inner ring.
             return false;
         }
     }
     return true;
 }
        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);
        }
        /// <summary>
        /// Converts a lineairring into osm objects.
        /// </summary>
        /// <param name="linearRing"></param>
        /// <returns></returns>
        private static LineairRing ConvertLinearRing(OsmSharp.Xml.Kml.v2_1.LinearRingType linearRing)
        {
            // convert the coordinates.
            IList<GeoCoordinate> coordinates = KmlGeoStreamSource.ConvertCoordinates(linearRing.coordinates);

            // create the ring.
            LineairRing ring = new LineairRing(coordinates);
            ring.Attributes = new SimpleGeometryAttributeCollection();
            ring.Attributes.Add("id", linearRing.id);

            return ring;
        }
Example #27
0
 /// <summary>
 /// Creates a new polygon.
 /// </summary>
 public Polygon(LineairRing outline)
 {
     this.Holes = new List<LineairRing>();
     this.Ring = outline;
 }
        /// <summary>
        /// Interprets an OSM-object and returns the corresponding geometry.
        /// </summary>
        /// <param name="osmObject"></param>
        /// <returns></returns>
        public override GeometryCollection Interpret(CompleteOsmGeo osmObject)
        {
            // DISCLAIMER: this is a very very very simple geometry interpreter and
            // contains hardcoded all relevant tags.

            GeometryCollection collection = new GeometryCollection();
            TagsCollectionBase tags;
            if (osmObject != null)
            {
                switch (osmObject.Type)
                {
                    case CompleteOsmType.Node:
                        TagsCollection newCollection = new TagsCollection(
                            osmObject.Tags);
                        newCollection.RemoveKey("FIXME");
                        newCollection.RemoveKey("node");
                        newCollection.RemoveKey("source");

                        if (newCollection.Count > 0)
                        { // there is still some relevant information left.
                            collection.Add(new Point((osmObject as CompleteNode).Coordinate));
                        }
                        break;
                    case CompleteOsmType.Way:
                        tags = osmObject.Tags;

                        bool isArea = false;
                        if ((tags.ContainsKey("building") && !tags.IsFalse("building")) ||
                            (tags.ContainsKey("landuse") && !tags.IsFalse("landuse")) ||
                            (tags.ContainsKey("amenity") && !tags.IsFalse("amenity")) ||
                            (tags.ContainsKey("harbour") && !tags.IsFalse("harbour")) ||
                            (tags.ContainsKey("historic") && !tags.IsFalse("historic")) ||
                            (tags.ContainsKey("leisure") && !tags.IsFalse("leisure")) ||
                            (tags.ContainsKey("man_made") && !tags.IsFalse("man_made")) ||
                            (tags.ContainsKey("military") && !tags.IsFalse("military")) ||
                            (tags.ContainsKey("natural") && !tags.IsFalse("natural")) ||
                            (tags.ContainsKey("office") && !tags.IsFalse("office")) ||
                            (tags.ContainsKey("place") && !tags.IsFalse("place")) ||
                            (tags.ContainsKey("power") && !tags.IsFalse("power")) ||
                            (tags.ContainsKey("public_transport") && !tags.IsFalse("public_transport")) ||
                            (tags.ContainsKey("shop") && !tags.IsFalse("shop")) ||
                            (tags.ContainsKey("sport") && !tags.IsFalse("sport")) ||
                            (tags.ContainsKey("tourism") && !tags.IsFalse("tourism")) ||
                            (tags.ContainsKey("waterway") && !tags.IsFalse("waterway")) ||
                            (tags.ContainsKey("wetland") && !tags.IsFalse("wetland")) ||
                            (tags.ContainsKey("water") && !tags.IsFalse("water")) ||
                            (tags.ContainsKey("aeroway") && !tags.IsFalse("aeroway")))
                        { // these tags usually indicate an area.
                            isArea = true;
                        }

                        if (tags.IsTrue("area"))
                        { // explicitly indicated that this is an area.
                            isArea = true;
                        }
                        else if (tags.IsFalse("area"))
                        { // explicitly indicated that this is not an area.
                            isArea = false;
                        }

                        if (isArea)
                        { // area tags leads to simple polygon
                            LineairRing lineairRing = new LineairRing((osmObject as CompleteWay).GetCoordinates().ToArray<GeoCoordinate>());
                            lineairRing.Attributes = new SimpleGeometryAttributeCollection(tags);
                            collection.Add(lineairRing);
                        }
                        else
                        { // no area tag leads to just a line.
                            LineString lineString = new LineString((osmObject as CompleteWay).GetCoordinates().ToArray<GeoCoordinate>());
                            lineString.Attributes = new SimpleGeometryAttributeCollection(tags);
                            collection.Add(lineString);
                        }
                        break;
                    case CompleteOsmType.Relation:
                        CompleteRelation relation = (osmObject as CompleteRelation);
                        tags = relation.Tags;

                        string typeValue;
                        if (tags.TryGetValue("type", out typeValue))
                        { // there is a type in this relation.
                            if (typeValue == "multipolygon")
                            { // this relation is a multipolygon.
                                Geometry geometry = this.InterpretMultipolygonRelation(relation);
                                if (geometry != null)
                                { // add the geometry.
                                    collection.Add(geometry);
                                }
                            }
                            else if (typeValue == "boundary")
                            { // this relation is a boundary.

                            }
                        }
                        break;
                }
            }
            return collection;
        }
Example #29
0
 /// <summary>
 /// Returns false if this mapcss interpreter does not contain an interpretation for an area with the given tags.
 /// </summary>
 /// <param name="tagsCollection"></param>
 /// <returns></returns>
 private bool AppliesToArea(TagsCollectionBase tagsCollection)
 {
     LineairRing ring = new LineairRing();
     ring.Attributes = new SimpleGeometryAttributeCollection(tagsCollection);
     List<MapCSSRuleProperties> rules = this.BuildRules(new MapCSSObject(ring));
     return rules != null && rules.Count > 0;
 }