Overpass API regression suite coverage (Saxony) / overpass_api/statements/coord_query.cc

overpass_api/statements/coord_query.cc

281 of 297 instrumented lines run by the suite (94.6%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
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.
9 *
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.
14 *
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/>.
17 */
19#include <cctype>
20#include <fstream>
21#include <iostream>
22#include <map>
23#include <set>
24#include <sstream>
25#include <string>
26#include <math.h>
27#include <stdlib.h>
28#include <vector>
30#include <iomanip>
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_)
4515050 q{
4615050 q is_used_ = true;
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"]);
6015050 q lat = 100.0;
6115050 q lon = 200.0;
6215050 q if (attributes["lat"] != "" || attributes["lon"] != "")
636321 q {
646321 q lat = atof(attributes["lat"].c_str());
656321 q if ((lat < -90.0) || (lat > 90.0) || (attributes["lat"] == ""))
660 {
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());
710 }
736321 q lon = atof(attributes["lon"].c_str());
746321 q if ((lon < -180.0) || (lon > 180.0) || (attributes["lon"] == ""))
750 {
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());
800 }
816321 q }
8315050 q}
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.
89*/
90int Coord_Query_Statement::check_area_block
91 (uint32 ll_index, const Area_Block& area_block,
92 uint32 coord_lat, int32 coord_lon)
93360675525105 q{
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.
107360675525105 q int state = 0;
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())
1123377377903105 q {
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));
1183377377903105 q if (last_lon < lon)
1191635069243105 q {
1201635069243105 q if (lon < coord_lon)
121795255339104 q continue; // case (1)
122839813904102 q else if (last_lon > coord_lon)
123828277212102 q continue; // case (1)
1241153669299 q else if (lon == coord_lon)
1254950750 q {
1264950750 q if (lat < coord_lat)
12784841 q state ^= TOGGLE_WEST; // case (4)
1284865944 q else if (lat == coord_lat)
1294756633 q return HIT; // case (2)
130 // else: case (1)
131194145 q continue;
1324950750 q }
1331148718599 q else if (last_lon == coord_lon)
134286448 q {
135286448 q if (last_lat < coord_lat)
13681139 q state ^= TOGGLE_EAST; // case (4)
137205346 q else if (last_lat == coord_lat)
13896729 q return HIT; // case (2)
139 // else: case (1)
140189746 q continue;
141286448 q }
1421635069243105 q }
1431742308660104 q else if (last_lon > lon)
1441738899659104 q {
1451738899659104 q if (lon > coord_lon)
146891133435101 q continue; // case (1)
147847766224103 q else if (last_lon < coord_lon)
148838734369103 q continue; // case (1)
149903185599 q else if (lon == coord_lon)
1507197448 q {
1517197448 q if (lat < coord_lat)
15288236 q state ^= TOGGLE_EAST; // case (4)
1537109244 q else if (lat == coord_lat)
1546984534 q return HIT; // case (2)
155 // else: case (1)
156212943 q continue;
1577197448 q }
158895988199 q else if (last_lon == coord_lon)
159380549 q {
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)
164 // else: case (1)
165209046 q continue;
166380549 q }
1671738899659104 q }
168340900147 q else // last_lon == lon
169340900147 q {
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)
174340900147 q }
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)
1816420 q return HIT; // case (2)
182 // else: case (1)
18320440397100 q }
184360555279105 q return state;
185360675525105 q}
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)
1904107041 q{
1914107041 q Uint31_Index idx = Uint31_Index(::ll_upper_(lat, lon) & 0xffffff00);
1924107041 q req.insert(idx);
1934107041 q coord_per_req[idx].push_back(std::make_pair(lat, lon));
1944107041 q}
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(); }
204};
207void Coord_Query_Statement::execute(Resource_Manager& rman)
2095350 q if (rman.area_updater())
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;
2175350 q if (lat != 100.0)
2192221 q node_idxs.insert(::ll_upper_(lat, lon));
2202221 q register_coord(lat, lon, req, coord_per_req);
2223129 q else
2243129 q input_set = rman.get_set(input);
2253129 q if (input_set)
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)
230177818 q {
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),
2344104518 q req, coord_per_req);
235177818 q }
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),
24532 q req, coord_per_req);
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)
2617823139 q {
2627823139 q if (!(it.index() == last_idx))
26310712 q {
26410712 q last_idx = it.index();
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)
2682766112 q {
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)
27148150612 q {
27248150612 q if (inside_it2->second != 0)
27321128412 q areas_found.insert(inside_it2->first);
27448150612 q }
2752766112 q }
27610712 q areas_inside.clear();
27821412 q while (coord_block_it != coord_per_req.end() && coord_block_it->first < it.index())
27910712 q ++coord_block_it;
28010712 q if (coord_block_it == coord_per_req.end())
2810 break;
28210712 q }
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)
2862436803039 q {
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);
2912436803039 q if (check == HIT)
29233578 q areas_found.insert(it.object().id);
2932436467339 q else if (check != 0)
29493968937 q {
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;
29860981137 q else
29960981137 q areas_inside[*coord_it].insert(std::make_pair(it.object().id, check));
30093968937 q }
3012436803039 q }
3027823139 q }
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)
3061291137 q {
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)
30912830537 q {
31012830537 q if (inside_it2->second != 0)
3119814537 q areas_found.insert(inside_it2->first);
31212830537 q }
3131291137 q }
3145350 q areas_inside.clear();
3165350 q Set into;
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)
3331512037 q {
3341512037 q if (areas_found.find(it.object().id) != areas_found.end())
335104637 q into.areas[it.index()].push_back(it.object());
3361512037 q }
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);
3455350 q if (lat != 100.0)
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)
35129303521 q tai.next();
3522221 q if (!tai.is_end())
3532221 q tai.move_covering_ways(idx, ::ll_lower(lat, lon), into.ways, into.attic_ways);
3553129 q else if (input_set)
3572927 q Tilewise_Area_Iterator tai(current_candidates, attic_candidates, *this, rman);
3582927 q uint32 minlat = 0x7fff0000u;
3592927 q uint32 maxlat = 0u;
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)
362177818 q {
363177818 q minlat = std::min(minlat, ilat(it->first.val(), 0u));
364177818 q maxlat = std::max(maxlat, ilat(it->first.val(), 0u));
365177818 q }
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())
379169120 q {
380169120 q Uint32_Index idx =
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())
38416778620 q tai.next();
385169120 q if (tai.is_end())
3860 break;
388169120 q if (cur_it->first == tai.get_idx())
389166318 q {
3904270718 q for (std::vector< Node_Skeleton >::const_iterator it2 = cur_it->second.begin(); it2 != cur_it->second.end();
3914104418 q ++it2)
3924104418 q tai.move_covering_ways(cur_it->first.val(), it2->ll_lower, into.ways, into.attic_ways);
393166318 q }
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()))
401177818 q ++cur_it;
402169320 q while (attic_it != input_set->attic_nodes.end() && !(tai.get_idx().val() < attic_it->first.val()))
40322 q ++attic_it;
404169120 q }
4075350 q transfer_output(rman, into);
4085350 q rman.health_check(*this);