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 "bbox_filter.h" 22const Ranges< Uint32_Index >& Bbox_Filter::get_ranges_32() const 24779216 q if (ranges_32.empty() && bbox.valid()) 25244216 q ranges_32 = ::get_ranges_32(bbox.south, bbox.north, bbox.west, bbox.east); 30const Ranges< Uint31_Index >& Bbox_Filter::get_ranges_31() const 33244216 q ranges_31 = calc_parents(get_ranges_32()); 38template< typename Index, typename Coord > 39void filter_by_bbox(const Bbox_Double& bbox, std::map< Index, std::vector< Coord > >& nodes) 41652216 q uint32 south = ilat_(bbox.south); 42652216 q uint32 north = ilat_(bbox.north); 43652216 q int32 west = ilon_(bbox.west); 44652216 q int32 east = ilon_(bbox.east); 4637612216 q for (typename std::map< Index, std::vector< Coord > >::iterator it = nodes.begin(); it != nodes.end(); ++it) 4836960111 q std::vector< Coord > local_into; 4936960111 q for (typename std::vector< Coord >::const_iterator iit = it->second.begin(); 5011060447111 q iit != it->second.end(); ++iit) 5211023487108 q uint32 lat(::ilat(it->first.val(), iit->ll_lower)); 5311023487108 q int32 lon(::ilon(it->first.val(), iit->ll_lower)); 5411023487108 q if ((lat >= south) && (lat <= north) && 5511023487108 q (((lon >= west) && (lon <= east)) 5610206983108 q || ((east < west) && ((lon >= west) || (lon <= east))))) 579791525108 q local_into.push_back(*iit); 5936960111 q it->second.swap(local_into); 64void filter_deriveds_by_bbox(const Bbox_Double& bbox, 65 std::map< Uint31_Index, std::vector< Derived_Structure > >& items) 67326216 q for (std::map< Uint31_Index, std::vector< Derived_Structure > >::iterator it_idx = items.begin(); 68326216 q it_idx != items.end(); ++it_idx) 700 std::vector< Derived_Structure > result; 710 for (std::vector< Derived_Structure >::const_iterator it_elem = it_idx->second.begin(); 720 it_elem != it_idx->second.end(); ++it_elem) 740 if (it_elem->get_geometry() && it_elem->get_geometry()->relevant_to_bbox(bbox)) 750 result.push_back(*it_elem); 770 result.swap(it_idx->second); 82void Bbox_Filter::filter(Set& into) const 88326216 q filter_by_bbox(bbox, into.nodes); 89326216 q filter_by_bbox(bbox, into.attic_nodes); 91326216 q const Ranges< Uint31_Index >& ranges = get_ranges_31(); 93 // pre-process ways to reduce the load of the expensive filter 94326216 q filter_ways_by_ranges(into.ways, ranges); 95326216 q filter_ways_by_ranges(into.attic_ways, ranges); 97 // pre-filter relations 98326216 q filter_relations_by_ranges(into.relations, ranges); 99326216 q filter_relations_by_ranges(into.attic_relations, ranges); 101326216 q filter_deriveds_by_bbox(bbox, into.deriveds); 106bool Bbox_Filter::matches(const std::vector< Quad_Coord >& way_geometry) const 1082015500138 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin(); 1092015500138 q if (nit == way_geometry.end()) 112 // ways with single node only 1132015368138 q if (way_geometry.size() == 1) 11411 q return bbox.contains(Point_Double(::lat(nit->ll_upper, nit->ll_lower), ::lon(nit->ll_upper, nit->ll_lower))); 1162015367138 q Point_Double first(::lat(nit->ll_upper, nit->ll_lower), ::lon(nit->ll_upper, nit->ll_lower)); 1177269297138 q for (++nit; nit != way_geometry.end(); ++nit) 1196998548138 q Point_Double second(::lat(nit->ll_upper, nit->ll_lower), ::lon(nit->ll_upper, nit->ll_lower)); 1206998548138 q if (bbox.intersects(first, second)) 128template< typename Way_Skeleton > 129void filter_ways_expensive(const Bbox_Filter& filter, const Way_Geometry_Store& way_geometries, 130 std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways) 132395196 q if (!filter.get_bbox().valid()) 135395196 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); 13841343127 q std::vector< Way_Skeleton > local_into; 13941343127 q for (typename std::vector< Way_Skeleton >::const_iterator iit = it->second.begin(); 1401994330127 q iit != it->second.end(); ++iit) 1421952987118 q if (filter.matches(way_geometries.get_geometry(*iit))) 1431722714117 q local_into.push_back(*iit); 14541343127 q it->second.swap(local_into); 150template< typename Relation_Skeleton > 151void filter_relations_expensive(const Bbox_Filter& filter, 152 const std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id, 153 const std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id, 154 const Way_Geometry_Store& way_geometries, 155 std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations) 157395196 q for (typename std::map< Uint31_Index, std::vector< Relation_Skeleton > >::iterator it = relations.begin(); 15811186196 q it != relations.end(); ++it) 1601079168 q std::vector< Relation_Skeleton > local_into; 1611079168 q for (typename std::vector< Relation_Skeleton >::const_iterator iit = it->second.begin(); 1629271668 q iit != it->second.end(); ++iit) 1648192561 q for (std::vector< Relation_Entry >::const_iterator nit = iit->members.begin(); 1651648877361 q nit != iit->members.end(); ++nit) 1671643839361 q if (nit->type == Relation_Entry::NODE) 16966194957 q const std::pair< Uint32_Index, const Node_Skeleton* >* second_nd = 17066194957 q binary_search_for_pair_id(node_members_by_id, nit->ref); 1741392051 q if (filter.get_bbox().contains(Point_Double( 1751392051 q ::lat(second_nd->first.val(), second_nd->second->ll_lower), 1761392051 q ::lon(second_nd->first.val(), second_nd->second->ll_lower)))) 178964051 q local_into.push_back(*iit); 1821577644461 q else if (nit->type == Relation_Entry::WAY) 1841572440861 q const std::pair< Uint31_Index, const Way_Skeleton* >* second_nd = 1851572440861 q binary_search_for_pair_id(way_members_by_id, nit->ref32()); 1886251360 q if (filter.matches(way_geometries.get_geometry(*second_nd->second))) 1902190560 q local_into.push_back(*iit); 1961079168 q it->second.swap(local_into); 201void Bbox_Filter::filter(const Statement& query, Resource_Manager& rman, Set& into) const 207301196 q filter_ways_expensive(*this, Way_Geometry_Store(into.ways, query, rman), into.ways); 209301196 q Request_Context context(&query, rman); 213 // Retrieve all nodes referred by the relations. 214301196 q Timeless< Uint32_Index, Node_Skeleton > node_members 215301196 q = relation_node_members(context, into.relations, {}, get_ranges_32(), {}, true); 216301196 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id 217301196 q = order_by_id(node_members.current, Order_By_Node_Id()); 219 // Retrieve all ways referred by the relations. 220301196 q Timeless< Uint31_Index, Way_Skeleton > way_members_ 221301196 q = relation_way_members(context, into.relations, {}, get_ranges_31(), {}, true); 222301196 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id 223301196 q = order_by_id(way_members_.current, Order_By_Way_Id()); 225301196 q filter_relations_expensive(*this, node_members_by_id, way_members_by_id, 226301196 q Way_Geometry_Store(way_members_.current, query, rman), into.relations); 229301196 q if (rman.get_desired_timestamp() != NOW) 2329483 q filter_ways_expensive(*this, Way_Geometry_Store(into.attic_ways, query, rman), into.attic_ways); 234 //Process attic relations 236 // Retrieve all nodes referred by the relations. 2379483 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members 2389483 q = relation_node_members(context, into.attic_relations, get_ranges_32()); 2399483 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id 2409483 q = order_attic_by_id(node_members, Order_By_Node_Id()); 242 // Retrieve all ways referred by the relations. 2439483 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_ 2449483 q = relation_way_members(context, into.attic_relations, get_ranges_31()); 2459483 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id 2469483 q = order_attic_by_id(way_members_, Order_By_Way_Id()); 2489483 q filter_relations_expensive(*this, node_members_by_id, way_members_by_id, 2499483 q Way_Geometry_Store(way_members_, query, rman), into.attic_relations);