/// <summary> /// Returns true if this point is visible inside the given bounding box. /// </summary> /// <param name="box"></param> /// <returns></returns> public override bool IsInside(GeoCoordinateBox box) { if (box == null) { throw new ArgumentNullException(); } return(box.IsInside(this.Coordinate)); }
/// <summary> /// Adds the given node. /// </summary> /// <param name="node"></param> public override void AddNode(Node node) { if (!_preIndexMode) { if (_preIndex != null && _preIndex.Contains(node.Id.Value)) { // only save the coordinates for relevant nodes. // save the node-coordinates. // add the relevant nodes. if (_box == null || _box.IsInside(new GeoCoordinate((float)node.Latitude.Value, (float)node.Longitude.Value))) { // the coordinate is acceptable. _coordinates[node.Id.Value] = new float[] { (float)node.Latitude.Value, (float)node.Longitude.Value }; if (_coordinates.Count == _preIndex.Count) { _preIndex.Clear(); _preIndex = null; } } } } }
/// <summary> /// Builds the scene. /// </summary> /// <param name="map"></param> /// <param name="zoomFactor"></param> /// <param name="center"></param> /// <param name="view"></param> private void BuildScene(Map map, float zoomFactor, GeoCoordinate center, View2D view) { // build the boundingbox. var viewBox = view.OuterBox; var box = new GeoCoordinateBox(map.Projection.ToGeoCoordinates(viewBox.Min [0], viewBox.Min [1]), map.Projection.ToGeoCoordinates(viewBox.Max [0], viewBox.Max [1])); var zoomLevel = (int)map.Projection.ToZoomLevel(zoomFactor); if (_lastBox != null && _lastBox.IsInside(box) && zoomLevel == _lastZoom) { return; } _lastBox = box; _lastZoom = zoomLevel; // reset the scene. _scene2DSimple = new Scene2DSimple(); // get from the index. this.Scene.BackColor = SimpleColor.FromKnownColor(KnownColor.White).Value; _index.Get(_scene2DSimple, view, zoomFactor); }
public override bool IsVisibleIn(GeoCoordinateBox box) { return box.IsInside(_center); }
public override bool IsVisibleIn(GeoCoordinateBox box) { return box.IsInside(_dot.Point); }
/// <summary> /// Returns all the objects within a given bounding box and filtered by a given filter. /// </summary> /// <param name="box"></param> /// <param name="filter"></param> /// <returns></returns> public override IList <OsmGeo> Get(GeoCoordinateBox box, Filter filter) { List <OsmGeo> res = new List <OsmGeo>(); // load all nodes and keep the ids in a collection. HashSet <long> ids = new HashSet <long>(); foreach (Node node in _nodes.Values) { if ((filter == null || filter.Evaluate(node)) && box.IsInside(new GeoCoordinate(node.Latitude.Value, node.Longitude.Value))) { res.Add(node); ids.Add(node.Id.Value); } } // load all ways that contain the nodes that have been found. res.AddRange(this.GetWaysFor(ids).Cast <OsmGeo>()); // the .Cast<> is here for Windows Phone. // get relations containing any of the nodes or ways in the current results-list. List <Relation> relations = new List <Relation>(); HashSet <long> relationIds = new HashSet <long>(); foreach (OsmGeo osmGeo in res) { IList <Relation> relationsFor = this.GetRelationsFor(osmGeo); foreach (Relation relation in relationsFor) { if (!relationIds.Contains(relation.Id.Value)) { relations.Add(relation); relationIds.Add(relation.Id.Value); } } } // recursively add all relations containing other relations as a member. do { res.AddRange(relations.Cast <OsmGeo>()); // the .Cast<> is here for Windows Phone. List <Relation> newRelations = new List <Relation>(); foreach (OsmGeo osmGeo in relations) { IList <Relation> relationsFor = this.GetRelationsFor(osmGeo); foreach (Relation relation in relationsFor) { if (!relationIds.Contains(relation.Id.Value)) { newRelations.Add(relation); relationIds.Add(relation.Id.Value); } } } relations = newRelations; } while (relations.Count > 0); if (filter != null) { List <OsmGeo> filtered = new List <OsmGeo>(); foreach (OsmGeo geo in res) { if (filter.Evaluate(geo)) { filtered.Add(geo); } } } return(res); }
/// <summary> /// Returns all objects in the given bounding box and that pass the given filter. /// </summary> /// <param name="box"></param> /// <param name="filter"></param> /// <returns></returns> public override IList <OsmGeo> Get(GeoCoordinateBox box, OsmSharp.Osm.Filters.Filter filter) { List <OsmGeo> res = new List <OsmGeo>(); // create a range or tiles around the given bounding box. TileRange range = TileRange.CreateAroundBoundingBox(box, 14); // build all redis keys for the given boxes. var hashKeys = new List <string>(); foreach (Tile tile in range) { hashKeys.Add(tile.Id.ToString()); } byte[][] box_members = _client.SUnion(hashKeys.ToArray()); HashSet <string> nodeKeys = new HashSet <string>(); foreach (byte[] box_member in box_members) { long node_id = BitConverter.ToInt64(box_member, 0); string node_key = PrimitiveExtensions.BuildNodeRedisKey(node_id); nodeKeys.Add(node_key); } List <RedisNode> redisNodes = _clientNode.GetValues(new List <string>(nodeKeys)); var nodeIds = new List <long>(); foreach (RedisNode redisNode in redisNodes) { // test if the node is in the given bb. GeoCoordinate coordinate = new GeoCoordinate(redisNode.Latitude.Value, redisNode.Longitude.Value); if (box.IsInside(coordinate)) { res.Add(PrimitiveExtensions.ConvertFrom(redisNode)); nodeIds.Add(redisNode.Id.Value); } } // load all ways that contain the nodes that have been found. res.AddRange(this.GetWaysFor(nodeIds)); // get relations containing any of the nodes or ways in the current results-list. List <Relation> relations = new List <Relation>(); HashSet <long> relationIds = new HashSet <long>(); foreach (OsmGeo osmGeo in res) { IList <Relation> relationsFor = this.GetRelationsFor(osmGeo); foreach (Relation relation in relationsFor) { if (!relationIds.Contains(relation.Id.Value)) { relations.Add(relation); relationIds.Add(relation.Id.Value); } } } // recursively add all relations containing other relations as a member. do { res.AddRange(relations); // add previous relations-list. List <Relation> newRelations = new List <Relation>(); foreach (OsmGeo osmGeo in relations) { IList <Relation> relationsFor = this.GetRelationsFor(osmGeo); foreach (Relation relation in relationsFor) { if (!relationIds.Contains(relation.Id.Value)) { newRelations.Add(relation); relationIds.Add(relation.Id.Value); } } } relations = newRelations; } while (relations.Count > 0); if (filter != null) { List <OsmGeo> filtered = new List <OsmGeo>(); foreach (OsmGeo geo in res) { if (filter.Evaluate(geo)) { filtered.Add(geo); } } } return(res); }
/// <summary> /// Returns true if the given object is relevant in the bounding box. /// </summary> /// <param name="osmGeo"></param> /// <returns></returns> private bool IsInBB(OsmGeo osmGeo) { bool isIn = false; switch (osmGeo.Type) { case OsmGeoType.Node: isIn = _box.IsInside(new GeoCoordinate( (osmGeo as Node).Latitude.Value, (osmGeo as Node).Longitude.Value)); break; case OsmGeoType.Way: foreach (long nodeId in (osmGeo as Way).Nodes) { if (_nodesIn.Contains(nodeId)) { isIn = true; break; } } if (isIn) { foreach (long nodeId in (osmGeo as Way).Nodes) { _nodesToInclude.Add(nodeId); } } break; case OsmGeoType.Relation: if (!_relationsConsidered.Contains(osmGeo.Id.Value)) { foreach (RelationMember member in (osmGeo as Relation).Members) { switch (member.MemberType.Value) { case OsmGeoType.Node: if (_nodesIn.Contains(member.MemberId.Value)) { isIn = true; break; } break; case OsmGeoType.Way: if (_waysIn.Contains(member.MemberId.Value)) { isIn = true; break; } break; case OsmGeoType.Relation: if (_relationIn.Contains(member.MemberId.Value)) { isIn = true; break; } break; } } if (isIn) { foreach (RelationMember member in (osmGeo as Relation).Members) { switch (member.MemberType.Value) { case OsmGeoType.Node: _nodesToInclude.Add(member.MemberId.Value); break; case OsmGeoType.Way: _waysToInclude.Add(member.MemberId.Value); break; case OsmGeoType.Relation: _relationsToInclude.Add(member.MemberId.Value); break; } } } } break; } return(isIn); }
/// <summary> /// Returns true if the element is visible inside the given box. /// </summary> /// <param name="box"></param> /// <returns></returns> public override bool IsVisibleIn(GeoCoordinateBox box) { if (_box != null) { return box.Overlaps(_box); } else { return box.IsInside(_center); } }
private IList<OsmGeo> IndexObjectsInBox(int x, int y) { IList<OsmGeo> objects_in_box = null; if (_tiles_cache.ContainsKey(x) && _tiles_cache[x].TryGetValue(y, out objects_in_box)) { } else { // get all the objects in box x,y. objects_in_box = this.GetObjectsInBox(x, y); // get the box x-y. GeoCoordinateBox box = (new Tile(x, y, _zoom_level)).Box; // make box slightly smaller to prevent rounding errors. box = new GeoCoordinateBox( new GeoCoordinate(box.MaxLat - 0.00001, box.MaxLon - 0.00001), new GeoCoordinate(box.MinLat + 0.00001, box.MinLon + 0.00001)); // post-process elements to add to the elements caches! Dictionary<long, List<Way>> ways_per_node = new Dictionary<long, List<Way>>(); List<Node> nodes_outside = new List<Node>(); foreach (OsmBase base_object in objects_in_box) { if (base_object is Node) { Node other_node = this.NodeCacheTryGet(base_object.Id); if (other_node == null) { this.NodeCachePut(base_object as Node); } } else if (base_object is Way) { Way other_way = this.WayCacheTryGet(base_object.Id); if (other_way == null) { this.WayCachePut(base_object as Way); } foreach (Node node in (base_object as Way).Nodes) { Node other_node = this.NodeCacheTryGet(node.Id); if (other_node == null) { this.NodeCachePut(node); } if (box.IsInside(node.Coordinate)) { if (this.WaysPerNodeCacheTryGet(node.Id) == null) { List<Way> ways = null; if (!ways_per_node.TryGetValue(node.Id, out ways)) { ways = new List<Way>(); ways_per_node.Add(node.Id, ways); } if (!ways.Contains((base_object as Way))) { ways.Add((base_object as Way)); } } } else { nodes_outside.Add(node); } } } else if (base_object is Relation) { // TODO: cache the relation members! Relation other_relation = this.RelationCacheTryGet(base_object.Id); if (other_relation == null) { this.RelationCachePut(base_object as Relation); } } } foreach (KeyValuePair<long, List<Way>> pair in ways_per_node) { //IList<Way> ways = this.GetWaysFor(this.GetNode(pair.Key)); //if (ways.Count != pair.Value.Count) //{ // throw new Exception(); //} this.WaysPerNodeCachePut(pair.Key, pair.Value); } //foreach (Node node_outside in nodes_outside) //{ // if (this.WaysPerNodeCacheTryGet(node_outside.Id) == null) // { // IList<Way> ways = this.GetWaysFor(node_outside, false); // //this.WaysPerNodeCachePut(node_outside.Id, ways); // } //} } return objects_in_box; }