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_) 292281211 q : base_lat(0), base_lon(2000000000), base_significant_bits(0), area_oracle(area_oracle_), 302281211 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) 393621911 q if (ilat & 0x00010000) 4190437 q if (ilon & 0x00010000) 4345287 q result = entry.ne; 4445287 q entry.ne = value; 4845156 q result = entry.nw; 4945156 q entry.nw = value; 542717610 q if (ilon & 0x00010000) 56221087 q result = entry.se; 57221087 q entry.se = value; 6150689 q result = entry.sw; 6250689 q entry.sw = value; 71int32 Four_Field_Index::add_point(double lat, double lon, int32 value) 733621911 q if (lat < -90. || lat > 90. || lon < -180. || lon > 180.) 763621911 q uint32 ilat = ::ilat(lat); 773621911 q int32 ilon = ::ilon(lon); 793621911 q return exchange_value(make_available(ilat & 0xfffe0000, ilon & 0xfffe0000, 15), 803621911 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) 1292281211 q base_significant_bits = significant_bits; 1312281211 q tree.push_back(Four_Field_Entry()); 135134076 q uint32 bitmask = ~(0xffffffffu>>base_significant_bits); 136188876 q while ((lat & bitmask) != (base_lat & bitmask) || (lon & bitmask) != (base_lon & bitmask)) 13854806 q --base_significant_bits; 14054806 q tree.push_back(tree[0]); 14154806 q tree[0] = Four_Field_Entry(); 14254806 q if (base_lat & (0x80000000u>>base_significant_bits)) 14427646 q if (base_lon & (0x80000000u>>base_significant_bits)) 14514056 q tree[0].ne = -(int)tree.size()+1; 14713595 q tree[0].nw = -(int)tree.size()+1; 15127166 q if (base_lon & (0x80000000u>>base_significant_bits)) 15213116 q tree[0].se = -(int)tree.size()+1; 15414056 q tree[0].sw = -(int)tree.size()+1; 15754806 q bitmask = ~(0xffffffffu>>base_significant_bits); 15854806 q base_lat &= bitmask; 15954806 q base_lon &= bitmask; 162134076 q int cur_bits = base_significant_bits; 164588426 q while (cur_bits < significant_bits) 166454356 q if (lat & (0x80000000u>>cur_bits)) 168223736 q if (lon & (0x80000000u>>cur_bits)) 170114946 q if (tree[cur_pos].ne < 0) 17187866 q cur_pos = -tree[cur_pos].ne; 17427086 q tree[cur_pos].ne = -(int)tree.size(); 17527086 q cur_pos = tree.size(); 17627086 q tree.push_back(Four_Field_Entry()); 181108796 q if (tree[cur_pos].nw < 0) 18281216 q cur_pos = -tree[cur_pos].nw; 18527586 q tree[cur_pos].nw = -(int)tree.size(); 18627586 q cur_pos = tree.size(); 18727586 q tree.push_back(Four_Field_Entry()); 193230626 q if (lon & (0x80000000u>>cur_bits)) 195115796 q if (tree[cur_pos].se < 0) 19688026 q cur_pos = -tree[cur_pos].se; 19927776 q tree[cur_pos].se = -(int)tree.size(); 20027776 q cur_pos = tree.size(); 20127776 q tree.push_back(Four_Field_Entry()); 206114836 q if (tree[cur_pos].sw < 0) 20788256 q cur_pos = -tree[cur_pos].sw; 21026586 q tree[cur_pos].sw = -(int)tree.size(); 21126586 q cur_pos = tree.size(); 21226586 q tree.push_back(Four_Field_Entry()); 216454356 q if (cur_pos == tree.size()) 2170 tree.push_back(Four_Field_Entry()); 222134076 q return tree[cur_pos]; 226void Four_Field_Index::compute_inside_parts() 2282281211 q if (area_oracle && !tree.empty()) 2292281211 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) 235222149 q if (!area_oracle || tree.empty()) 238222149 q uint32 ilat = ::ilat(lat); 239222149 q int32 ilon = ::ilon(lon); 241222149 q int cur_bits = base_significant_bits; 243276949 q while (cur_bits < 32) 245276949 q if (ilat & (0x80000000u>>cur_bits)) 24749886 q if (ilon & (0x80000000u>>cur_bits)) 24925265 q if (tree[cur_pos].ne < 0) 25013725 q cur_pos = -tree[cur_pos].ne; 25111543 q else if (tree[cur_pos].ne > 0) 25211543 q return area_oracle->get_point_status(tree[cur_pos].ne, lat, lon); 25824626 q if (tree[cur_pos].nw < 0) 25913306 q cur_pos = -tree[cur_pos].nw; 26011324 q else if (tree[cur_pos].nw > 0) 26111324 q return area_oracle->get_point_status(tree[cur_pos].nw, lat, lon); 268227069 q if (ilon & (0x80000000u>>cur_bits)) 270202177 q if (tree[cur_pos].se < 0) 27114346 q cur_pos = -tree[cur_pos].se; 272187836 q else if (tree[cur_pos].se > 0) 273187836 q return area_oracle->get_point_status(tree[cur_pos].se, lat, lon); 27924898 q if (tree[cur_pos].sw < 0) 28013445 q cur_pos = -tree[cur_pos].sw; 28111455 q else if (tree[cur_pos].sw > 0) 28211455 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) 2983919311 q if (!significant_bits || min_lon <= lon + int32(0x80000000u>>significant_bits)) 30540636 q compute_inside_parts(lat, lon, significant_bits+1, -tree[pos].sw, sw, &se, &nw); 3063513011 q else if (tree[pos].sw > 0) 30750689 q area_oracle->build_area(sw, tree[pos].sw, &se, &nw); 310300628 q tree[pos].sw = sw; 31640886 q compute_inside_parts(lat, lon + (0x80000000u>>significant_bits), significant_bits+1, -tree[pos].se, 3183510511 q else if (tree[pos].se > 0) 319221087 q area_oracle->build_area(se, tree[pos].se, r_se, 0); 32841176 q compute_inside_parts(lat + (0x80000000u>>significant_bits), lon, significant_bits+1, -tree[pos].nw, 3303507611 q else if (tree[pos].nw > 0) 33145156 q area_oracle->build_area(nw, tree[pos].nw, &ne, r_nw); 34141136 q compute_inside_parts(lat + (0x80000000u>>significant_bits), lon + (0x80000000u>>significant_bits), 34241136 q significant_bits+1, -tree[pos].ne, ne, 0, 0); 3433508011 q else if (tree[pos].ne > 0) 34445287 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() + "]";