Ejemplo n.º 1
0
        public void APITestDeleteBoundingBox()
        {
            // intialize the connection.
            var apiInstance = new APIConnection("http://api06.dev.openstreetmap.org/",
                                                "osmsharp", "osmsharp");

            // get all objects in the box.
            var box = new GeoCoordinateBox(new GeoCoordinate(-0.494497, -24.119325),
                                           new GeoCoordinate(-0.494497, -24.119325));

            box = box.Resize(0.001f); // create a box around the usual test coordinates.
            List <OsmGeo> boxObjects = apiInstance.BoundingBoxGet(box);

            // delete all objects.
            apiInstance.ChangeSetOpen("delete test objects again");

            foreach (OsmGeo geoObject in boxObjects)
            {
                switch (geoObject.Type)
                {
                case OsmGeoType.Relation:
                    apiInstance.RelationDelete(geoObject as Relation);
                    break;
                }
            }

            foreach (OsmGeo geoObject in boxObjects)
            {
                switch (geoObject.Type)
                {
                case OsmGeoType.Way:
                    apiInstance.WayDelete(geoObject as Way);
                    break;
                }
            }

            foreach (OsmGeo geoObject in boxObjects)
            {
                switch (geoObject.Type)
                {
                case OsmGeoType.Node:
                    apiInstance.NodeDelete(geoObject as Node);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public void TestGetBoundingBox()
        {
            var dataSource = new MemoryDataSource();

            // test nodes.
            Node node = new Node();

            node.Id        = 1;
            node.Longitude = -2;
            node.Latitude  = -1;
            dataSource.AddNode(node);

            node           = new Node();
            node.Id        = 2;
            node.Longitude = 2;
            node.Latitude  = 1;
            dataSource.AddNode(node);

            GeoCoordinateBox box = dataSource.BoundingBox;

            IList <OsmGeo> boxResults = dataSource.Get(box, null);

            Assert.IsNotNull(boxResults);
            Assert.AreEqual(1, boxResults.Count);

            boxResults = dataSource.Get(box.Resize(0.1), null);
            Assert.IsNotNull(boxResults);
            Assert.AreEqual(2, boxResults.Count);

            node           = new Node();
            node.Id        = 3;
            node.Latitude  = 10;
            node.Longitude = 10;
            dataSource.AddNode(node);

            node           = new Node();
            node.Id        = 4;
            node.Latitude  = -10;
            node.Longitude = -10;
            dataSource.AddNode(node);

            boxResults = dataSource.Get(box, null);
            Assert.IsNotNull(boxResults);
            Assert.AreEqual(1, boxResults.Count);

            boxResults = dataSource.Get(box.Resize(0.1), null);
            Assert.IsNotNull(boxResults);
            Assert.AreEqual(2, boxResults.Count);

            // test ways.
            Way positive = new Way();

            positive.Id    = 1;
            positive.Nodes = new List <long>();
            positive.Nodes.Add(1);
            positive.Nodes.Add(2);
            dataSource.AddWay(positive);

            Way halfPositive = new Way();

            halfPositive.Id    = 2;
            halfPositive.Nodes = new List <long>();
            halfPositive.Nodes.Add(1);
            halfPositive.Nodes.Add(3);
            dataSource.AddWay(halfPositive);

            Way negative = new Way();

            negative.Id    = 3;
            negative.Nodes = new List <long>();
            negative.Nodes.Add(3);
            negative.Nodes.Add(4);
            dataSource.AddWay(negative);

            HashSet <OsmGeo> boxResultWithWays = new HashSet <OsmGeo>(dataSource.Get(box, null));

            Assert.IsTrue(boxResultWithWays.Contains(positive));
            Assert.IsTrue(boxResultWithWays.Contains(halfPositive));
            Assert.IsFalse(boxResultWithWays.Contains(negative));

            // test relations.
            Relation positiveRelation1 = new Relation();

            positiveRelation1.Id      = 1;
            positiveRelation1.Members = new List <RelationMember>();
            positiveRelation1.Members.Add(new RelationMember()
            {
                MemberId   = 1,
                MemberType = OsmGeoType.Node,
                MemberRole = "node"
            });
            dataSource.AddRelation(positiveRelation1);

            Relation positiveRelation2 = new Relation();

            positiveRelation2.Id      = 2;
            positiveRelation2.Members = new List <RelationMember>();
            positiveRelation2.Members.Add(new RelationMember()
            {
                MemberId   = 1,
                MemberType = OsmGeoType.Way,
                MemberRole = "way"
            });
            dataSource.AddRelation(positiveRelation2);

            Relation negativeRelation3 = new Relation();

            negativeRelation3.Id      = 3;
            negativeRelation3.Members = new List <RelationMember>();
            negativeRelation3.Members.Add(new RelationMember()
            {
                MemberId   = 3,
                MemberType = OsmGeoType.Way,
                MemberRole = "way"
            });
            dataSource.AddRelation(positiveRelation2);

            HashSet <OsmGeo> boxResultWithWaysAndRelations = new HashSet <OsmGeo>(dataSource.Get(box, null));

            Assert.IsTrue(boxResultWithWaysAndRelations.Contains(positiveRelation1));
            Assert.IsTrue(boxResultWithWaysAndRelations.Contains(positiveRelation2));
            Assert.IsFalse(boxResultWithWaysAndRelations.Contains(negativeRelation3));

            // test recursive relations.
            Relation recusive1 = new Relation();

            recusive1.Id      = 10;
            recusive1.Members = new List <RelationMember>();
            recusive1.Members.Add(new RelationMember()
            {
                MemberId   = 1,
                MemberType = OsmGeoType.Relation,
                MemberRole = "relation"
            });
            dataSource.AddRelation(recusive1);
            Relation recusive2 = new Relation();

            recusive2.Id      = 11;
            recusive2.Members = new List <RelationMember>();
            recusive2.Members.Add(new RelationMember()
            {
                MemberId   = 10,
                MemberType = OsmGeoType.Relation,
                MemberRole = "relation"
            });
            dataSource.AddRelation(recusive2);
            Relation recusive3 = new Relation();

            recusive3.Id      = 12;
            recusive3.Members = new List <RelationMember>();
            recusive3.Members.Add(new RelationMember()
            {
                MemberId   = 11,
                MemberType = OsmGeoType.Relation,
                MemberRole = "relation"
            });
            dataSource.AddRelation(recusive3);

            boxResultWithWaysAndRelations = new HashSet <OsmGeo>(dataSource.Get(box, null));
            Assert.IsTrue(boxResultWithWaysAndRelations.Contains(recusive1));
            Assert.IsTrue(boxResultWithWaysAndRelations.Contains(recusive2));
            Assert.IsTrue(boxResultWithWaysAndRelations.Contains(recusive3));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Finds candidate vertices for a location reference point.
        /// </summary>
        /// <param name="lrp"></param>
        /// <param name="maxVertexDistance"></param>
        /// <returns></returns>
        public virtual IEnumerable <CandidateVertex> FindCandidateVerticesFor(LocationReferencePoint lrp, Meter maxVertexDistance)
        {
            // convert to geo coordinate.
            var geoCoordinate = new GeoCoordinate(lrp.Coordinate.Latitude, lrp.Coordinate.Longitude);

            // build candidates list.
            var candidates       = new HashSet <long>();
            var scoredCandidates = new List <CandidateVertex>();

            float latitude, longitude;

            // create a search box.
            var box = new GeoCoordinateBox(geoCoordinate, geoCoordinate);

            box = box.Resize(_candidateSearchBoxSize);

            // get arcs.
            var arcs = this.Graph.GetEdges(box);

            foreach (var arc in arcs)
            {
                long vertex = arc.Item1;
                if (!candidates.Contains(vertex))
                {
                    this.Graph.GetVertex(vertex, out latitude, out longitude);
                    var distance = geoCoordinate.DistanceEstimate(new GeoCoordinate(latitude, longitude));
                    if (distance.Value < maxVertexDistance.Value)
                    {
                        candidates.Add(vertex);
                        scoredCandidates.Add(new CandidateVertex()
                        {
                            Score  = Score.New(Score.VERTEX_DISTANCE, string.Format("The vertex score compare to max distance {0}", _maxVertexDistance), (float)System.Math.Max(0, (1.0 - (distance.Value / _maxVertexDistance.Value))), 1), // calculate scoring compared to the fixed max distance.
                            Vertex = vertex
                        });
                    }
                }
                vertex = arc.Item2;
                if (!candidates.Contains(vertex))
                {
                    this.Graph.GetVertex(vertex, out latitude, out longitude);
                    var distance = geoCoordinate.DistanceEstimate(new GeoCoordinate(latitude, longitude));
                    if (distance.Value < maxVertexDistance.Value)
                    {
                        candidates.Add(vertex);
                        scoredCandidates.Add(new CandidateVertex()
                        {
                            Score  = Score.New(Score.VERTEX_DISTANCE, string.Format("The vertex score compare to max distance {0}", _maxVertexDistance), (float)System.Math.Max(0, (1.0 - (distance.Value / _maxVertexDistance.Value))), 1), // calculate scoring compared to the fixed max distance.
                            Vertex = vertex
                        });
                    }
                }
            }

            if (scoredCandidates.Count == 0)
            { // no candidates, create a virtual candidate.
                var closestEdge = this.Graph.GetClosestEdge(geoCoordinate, maxVertexDistance, 0.1);
                if (closestEdge != null)
                {
                    var coordinates = this.Graph.GetCoordinates(closestEdge);

                    OsmSharp.Math.Primitives.PointF2D          bestProjected;
                    OsmSharp.Math.Primitives.LinePointPosition bestPosition;
                    Meter bestOffset;
                    int   bestIndex;
                    if (coordinates.ProjectOn(geoCoordinate, out bestProjected, out bestPosition, out bestOffset, out bestIndex))
                    { // successfully projected, insert virtual vertex.
                        var distance = geoCoordinate.DistanceEstimate(new GeoCoordinate(bestProjected[1], bestProjected[0]));
                        if (distance.Value < maxVertexDistance.Value)
                        {
                            this.Graph.RemoveEdge(closestEdge.Item1, closestEdge.Item2);
                            this.Graph.RemoveEdge(closestEdge.Item2, closestEdge.Item1);

                            var newVertex = this.Graph.AddVertex((float)bestProjected[1], (float)bestProjected[0]);

                            // build distance before/after.
                            var distanceBefore = bestOffset.Value;
                            var distanceAfter  = closestEdge.Item3.Distance - bestOffset.Value;

                            // build coordinates before/after.
                            var coordinatesBefore = new List <GeoCoordinateSimple>(
                                coordinates.GetRange(1, bestIndex).Select(x => new GeoCoordinateSimple()
                            {
                                Latitude  = (float)x.Latitude,
                                Longitude = (float)x.Longitude
                            }));
                            var coordinatesAfter = new List <GeoCoordinateSimple>(
                                coordinates.GetRange(bestIndex + 1, coordinates.Count - 1 - bestIndex - 1).Select(x => new GeoCoordinateSimple()
                            {
                                Latitude  = (float)x.Latitude,
                                Longitude = (float)x.Longitude
                            }));

                            this.Graph.AddEdge(closestEdge.Item1, newVertex, new LiveEdge()
                            {
                                Distance = (float)distanceBefore,
                                Forward  = closestEdge.Item3.Forward,
                                Tags     = closestEdge.Item3.Tags
                            }, coordinatesBefore.Count > 0 ? coordinatesBefore.ToArray() : null);
                            this.Graph.AddEdge(newVertex, closestEdge.Item2, new LiveEdge()
                            {
                                Distance = (float)distanceAfter,
                                Forward  = closestEdge.Item3.Forward,
                                Tags     = closestEdge.Item3.Tags
                            }, coordinatesAfter.Count > 0 ? coordinatesAfter.ToArray() : null);

                            scoredCandidates.Add(new CandidateVertex()
                            {
                                Score = Score.New(Score.VERTEX_DISTANCE,
                                                  string.Format("The vertex score compare to max distance {0}", _maxVertexDistance),
                                                  (float)System.Math.Max(0, (1.0 - (distance.Value / _maxVertexDistance.Value))), 1), // calculate scoring compared to the fixed max distance.
                                Vertex = newVertex
                            });
                        }
                    }
                }
            }
            return(scoredCandidates);
        }
Ejemplo n.º 4
0
        public void APITestDeleteBoundingBox()
        {
            // intialize the connection.
            APIConnection api_instance = new APIConnection("http://api06.dev.openstreetmap.org/",
                "osmsharp", "osmsharp");

            // get all objects in the box.
            GeoCoordinateBox box = new GeoCoordinateBox(new GeoCoordinate(-0.494497, -24.119325),
                new GeoCoordinate(-0.494497, -24.119325));
            box = box.Resize(0.001f); // create a box around the usual test coordinates.
            List<SimpleOsmGeo> box_objects = api_instance.BoundingBoxGet(box);

            // delete all objects.
            long changeset_id = api_instance.ChangeSetOpen("delete test objects again");

            foreach (SimpleOsmGeo geo_object in box_objects)
            {
                switch(geo_object.Type)
                {
                    case SimpleOsmGeoType.Relation:
                        api_instance.RelationDelete(geo_object as SimpleRelation);
                        break;
                }
            }

            foreach (SimpleOsmGeo geo_object in box_objects)
            {
                switch (geo_object.Type)
                {
                    case SimpleOsmGeoType.Way:
                        api_instance.WayDelete(geo_object as SimpleWay);
                        break;
                }
            }

            foreach (SimpleOsmGeo geo_object in box_objects)
            {
                switch (geo_object.Type)
                {
                    case SimpleOsmGeoType.Node:
                        api_instance.NodeDelete(geo_object as SimpleNode);
                        break;
                }
            }
        }