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/>. 32#include "../../template_db/block_backend.h" 33#include "../data/collect_items.h" 34#include "../data/tilewise_geometry.h" 35#include "coord_query.h" 38bool Coord_Query_Statement::is_used_ = false; 40Generic_Statement_Maker< Coord_Query_Statement > Coord_Query_Statement::statement_maker("coord-query"); 42Coord_Query_Statement::Coord_Query_Statement 43 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings) 4415050 q : Output_Statement(line_number_) 4815050 q std::map< std::string, std::string > attributes; 5015050 q attributes["from"] = "_"; 5115050 q attributes["into"] = "_"; 5215050 q attributes["lat"] = ""; 5315050 q attributes["lon"] = ""; 5515050 q eval_attributes_array(get_name(), attributes, input_attributes); 5715050 q input = attributes["from"]; 5815050 q set_output(attributes["into"]); 6215050 q if (attributes["lat"] != "" || attributes["lon"] != "") 646321 q lat = atof(attributes["lat"].c_str()); 656321 q if ((lat < -90.0) || (lat > 90.0) || (attributes["lat"] == "")) 670 std::ostringstream temp; 680 temp<<"For the attribute \"lat\" of the element \"coord-query\"" 690 <<" the only allowed values are floats between -90.0 and 90.0."; 700 add_static_error(temp.str()); 736321 q lon = atof(attributes["lon"].c_str()); 746321 q if ((lon < -180.0) || (lon > 180.0) || (attributes["lon"] == "")) 760 std::ostringstream temp; 770 temp<<"For the attribute \"lon\" of the element \"coord-query\"" 780 <<" the only allowed values are floats between -180.0 and 180.0."; 790 add_static_error(temp.str()); 86/*overall strategy: we count the number of intersections of a straight line from 87 the coordinates to the southern end of the block. If it is odd, the coordinate 88 is inside the area, if not, they are not. 90int Coord_Query_Statement::check_area_block 91 (uint32 ll_index, const Area_Block& area_block, 92 uint32 coord_lat, int32 coord_lon) 94 // An area block is a chain of segments. We consider each 95 // segment individually. This falls into different cases, determined by 96 // the relative position of the intersection of the segment and a straight 97 // line from north to south through the coordinate to test. 98 // (1) If the segment intersects north of the coordinates or doesn't intersect 99 // at all, we can ignore it. 100 // (2) If the segment intersects at exactly the coordinates, the coordinates 101 // are on the boundary, hence in our definition part of the area. 102 // (3) If the segment intersects south of us, it toggles whether we are inside 103 // or outside the area 104 // (4) A special case is if one endpoint is the intersection point. We then toggle 105 // only either the western or the eastern side. We are part of the area if in the 106 // end the western or eastern side have an odd state. 108360675525105 q std::vector< uint64 >::const_iterator it(area_block.coors.begin()); 109360675525105 q uint32 lat = ::ilat(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff)); 110360675525105 q int32 lon = ::ilon(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff)); 1113737933182105 q while (++it != area_block.coors.end()) 1133377377903105 q uint32 last_lat = lat; 1143377377903105 q int32 last_lon = lon; 1153377377903105 q lon = ::ilon(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff)); 1163377377903105 q lat = ::ilat(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff)); 122839813904102 q else if (last_lon > coord_lon) 1241153669299 q else if (lon == coord_lon) 12784841 q state ^= TOGGLE_WEST; // case (4) 1284865944 q else if (lat == coord_lat) 1294756633 q return HIT; // case (2) 1331148718599 q else if (last_lon == coord_lon) 135286448 q if (last_lat < coord_lat) 13681139 q state ^= TOGGLE_EAST; // case (4) 137205346 q else if (last_lat == coord_lat) 1431742308660104 q else if (last_lon > lon) 147847766224103 q else if (last_lon < coord_lon) 149903185599 q else if (lon == coord_lon) 15288236 q state ^= TOGGLE_EAST; // case (4) 1537109244 q else if (lat == coord_lat) 1546984534 q return HIT; // case (2) 158895988199 q else if (last_lon == coord_lon) 160380549 q if (last_lat < coord_lat) 16184937 q state ^= TOGGLE_WEST; // case (4) 162295644 q else if (last_lat == coord_lat) 163171526 q return HIT; // case (2) 168340900147 q else // last_lon == lon 170340900147 q if (lon == coord_lon && 171340900147 q ((last_lat <= coord_lat && coord_lat <= lat) || (lat <= coord_lat && coord_lat <= last_lat))) 172898 q return HIT; // case (2) 173340891247 q continue; // else: case (1) 17620440397100 q uint32 intersect_lat = lat + 17720440397100 q ((int64)coord_lon - lon)*((int64)last_lat - lat)/((int64)last_lon - lon); 17820440397100 q if (coord_lat > intersect_lat) 1791050731599 q state ^= (TOGGLE_EAST | TOGGLE_WEST); // case (3) 180993308297 q else if (coord_lat == intersect_lat) 188void register_coord(double lat, double lon, 189 std::set< Uint31_Index >& req, std::map< Uint31_Index, std::vector< std::pair< double, double > > >& coord_per_req) 1914107041 q Uint31_Index idx = Uint31_Index(::ll_upper_(lat, lon) & 0xffffff00); 1934107041 q coord_per_req[idx].push_back(std::make_pair(lat, lon)); 197struct Closedness_Predicate 1992299812 q bool match(const Way_Skeleton& obj) const { return !obj.nds.empty() && obj.nds.front() == obj.nds.back(); } 200 bool match(const Handle< Way_Skeleton >& h) const 20150651034 q { return !h.object().nds.empty() && h.object().nds.front() == h.object().nds.back(); } 202 bool match(const Handle< Attic< Way_Skeleton > >& h) const 2030 { return !h.object().nds.empty() && h.object().nds.front() == h.object().nds.back(); } 207void Coord_Query_Statement::execute(Resource_Manager& rman) 2100 rman.area_updater()->flush(); 2125350 q std::set< Uint31_Index > req; 2135350 q std::set< Uint31_Index > node_idxs; 2145350 q std::map< Uint31_Index, std::vector< std::pair< double, double > > > coord_per_req; 2165350 q const Set* input_set = 0; 2192221 q node_idxs.insert(::ll_upper_(lat, lon)); 2202221 q register_coord(lat, lon, req, coord_per_req); 2243129 q input_set = rman.get_set(input); 2272927 q const std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes = input_set->nodes; 2282927 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator it = nodes.begin(); 229180727 q it != nodes.end(); ++it) 231177818 q node_idxs.insert(Uint31_Index(it->first.val())); 2324282318 q for (std::vector< Node_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 2334104518 q register_coord(::lat(it->first.val(), it2->ll_lower), ::lon(it->first.val(), it2->ll_lower), 2372927 q const std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >& attic_nodes = input_set->attic_nodes; 2382927 q for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator it = attic_nodes.begin(); 2393127 q it != attic_nodes.end(); ++it) 24122 q node_idxs.insert(Uint31_Index(it->first.val())); 24222 q for (std::vector< Attic< Node_Skeleton > >::const_iterator it2 = it->second.begin(); 24352 q it2 != it->second.end(); ++it2) 24432 q register_coord(::lat(it->first.val(), it2->ll_lower), ::lon(it->first.val(), it2->ll_lower), 2505350 q std::map< std::pair< double, double >, std::map< Area::Id_Type, int > > areas_inside; 2515350 q std::set< Area::Id_Type > areas_found; 2535350 q std::map< Uint31_Index, std::vector< std::pair< double, double > > >::const_iterator coord_block_it = coord_per_req.begin(); 2545350 q Uint31_Index last_idx = req.empty() ? Uint31_Index(0u) : *req.begin(); 2565350 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator > area_blocks_db 2575350 q (rman.get_area_transaction()->data_index(area_settings().AREA_BLOCKS)); 2585350 q for (Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator >::Discrete_Iterator 2595350 q it(area_blocks_db.discrete_begin(req.begin(), req.end())); 2607828450 q !(it == area_blocks_db.discrete_end()); ++it) 2627823139 q if (!(it.index() == last_idx)) 26610712 q for (std::map< std::pair< double, double >, std::map< Area::Id_Type, int > >::const_iterator 2672776812 q inside_it = areas_inside.begin(); inside_it != areas_inside.end(); ++inside_it) 2692766112 q for (std::map< Area::Id_Type, int >::const_iterator inside_it2 = inside_it->second.begin(); 27050916712 q inside_it2 != inside_it->second.end(); ++inside_it2) 27248150612 q if (inside_it2->second != 0) 27321128412 q areas_found.insert(inside_it2->first); 27821412 q while (coord_block_it != coord_per_req.end() && coord_block_it->first < it.index()) 28010712 q if (coord_block_it == coord_per_req.end()) 2847823139 q for (std::vector< std::pair< double, double > >::const_iterator coord_it = coord_block_it->second.begin(); 2852444626139 q coord_it != coord_block_it->second.end(); ++coord_it) 2872436803039 q uint32 ilat((coord_it->first + 91.0)*10000000+0.5); 2882436803039 q int32 ilon(coord_it->second*10000000 + (coord_it->second > 0 ? 0.5 : -0.5)); 2902436803039 q int check = check_area_block(it.index().val(), it.object(), ilat, ilon); 29233578 q areas_found.insert(it.object().id); 2932436467339 q else if (check != 0) 29593968937 q std::map< Area::Id_Type, int >::iterator it2 = areas_inside[*coord_it].find(it.object().id); 29693968937 q if (it2 != areas_inside[*coord_it].end()) 29732987834 q it2->second ^= check; 29960981137 q areas_inside[*coord_it].insert(std::make_pair(it.object().id, check)); 3045350 q for (std::map< std::pair< double, double >, std::map< Area::Id_Type, int > >::const_iterator 3051296450 q inside_it = areas_inside.begin(); inside_it != areas_inside.end(); ++inside_it) 3071291137 q for (std::map< Area::Id_Type, int >::const_iterator inside_it2 = inside_it->second.begin(); 30814121637 q inside_it2 != inside_it->second.end(); ++inside_it2) 31012830537 q if (inside_it2->second != 0) 3119814537 q areas_found.insert(inside_it2->first); 3185350 q if (!areas_found.empty()) 3203837 q std::vector< uint32 > req_v; 32118337 q for (std::set< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it) 32214537 q req_v.push_back(it->val()); 3233837 q std::vector< uint32 > idx_req_v = ::calc_parents(req_v); 3243837 q std::vector< Uint31_Index > idx_req; 325231837 q for (std::vector< uint32 >::const_iterator it = idx_req_v.begin(); it != idx_req_v.end(); ++it) 326228037 q idx_req.push_back(*it); 3273837 q sort(idx_req.begin(), idx_req.end()); 3283837 q Block_Backend< Uint31_Index, Area_Skeleton, std::vector< Uint31_Index >::const_iterator > area_locations_db 3293837 q (rman.get_area_transaction()->data_index(area_settings().AREAS)); 3303837 q for (Block_Backend< Uint31_Index, Area_Skeleton, std::vector< Uint31_Index >::const_iterator >::Discrete_Iterator 3313837 q it = area_locations_db.discrete_begin(idx_req.begin(), idx_req.end()); 3321515837 q !(it == area_locations_db.discrete_end()); ++it) 3341512037 q if (areas_found.find(it.object().id) != areas_found.end()) 335104637 q into.areas[it.index()].push_back(it.object()); 3395350 q std::set< Uint31_Index > way_idxs = calc_parents(node_idxs); 3405350 q std::map< Uint31_Index, std::vector< Way_Skeleton > > current_candidates; 3415350 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > attic_candidates; 3425350 q Request_Context context(this, rman); 3435350 q collect_items_discrete(context, way_idxs, Closedness_Predicate(), current_candidates, attic_candidates); 3472221 q Tilewise_Area_Iterator tai(current_candidates, attic_candidates, *this, rman); 3482221 q uint32 idx = ::ll_upper_(lat, lon); 3492221 q tai.set_limits(ilat(idx, 0u), ilat(idx, 0u)); 35029305721 q while (!tai.is_end() && tai.get_idx().val() < idx) 3532221 q tai.move_covering_ways(idx, ::ll_lower(lat, lon), into.ways, into.attic_ways); 3572927 q Tilewise_Area_Iterator tai(current_candidates, attic_candidates, *this, rman); 3582927 q uint32 minlat = 0x7fff0000u; 3602927 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator it = input_set->nodes.begin(); 361180727 q it != input_set->nodes.end(); ++it) 363177818 q minlat = std::min(minlat, ilat(it->first.val(), 0u)); 364177818 q maxlat = std::max(maxlat, ilat(it->first.val(), 0u)); 3662927 q for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator it = input_set->attic_nodes.begin(); 3673127 q it != input_set->attic_nodes.end(); ++it) 36922 q minlat = std::min(minlat, ilat(it->first.val(), 0u)); 37022 q maxlat = std::max(maxlat, ilat(it->first.val(), 0u)); 3722927 q tai.set_limits(minlat, maxlat); 3742927 q std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator cur_it = input_set->nodes.begin(); 3752927 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator attic_it = 3762927 q input_set->attic_nodes.begin(); 378172027 q while (cur_it != input_set->nodes.end() || attic_it != input_set->attic_nodes.end()) 381169120 q (attic_it == input_set->attic_nodes.end() || 382169120 q (cur_it != input_set->nodes.end() && !(attic_it->first < cur_it->first))) ? cur_it->first : attic_it->first; 38316947720 q while (!tai.is_end() && tai.get_idx().val() < idx.val()) 388169120 q if (cur_it->first == tai.get_idx()) 3904270718 q for (std::vector< Node_Skeleton >::const_iterator it2 = cur_it->second.begin(); it2 != cur_it->second.end(); 3924104418 q tai.move_covering_ways(cur_it->first.val(), it2->ll_lower, into.ways, into.attic_ways); 394169120 q if (attic_it->first == tai.get_idx()) 39611 q for (std::vector< Attic< Node_Skeleton > >::const_iterator it2 = attic_it->second.begin(); 39721 q it2 != attic_it->second.end(); ++it2) 39811 q tai.move_covering_ways(attic_it->first.val(), it2->ll_lower, into.ways, into.attic_ways); 400346920 q while (cur_it != input_set->nodes.end() && !(tai.get_idx().val() < cur_it->first.val())) 402169320 q while (attic_it != input_set->attic_nodes.end() && !(tai.get_idx().val() < attic_it->first.val())) 4075350 q transfer_output(rman, into); 4085350 q rman.health_check(*this);