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 "four_field_index.h" 20#include "index_computations.h" 28Four_Field_Index::Four_Field_Index(Area_Oracle* area_oracle_) 29606108 q : base_lat(0), base_lon(2000000000), base_significant_bits(0), area_oracle(area_oracle_), 30606108 q min_lat(::ilat(-90.)), max_lat(::ilat(90.)), min_lon(::ilon(-180.)), max_lon(::ilon(180.)) {} 35 int32 exchange_value(Four_Field_Entry& entry, uint32 ilat, int32 ilon, int32 value) 39613387 q if (ilat & 0x00010000) 41606075 q if (ilon & 0x00010000) 43603405 q result = entry.ne; 44603405 q entry.ne = value; 482674 q result = entry.nw; 492674 q entry.nw = value; 547316 q if (ilon & 0x00010000) 563074 q result = entry.se; 573074 q entry.se = value; 614246 q result = entry.sw; 624246 q entry.sw = value; 66613387 q return result; 71int32 Four_Field_Index::add_point(double lat, double lon, int32 value) 73613387 q if (lat < -90. || lat > 90. || lon < -180. || lon > 180.) 76613387 q uint32 ilat = ::ilat(lat); 77613387 q int32 ilon = ::ilon(lon); 79613387 q return exchange_value(make_available(ilat & 0xfffe0000, ilon & 0xfffe0000, 15), 80613387 q ilat, ilon, value); 84Four_Field_Entry Four_Field_Index::add_segment( 85 double lhs_lat, double lhs_lon, double rhs_lat, double rhs_lon, int32 value) 870 Four_Field_Entry result; 890 if (lhs_lat < -90. || lhs_lat > 90. || lhs_lon < -180. || lhs_lon > 180. 900 || rhs_lat < -90. || rhs_lat > 90. || rhs_lon < -180. || rhs_lon > 180.) 920 if (fabs(lhs_lat - rhs_lat) > .0065536) 940 if (fabs(lhs_lon - rhs_lon) > .0065536) 960 if (fabs(lhs_lon - rhs_lon) - 360 < -.0065536) 1000 uint32 lhs_ilat = ::ilat(lhs_lat); 1010 int32 lhs_ilon = ::ilon(lhs_lon); 1020 uint32 rhs_ilat = ::ilat(rhs_lat); 1030 int32 rhs_ilon = ::ilon(rhs_lon); 1050 result.sw = exchange_value(make_available(lhs_ilat & 0xfffe0000, lhs_ilon & 0xfffe0000, 15), 1060 lhs_ilat, lhs_ilon, value); 1070 if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000)) 1080 result.se = exchange_value(make_available(lhs_ilat & 0xfffe0000, rhs_ilon & 0xfffe0000, 15), 1090 lhs_ilat, rhs_ilon, value); 1100 if ((lhs_ilat & 0xffff0000) != (rhs_ilat & 0xffff0000)) 1120 result.nw = exchange_value(make_available(rhs_ilat & 0xfffe0000, lhs_ilon & 0xfffe0000, 15), 1130 rhs_ilat, lhs_ilon, value); 1140 if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000)) 1150 result.ne = exchange_value(make_available(rhs_ilat & 0xfffe0000, rhs_ilon & 0xfffe0000, 15), 1160 rhs_ilat, rhs_ilon, value); 123Four_Field_Entry& Four_Field_Index::make_available(uint32 lat, int32 lon, int significant_bits) 129605557 q base_significant_bits = significant_bits; 131605557 q tree.push_back(Four_Field_Entry()); 132605557 q return tree.back(); 1357834 q uint32 bitmask = ~(0xffffffffu>>base_significant_bits); 1369064 q while ((lat & bitmask) != (base_lat & bitmask) || (lon & bitmask) != (base_lon & bitmask)) 1381234 q --base_significant_bits; 1401234 q tree.push_back(tree[0]); 1411234 q tree[0] = Four_Field_Entry(); 1421234 q if (base_lat & (0x80000000u>>base_significant_bits)) 144824 q if (base_lon & (0x80000000u>>base_significant_bits)) 145384 q tree[0].ne = -(int)tree.size()+1; 147443 q tree[0].nw = -(int)tree.size()+1; 151414 q if (base_lon & (0x80000000u>>base_significant_bits)) 152244 q tree[0].se = -(int)tree.size()+1; 154173 q tree[0].sw = -(int)tree.size()+1; 1571234 q bitmask = ~(0xffffffffu>>base_significant_bits); 1581234 q base_lat &= bitmask; 1591234 q base_lon &= bitmask; 1627834 q int cur_bits = base_significant_bits; 16450504 q while (cur_bits < significant_bits) 16642674 q if (lat & (0x80000000u>>cur_bits)) 16821894 q if (lon & (0x80000000u>>cur_bits)) 17012624 q if (tree[cur_pos].ne < 0) 17110904 q cur_pos = -tree[cur_pos].ne; 1741724 q tree[cur_pos].ne = -(int)tree.size(); 1751724 q cur_pos = tree.size(); 1761724 q tree.push_back(Four_Field_Entry()); 1819274 q if (tree[cur_pos].nw < 0) 1827694 q cur_pos = -tree[cur_pos].nw; 1851584 q tree[cur_pos].nw = -(int)tree.size(); 1861584 q cur_pos = tree.size(); 1871584 q tree.push_back(Four_Field_Entry()); 19320784 q if (lon & (0x80000000u>>cur_bits)) 19510864 q if (tree[cur_pos].se < 0) 1969444 q cur_pos = -tree[cur_pos].se; 1991424 q tree[cur_pos].se = -(int)tree.size(); 2001424 q cur_pos = tree.size(); 2011424 q tree.push_back(Four_Field_Entry()); 2069924 q if (tree[cur_pos].sw < 0) 2078564 q cur_pos = -tree[cur_pos].sw; 2101364 q tree[cur_pos].sw = -(int)tree.size(); 2111364 q cur_pos = tree.size(); 2121364 q tree.push_back(Four_Field_Entry()); 21642674 q if (cur_pos == tree.size()) 2170 tree.push_back(Four_Field_Entry()); 2227834 q return tree[cur_pos]; 226void Four_Field_Index::compute_inside_parts() 228606108 q if (area_oracle && !tree.empty()) 229605557 q compute_inside_parts(base_lat, base_lon, base_significant_bits, 0, 0, 0, 0); 233Area_Oracle::point_status Four_Field_Index::get_point_status(double lat, double lon) 235605557 q if (!area_oracle || tree.empty()) 238605557 q uint32 ilat = ::ilat(lat); 239605557 q int32 ilon = ::ilon(lon); 241605557 q int cur_bits = base_significant_bits; 243606787 q while (cur_bits < 32) 245606787 q if (ilat & (0x80000000u>>cur_bits)) 247603365 q if (ilon & (0x80000000u>>cur_bits)) 249602045 q if (tree[cur_pos].ne < 0) 250434 q cur_pos = -tree[cur_pos].ne; 251601614 q else if (tree[cur_pos].ne > 0) 252601614 q return area_oracle->get_point_status(tree[cur_pos].ne, lat, lon); 2581324 q if (tree[cur_pos].nw < 0) 259424 q cur_pos = -tree[cur_pos].nw; 260903 q else if (tree[cur_pos].nw > 0) 261903 q return area_oracle->get_point_status(tree[cur_pos].nw, lat, lon); 2683426 q if (ilon & (0x80000000u>>cur_bits)) 2701234 q if (tree[cur_pos].se < 0) 271194 q cur_pos = -tree[cur_pos].se; 2721043 q else if (tree[cur_pos].se > 0) 2731043 q return area_oracle->get_point_status(tree[cur_pos].se, lat, lon); 2792196 q if (tree[cur_pos].sw < 0) 280194 q cur_pos = -tree[cur_pos].sw; 2812006 q else if (tree[cur_pos].sw > 0) 2822006 q return area_oracle->get_point_status(tree[cur_pos].sw, lat, lon); 295void Four_Field_Index::compute_inside_parts(uint32 lat, int32 lon, int significant_bits, unsigned int pos, 296 bool sw, bool* r_se, bool* r_nw) 298612867 q if (!significant_bits || min_lon <= lon + int32(0x80000000u>>significant_bits)) 304612867 q if (tree[pos].sw < 0) 3051534 q compute_inside_parts(lat, lon, significant_bits+1, -tree[pos].sw, sw, &se, &nw); 306611337 q else if (tree[pos].sw > 0) 3074246 q area_oracle->build_area(sw, tree[pos].sw, &se, &nw); 310607095 q tree[pos].sw = sw; 315612867 q if (tree[pos].se < 0) 3161664 q compute_inside_parts(lat, lon + (0x80000000u>>significant_bits), significant_bits+1, -tree[pos].se, 318611207 q else if (tree[pos].se > 0) 3193074 q area_oracle->build_area(se, tree[pos].se, r_se, 0); 322608137 q tree[pos].se = se; 327612867 q if (tree[pos].nw < 0) 3282024 q compute_inside_parts(lat + (0x80000000u>>significant_bits), lon, significant_bits+1, -tree[pos].nw, 330610847 q else if (tree[pos].nw > 0) 3312674 q area_oracle->build_area(nw, tree[pos].nw, &ne, r_nw); 334608177 q tree[pos].nw = nw; 340612867 q if (tree[pos].ne < 0) 3412104 q compute_inside_parts(lat + (0x80000000u>>significant_bits), lon + (0x80000000u>>significant_bits), 3422104 q significant_bits+1, -tree[pos].ne, ne, 0, 0); 343610767 q else if (tree[pos].ne > 0) 344603405 q area_oracle->build_area(ne, tree[pos].ne, 0, 0); 3520 if (tree[pos].se < 0) 3530 compute_inside_parts(lat, lon + (0x80000000u>>significant_bits), significant_bits+1, -tree[pos].se, 3550 else if (tree[pos].se > 0) 3560 area_oracle->build_area(sw, tree[pos].se, r_se, &ne); 3640 if (tree[pos].ne < 0) 3650 compute_inside_parts(lat + (0x80000000u>>significant_bits), lon + (0x80000000u>>significant_bits), 3660 significant_bits+1, -tree[pos].ne, ne, 0, r_nw); 3670 else if (tree[pos].ne > 0) 3680 area_oracle->build_area(ne, tree[pos].ne, 0, r_nw); 381 void print_index(std::ostringstream& out, const std::vector< Four_Field_Entry >& tree, 382 uint32 base_lat, int32 base_lon, int significant_bits, int32 value, 383 uint32 min_lat, uint32 max_lat, int32 min_lon, int32 max_lon) 3850 if (base_lat < max_lat && base_lon < max_lon 3860 && base_lat + (0x80000000u>>significant_bits) - 1 > min_lat 3870 && base_lon + int32((0x80000000u>>significant_bits) - 1) > min_lon) 3880 out<<" {("<<std::fixed<<std::setprecision(7)<<::lat(std::max(base_lat, min_lat))<<", " 3890 <<std::fixed<<std::setprecision(7)<<::lon(std::max(base_lon, min_lon))<<", " 3900 <<std::fixed<<std::setprecision(7) 3910 <<::lat(std::min(base_lat + (0x80000000u>>significant_bits) - 1, max_lat))<<", " 3920 <<std::fixed<<std::setprecision(7) 3930 <<::lon(std::min(base_lon + int32((0x80000000u>>significant_bits) - 1), max_lon))<<"), " 398 void print_indexes(std::ostringstream& out, const std::vector< Four_Field_Entry >& tree, 399 uint pos, uint32 base_lat, int32 base_lon, int significant_bits, 400 uint32 min_lat, uint32 max_lat, int32 min_lon, int32 max_lon) 4020 const Four_Field_Entry& entry = tree[pos]; 4050 print_indexes(out, tree, -entry.sw, base_lat, base_lon, significant_bits+1, 4060 min_lat, max_lat, min_lon, max_lon); 4070 else if (entry.sw > 0) 4080 print_index(out, tree, base_lat, base_lon, significant_bits, entry.sw, 4090 min_lat, max_lat, min_lon, max_lon); 4120 print_indexes(out, tree, -entry.se, base_lat, base_lon + (0x80000000u>>significant_bits), significant_bits+1, 4130 min_lat, max_lat, min_lon, max_lon); 4140 else if (entry.se > 0) 4150 print_index(out, tree, base_lat, base_lon + (0x80000000u>>significant_bits), significant_bits, entry.se, 4160 min_lat, max_lat, min_lon, max_lon); 4190 print_indexes(out, tree, -entry.nw, base_lat + (0x80000000u>>significant_bits), base_lon, significant_bits+1, 4200 min_lat, max_lat, min_lon, max_lon); 4210 else if (entry.nw > 0) 4220 print_index(out, tree, base_lat + (0x80000000u>>significant_bits), base_lon, significant_bits, entry.nw, 4230 min_lat, max_lat, min_lon, max_lon); 4260 print_indexes(out, tree, -entry.ne, base_lat + (0x80000000u>>significant_bits), 4270 base_lon + (0x80000000u>>significant_bits), significant_bits+1, 4280 min_lat, max_lat, min_lon, max_lon); 4290 else if (entry.ne > 0) 4300 print_index(out, tree, base_lat + (0x80000000u>>significant_bits), 4310 base_lon + (0x80000000u>>significant_bits), significant_bits, entry.ne, 4320 min_lat, max_lat, min_lon, max_lon); 437std::string Four_Field_Index::to_string() const 4390 std::ostringstream out; 4430 print_indexes(out, tree, 0, base_lat, base_lon, base_significant_bits, 4440 min_lat, max_lat, min_lon, max_lon); 4460 return out.str() + "]";