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/>. 30#include "../../template_db/block_backend.h" 31#include "../../template_db/random_file.h" 32#include "../data/collect_members.h" 33#include "../osm-backend/area_updater.h" 38bool Make_Area_Statement::is_used_ = false; 40Generic_Statement_Maker< Make_Area_Statement > Make_Area_Statement::statement_maker("make-area"); 42Make_Area_Statement::Make_Area_Statement 43 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings) 440 : Output_Statement(line_number_), return_area(true) 480 std::map< std::string, std::string > attributes; 500 attributes["from"] = "_"; 510 attributes["into"] = "_"; 520 attributes["pivot"] = ""; 530 attributes["return-area"] = "yes"; 550 eval_attributes_array(get_name(), attributes, input_attributes); 570 input = attributes["from"]; 580 set_output(attributes["into"]); 590 pivot = attributes["pivot"]; 610 if (attributes["return-area"] != "yes") 630 if (attributes["return-area"] == "no") 660 add_static_error("For the attribute \"return-area\" of the element \"make-area\"" 670 " the only allowed values are \"yes\" or \"no\", default is \"yes\"."); 72std::pair< uint32, Uint64 > Make_Area_Statement::detect_pivot(const Set& pivot) 740 uint32 pivot_type(0); 750 Node::Id_Type pivot_id(0ull); 760 std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator 770 nit(pivot.nodes.begin()); 780 while ((pivot_id.val() == 0) && (nit != pivot.nodes.end())) 800 if (nit->second.size() > 0) 820 pivot_id = nit->second.front().id; 870 std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator 880 wit(pivot.ways.begin()); 890 while ((pivot_id.val() == 0) && (wit != pivot.ways.end())) 910 if (wit->second.size() > 0) 930 pivot_id = Uint64(wit->second.front().id.val()); 980 std::map< Uint31_Index, std::vector< Relation_Skeleton > >::const_iterator 990 rit(pivot.relations.begin()); 1000 while ((pivot_id.val() == 0) && (rit != pivot.relations.end())) 1020 if (rit->second.size() > 0) 1040 pivot_id = Uint64(rit->second.front().id.val()); 1050 pivot_type = RELATION; 1100 return std::make_pair(pivot_type, pivot_id); 113Node::Id_Type Make_Area_Statement::check_node_parity(const Set& pivot) 1150 std::set< Node::Id_Type > node_parity_control; 1160 for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator 1170 it(pivot.ways.begin()); it != pivot.ways.end(); ++it) 1190 for (std::vector< Way_Skeleton >::const_iterator it2(it->second.begin()); 1200 it2 != it->second.end(); ++it2) 1220 if (it2->nds.size() < 2) 1240 std::pair< std::set< Node::Id_Type >::iterator, bool > npp(node_parity_control.insert 1250 (it2->nds.front())); 1270 node_parity_control.erase(npp.first); 1280 npp = node_parity_control.insert 1310 node_parity_control.erase(npp.first); 1340 if (node_parity_control.size() > 0) 1350 return *(node_parity_control.begin()); 1360 return Node::Id_Type(0ull); 140std::pair< Node::Id_Type, Uint32_Index > Make_Area_Statement::create_area_blocks 141 (std::map< Uint31_Index, std::vector< Area_Block > >& areas, bool& wraps_around_date_line, 142 uint32 id, const Set& pivot) 1440 std::vector< Node > nodes; 1450 for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator 1460 it(pivot.nodes.begin()); it != pivot.nodes.end(); ++it) 1480 for (std::vector< Node_Skeleton >::const_iterator it2(it->second.begin()); 1490 it2 != it->second.end(); ++it2) 1500 nodes.push_back(Node(it2->id.val(), it->first.val(), it2->ll_lower)); 1520 sort(nodes.begin(), nodes.end(), Node_Comparator_By_Id()); 1540 for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator 1550 it(pivot.ways.begin()); it != pivot.ways.end(); ++it) 1570 for (std::vector< Way_Skeleton >::const_iterator it2(it->second.begin()); 1580 it2 != it->second.end(); ++it2) 1590 wraps_around_date_line ^= add_way_to_area_blocks(make_geometry(*it2, nodes), id, areas); 1620 return std::make_pair< uint32, uint32 >(0, 0); 166uint32 Make_Area_Statement::shifted_lat(uint32 ll_index, uint64 coord) 1680 return ::ilat(ll_index | (coord>>32), coord & 0xffffffff); 172int32 Make_Area_Statement::lon_(uint32 ll_index, uint64 coord) 1740 return ::ilon(ll_index | (coord>>32), coord & 0xffffffff); 177void Make_Area_Statement::add_segment_blocks 178 (std::map< Uint31_Index, std::vector< Area_Block > >& area_blocks, uint32 id) 180 /* We use that more northern segments always have bigger indices. 181 Thus we can collect each block's end points and add them, if they do not 182 cancel out, to the next block further northern.*/ 1830 for (std::map< Uint31_Index, std::vector< Area_Block > >::const_iterator 1840 it = area_blocks.begin(); it != area_blocks.end(); ++it) 1860 if (area_blocks.size() > 384*1024) 1880 area_blocks.clear(); 1920 std::set< int32 > lons; 1940 for (std::vector< Area_Block >::const_iterator it2(it->second.begin()); 1950 it2 != it->second.end(); ++it2) 1970 if (it2->coors.empty()) 2000 const uint64& ll_front(it2->coors.front()); 2010 int32 lon_front(lon_(ll_front>>32, ll_front & 0xffffffffull)); 2020 const uint64& ll_back(it2->coors.back()); 2030 int32 lon_back(lon_(ll_back>>32, ll_back & 0xffffffffull)); 2040 if (lons.find(lon_front) == lons.end()) 2050 lons.insert(lon_front); 2070 lons.erase(lon_front); 2080 if (lons.find(lon_back) == lons.end()) 2090 lons.insert(lon_back); 2110 lons.erase(lon_back); 2170 uint32 current_idx(it->first.val()); 2200 uint32 lat(shifted_lat(it->first.val(), 0) + 16*65536); 2210 int32 lon(lon_(it->first.val(), 0)); 2220 uint32 northern_ll_upper(::ll_upper(lat, lon) ^ 0x40000000); 2240 std::vector< Area_Block >& northern_block(area_blocks[northern_ll_upper]); 2250 for (std::set< int32 >::const_iterator it2(lons.begin()); it2 != lons.end(); ++it2) 2300 std::vector< uint64 > coors; 2320 ((((uint64)(::ll_upper(lat, from) ^ 0x40000000))<<32) | ::ll_lower(lat, from)); 2340 ((((uint64)(::ll_upper(lat, to) ^ 0x40000000))<<32) | ::ll_lower(lat, to)); 2350 Area_Block new_block(id, coors); 2360 northern_block.push_back(new_block); 2390 it = area_blocks.find(Uint31_Index(current_idx)); 244void add_south_pole_line 245 (std::map< Uint31_Index, std::vector< Area_Block > >& area_blocks, uint32 id) 2470 int south_delta = 0; 2490 for (std::map< Uint31_Index, std::vector< Area_Block > >::const_iterator 2500 it = area_blocks.begin(); it != area_blocks.end(); ++it) 2520 if (::lat(it->first.val(), 0u) < 0) 2570 if (south_delta < 0) 2600 for (int32 lon = -1800000000 + 0x100000; lon < 1800000000 - 0x100000; lon += 0x100000) 2620 int32 from = lon & 0xfff00000; 2640 std::vector< uint64 > coors; 2660 ((((uint64)(::ll_upper(10000000, from) ^ 0x40000000))<<32) | ::ll_lower(10000000, from)); 2680 ((((uint64)(::ll_upper(10000000, from + 0xfffff) ^ 0x40000000))<<32) 2690 | ::ll_lower(10000000, from + 0xfffff)); 2710 area_blocks[(::ll_upper(10000000, lon) & 0xffffff00) ^ 0x40000000].push_back(Area_Block(id, coors)); 2740 int32 to = (-1800000000 & 0xfff00000) + 0xfffff; 2760 std::vector< uint64 > coors; 2780 ((((uint64)(::ll_upper(10000000, -1800000000) ^ 0x40000000))<<32) | ::ll_lower(10000000, -1800000000)); 2800 ((((uint64)(::ll_upper(10000000, to) ^ 0x40000000))<<32) | ::ll_lower(10000000, to)); 2820 area_blocks[(::ll_upper(10000000, -1800000000) & 0xffffff00) ^ 0x40000000].push_back(Area_Block(id, coors)); 2840 int32 from = 1800000000 & 0xfff00000; 2880 ((((uint64)(::ll_upper(10000000, from) ^ 0x40000000))<<32) | ::ll_lower(10000000, from)); 2900 ((((uint64)(::ll_upper(10000000, 1800000000) ^ 0x40000000))<<32) | ::ll_lower(10000000, 1800000000)); 2920 area_blocks[(::ll_upper(10000000, 1800000000) & 0xffffff00) ^ 0x40000000].push_back(Area_Block(id, coors)); 296void Make_Area_Statement::execute(Resource_Manager& rman) 300 // detect pivot element 3010 const Set* pivot_set = rman.get_set(pivot); 3040 transfer_output(rman, into); 3070 std::pair< uint32, Uint64 > pivot_pair(detect_pivot(*pivot_set)); 3080 int pivot_type(pivot_pair.first); 3090 uint32 pivot_id(pivot_pair.second.val()); 3110 if (pivot_type == 0) 3130 transfer_output(rman, into); 317 //formulate range query to query tags of the pivot 3180 std::set< Uint31_Index > coarse_indices; 3190 if (pivot_type == NODE) 3200 coarse_indices.insert(pivot_set->nodes.begin()->first.val() & 0x7fffff00); 3210 else if (pivot_type == WAY) 3220 coarse_indices.insert(pivot_set->ways.begin()->first.val() & 0x7fffff00); 3230 else if (pivot_type == RELATION) 3240 coarse_indices.insert(pivot_set->relations.begin()->first.val() & 0x7fffff00); 3260 Ranges< Tag_Index_Local > ranges = formulate_range_query(coarse_indices); 328 // iterate over the result 3290 std::vector< std::pair< std::string, std::string > > new_tags; 3300 File_Properties* file_prop = 0; 3310 if (pivot_type == NODE) 3320 file_prop = osm_base_settings().NODE_TAGS_LOCAL; 3330 else if (pivot_type == WAY) 3340 file_prop = osm_base_settings().WAY_TAGS_LOCAL; 3350 else if (pivot_type == RELATION) 3360 file_prop = osm_base_settings().RELATION_TAGS_LOCAL; 3370 Block_Backend< Tag_Index_Local, Uint32_Index > items_db 3380 (rman.get_transaction()->data_index(file_prop)); 3390 Block_Backend< Tag_Index_Local, Uint32_Index >::Range_Iterator 3400 tag_it(items_db.range_begin(ranges)); 3410 for (; !(tag_it == items_db.range_end()); ++tag_it) 3430 if (tag_it.object().val() == pivot_id) 3440 new_tags.push_back(std::make_pair(tag_it.index().key, tag_it.index().value)); 3470 if (pivot_type == WAY) 3480 pivot_id += 2400000000u; 3490 else if (pivot_type == RELATION) 3500 pivot_id += 3600000000u; 3520 const Set* input_set = rman.get_set(input); 3550 transfer_output(rman, into); 3600 Node::Id_Type odd_id(check_node_parity(*input_set)); 3610 if (!(odd_id == Node::Id_Type(0ull))) 3630 std::ostringstream temp; 3640 temp<<"make-area: Node "<<odd_id.val() 3650 <<" is contained in an odd number of ways.\n"; 3660 runtime_remark(temp.str()); 369 // create area blocks 3700 std::map< Uint31_Index, std::vector< Area_Block > > area_blocks; 3710 bool wraps_around_date_line = false; 3720 std::pair< Node::Id_Type, Way::Id_Type > odd_pair 3730 (create_area_blocks(area_blocks, wraps_around_date_line, pivot_id, *input_set)); 3740 if (!(odd_pair.first == Node::Id_Type(0ull))) 3760 std::ostringstream temp; 3770 temp<<"make-area: Node "<<odd_pair.first.val() 3780 <<" referred by way "<<odd_pair.second.val() 3790 <<" is not contained in set \""<<input<<"\".\n"; 3800 runtime_remark(temp.str()); 3830 if (!(odd_id == Node::Id_Type(0ull)) || !(odd_pair.first == Node::Id_Type(0ull))) 3850 transfer_output(rman, into); 3890 if (wraps_around_date_line) 3900 add_south_pole_line(area_blocks, pivot_id); 3920 add_segment_blocks(area_blocks, pivot_id); 3940 std::vector< uint32 > used_indices; 3950 for (std::map< Uint31_Index, std::vector< Area_Block > >::const_iterator 3960 it(area_blocks.begin()); it != area_blocks.end(); ++it) 3970 used_indices.push_back(it->first.val()); 3980 std::sort(used_indices.begin(), used_indices.end()); 4000 Area_Location new_location(pivot_id, used_indices); 4010 new_location.tags = new_tags; 4020 Uint31_Index new_index(new_location.calc_index()); 4040 if (new_index.val() == 0) 4060 transfer_output(rman, into); 4100 if (rman.area_updater()) 4120 Area_Updater* area_updater = dynamic_cast< Area_Updater* >(rman.area_updater()); 4130 area_updater->set_area(new_index, new_location); 4140 area_updater->add_blocks(area_blocks); 4150 area_updater->commit(); 4190 into.areas[new_index].push_back(Area_Skeleton(new_location)); 4210 transfer_output(rman, into); 4220 rman.health_check(*this);