1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "collect_members.h" 20#include "relation_geometry_store.h" 23Relation_Geometry_Store::~Relation_Geometry_Store() 251764187 q delete way_geometry_store; 29Relation_Geometry_Store::Relation_Geometry_Store 30 (const std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations, 31 const Statement& query, Resource_Manager& rman, 32 double south_, double north_, double west_, double east_) 331128187 q : way_geometry_store(0), south(ilat_(south_)), north(ilat_(north_)), west(ilon_(west_)), east(ilon_(east_)) 37 // Turn off bounding box, because it isn't needed anyway 411128187 q Request_Context context(&query, rman); 431128187 q Ranges< Uint32_Index > node_ranges(south <= north 441128187 q ? get_ranges_32(south_, north_, west_, east_) : Ranges< Uint32_Index >::global()); 46 // Retrieve all nodes referred by the relations. 471128187 q Timeless< Uint32_Index, Node_Skeleton > node_members 481128187 q = relation_node_members(context, relations, {}, node_ranges, {}, true); 50 // Order node ids by id. 511128187 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator it = node_members.current.begin(); 5212718187 q it != node_members.current.end(); ++it) 541159033 q for (std::vector< Node_Skeleton >::const_iterator iit = it->second.begin(); 553870533 q iit != it->second.end(); ++iit) 562711533 q nodes.push_back(Node(iit->id, it->first.val(), iit->ll_lower)); 581128187 q sort(nodes.begin(), nodes.end(), Node_Comparator_By_Id()); 601128187 q Ranges< Uint31_Index > way_ranges(south <= north 611128187 q ? calc_parents(node_ranges) : Ranges< Uint31_Index >::global()); 63 // Retrieve all ways referred by the relations. 641128187 q Timeless< Uint31_Index, Way_Skeleton > way_members 651128187 q = relation_way_members(context, relations, {}, way_ranges, {}, true); 671128187 q way_geometry_store = new Way_Geometry_Store(way_members.current, query, rman); 69 // Order way ids by id. 701128187 q for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = way_members.current.begin(); 7155837187 q it != way_members.current.end(); ++it) 735470944 q for (std::vector< Way_Skeleton >::const_iterator iit = it->second.begin(); 7423916044 q iit != it->second.end(); ++iit) 7518445144 q ways.push_back(*iit); 771128187 q sort(ways.begin(), ways.end()); 81Relation_Geometry_Store::Relation_Geometry_Store 82 (const std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >& relations, 83 const Statement& query, Resource_Manager& rman, 84 double south_, double north_, double west_, double east_) 85636100 q : way_geometry_store(0), south(ilat_(south_)), north(ilat_(north_)), west(ilon_(west_)), east(ilon_(east_)) 89 // Turn off bounding box, because it isn't needed anyway 93636100 q Request_Context context(&query, rman); 95636100 q Ranges< Uint32_Index > node_ranges(south <= north 96636100 q ? get_ranges_32(south_, north_, west_, east_) : Ranges< Uint32_Index >::global()); 98 // Retrieve all nodes referred by the relations. 99636100 q auto nodes_by_idx = relation_node_members(context, {}, relations, node_ranges, {}, true); 101 // Order node ids by id. 102636100 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator it = nodes_by_idx.current.begin(); 10313644100 q it != nodes_by_idx.current.end(); ++it) 1051300810 q for (std::vector< Node_Skeleton >::const_iterator iit = it->second.begin(); 1061403110 q iit != it->second.end(); ++iit) 10710236 q nodes.push_back(Node(iit->id, it->first.val(), iit->ll_lower)); 109636100 q for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::iterator it = nodes_by_idx.attic.begin(); 11013644100 q it != nodes_by_idx.attic.end(); ++it) 1121300810 q for (std::vector< Attic< Node_Skeleton > >::const_iterator iit = it->second.begin(); 1131357510 q iit != it->second.end(); ++iit) 1145677 q nodes.push_back(Node(iit->id, it->first.val(), iit->ll_lower)); 116636100 q sort(nodes.begin(), nodes.end(), Node_Comparator_By_Id()); 118636100 q Ranges< Uint31_Index > way_ranges(south <= north 119636100 q ? calc_parents(node_ranges) : Ranges< Uint31_Index >::global()); 121 // Retrieve all ways referred by the relations. 122636100 q Timeless< Uint31_Index, Way_Skeleton > ways_by_idx_pair 123636100 q = relation_way_members(context, 124636100 q std::map< Uint31_Index, std::vector< Relation_Skeleton > >(), relations, 126636100 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > ways_by_idx; 127636100 q keep_matching_skeletons(ways_by_idx, ways_by_idx_pair.current, ways_by_idx_pair.attic, 128636100 q rman.get_desired_timestamp()); 130636100 q way_geometry_store = new Way_Geometry_Store(ways_by_idx, query, rman); 132636100 q for (std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >::iterator it = ways_by_idx.begin(); 1336330100 q it != ways_by_idx.end(); ++it) 135569414 q for (std::vector< Attic< Way_Skeleton > >::const_iterator iit = it->second.begin(); 1362487514 q iit != it->second.end(); ++iit) 139636100 q sort(ways.begin(), ways.end()); 143bool Relation_Geometry_Store::matches_bbox(uint32 ll_upper, uint32 ll_lower) const 14719292 q uint32 lat(::ilat(ll_upper, ll_lower)); 14819292 q int32 lon(::ilon(ll_upper, ll_lower)); 14919292 q return (lat >= south && lat <= north && 15019292 q ((lon >= west && lon <= east) 15117222 q || (east < west && (lon >= west || lon <= east)))); 155std::vector< std::vector< Quad_Coord > > Relation_Geometry_Store::get_geometry 156 (const Relation_Skeleton& relation) const 1581679351 q std::vector< std::vector< Quad_Coord > > result; 1591679351 q for (std::vector< Relation_Entry >::const_iterator it = relation.members.begin(); 160215200451 q it != relation.members.end(); ++it) 162213521151 q if (it->type == Relation_Entry::NODE) 16417218240 q const Node* node = binary_search_for_id(nodes, it->ref); 16517218240 q if (node == 0 || !matches_bbox(node->index, node->ll_lower_)) 1665172422 q result.push_back(std::vector< Quad_Coord >(1, Quad_Coord(0u, 0u))); 16812045836 q result.push_back(std::vector< Quad_Coord >(1, Quad_Coord(node->index, node->ll_lower_))); 170196302951 q else if (it->type == Relation_Entry::WAY) 172195824851 q const Way_Skeleton* way = binary_search_for_id(ways, Way_Skeleton::Id_Type(it->ref.val())); 17496800730 q result.push_back(std::vector< Quad_Coord >()); 17799024147 q result.push_back(way_geometry_store->get_geometry(*way)); 17899024147 q if (result.back().empty()) 18099022147 q else if (result.back().size() == 1) 1820 if (!matches_bbox(result.back().begin()->ll_upper, result.back().begin()->ll_lower)) 1830 *result.back().begin() = Quad_Coord(0u, 0u); 18799022147 q bool this_matches = matches_bbox(result.back()[0].ll_upper, result.back()[0].ll_lower); 18899022147 q bool next_matches = matches_bbox(result.back()[1].ll_upper, result.back()[1].ll_lower); 18999022147 q if (!this_matches && !next_matches) 190802 q result.back()[0] = Quad_Coord(0u, 0u); 191917961247 q for (uint i = 1; i < result.back().size() - 1; ++i) 193818939147 q bool last_matches = this_matches; 194818939147 q this_matches = next_matches; 195818939147 q next_matches = matches_bbox(result.back()[i+1].ll_upper, result.back()[i+1].ll_lower); 196818939147 q if (!last_matches && !this_matches && !next_matches) 1976362 q result.back()[i] = Quad_Coord(0u, 0u); 19999022147 q if (!this_matches && !next_matches) 200752 q result.back()[result.back().size()-1] = Quad_Coord(0u, 0u); 204478120 q else if (it->type == Relation_Entry::RELATION) 205478120 q result.push_back(std::vector< Quad_Coord >());