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/>. 23#include "../../template_db/block_backend.h" 24#include "../../template_db/random_file.h" 25#include "../core/settings.h" 26#include "../data/abstract_processing.h" 27#include "../data/collect_members.h" 28#include "area_query.h" 29#include "coord_query.h" 31#include "polygon_query.h" 35//----------------------------------------------------------------------------- 37class Polygon_Constraint : public Query_Constraint 40 Query_Filter_Strategy delivers_data(Resource_Manager& rman); 4212743 q Polygon_Constraint(Polygon_Query_Statement& polygon_) : polygon(&polygon_) {} 43 bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges); 44 bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges); 46 void filter(Resource_Manager& rman, Set& into); 47 void filter(const Statement& query, Resource_Manager& rman, Set& into); 480 virtual ~Polygon_Constraint() {} 51 Polygon_Query_Statement* polygon; 55Query_Filter_Strategy Polygon_Constraint::delivers_data(Resource_Manager& rman) 574939 q return (polygon && !polygon->covers_large_area()) ? prefer_ranges : ids_useful; 61bool Polygon_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges) 637043 q ranges = polygon->calc_ranges(); 68bool Polygon_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges) 7016043 q Ranges< Uint32_Index > node_ranges = polygon->calc_ranges(); 7116043 q ranges = calc_parents(node_ranges); 76void Polygon_Constraint::filter(Resource_Manager& rman, Set& into) 785343 q polygon->collect_nodes(into.nodes, true); 795343 q if (!into.attic_nodes.empty()) 801312 q polygon->collect_nodes(into.attic_nodes, true); 825343 q Ranges< Uint31_Index > ranges; 835343 q get_ranges(rman, ranges); 85 // pre-process ways to reduce the load of the expensive filter 865343 q filter_ways_by_ranges(into.ways, ranges); 875343 q if (!into.attic_ways.empty()) 882015 q filter_ways_by_ranges(into.attic_ways, ranges); 90 // pre-filter relations 915343 q filter_relations_by_ranges(into.relations, ranges); 925343 q if (!into.attic_relations.empty()) 931612 q filter_relations_by_ranges(into.attic_relations, ranges); 99void Polygon_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into) 1024939 q polygon->collect_ways(into.ways, Way_Geometry_Store(into.ways, query, rman), true, query, rman); 1054939 q Request_Context context(&query, rman); 107 // Retrieve all nodes referred by the relations. 1084939 q Ranges< Uint32_Index > node_ranges; 1094939 q get_ranges(rman, node_ranges); 1104939 q Timeless< Uint32_Index, Node_Skeleton > node_members 1114939 q = relation_node_members(context, into.relations, {}, node_ranges, {}, true); 113 // filter for those nodes that are in one of the areas 1144939 q polygon->collect_nodes(node_members.current, false); 116 // Retrieve all ways referred by the relations. 1174939 q Ranges< Uint31_Index > way_ranges; 1184939 q get_ranges(rman, way_ranges); 1194939 q Timeless< Uint31_Index, Way_Skeleton > way_members_ 1204939 q = relation_way_members(context, into.relations, {}, way_ranges, {}, true); 1224939 q polygon->collect_ways(way_members_.current, Way_Geometry_Store(way_members_.current, query, rman), false, query, rman); 1244939 q filter_relations_expensive(order_by_id(node_members.current, Order_By_Node_Id()), 1254939 q order_by_id(way_members_.current, Order_By_Way_Id()), 1294939 q if (!into.attic_ways.empty()) 1302015 q polygon->collect_ways(into.attic_ways, Way_Geometry_Store(into.attic_ways, query, rman), 1344939 q if (!into.attic_relations.empty()) 136 // Retrieve all nodes referred by the relations. 1371511 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members 1381511 q = relation_node_members(context, into.attic_relations, node_ranges); 140 // filter for those nodes that are in one of the areas 1411511 q polygon->collect_nodes(node_members, false); 143 // Retrieve all ways referred by the relations. 1441511 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_ 1451511 q = relation_way_members(context, into.attic_relations, way_ranges); 1471511 q polygon->collect_ways(way_members_, Way_Geometry_Store(way_members_, query, rman), 1501511 q filter_relations_expensive(order_attic_by_id(node_members, Order_By_Node_Id()), 1511511 q order_attic_by_id(way_members_, Order_By_Way_Id()), 158//----------------------------------------------------------------------------- 161void add_segment_blocks(std::vector< Aligned_Segment >& segments) 16318043 q std::map< Uint31_Index, std::vector< Aligned_Segment > > by_upper; 164770043 q for (std::vector< Aligned_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it) 165752043 q by_upper[it->ll_upper_].push_back(*it); 167 /* We use that more northern segments always have bigger indices. 168 Thus we can collect each block's end points and add them, if they do not 169 cancel out, to the next block further northern.*/ 17018043 q for (std::map< Uint31_Index, std::vector< Aligned_Segment > >::const_iterator 17151243 q it = by_upper.begin(); it != by_upper.end(); ++it) 17533243 q for (std::vector< Aligned_Segment >::const_iterator it2 = it->second.begin(); 176797243 q it2 != it->second.end(); ++it2) 178764043 q int32 lon_front(ilon(it->first.val() | (it2->ll_lower_a>>32), (it2->ll_lower_a) & 0xffffffffull)); 179764043 q int32 lon_back(ilon(it->first.val() | (it2->ll_lower_b>>32), (it2->ll_lower_b) & 0xffffffffull)); 180764043 q if (lons.find(lon_front) == lons.end()) 181517643 q lons.insert(lon_front); 184764043 q if (lons.find(lon_back) == lons.end()) 1947613 q uint32 lat = ilat(it->first.val(), 0) + 16*65536; 1957613 q int32 lon = ilon(it->first.val(), 0); 1967613 q uint32 northern_ll_upper = ::ll_upper_(lat, lon); 1997613 q std::vector< Aligned_Segment >& northern_block(by_upper[northern_ll_upper]); 20019613 q for (std::set< int32 >::const_iterator it2 = lons.begin(); it2 != lons.end(); ++it2) 20512013 q Aligned_Segment segment; 20612013 q segment.ll_upper_ = northern_ll_upper; 20712013 q segment.ll_lower_a = (((uint64)(::ll_upper_(lat, from)) & 0xff)<<32) | ::ll_lower(lat, from); 20812013 q segment.ll_lower_b = (((uint64)(::ll_upper_(lat, to)) & 0xff)<<32) | ::ll_lower(lat, to); 21012013 q northern_block.push_back(segment); 21518043 q for (std::map< Uint31_Index, std::vector< Aligned_Segment > >::const_iterator it = by_upper.begin(); 21651243 q it != by_upper.end(); ++it) 218797243 q for (std::vector< Aligned_Segment >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 219764043 q segments.push_back(*it2); 224Polygon_Query_Statement::Statement_Maker Polygon_Query_Statement::statement_maker; 225Polygon_Query_Statement::Criterion_Maker Polygon_Query_Statement::criterion_maker; 228Statement* Polygon_Query_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& tree_it, 229 const std::string& type, const std::string& into, 230 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output) 23218043 q uint line_nr = tree_it->line_col.first; 23418043 q if (tree_it->token == ":" && tree_it->rhs) 23618043 q std::map< std::string, std::string > attributes; 23718043 q attributes["bounds"] = decode_json(tree_it.rhs()->token, error_output); 23818043 q attributes["into"] = into; 23918043 q return new Polygon_Query_Statement(line_nr, attributes, global_settings); 246bool covers_large_area(const std::vector< std::pair< double, double > >& edges) 253690043 q for (std::vector< std::pair< double, double > >::const_iterator it = edges.begin(); it != edges.end(); ++it) 255672043 q max_lat = std::max(max_lat, it->first); 256672043 q min_lat = std::min(min_lat, it->first); 257672043 q max_lon = std::max(max_lon, it->second); 258672043 q min_lon = std::min(min_lon, it->second); 26118043 q if (max_lat < min_lat || max_lon < min_lon) 26318043 q return (max_lat - min_lat) * (max_lon - min_lon) > 1.; 267Polygon_Query_Statement::Polygon_Query_Statement 268 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings) 26918043 q : Output_Statement(line_number_) 27118043 q std::map< std::string, std::string > attributes; 27318043 q attributes["into"] = "_"; 27418043 q attributes["bounds"] = ""; 27618043 q eval_attributes_array(get_name(), attributes, input_attributes); 27818043 q set_output(attributes["into"]); 28118043 q std::istringstream in(attributes["bounds"]); 2880 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\"" 2890 " an even number of float values must be provided."); 293672043 q edges.push_back(std::make_pair(lat, lon)); 2970 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\"" 2980 " at least 3 lat/lon float value pairs must be provided."); 30018043 q for (std::vector< std::pair< double, double > >::const_iterator it = edges.begin(); 301690043 q it != edges.end(); ++it) 303672043 q if (it->first < -90.0 || it->first > 90.0) 3040 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\"" 3050 " the only allowed values for latitude are floats between -90.0 and 90.0."); 307672043 q if (it->second < -180.0 || it->second > 180.0) 3080 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\"" 3090 " the only allowed values for longitude are floats between -180.0 and 180.0."); 31218043 q covers_large_area_ = ::covers_large_area(edges); 314672043 q for (unsigned int i = 1; i < edges.size(); ++i) 315654043 q Area::calc_aligned_segments(segments, edges[i-1].first, edges[i-1].second, edges[i].first, edges[i].second); 31718043 q Area::calc_aligned_segments( 31818043 q segments, edges.back().first, edges.back().second, edges[0].first, edges[0].second); 31918043 q std::sort(segments.begin(), segments.end()); 32118043 q add_segment_blocks(segments); 325Polygon_Query_Statement::~Polygon_Query_Statement() 32718043 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin(); 32830343 q it != constraints.end(); ++it) 333Ranges< Uint32_Index > Polygon_Query_Statement::calc_ranges() 33523043 q Ranges< Uint32_Index > result; 336986043 q for (std::vector< Aligned_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it) 337963043 q result.push_back(it->ll_upper_, it->ll_upper_ + 0x100); 342template< typename Node_Skeleton > 343void Polygon_Query_Statement::collect_nodes(std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes, 34613043 q std::vector< Aligned_Segment >::const_iterator area_it = segments.begin(); 34713043 q typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator nodes_it = nodes.begin(); 35137943 q while (area_it != segments.end()) 35324943 q current_idx = area_it->ll_upper_; 35524943 q std::vector< Area_Block > areas; 356508643 q while (area_it != segments.end() && area_it->ll_upper_ == current_idx) 359483743 q block.coors.push_back(area_it->ll_lower_a); 360483743 q block.coors.push_back(area_it->ll_lower_b); 361483743 q areas.push_back(block); 36524943 q while (nodes_it != nodes.end() && nodes_it->first.val() < current_idx) 3670 nodes_it->second.clear(); 3701733643 q while (nodes_it != nodes.end() && 3711733643 q (nodes_it->first.val() & 0xffffff00) == current_idx) 3731708729 q std::vector< Node_Skeleton > into; 3741708729 q for (typename std::vector< Node_Skeleton >::const_iterator iit = nodes_it->second.begin(); 375378829929 q iit != nodes_it->second.end(); ++iit) 377377121229 q uint32 ilat((::lat(nodes_it->first.val(), iit->ll_lower) 378377121229 q + 91.0)*10000000+0.5); 379377121229 q int32 ilon(::lon(nodes_it->first.val(), iit->ll_lower)*10000000 380377121229 q + (::lon(nodes_it->first.val(), iit->ll_lower) > 0 ? 0.5 : -0.5)); 383377121229 q for (std::vector< Area_Block >::const_iterator it = areas.begin(); 3849351888229 q it != areas.end(); ++it) 3868974767429 q int check(Coord_Query_Statement::check_area_block(current_idx, *it, ilat, ilon)); 3878974767429 q if (check == Coord_Query_Statement::HIT && add_border) 3928974767029 q else if (check != 0) 39630347127 q into.push_back(*iit); 3981708729 q nodes_it->second.swap(into); 40213043 q while (nodes_it != nodes.end()) 4040 nodes_it->second.clear(); 410template< typename Way_Skeleton > 411void Polygon_Query_Statement::collect_ways 412 (std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways, 413 const Way_Geometry_Store& way_geometries, 414 bool add_border, const Statement& query, Resource_Manager& rman) 41613339 q std::map< uint32, std::vector< std::pair< uint32, Way::Id_Type > > > way_coords_to_id; 4172139139 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it) 41937991433 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 42135865632 q std::vector< Quad_Coord > coords = way_geometries.get_geometry(*it2); 422371536232 q for (std::vector< Quad_Coord >::const_iterator it3 = coords.begin(); it3 != coords.end(); ++it3) 423335670632 q way_coords_to_id[it3->ll_upper].push_back(std::make_pair(it3->ll_lower, it2->id)); 42713339 q std::map< uint32, std::vector< std::pair< uint32, Way::Id_Type > > >::const_iterator nodes_it = way_coords_to_id.begin(); 42913339 q std::vector< Aligned_Segment >::const_iterator area_it = segments.begin(); 43113339 q std::map< Uint31_Index, std::vector< Area_Block > > way_segments; 4322139139 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it) 43437991433 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 43535865632 q add_way_to_area_blocks(way_geometries.get_geometry(*it2), it2->id.val(), way_segments); 43813339 q std::map< Way::Id_Type, bool > ways_inside; 440 // Fill node_status with the area related status of each node and segment 44239039 q while (area_it != segments.end()) 44425739 q current_idx = area_it->ll_upper_; 44625739 q std::vector< Area_Block > areas; 447526939 q while (area_it != segments.end() && area_it->ll_upper_ == current_idx) 450501239 q block.coors.push_back(area_it->ll_lower_a); 451501239 q block.coors.push_back(area_it->ll_lower_b); 452501239 q areas.push_back(block); 4572253239 q while (nodes_it != way_coords_to_id.end() && nodes_it->first < current_idx) 4591619239 q while (nodes_it != way_coords_to_id.end() && 4601619239 q (nodes_it->first & 0xffffff00) == current_idx) 4621593531 q std::vector< std::pair< uint32, Way::Id_Type > > into; 4631593531 q for (std::vector< std::pair< uint32, Way::Id_Type > >::const_iterator iit = nodes_it->second.begin(); 464281122431 q iit != nodes_it->second.end(); ++iit) 466279528931 q uint32 ilat = ::ilat(nodes_it->first, iit->first); 467279528931 q int32 ilon = ::ilon(nodes_it->first, iit->first); 4707990142031 q for (std::vector< Area_Block >::const_iterator it2 = areas.begin(); it2 != areas.end(); 4737710613331 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon)); 4747710613331 q if (check == Coord_Query_Statement::HIT) 47622 q inside = Coord_Query_Statement::HIT; 482279528931 q if (inside & (Coord_Query_Statement::TOGGLE_EAST | Coord_Query_Statement::TOGGLE_WEST)) 48314113526 q ways_inside[iit->second] = true; 48925739 q for (std::vector< Area_Block >::const_iterator sit = way_segments[Uint31_Index(current_idx)].begin(); 49036884339 q sit != way_segments[Uint31_Index(current_idx)].end(); ++sit) 49336858631 q for (std::vector< Area_Block >::const_iterator it = areas.begin(); 4941014866431 q it != areas.end(); ++it) 496 // If an area segment intersects this way segment in the inner of the way, 497 // the way is contained in the area. 498 // The endpoints are properly handled via the point-in-area test 499 // Check additionally the middle of the segment to also get segments 500 // that run through the area 501978215731 q int intersect = intersects_inner(*sit, *it); 502978215731 q if (intersect == Coord_Query_Statement::INTERSECT) 504207925 q ways_inside[Way::Id_Type(sit->id)] = true; 507978007831 q else if (intersect == Coord_Query_Statement::HIT) 5100 ways_inside[Way::Id_Type(sit->id)] = true; 515978007831 q has_inner_points(*sit, *it, inside); 51736858631 q if ((inside && (!(inside & Coord_Query_Statement::HIT))) || 51836858631 q (inside && add_border)) 519957326 q ways_inside[Way::Id_Type(sit->id)] = true; 52313339 q std::map< Uint31_Index, std::vector< Way_Skeleton > > result; 525 // Mark ways as found that intersect the area border 5262139139 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it) 5282125833 q std::vector< Way_Skeleton > cur_result; 52937991433 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 53135865632 q if (ways_inside[it2->id]) 5332061926 q cur_result.push_back(*it2); 5342061926 q it2->id = Way::Id_Type(0u); 5372125833 q result[it->first].swap(cur_result); 544void Polygon_Query_Statement::execute(Resource_Manager& rman) 54844 q Polygon_Constraint constraint(*this); 54944 q Ranges< Uint32_Index > ranges; 55044 q constraint.get_ranges(rman, ranges); 55144 q get_elements_from_db< Uint32_Index, Node_Skeleton >(ranges, *this, rman) 55244 q .swap(into.nodes, into.attic_nodes); 55344 q constraint.filter(rman, into); 55444 q filter_attic_elements(rman, rman.get_desired_timestamp(), into.nodes, into.attic_nodes); 55644 q transfer_output(rman, into); 55744 q rman.health_check(*this); 561Query_Constraint* Polygon_Query_Statement::get_query_constraint() 56312339 q constraints.push_back(new Polygon_Constraint(*this)); 56412339 q return constraints.back();