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

overpass_api/statements/coord_query.cc

282 of 297 instrumented lines run by the suite (94.9%) 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)
4412641 q : Output_Statement(line_number_)
4512641 q{
4612641 q is_used_ = true;
4812641 q std::map< std::string, std::string > attributes;
5012641 q attributes["from"] = "_";
5112641 q attributes["into"] = "_";
5212641 q attributes["lat"] = "";
5312641 q attributes["lon"] = "";
5512641 q eval_attributes_array(get_name(), attributes, input_attributes);
5712641 q input = attributes["from"];
5812641 q set_output(attributes["into"]);
6012641 q lat = 100.0;
6112641 q lon = 200.0;
6212641 q if (attributes["lat"] != "" || attributes["lon"] != "")
635418 q {
645418 q lat = atof(attributes["lat"].c_str());
655418 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 }
735418 q lon = atof(attributes["lon"].c_str());
745418 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 }
815418 q }
8312641 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)
93109607839110 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.
107109607839110 q int state = 0;
108109607839110 q std::vector< uint64 >::const_iterator it(area_block.coors.begin());
109109607839110 q uint32 lat = ::ilat(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff));
110109607839110 q int32 lon = ::ilon(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff));
1111624557961110 q while (++it != area_block.coors.end())
1121515184970110 q {
1131515184970110 q uint32 last_lat = lat;
1141515184970110 q int32 last_lon = lon;
1151515184970110 q lon = ::ilon(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff));
1161515184970110 q lat = ::ilat(ll_index | (((*it)>>32)&0xff), (*it & 0xffffffff));
1181515184970110 q if (last_lon < lon)
119751620689110 q {
120751620689110 q if (lon < coord_lon)
121372437480109 q continue; // case (1)
122379183209108 q else if (last_lon > coord_lon)
123370176188108 q continue; // case (1)
1249007021107 q else if (lon == coord_lon)
12510918750 q {
12610918750 q if (lat < coord_lat)
12783133 q state ^= TOGGLE_WEST; // case (4)
12810835646 q else if (lat == coord_lat)
12910731337 q return HIT; // case (2)
130 // else: case (1)
131187442 q continue;
13210918750 q }
1338897834107 q else if (last_lon == coord_lon)
134595450 q {
135595450 q if (last_lat < coord_lat)
13678134 q state ^= TOGGLE_EAST; // case (4)
137517347 q else if (last_lat == coord_lat)
138428335 q return HIT; // case (2)
139 // else: case (1)
140167145 q continue;
141595450 q }
142751620689110 q }
143763564281106 q else if (last_lon > lon)
144761665735106 q {
145761665735106 q if (lon > coord_lon)
146385017070104 q continue; // case (1)
147376648665105 q else if (last_lon < coord_lon)
148372069408105 q continue; // case (1)
1494579257103 q else if (lon == coord_lon)
15011842252 q {
15111842252 q if (lat < coord_lat)
15290136 q state ^= TOGGLE_EAST; // case (4)
15311752150 q else if (lat == coord_lat)
15411656638 q return HIT; // case (2)
155 // else: case (1)
156185647 q continue;
15711842252 q }
1584460835103 q else if (last_lon == coord_lon)
159743448 q {
160743448 q if (last_lat < coord_lat)
16186832 q state ^= TOGGLE_WEST; // case (4)
162656643 q else if (last_lat == coord_lat)
163554334 q return HIT; // case (2)
164 // else: case (1)
165189143 q continue;
166743448 q }
167761665735106 q }
168189854637 q else // last_lon == lon
169189854637 q {
170189854637 q if (lon == coord_lon &&
171189854637 q ((last_lat <= coord_lat && coord_lat <= lat) || (lat <= coord_lat && coord_lat <= last_lat)))
17218211 q return HIT; // case (2)
173189836437 q continue; // else: case (1)
174189854637 q }
17613345281107 q uint32 intersect_lat = lat +
17713345281107 q ((int64)coord_lon - lon)*((int64)last_lat - lat)/((int64)last_lon - lon);
17813345281107 q if (coord_lat > intersect_lat)
1798150861106 q state ^= (TOGGLE_EAST | TOGGLE_WEST); // case (3)
1805194420105 q else if (coord_lat == intersect_lat)
18196129 q return HIT; // case (2)
182 // else: case (1)
18313345281107 q }
184109372991110 q return state;
185109607839110 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)
19015495336 q{
19115495336 q Uint31_Index idx = Uint31_Index(::ll_upper_(lat, lon) & 0xffffff00);
19215495336 q req.insert(idx);
19315495336 q coord_per_req[idx].push_back(std::make_pair(lat, lon));
19415495336 q}
197struct Closedness_Predicate
1991043410 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
2018358428 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)
2094341 q if (rman.area_updater())
2100 rman.area_updater()->flush();
2124341 q std::set< Uint31_Index > req;
2134341 q std::set< Uint31_Index > node_idxs;
2144341 q std::map< Uint31_Index, std::vector< std::pair< double, double > > > coord_per_req;
2164341 q const Set* input_set = 0;
2174341 q if (lat != 100.0)
2191918 q node_idxs.insert(::ll_upper_(lat, lon));
2201918 q register_coord(lat, lon, req, coord_per_req);
2222424 q else
2242424 q input_set = rman.get_set(input);
2252424 q if (input_set)
2272222 q const std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes = input_set->nodes;
2282222 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator it = nodes.begin();
229754722 q it != nodes.end(); ++it)
230752519 q {
231752519 q node_idxs.insert(Uint31_Index(it->first.val()));
23216149819 q for (std::vector< Node_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
23315397319 q register_coord(::lat(it->first.val(), it2->ll_lower), ::lon(it->first.val(), it2->ll_lower),
23415397319 q req, coord_per_req);
235752519 q }
2372222 q const std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >& attic_nodes = input_set->attic_nodes;
2382222 q for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator it = attic_nodes.begin();
23919822 q it != attic_nodes.end(); ++it)
2401762 q {
2411762 q node_idxs.insert(Uint31_Index(it->first.val()));
2421762 q for (std::vector< Attic< Node_Skeleton > >::const_iterator it2 = it->second.begin();
24311372 q it2 != it->second.end(); ++it2)
2449612 q register_coord(::lat(it->first.val(), it2->ll_lower), ::lon(it->first.val(), it2->ll_lower),
2459612 q req, coord_per_req);
2461762 q }
2504341 q std::map< std::pair< double, double >, std::map< Area::Id_Type, int > > areas_inside;
2514341 q std::set< Area::Id_Type > areas_found;
2534341 q std::map< Uint31_Index, std::vector< std::pair< double, double > > >::const_iterator coord_block_it = coord_per_req.begin();
2544341 q Uint31_Index last_idx = req.empty() ? Uint31_Index(0u) : *req.begin();
2564341 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator > area_blocks_db
2574341 q (rman.get_area_transaction()->data_index(area_settings().AREA_BLOCKS));
2584341 q for (Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator >::Discrete_Iterator
2594341 q it(area_blocks_db.discrete_begin(req.begin(), req.end()));
2605027041 q !(it == area_blocks_db.discrete_end()); ++it)
2615022735 q {
2625022735 q if (!(it.index() == last_idx))
26317314 q {
26417314 q last_idx = it.index();
26617314 q for (std::map< std::pair< double, double >, std::map< Area::Id_Type, int > >::const_iterator
26714892214 q inside_it = areas_inside.begin(); inside_it != areas_inside.end(); ++inside_it)
26814874914 q {
26914874914 q for (std::map< Area::Id_Type, int >::const_iterator inside_it2 = inside_it->second.begin();
270139736614 q inside_it2 != inside_it->second.end(); ++inside_it2)
271124861714 q {
272124861714 q if (inside_it2->second != 0)
27398596214 q areas_found.insert(inside_it2->first);
274124861714 q }
27514874914 q }
27617314 q areas_inside.clear();
27834614 q while (coord_block_it != coord_per_req.end() && coord_block_it->first < it.index())
27917314 q ++coord_block_it;
28017314 q if (coord_block_it == coord_per_req.end())
2810 break;
28217314 q }
2845022735 q for (std::vector< std::pair< double, double > >::const_iterator coord_it = coord_block_it->second.begin();
2851675323535 q coord_it != coord_block_it->second.end(); ++coord_it)
2861670300835 q {
2871670300835 q uint32 ilat((coord_it->first + 91.0)*10000000+0.5);
2881670300835 q int32 ilon(coord_it->second*10000000 + (coord_it->second > 0 ? 0.5 : -0.5));
2901670300835 q int check = check_area_block(it.index().val(), it.object(), ilat, ilon);
2911670300835 q if (check == HIT)
29211473014 q areas_found.insert(it.object().id);
2931658827835 q else if (check != 0)
294159649535 q {
295159649535 q std::map< Area::Id_Type, int >::iterator it2 = areas_inside[*coord_it].find(it.object().id);
296159649535 q if (it2 != areas_inside[*coord_it].end())
29730405134 q it2->second ^= check;
298129244435 q else
299129244435 q areas_inside[*coord_it].insert(std::make_pair(it.object().id, check));
300159649535 q }
3011670300835 q }
3025022735 q }
3044341 q for (std::map< std::pair< double, double >, std::map< Area::Id_Type, int > >::const_iterator
305623241 q inside_it = areas_inside.begin(); inside_it != areas_inside.end(); ++inside_it)
306618935 q {
307618935 q for (std::map< Area::Id_Type, int >::const_iterator inside_it2 = inside_it->second.begin();
3085001635 q inside_it2 != inside_it->second.end(); ++inside_it2)
3094382735 q {
3104382735 q if (inside_it2->second != 0)
3113506735 q areas_found.insert(inside_it2->first);
3124382735 q }
313618935 q }
3144341 q areas_inside.clear();
3164341 q Set into;
3184341 q if (!areas_found.empty())
3203735 q std::vector< uint32 > req_v;
32125235 q for (std::set< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it)
32221535 q req_v.push_back(it->val());
3233735 q std::vector< uint32 > idx_req_v = ::calc_parents(req_v);
3243735 q std::vector< Uint31_Index > idx_req;
325306635 q for (std::vector< uint32 >::const_iterator it = idx_req_v.begin(); it != idx_req_v.end(); ++it)
326302935 q idx_req.push_back(*it);
3273735 q sort(idx_req.begin(), idx_req.end());
3283735 q Block_Backend< Uint31_Index, Area_Skeleton, std::vector< Uint31_Index >::const_iterator > area_locations_db
3293735 q (rman.get_area_transaction()->data_index(area_settings().AREAS));
3303735 q for (Block_Backend< Uint31_Index, Area_Skeleton, std::vector< Uint31_Index >::const_iterator >::Discrete_Iterator
3313735 q it = area_locations_db.discrete_begin(idx_req.begin(), idx_req.end());
3321009735 q !(it == area_locations_db.discrete_end()); ++it)
3331006035 q {
3341006035 q if (areas_found.find(it.object().id) != areas_found.end())
33592735 q into.areas[it.index()].push_back(it.object());
3361006035 q }
3394341 q std::set< Uint31_Index > way_idxs = calc_parents(node_idxs);
3404341 q std::map< Uint31_Index, std::vector< Way_Skeleton > > current_candidates;
3414341 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > attic_candidates;
3424341 q Request_Context context(this, rman);
3434341 q collect_items_discrete(context, way_idxs, Closedness_Predicate(), current_candidates, attic_candidates);
3454341 q if (lat != 100.0)
3471918 q Tilewise_Area_Iterator tai(current_candidates, attic_candidates, *this, rman);
3481918 q uint32 idx = ::ll_upper_(lat, lon);
3491918 q tai.set_limits(ilat(idx, 0u), ilat(idx, 0u));
3502892618 q while (!tai.is_end() && tai.get_idx().val() < idx)
3512890718 q tai.next();
3521918 q if (!tai.is_end())
3531918 q tai.move_covering_ways(idx, ::ll_lower(lat, lon), into.ways, into.attic_ways);
3552424 q else if (input_set)
3572222 q Tilewise_Area_Iterator tai(current_candidates, attic_candidates, *this, rman);
3582222 q uint32 minlat = 0x7fff0000u;
3592222 q uint32 maxlat = 0u;
3602222 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator it = input_set->nodes.begin();
361754722 q it != input_set->nodes.end(); ++it)
362752519 q {
363752519 q minlat = std::min(minlat, ilat(it->first.val(), 0u));
364752519 q maxlat = std::max(maxlat, ilat(it->first.val(), 0u));
365752519 q }
3662222 q for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator it = input_set->attic_nodes.begin();
36719822 q it != input_set->attic_nodes.end(); ++it)
3681762 q {
3691762 q minlat = std::min(minlat, ilat(it->first.val(), 0u));
3701762 q maxlat = std::max(maxlat, ilat(it->first.val(), 0u));
3711762 q }
3722222 q tai.set_limits(minlat, maxlat);
3742222 q std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator cur_it = input_set->nodes.begin();
3752222 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator attic_it =
3762222 q input_set->attic_nodes.begin();
378666722 q while (cur_it != input_set->nodes.end() || attic_it != input_set->attic_nodes.end())
379664719 q {
380664719 q Uint32_Index idx =
381664719 q (attic_it == input_set->attic_nodes.end() ||
382664719 q (cur_it != input_set->nodes.end() && !(attic_it->first < cur_it->first))) ? cur_it->first : attic_it->first;
38317537119 q while (!tai.is_end() && tai.get_idx().val() < idx.val())
38416872418 q tai.next();
385664719 q if (tai.is_end())
38622 q break;
388664518 q if (cur_it->first == tai.get_idx())
389606617 q {
39014748217 q for (std::vector< Node_Skeleton >::const_iterator it2 = cur_it->second.begin(); it2 != cur_it->second.end();
39114141617 q ++it2)
39214141617 q tai.move_covering_ways(cur_it->first.val(), it2->ll_lower, into.ways, into.attic_ways);
393606617 q }
394664518 q if (attic_it->first == tai.get_idx())
3951762 q {
3961762 q for (std::vector< Attic< Node_Skeleton > >::const_iterator it2 = attic_it->second.begin();
39711372 q it2 != attic_it->second.end(); ++it2)
3989612 q tai.move_covering_ways(attic_it->first.val(), it2->ll_lower, into.ways, into.attic_ways);
3991762 q }
4001416518 q while (cur_it != input_set->nodes.end() && !(tai.get_idx().val() < cur_it->first.val()))
401752018 q ++cur_it;
402682118 q while (attic_it != input_set->attic_nodes.end() && !(tai.get_idx().val() < attic_it->first.val()))
4031762 q ++attic_it;
404664518 q }
4074341 q transfer_output(rman, into);
4084341 q rman.health_check(*this);