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 24703188 q if (ranges_32.empty() && bbox.valid()) 25212188 q ranges_32 = ::get_ranges_32(bbox.south, bbox.north, bbox.west, bbox.east); 30const Ranges< Uint31_Index >& Bbox_Filter::get_ranges_31() const 33212188 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) 41570188 q uint32 south = ilat_(bbox.south); 42570188 q uint32 north = ilat_(bbox.north); 43570188 q int32 west = ilon_(bbox.west); 44570188 q int32 east = ilon_(bbox.east); 4626934188 q for (typename std::map< Index, std::vector< Coord > >::iterator it = nodes.begin(); it != nodes.end(); ++it) 4826364110 q std::vector< Coord > local_into; 4926364110 q for (typename std::vector< Coord >::const_iterator iit = it->second.begin(); 503705495110 q iit != it->second.end(); ++iit) 523679131110 q uint32 lat(::ilat(it->first.val(), iit->ll_lower)); 533679131110 q int32 lon(::ilon(it->first.val(), iit->ll_lower)); 543679131110 q if ((lat >= south) && (lat <= north) && 553679131110 q (((lon >= west) && (lon <= east)) 563364203109 q || ((east < west) && ((lon >= west) || (lon <= east))))) 573148320109 q local_into.push_back(*iit); 5926364110 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) 67285188 q for (std::map< Uint31_Index, std::vector< Derived_Structure > >::iterator it_idx = items.begin(); 68285188 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 88285188 q filter_by_bbox(bbox, into.nodes); 89285188 q filter_by_bbox(bbox, into.attic_nodes); 91285188 q const Ranges< Uint31_Index >& ranges = get_ranges_31(); 93 // pre-process ways to reduce the load of the expensive filter 94285188 q filter_ways_by_ranges(into.ways, ranges); 95285188 q filter_ways_by_ranges(into.attic_ways, ranges); 97 // pre-filter relations 98285188 q filter_relations_by_ranges(into.relations, ranges); 99285188 q filter_relations_by_ranges(into.attic_relations, ranges); 101285188 q filter_deriveds_by_bbox(bbox, into.deriveds); 106bool Bbox_Filter::matches(const std::vector< Quad_Coord >& way_geometry) const 108247458129 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin(); 109247458129 q if (nit == way_geometry.end()) 112 // ways with single node only 113247455129 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))); 116247454129 q Point_Double first(::lat(nit->ll_upper, nit->ll_lower), ::lon(nit->ll_upper, nit->ll_lower)); 1174641647129 q for (++nit; nit != way_geometry.end(); ++nit) 1194570673129 q Point_Double second(::lat(nit->ll_upper, nit->ll_lower), ::lon(nit->ll_upper, nit->ll_lower)); 1204570673129 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) 132351169 q if (!filter.get_bbox().valid()) 135351169 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); 13819541110 q std::vector< Way_Skeleton > local_into; 13919541110 q for (typename std::vector< Way_Skeleton >::const_iterator iit = it->second.begin(); 140245302110 q iit != it->second.end(); ++iit) 142225761101 q if (filter.matches(way_geometries.get_geometry(*iit))) 143172247101 q local_into.push_back(*iit); 14519541110 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) 157351169 q for (typename std::map< Uint31_Index, std::vector< Relation_Skeleton > >::iterator it = relations.begin(); 1584288169 q it != relations.end(); ++it) 160393781 q std::vector< Relation_Skeleton > local_into; 161393781 q for (typename std::vector< Relation_Skeleton >::const_iterator iit = it->second.begin(); 1622665781 q iit != it->second.end(); ++iit) 1642272076 q for (std::vector< Relation_Entry >::const_iterator nit = iit->members.begin(); 165247095576 q nit != iit->members.end(); ++nit) 167245260076 q if (nit->type == Relation_Entry::NODE) 1691879873 q const std::pair< Uint32_Index, const Node_Skeleton* >* second_nd = 1701879873 q binary_search_for_pair_id(node_members_by_id, nit->ref); 17429127 q if (filter.get_bbox().contains(Point_Double( 17529127 q ::lat(second_nd->first.val(), second_nd->second->ll_lower), 17629127 q ::lon(second_nd->first.val(), second_nd->second->ll_lower)))) 17813125 q local_into.push_back(*iit); 182243380276 q else if (nit->type == Relation_Entry::WAY) 184239539576 q const std::pair< Uint31_Index, const Way_Skeleton* >* second_nd = 185239539576 q binary_search_for_pair_id(way_members_by_id, nit->ref32()); 1882169774 q if (filter.matches(way_geometries.get_geometry(*second_nd->second))) 190423472 q local_into.push_back(*iit); 196393781 q it->second.swap(local_into); 201void Bbox_Filter::filter(const Statement& query, Resource_Manager& rman, Set& into) const 207262169 q filter_ways_expensive(*this, Way_Geometry_Store(into.ways, query, rman), into.ways); 209262169 q Request_Context context(&query, rman); 213 // Retrieve all nodes referred by the relations. 214262169 q Timeless< Uint32_Index, Node_Skeleton > node_members 215262169 q = relation_node_members(context, into.relations, {}, get_ranges_32(), {}, true); 216262169 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id 217262169 q = order_by_id(node_members.current, Order_By_Node_Id()); 219 // Retrieve all ways referred by the relations. 220262169 q Timeless< Uint31_Index, Way_Skeleton > way_members_ 221262169 q = relation_way_members(context, into.relations, {}, get_ranges_31(), {}, true); 222262169 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id 223262169 q = order_by_id(way_members_.current, Order_By_Way_Id()); 225262169 q filter_relations_expensive(*this, node_members_by_id, way_members_by_id, 226262169 q Way_Geometry_Store(way_members_.current, query, rman), into.relations); 229262169 q if (rman.get_desired_timestamp() != NOW) 2328975 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. 2378975 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members 2388975 q = relation_node_members(context, into.attic_relations, get_ranges_32()); 2398975 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id 2408975 q = order_attic_by_id(node_members, Order_By_Node_Id()); 242 // Retrieve all ways referred by the relations. 2438975 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_ 2448975 q = relation_way_members(context, into.attic_relations, get_ranges_31()); 2458975 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id 2468975 q = order_attic_by_id(way_members_, Order_By_Way_Id()); 2488975 q filter_relations_expensive(*this, node_members_by_id, way_members_by_id, 2498975 q Way_Geometry_Store(way_members_, query, rman), into.attic_relations);