Overpass API regression suite coverage (Saxony) / overpass_api/core/geometry.cc

overpass_api/core/geometry.cc

1120 of 1795 instrumented lines run by the suite (62.4%) 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 "four_field_index.h"
20#include "geometry.h"
21#include "index_computations.h"
23#include <cmath>
24#include <limits>
25#include <map>
28const Bbox_Double Bbox_Double::invalid(100.0, 200.0, 100.0, 200.0);
31double Bbox_Double::center_lat() const
328985523 q{
338985523 q return (south + north) / 2;
348985523 q}
37double Bbox_Double::center_lon() const
389254624 q{
399254624 q if (west <= east)
409254424 q return (west + east) / 2;
4121 q else if ((west + east) / 2 <= 0)
4221 q return (west + east) / 2 + 180.0;
430 else
440 return (west + east) / 2 - 180.0;
459254624 q}
48bool Bbox_Double::contains(const Point_Double& point) const
491392152 q{
501392152 q if (point.lat + 5e-8 < south || point.lat - 5e-8 > north)
51248634 q return false;
531143552 q if (east >= west)
541143552 q return point.lon + 5e-8 >= west && point.lon - 5e-8 <= east;
560 return point.lon + 5e-8 >= west || point.lon - 5e-8 <= east;
571143552 q}
59bool Bbox_Double::intersects(const Point_Double& from, const Point_Double& to) const
607041842143 q{
617041842143 q double from_lon = from.lon;
627041842143 q double to_lon = to.lon;
637041842143 q if (from.lon < 0. && to.lon - from.lon > 180.)
6473 q from_lon += 360.;
657041842143 q if (to.lon < 0. && from.lon - to.lon > 180.)
6683 q to_lon += 360.;
687041842143 q double delta_from = 0;
697041842143 q if (from.lat + 5e-8 < south)
702876626128 q {
712876626128 q if (to.lat + 5e-8 < south)
722869669128 q return false;
736957118 q if (to.lat - 5e-8 > from.lat)
746957118 q delta_from = (to_lon - from_lon)*(south - from.lat)/(to.lat - from.lat);
75 // Asserted: (south, from_lon + delta_from) is a point on the segment
766957118 q }
774165216142 q else if (from.lat - 5e-8 > north)
781210628129 q {
791210628129 q if (to.lat - 5e-8 > north)
801204319129 q return false;
816309120 q if (to.lat + 5e-8 < from.lat)
826309120 q delta_from = (to_lon - from_lon)*(north - from.lat)/(to.lat - from.lat);
83 // Asserted: (north, from_lon + delta_from) is a point on the segment
846309120 q }
862967854139 q if (to.lat + 5e-8 < south && from.lat - 5e-8 > to.lat)
87 // Adjust to.lat and to.lon
883386116 q to_lon += (from_lon - to_lon)*(south - to.lat)/(from.lat - to.lat);
89 // Asserted: (south, to_lon) is a point on the segment
902964468139 q else if (to.lat - 5e-8 > north && from.lat + 5e-8 < to.lat)
91 // Adjust to.lat and to.lon
922955114 q to_lon += (from_lon - to_lon)*(north - to.lat)/(from.lat - to.lat);
93 // Asserted: (north, to_lon) is a point on the segment
942967854139 q from_lon += delta_from;
96 // Asserted: (std::min(north, std::max(south, from.lat)), from_lon) is on the segment
97 // Asserted: (std::min(north, std::max(south, to.lat)), to_lon) is on the segment
99 // Now we know that both latitudes are between south and north.
100 // Thus we only need to check whether the segment touches the bbox in its east-west-extent.
101 // Note that the lons have now values between -180.0 and 360.0.
1022967854139 q double min_lon = std::min(from_lon, to_lon);
1032967854139 q double max_lon = std::max(from_lon, to_lon);
1042967854139 q if (west <= east)
1052967854139 q {
1062967854139 q if (max_lon < 180.)
1072967854139 q return min_lon - 5e-8 <= east && max_lon + 5e-8 >= west;
1080 else if (min_lon > 180.)
1090 return min_lon - 5e-8 - 360. <= east && max_lon + 5e-8 - 360. >= west;
1110 return min_lon - 5e-8 <= east && max_lon + 5e-8 - 360. >= west;
1122967854139 q }
1140 if (max_lon < 180.)
1150 return min_lon - 5e-8 <= east || max_lon + 5e-8 >= west;
1160 else if (min_lon > 180.)
1170 return min_lon - 5e-8 - 360. <= east || max_lon + 5e-8 - 360. >= west;
1190 return true;
1200}
123bool Point_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const
1240{
1250 return bbox.contains(pt);
1260}
129Bbox_Double* calc_bounds(const std::vector< Point_Double >& points)
13058764536 q{
13158764536 q double south = 100.0;
13258764536 q double west = 200.0;
13358764536 q double north = -100.0;
13458764536 q double east = -200.0;
136541922036 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it)
137483157536 q {
138483157536 q if (it->lat < 100.)
139482758736 q {
140482758736 q south = std::min(south, it->lat);
141482758736 q west = std::min(west, it->lon);
142482758736 q north = std::max(north, it->lat);
143482758736 q east = std::max(east, it->lon);
144482758736 q }
145483157536 q }
14758764536 q if (north == -100.0)
1480 return new Bbox_Double(Bbox_Double::invalid);
14958764536 q else if (east - west > 180.0)
150 // In this special case we should check whether the bounding box should rather cross the date line
1510 {
1520 double wrapped_west = 180.0;
1530 double wrapped_east = -180.0;
1550 for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it)
1560 {
1570 if (it->lat < 100.)
1580 {
1590 if (it->lon > 0)
1600 wrapped_west = std::min(wrapped_west, it->lon);
1610 else
1620 wrapped_east = std::max(wrapped_east, it->lon);
1630 }
1640 }
1660 if (wrapped_west - wrapped_east > 180.0)
1670 return new Bbox_Double(south, wrapped_west, north, wrapped_east);
1680 else
169 // The points go around the world, hence a bounding box limit doesn't make sense.
1700 return new Bbox_Double(south, -180.0, north, 180.0);
1710 }
17258764536 q else
17358764536 q return new Bbox_Double(south, west, north, east);
17458764536 q}
177double Linestring_Geometry::center_lat() const
1788374811 q{
1798374811 q if (!bounds)
1808266511 q bounds = calc_bounds(points);
1828374811 q return bounds->center_lat();
1838374811 q}
186double Linestring_Geometry::center_lon() const
1878610312 q{
1888610312 q if (!bounds)
189776675 q bounds = calc_bounds(points);
1918610312 q return bounds->center_lon();
1928610312 q}
195double Linestring_Geometry::south() const
19634405525 q{
19734405525 q if (!bounds)
19834242024 q bounds = calc_bounds(points);
20034405525 q return bounds->south;
20134405525 q}
204double Linestring_Geometry::north() const
20534351025 q{
20634351025 q if (!bounds)
2070 bounds = calc_bounds(points);
20934351025 q return bounds->north;
21034351025 q}
213double Linestring_Geometry::west() const
21468011425 q{
21568011425 q if (!bounds)
2165451 q bounds = calc_bounds(points);
21868011425 q return bounds->west;
21968011425 q}
222double Linestring_Geometry::east() const
22367956925 q{
22467956925 q if (!bounds)
2250 bounds = calc_bounds(points);
22767956925 q return bounds->east;
22867956925 q}
231bool Linestring_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const
2320{
2330 for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it)
2340 {
2350 if (bbox.contains(*it))
2360 return true;
2370 }
2390 for (uint i = 1; i < points.size(); ++i)
2400 {
2410 if (bbox.intersects(points[i-1], points[i]))
2420 return true;
2430 }
2450 return false;
2460}
249double Partial_Way_Geometry::center_lat() const
2500{
2510 if (!has_coords)
2520 return 0;
2540 if (!bounds)
2550 bounds = calc_bounds(points);
2570 return bounds->center_lat();
2580}
261double Partial_Way_Geometry::center_lon() const
2620{
2630 if (!has_coords)
2640 return 0;
2660 if (!bounds)
2670 bounds = calc_bounds(points);
2690 return bounds->center_lon();
2700}
273double Partial_Way_Geometry::south() const
2748434813 q{
2758434813 q if (!has_coords)
2760 return 0;
2788434813 q if (!bounds)
2798434813 q bounds = calc_bounds(points);
2818434813 q return bounds->south;
2828434813 q}
285double Partial_Way_Geometry::north() const
2868434813 q{
2878434813 q if (!has_coords)
2880 return 0;
2908434813 q if (!bounds)
2910 bounds = calc_bounds(points);
2938434813 q return bounds->north;
2948434813 q}
297double Partial_Way_Geometry::west() const
29816863513 q{
29916863513 q if (!has_coords)
3000 return 0;
30216863513 q if (!bounds)
3030 bounds = calc_bounds(points);
30516863513 q return bounds->west;
30616863513 q}
309double Partial_Way_Geometry::east() const
31016863513 q{
31116863513 q if (!has_coords)
3120 return 0;
31416863513 q if (!bounds)
3150 bounds = calc_bounds(points);
31716863513 q return bounds->east;
31816863513 q}
321Partial_Way_Geometry::Partial_Way_Geometry(const std::vector< Point_Double >& points_)
3221812944 q : points(points_), bounds(0), has_coords(false)
3231812944 q{
3243625884 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end() && !has_coords; ++it)
3251812944 q has_coords |= (it->lat < 100.);
3271812944 q if (has_coords)
3281798544 q {
3291798544 q valid_segments.push_back(std::vector< Point_Double >());
3301798544 q if (!points.empty() && points.front().lat < 100.)
3311798544 q valid_segments.back().push_back(points.front());
33210750474 q for (unsigned int i = 1; i < points.size(); ++i)
3338951934 q {
3348951934 q if (points[i].lat < 100.)
3358951934 q {
3368951934 q if (points[i-1].lat >= 100.)
3370 valid_segments.push_back(std::vector< Point_Double >());
3388951934 q valid_segments.back().push_back(points[i]);
3398951934 q }
3408951934 q }
3411798544 q }
3421812944 q}
344void Partial_Way_Geometry::add_point(const Point_Double& point)
345228632521 q{
346228632521 q delete bounds;
347228632521 q bounds = 0;
348228632521 q if (point.lat < 100.)
349219014218 q {
350219014218 q if (points.empty() || (points.back().lat >= 100.))
35135196018 q valid_segments.push_back(std::vector< Point_Double >());
352219014218 q valid_segments.back().push_back(point);
353219014218 q has_coords = true;
354219014218 q }
355228632521 q points.push_back(point);
356228632521 q}
359bool Partial_Way_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const
3600{
3610 for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it)
3620 {
3630 if (it->lat < 100. && bbox.contains(*it))
3640 return true;
3650 }
3670 for (uint i = 1; i < points.size(); ++i)
3680 {
3690 if (points[i-1].lat < 100. && points[i].lat < 100. && bbox.intersects(points[i-1], points[i]))
3700 return true;
3710 }
3730 return false;
3740}
377Bbox_Double* calc_bounds(const std::vector< std::vector< Point_Double > >& linestrings)
3781272 q{
3791272 q double south = 100.0;
3801272 q double west = 200.0;
3811272 q double north = -100.0;
3821272 q double east = -200.0;
3841272 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
3852542 q iti != linestrings.end(); ++iti)
3861272 q {
38715152 q for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it)
38813882 q {
38913882 q if (it->lat < 100.)
39013882 q {
39113882 q south = std::min(south, it->lat);
39213882 q west = std::min(west, it->lon);
39313882 q north = std::max(north, it->lat);
39413882 q east = std::max(east, it->lon);
39513882 q }
39613882 q }
3971272 q }
3991272 q if (north == -100.0)
4000 return new Bbox_Double(Bbox_Double::invalid);
4011272 q else if (east - west > 180.0)
402 // In this special case we should check whether the bounding box should rather cross the date line
4030 {
4040 double wrapped_west = 180.0;
4050 double wrapped_east = -180.0;
4070 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
4080 iti != linestrings.end(); ++iti)
4090 {
4100 for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it)
4110 {
4120 if (it->lat < 100.)
4130 {
4140 if (it->lon > 0)
4150 wrapped_west = std::min(wrapped_west, it->lon);
4160 else
4170 wrapped_east = std::max(wrapped_east, it->lon);
4180 }
4190 }
4200 }
4220 if (wrapped_west - wrapped_east > 180.0)
4230 return new Bbox_Double(south, wrapped_west, north, wrapped_east);
4240 else
425 // The points go around the world, hence a bounding box limit doesn't make sense.
4260 return new Bbox_Double(south, -180.0, north, 180.0);
4270 }
4281272 q else
4291272 q return new Bbox_Double(south, west, north, east);
4301272 q}
4333925 qFree_Polygon_Geometry::Free_Polygon_Geometry(const std::vector< std::vector< Point_Double > >& linestrings_) : linestrings(linestrings_), bounds(0)
4343925 q{
4357845 q for (std::vector< std::vector< Point_Double > >::iterator it = linestrings.begin(); it != linestrings.end();
4363925 q ++it)
4373925 q {
4383925 q if (it->front() != it->back())
4393925 q it->push_back(it->front());
4403925 q }
4413925 q}
444double Free_Polygon_Geometry::center_lat() const
4450{
4460 if (!bounds)
4470 bounds = calc_bounds(linestrings);
4490 return bounds->center_lat();
4500}
453double Free_Polygon_Geometry::center_lon() const
4540{
4550 if (!bounds)
4560 bounds = calc_bounds(linestrings);
4580 return bounds->center_lon();
4590}
462double Free_Polygon_Geometry::south() const
4630{
4640 if (!bounds)
4650 bounds = calc_bounds(linestrings);
4670 return bounds->south;
4680}
471double Free_Polygon_Geometry::north() const
4720{
4730 if (!bounds)
4740 bounds = calc_bounds(linestrings);
4760 return bounds->north;
4770}
480double Free_Polygon_Geometry::west() const
4810{
4820 if (!bounds)
4830 bounds = calc_bounds(linestrings);
4850 return bounds->west;
4860}
489double Free_Polygon_Geometry::east() const
4900{
4910 if (!bounds)
4920 bounds = calc_bounds(linestrings);
4940 return bounds->east;
4950}
498void Free_Polygon_Geometry::add_linestring(const std::vector< Point_Double >& linestring)
499602183 q{
500602183 q if (linestring.size() < 2)
501551 q return;
503601632 q delete bounds;
504601632 q bounds = 0;
505601632 q linestrings.push_back(linestring);
506601632 q if (linestrings.back().front() != linestrings.back().back())
507601211 q linestrings.back().push_back(linestrings.back().front());
508601632 q}
511void toggle_if_inside(bool& is_inside, bool& on_vertex, bool& on_segment, double border_lat, double border_lon,
512 Point_Double pt, Point_Double lhs, Point_Double rhs)
5131825877 q{
5141825877 q if (pt.lon < -179.9)
5150 {
5160 lhs.lon -= lhs.lon > 0 ? 360. : 0.;
5170 rhs.lon -= rhs.lon > 0 ? 360. : 0.;
5180 }
5191825877 q else if (pt.lon > 179.9)
5200 {
5210 lhs.lon += lhs.lon < 0 ? 360. : 0.;
5220 rhs.lon += rhs.lon < 0 ? 360. : 0.;
5230 }
5251825877 q if (lhs.lat == rhs.lat)
526601643 q {
527601643 q if (lhs.lat < pt.lat)
5280 is_inside ^= ((lhs.lon - pt.lon)*(rhs.lon - pt.lon) < 0);
529601643 q else if (lhs.lat == pt.lat)
530601643 q {
531601643 q if (pt.lon == lhs.lon || pt.lon == rhs.lon)
532601643 q on_vertex = true;
5330 else
5340 on_segment |= ((lhs.lon - pt.lon)*(rhs.lon - pt.lon) < 0);
535601643 q }
536 // no else required -- such a segment does not play a role
537601643 q }
5381224237 q else if (lhs.lon == rhs.lon)
539601632 q {
540601632 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon))
541421 q on_vertex = true;
542601211 q else if (pt.lon == lhs.lon && (pt.lat - lhs.lat)*(pt.lat - rhs.lat) < 0)
5430 on_segment = true;
544601211 q else if (lhs.lon < pt.lon && (lhs.lat < border_lat || rhs.lat < border_lat))
5450 is_inside = !is_inside;
546 // no else required -- such a segment does not play a role
547601632 q }
548622607 q else if ((lhs.lat - rhs.lat)*(lhs.lon - rhs.lon) < 0)
549 // The segment runs from nw to se or se to nw
55011505 q {
55111505 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon))
5524225 q on_vertex = true;
5537285 q else if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0)
5541384 q {
5551384 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon);
5561384 q if (isect_lat < pt.lat)
557964 q is_inside = !is_inside;
558423 q else if (isect_lat == pt.lat)
5590 on_segment = true;
5601384 q }
5615905 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon)
5620 {
5630 if (lhs.lat < pt.lat)
5640 is_inside = !is_inside;
5650 }
5665905 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon)
5670 {
5680 if (rhs.lat < pt.lat)
5690 is_inside = !is_inside;
5700 }
5715905 q else if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0)
572213 q {
573213 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat);
574213 q if (border_lon < isect_lon && isect_lon < pt.lon)
57532 q is_inside = !is_inside;
576213 q }
57711505 q }
578611107 q else
579 // The segment runs from sw to ne or ne to sw
580611107 q {
581611107 q if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0)
582304 q {
583304 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat);
584304 q if ((lhs.lon <= pt.lon || rhs.lon <= pt.lon) && (border_lon < isect_lon))
585194 q is_inside = !is_inside;
586304 q }
587611107 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon))
588604825 q {
589604825 q on_vertex = true;
590604825 q return;
591604825 q }
5936286 q if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0)
594894 q {
595894 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon);
596894 q if (isect_lat < pt.lat)
597504 q is_inside = !is_inside;
598392 q else if (isect_lat == pt.lat)
5990 on_segment = true;
600894 q }
6015396 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon)
6020 {
6030 if (lhs.lat < pt.lat)
6040 is_inside = !is_inside;
6050 }
6065396 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon)
6070 {
6080 if (rhs.lat < pt.lat)
6090 is_inside = !is_inside;
6100 }
6116286 q }
6121825877 q}
615bool Free_Polygon_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const
6160{
6170 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
6180 iti != linestrings.end(); ++iti)
6190 {
6200 for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it)
6210 {
6220 if (bbox.contains(*it))
6230 return true;
6240 }
6250 }
6270 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
6280 iti != linestrings.end(); ++iti)
6290 {
6300 for (uint i = 1; i < iti->size(); ++i)
6310 {
6320 if (bbox.intersects((*iti)[i-1], (*iti)[i]))
6330 return true;
6340 }
6350 }
6370 return false;
6380}
641Point_Double interpolation_point(
642 double orth_x, double orth_y, double orth_z,
643 double lhs_gc_x, double lhs_gc_z, double factor, double lhs_lon)
6444984 q{
6454984 q static const double deg_to_arc = acos(0)/90.;
6474984 q double sin_factor = sin(factor);
6484984 q double cos_factor = cos(factor);
6494984 q double new_x = sin_factor * orth_x + cos_factor * lhs_gc_x;
6504984 q double new_y = sin_factor * orth_y;
6514984 q double new_z = sin_factor * orth_z + cos_factor * lhs_gc_z;
6534984 q double new_lat = asin(new_x)/deg_to_arc;
6544984 q double new_lon = atan2(new_y, new_z)/deg_to_arc + lhs_lon;
6554984 q if (new_lon < -180.)
6560 new_lon += 360.;
6574984 q else if (new_lon > 180.)
6580 new_lon -= 360.;
6604984 q return Point_Double(new_lat, new_lon);
6614984 q}
664namespace
666 struct Interpolation_Collector
667 {
668 Interpolation_Collector(double orth_x, double orth_y, double orth_z,
669 double lhs_gc_x, double lhs_gc_z, double dist, double lhs_lon, std::vector< Point_Double >& target);
6710 void collect_single_point(const Point_Double& pt) { target->push_back(pt); }
672 void collect_single_point(double dist);
673 void collect_sequence(double from, double to, double max_step);
674 void collect_center(double from, double to, int divisor);
676 double orth_x;
677 double orth_y;
678 double orth_z;
679 double lhs_gc_x;
680 double lhs_gc_z;
681 double lhs_lon;
682 double center;
683 Point_Double ex_pt;
684 double ex_gc_pt_lat;
685 double acceptable_max_length;
686 std::vector< Point_Double >* target;
687 };
690 Interpolation_Collector::Interpolation_Collector(double orth_x_, double orth_y_, double orth_z_,
691 double lhs_gc_x_, double lhs_gc_z_, double dist, double lhs_lon_, std::vector< Point_Double >& target_)
692534 q : orth_x(orth_x_), orth_y(orth_y_), orth_z(orth_z_),
693534 q lhs_gc_x(lhs_gc_x_), lhs_gc_z(lhs_gc_z_), lhs_lon(lhs_lon_),
694534 q center(lhs_gc_x == 0 ? dist : atan(orth_x/lhs_gc_x)),
695534 q ex_pt(interpolation_point(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z,
696534 q std::max(std::min(center, dist), 0.), lhs_lon)),
697534 q ex_gc_pt_lat(ex_pt.lat),
698534 q target(&target_)
699534 q {
700534 q static const double deg_to_arc = acos(0)/90.;
701534 q acceptable_max_length = .0065536*cos(ex_pt.lat * deg_to_arc);
703534 q double bounded_center = std::max(std::min(center, dist), 0.);
704534 q if (acceptable_max_length < .0016384 && center != bounded_center)
7050 {
7060 Point_Double ex_gc_pt = interpolation_point(orth_x, orth_y, orth_z,
7070 lhs_gc_x, lhs_gc_z, center, lhs_lon);
7080 ex_gc_pt_lat = ex_gc_pt.lat;
7090 }
710534 q center = bounded_center;
711534 q }
714 void Interpolation_Collector::collect_single_point(double dist)
7150 {
7160 target->push_back(interpolation_point(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, dist, lhs_lon));
7170 }
720 void Interpolation_Collector::collect_sequence(double from, double to, double max_step)
7211064 q {
7221064 q int num_sections = (int)((to - from)/max_step)+1;
7235514 q for (int j = 1; j < num_sections; ++j)
7244454 q target->push_back(interpolation_point(orth_x, orth_y, orth_z,
7254454 q lhs_gc_x, lhs_gc_z, from + (to - from)*j/num_sections, lhs_lon));
7261064 q }
729 void Interpolation_Collector::collect_center(double from, double to, int divisor)
730534 q {
731534 q static const double deg_to_arc = acos(0)/90.;
733534 q double max_length_threshold = .0065536/divisor;
734534 q if (acceptable_max_length < max_length_threshold && divisor < 65536)
7350 {
7360 double dist_ex_s = acos(sqrt(1-1./divisor/divisor)/sin(ex_gc_pt_lat * deg_to_arc));
7370 double bound_l = std::max(std::min(center - dist_ex_s, to), from);
7380 double bound_r = std::max(std::min(center + dist_ex_s, to), from);
7400 collect_sequence(from, bound_l, max_length_threshold*deg_to_arc);
7410 if (bound_l > 0 && bound_l < to)
7420 collect_single_point(bound_l);
7440 collect_center(bound_l, bound_r, divisor*4);
7460 if (bound_r > 0 && bound_r < to)
7470 collect_single_point(bound_r);
7480 collect_sequence(bound_r, to, max_length_threshold*deg_to_arc);
7490 }
750534 q else
751534 q {
752534 q if (acceptable_max_length < 1e-9)
7530 acceptable_max_length = 1e-9;
754534 q acceptable_max_length *= deg_to_arc;
756534 q collect_sequence(from, center, acceptable_max_length);
757534 q if (center > from && center < to)
7580 collect_single_point(center);
759534 q collect_sequence(center, to, acceptable_max_length);
760534 q }
761534 q }
765void interpolate_segment(double lhs_lat, double lhs_lon, double rhs_lat, double rhs_lon,
766 std::vector< Point_Double >& target)
7671829217 q{
7681829217 q static const double deg_to_arc = acos(0)/90.;
7701829217 q if (fabs(rhs_lat - lhs_lat) < .0065536 && fabs(rhs_lon - lhs_lon) < .0065536)
7711828687 q target.push_back(Point_Double(rhs_lat, rhs_lon));
772534 q else
773534 q {
774534 q if (fabs(rhs_lon - lhs_lon) > 180.)
7750 rhs_lon = (lhs_lon < 0 ? rhs_lon - 360. : rhs_lon + 360.);
777534 q if (fabs(rhs_lon - lhs_lon) > 179.999999)
7780 {
7790 double pole_lat = (lhs_lat + rhs_lat >= 0 ? 90. : -90.);
7810 int num_sections = (int)(fabs(pole_lat - lhs_lat)/.0065536)+1;
7820 for (int j = 1; j < num_sections; ++j)
7830 target.push_back(Point_Double(lhs_lat + (pole_lat - lhs_lat)*j/num_sections, lhs_lon));
7840 target.push_back(Point_Double(pole_lat, lhs_lon));
7860 num_sections = (int)(fabs(rhs_lon - lhs_lon)/.0065536)+1;
7870 for (int j = 1; j < num_sections; ++j)
7880 target.push_back(Point_Double(pole_lat, lhs_lon + (rhs_lon - lhs_lon)*j/num_sections));
7890 target.push_back(Point_Double(pole_lat, rhs_lon));
7910 num_sections = (int)(fabs(rhs_lat - pole_lat)/.0065536)+1;
7920 for (int j = 1; j < num_sections; ++j)
7930 target.push_back(Point_Double(pole_lat + (rhs_lat - pole_lat)*j/num_sections, rhs_lon));
7940 }
795534 q else
796534 q {
797534 q double lhs_cos = cos(lhs_lat * deg_to_arc);
798534 q double lhs_gc_x = sin(lhs_lat * deg_to_arc);
799534 q double lhs_gc_z = lhs_cos;
801534 q double rhs_cos = cos(rhs_lat * deg_to_arc);
802534 q double rhs_gc_x = sin(rhs_lat * deg_to_arc);
803534 q double rhs_gc_y = sin((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos;
804534 q double rhs_gc_z = cos((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos;
806534 q double prod = lhs_gc_x * rhs_gc_x + lhs_gc_z * rhs_gc_z;
807534 q double dist = acos(prod);
809534 q double orth_x = rhs_gc_x - prod * lhs_gc_x;
810534 q double orth_y = rhs_gc_y;
811534 q double orth_z = rhs_gc_z - prod * lhs_gc_z;
812534 q double lg_orth = sqrt(orth_x * orth_x + orth_y * orth_y + orth_z * orth_z);
813534 q orth_x /= lg_orth;
814534 q orth_y /= lg_orth;
815534 q orth_z /= lg_orth;
817534 q Interpolation_Collector collector(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, dist, lhs_lon, target);
818534 q collector.collect_center(0, dist, 4);
819534 q }
821534 q if (rhs_lon < -180.)
8220 rhs_lon += 360.;
823534 q else if (rhs_lon > 180.)
8240 rhs_lon -= 360.;
825534 q target.push_back(Point_Double(rhs_lat, rhs_lon));
826534 q }
8271829217 q}
830void add_segment(std::map< uint32, std::vector< unsigned int > >& segments_per_idx,
831 const Point_Double& from, const Point_Double& to, unsigned int pos)
8321833667 q{
8331833667 q uint32 lhs_ilat = ::ilat(from.lat);
8341833667 q int32 lhs_ilon = ::ilon(from.lon);
8351833667 q uint32 rhs_ilat = ::ilat(to.lat);
8361833667 q int32 rhs_ilon = ::ilon(to.lon);
8381833667 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos);
8391833667 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000))
8405504 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)].push_back(pos);
8411833667 q if ((lhs_ilat & 0xffff0000) != (rhs_ilat & 0xffff0000))
8422524 q {
8432524 q segments_per_idx[(rhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos);
8442524 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000))
8451384 q segments_per_idx[(rhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)].push_back(pos);
8462524 q }
8471833667 q}
850void replace_segment(std::vector< unsigned int >& segments, unsigned int old_pos, unsigned int new_pos)
8510{
8520 for (std::vector< unsigned int >::iterator it = segments.begin(); it != segments.end(); ++it)
8530 {
8540 if (*it == old_pos)
8550 *it = new_pos;
8560 }
8570}
860void replace_segment(std::map< uint32, std::vector< unsigned int > >& segments_per_idx,
861 const Point_Double& from, const Point_Double& via, const Point_Double& to,
862 unsigned int old_pos, unsigned int new_pos)
8630{
8640 uint32 lhs_ilat = ::ilat(from.lat) & 0xffff0000;
8650 int32 lhs_ilon = ::ilon(from.lon) & 0xffff0000;
8660 uint32 rhs_ilat = ::ilat(to.lat) & 0xffff0000;
8670 int32 rhs_ilon = ::ilon(to.lon) & 0xffff0000;
8680 uint32 via_ilat = ::ilat(via.lat) & 0xffff0000;
8690 int32 via_ilon = ::ilon(via.lon) & 0xffff0000;
871 // The upper part is the one that possibly crosses the index boundary
8720 if (via_ilat == lhs_ilat && via_ilon == lhs_ilon)
8730 ++new_pos;
8750 replace_segment(segments_per_idx[lhs_ilat | (uint32(lhs_ilon)>>16)], old_pos, new_pos);
8760 if (lhs_ilon != rhs_ilon)
8770 replace_segment(segments_per_idx[lhs_ilat | (uint32(rhs_ilon)>>16)], old_pos, new_pos);
8780 if (lhs_ilat != rhs_ilat)
8790 {
8800 replace_segment(segments_per_idx[rhs_ilat | (uint32(lhs_ilon)>>16)], old_pos, new_pos);
8810 if (lhs_ilon != rhs_ilon)
8820 {
8830 if ((via_ilat == lhs_ilat && via_ilon == lhs_ilon) || (via_ilat == rhs_ilat && via_ilon == rhs_ilon))
8840 replace_segment(segments_per_idx[rhs_ilat | (uint32(rhs_ilon)>>16)], old_pos, new_pos);
8850 else
8860 {
8870 segments_per_idx[lhs_ilat | (uint32(rhs_ilon)>>16)].push_back(new_pos+1);
8880 segments_per_idx[rhs_ilat | (uint32(lhs_ilon)>>16)].push_back(new_pos+1);
8890 replace_segment(segments_per_idx[rhs_ilat | (uint32(rhs_ilon)>>16)], old_pos, new_pos+1);
8900 }
8910 }
8920 }
8940 if (via_ilat == lhs_ilat && via_ilon == lhs_ilon)
8950 segments_per_idx[lhs_ilat | (uint32(lhs_ilon)>>16)].push_back(new_pos-1);
8960 else if (via_ilat == rhs_ilat && via_ilon == rhs_ilon)
8970 segments_per_idx[rhs_ilat | (uint32(rhs_ilon)>>16)].push_back(new_pos+1);
8980}
901bool try_intersect(const Point_Double& lhs_from, const Point_Double& lhs_to,
902 const Point_Double& rhs_from, const Point_Double& rhs_to, Point_Double& isect)
9031889457 q{
9041889457 q double rfmlt_lat = rhs_from.lat - lhs_to.lat;
9051889457 q double rfmlt_lon = rhs_from.lon - lhs_to.lon;
907 //The two segments are connected by a vertex
9081889457 q if (!rfmlt_lat && !rfmlt_lon)
9091228497 q return false;
910660967 q if (lhs_from == rhs_to || lhs_from == rhs_from || lhs_to == rhs_to)
911605617 q return false;
91355355 q double lfmlt_lat = lhs_from.lat - lhs_to.lat;
91455355 q double lfmlt_lon = lhs_from.lon - lhs_to.lon;
91555355 q double rfmrt_lat = rhs_from.lat - rhs_to.lat;
91655355 q double rfmrt_lon = rhs_from.lon - rhs_to.lon;
91755355 q double det = lfmlt_lat * rfmrt_lon - rfmrt_lat * lfmlt_lon;
91955355 q if (det == 0)
920242 q {
921 // Segments on parallel but distinct beams
922242 q if (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat != 0)
923242 q return false;
9250 if (fabs(rfmlt_lat) > fabs(rfmlt_lon))
9260 {
9270 if (lhs_from.lat < lhs_to.lat)
9280 {
9290 if (rhs_from.lat < rhs_to.lat)
9300 {
931 // Segments are non-overlapping
9320 if (lhs_to.lat < rhs_from.lat || rhs_to.lat < lhs_from.lat)
9330 return false;
9350 if (lhs_from.lat < rhs_from.lat)
9360 isect = rhs_from;
9370 else
9380 isect = lhs_from;
9390 }
9400 else
9410 {
942 // Segments are non-overlapping
9430 if (lhs_to.lat < rhs_to.lat || rhs_from.lat < lhs_from.lat)
9440 return false;
9460 if (lhs_from.lat < rhs_to.lat)
9470 isect = rhs_to;
9480 else
9490 isect = lhs_from;
9500 }
9510 }
9520 else
9530 {
9540 if (rhs_from.lat < rhs_to.lat)
9550 {
956 // Segments are non-overlapping
9570 if (lhs_from.lat < rhs_from.lat || rhs_to.lat < lhs_to.lat)
9580 return false;
9600 if (lhs_to.lat < rhs_from.lat)
9610 isect = rhs_from;
9620 else
9630 isect = lhs_to;
9640 }
9650 else
9660 {
967 // Segments are non-overlapping
9680 if (lhs_from.lat < rhs_to.lat || rhs_from.lat < lhs_to.lat)
9690 return false;
9710 if (lhs_to.lat < rhs_to.lat)
9720 isect = rhs_to;
9730 else
9740 isect = lhs_to;
9750 }
9760 }
9770 }
9780 else
9790 {
9800 if (lhs_from.lon < lhs_to.lon)
9810 {
9820 if (rhs_from.lon < rhs_to.lon)
9830 {
984 // Segments are non-overlapping
9850 if (lhs_to.lon < rhs_from.lon || rhs_to.lon < lhs_from.lon)
9860 return false;
9880 if (lhs_from.lon < rhs_from.lon)
9890 isect = rhs_from;
9900 else
9910 isect = lhs_from;
9920 }
9930 else
9940 {
995 // Segments are non-overlapping
9960 if (lhs_to.lon < rhs_to.lon || rhs_from.lon < lhs_from.lon)
9970 return false;
9990 if (lhs_from.lon < rhs_to.lon)
10000 isect = rhs_to;
10010 else
10020 isect = lhs_from;
10030 }
10040 }
10050 else
10060 {
10070 if (rhs_from.lon < rhs_to.lon)
10080 {
1009 // Segments are non-overlapping
10100 if (lhs_from.lon < rhs_from.lon || rhs_to.lon < lhs_to.lon)
10110 return false;
10130 if (lhs_to.lon < rhs_from.lon)
10140 isect = rhs_from;
10150 else
10160 isect = lhs_to;
10170 }
10180 else
10190 {
1020 // Segments are non-overlapping
10210 if (lhs_from.lon < rhs_to.lon || rhs_from.lon < lhs_to.lon)
10220 return false;
10240 if (lhs_to.lon < rhs_to.lon)
10250 isect = rhs_to;
10260 else
10270 isect = lhs_to;
10280 }
10290 }
10300 }
10310 return true;
10320 }
103455115 q double x = (rfmrt_lon * rfmlt_lat - rfmrt_lat * rfmlt_lon)/det;
103555115 q double y = (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat)/det;
103755115 q if (x <= -1e-7 || x >= 1+1e-7 || y <= -1e-7 || y >= 1+1e-7)
103855115 q return false;
10400 isect.lat = lhs_to.lat + x * lfmlt_lat;
10410 isect.lon = lhs_to.lon + x * lfmlt_lon;
10420 return true;
104355115 q}
1046struct Idx_Per_Point_Double
1048 Idx_Per_Point_Double(const Point_Double& pt_, unsigned int idx_, unsigned int remote_idx_)
10493667327 q : pt(pt_), idx(idx_), remote_idx(remote_idx_) {}
1051 Point_Double pt;
1052 unsigned int idx;
1053 unsigned int remote_idx;
1055 bool operator<(const Idx_Per_Point_Double& rhs) const
10568598837 q {
10578598837 q if (pt.lat != rhs.pt.lat)
10583141367 q return pt.lat < rhs.pt.lat;
10595457477 q if (pt.lon != rhs.pt.lon)
10603006503 q return pt.lon < rhs.pt.lon;
10622450977 q return idx < rhs.idx;
10635457477 q }
1067struct Line_Divertion
10696089527 q Line_Divertion() : to_idx(std::numeric_limits< uint >::max()),
10706089527 q to_remote_idx(std::numeric_limits< uint >::max()) {}
10713667327 q Line_Divertion(const Idx_Per_Point_Double& rhs) : to_idx(rhs.idx), to_remote_idx(rhs.remote_idx) {}
1073 unsigned int to_idx;
1074 unsigned int to_remote_idx;
1078void split_segments(
1079 std::vector< Point_Double >& all_segments,
1080 std::vector< unsigned int >& gap_positions,
1081 std::map< uint32, std::vector< unsigned int > >& segments_per_idx)
1082606108 q{
1083606108 q for (std::map< uint32, std::vector< unsigned int > >::iterator idx_it = segments_per_idx.begin();
10841219488 q idx_it != segments_per_idx.end(); ++idx_it)
1085613387 q {
1086613387 q Point_Double isect(100, 0);
10871843067 q for (unsigned int i = 1; i < idx_it->second.size(); ++i)
10881229687 q {
10893119137 q for (unsigned int j = 0; j < i; ++j)
10901889457 q {
10911889457 q if (try_intersect(all_segments[idx_it->second[j]], all_segments[idx_it->second[j]+1],
10921889457 q all_segments[idx_it->second[i]], all_segments[idx_it->second[i]+1], isect))
10930 {
10940 uint32 lhs_ilat = ::ilat(isect.lat);
10950 int32 lhs_ilon = ::ilon(isect.lon);
10970 if (((lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)) == idx_it->first)
1098 // Ensure that the same intersection is processed only in one index
10990 {
1100 // Avoid rounding artifacts
11010 for (unsigned int k = 0; k < idx_it->second.size(); ++k)
11020 {
11030 if (isect.epsilon_equal(all_segments[idx_it->second[k]]))
11040 {
11050 isect.lat = all_segments[idx_it->second[k]].lat;
11060 isect.lon = all_segments[idx_it->second[k]].lon;
11070 }
11080 if (isect.epsilon_equal(all_segments[idx_it->second[k]+1]))
11090 {
11100 isect.lat = all_segments[idx_it->second[k]+1].lat;
11110 isect.lon = all_segments[idx_it->second[k]+1].lon;
11120 }
11130 }
11150 if (isect != all_segments[idx_it->second[j]] && isect != all_segments[idx_it->second[j]+1])
11160 {
11170 all_segments.push_back(all_segments[idx_it->second[j]]);
11180 all_segments.push_back(isect);
11190 all_segments.push_back(all_segments[idx_it->second[j]+1]);
11200 gap_positions.insert(std::lower_bound(
11210 gap_positions.begin(), gap_positions.end(), idx_it->second[j]+1), idx_it->second[j]+1);
11220 gap_positions.push_back(all_segments.size());
11240 replace_segment(segments_per_idx, all_segments[idx_it->second[j]], isect,
11250 all_segments[idx_it->second[j]+1], idx_it->second[j], all_segments.size()-3);
11260 }
11280 if (isect != all_segments[idx_it->second[i]] && isect != all_segments[idx_it->second[i]+1])
11290 {
11300 all_segments.push_back(all_segments[idx_it->second[i]]);
11310 all_segments.push_back(isect);
11320 all_segments.push_back(all_segments[idx_it->second[i]+1]);
11330 gap_positions.insert(std::lower_bound(
11340 gap_positions.begin(), gap_positions.end(), idx_it->second[i]+1), idx_it->second[i]+1);
11350 gap_positions.push_back(all_segments.size());
11370 replace_segment(segments_per_idx, all_segments[idx_it->second[i]], isect,
11380 all_segments[idx_it->second[i]+1], idx_it->second[i], all_segments.size()-3);
11390 }
11400 }
1142 //TODO: check and track common segments
11430 }
11441889457 q }
11451229687 q }
1146613387 q }
1147606108 q}
1150unsigned int line_divertion_idx(const Idx_Per_Point_Double& rhs)
11513667327 q{
11523667327 q return rhs.idx*2 + ((int)rhs.remote_idx - (int)rhs.idx - 1)/2;
11533667327 q}
1156void collect_divertions(const std::vector< Point_Double >& all_segments,
1157 uint32 idx, const std::vector< unsigned int >& segments,
1158 std::vector< Line_Divertion >& divertions)
1159613387 q{
1160613387 q std::vector< Idx_Per_Point_Double > pos_per_pt;
1162613387 q for (std::vector< unsigned int >::const_iterator seg_it = segments.begin();
11632456447 q seg_it != segments.end(); ++seg_it)
11641843067 q {
11651843067 q uint32 lhs_ilat = ::ilat(all_segments[*seg_it].lat);
11661843067 q int32 lhs_ilon = ::ilon(all_segments[*seg_it].lon);
11671843067 q if (((lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)) == idx)
11681833667 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it], *seg_it, *seg_it+1));
11701843067 q uint32 rhs_ilat = ::ilat(all_segments[*seg_it+1].lat);
11711843067 q int32 rhs_ilon = ::ilon(all_segments[*seg_it+1].lon);
11721843067 q if (((rhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)) == idx)
11731833667 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it+1], *seg_it+1, *seg_it));
11741843067 q }
1176613387 q if (pos_per_pt.empty())
11772444 q return;
1179610947 q std::sort(pos_per_pt.begin(), pos_per_pt.end());
1181610947 q unsigned int same_since = 0;
1182610947 q unsigned int i = 0;
11834889207 q while (i <= pos_per_pt.size())
11844278267 q {
11854278267 q if (i == pos_per_pt.size() || pos_per_pt[i].pt != pos_per_pt[same_since].pt)
11861833667 q {
11871833667 q if (i - same_since == 2)
11881833667 q {
11891833667 q divertions[line_divertion_idx(pos_per_pt[same_since])] = Line_Divertion(pos_per_pt[same_since+1]);
11901833667 q divertions[line_divertion_idx(pos_per_pt[same_since+1])] = Line_Divertion(pos_per_pt[same_since]);
11911833667 q }
11920 else
11930 {
11940 std::vector< std::pair< double, unsigned int > > line_per_gradient;
11950 for (unsigned int j = same_since; j < i; ++j)
11960 line_per_gradient.push_back(std::make_pair(
11970 atan2(all_segments[pos_per_pt[j].remote_idx].lon - all_segments[pos_per_pt[j].idx].lon,
11980 all_segments[pos_per_pt[j].remote_idx].lat - all_segments[pos_per_pt[j].idx].lat),
11990 j));
12010 std::sort(line_per_gradient.begin(), line_per_gradient.end());
12030 for (unsigned int j = 0; j < line_per_gradient.size(); j += 2)
12040 {
12050 if (j+3 < line_per_gradient.size()
12060 && line_per_gradient[j+1].first == line_per_gradient[j+2].first)
12070 {
12080 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j].second])]
12090 = Line_Divertion(pos_per_pt[line_per_gradient[j+3].second]);
12100 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+1].second])]
12110 = Line_Divertion(pos_per_pt[line_per_gradient[j+2].second]);
12120 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+2].second])]
12130 = Line_Divertion(pos_per_pt[line_per_gradient[j+1].second]);
12140 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+3].second])]
12150 = Line_Divertion(pos_per_pt[line_per_gradient[j].second]);
12160 j += 2;
12170 }
12180 else
12190 {
12200 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j].second])]
12210 = Line_Divertion(pos_per_pt[line_per_gradient[j+1].second]);
12220 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+1].second])]
12230 = Line_Divertion(pos_per_pt[line_per_gradient[j].second]);
12240 }
12250 }
12260 }
12281833667 q same_since = i;
12291833667 q }
12304278267 q ++i;
12314278267 q }
1232610947 q}
1235void assemble_linestrings(const std::vector< Point_Double >& all_segments,
1236 std::vector< Line_Divertion >& divertions, std::vector< std::vector< Point_Double > >& linestrings)
1237606108 q{
1238 // the function changes divertions because divertions is no longer needed after the function
1240606108 q unsigned int pos = 0;
1241606108 q unsigned int count = 0;
12423045318 q for (unsigned int i = 0; i < divertions.size(); i += 2)
12432439217 q count += (divertions[i+1].to_idx == std::numeric_limits< uint >::max());
1245 //Invariant: count is always equal to the number of i with divertions[2*i+1] == uint::max()
12461211658 q while (count < all_segments.size())
1247605557 q {
1248605557 q while (divertions[2*pos].to_idx == std::numeric_limits< uint >::max())
12490 {
12500 ++pos;
12510 if (pos == all_segments.size()-1)
12520 pos = 0;
12530 }
1255605557 q linestrings.push_back(std::vector< Point_Double >());
1256605557 q std::vector< Point_Double >& cur_target = linestrings.back();
1258605557 q int dir = 1;
12592439217 q while (divertions[2*pos + dir].to_idx != std::numeric_limits< uint >::max())
12601833667 q {
12611833667 q if (cur_target.size() >= 2 && all_segments[pos].epsilon_equal(cur_target[cur_target.size()-2]))
1262 // Remove pairs of equal segments
12630 cur_target.pop_back();
12641833667 q else if (cur_target.empty() || !all_segments[pos].epsilon_equal(cur_target.back()))
12651833667 q cur_target.push_back(all_segments[pos]);
12671833667 q Line_Divertion& old_div = divertions[2*(int)pos + dir];
12681833667 q Line_Divertion& divertion = divertions[2*(int)pos + (3*dir-1)/2];
12691833667 q dir = divertion.to_remote_idx - divertion.to_idx;
12701833667 q pos = divertion.to_idx;
12721833667 q ++count;
12731833667 q old_div.to_idx = std::numeric_limits< uint >::max();
12741833667 q }
1276605557 q ++pos;
1277605557 q if (pos == all_segments.size()-1)
12780 pos = 0;
1280605557 q if (cur_target.size() <= 2)
12810 linestrings.pop_back();
1282605557 q else
1283605557 q cur_target.push_back(cur_target.front());
1284605557 q }
1285606108 q}
1288struct RHR_Polygon_Area_Oracle : Area_Oracle
1290 RHR_Polygon_Area_Oracle(
1291 const std::vector< Point_Double >& all_segments_,
1292 const std::map< uint32, std::vector< unsigned int > >& segments_per_idx_)
1293606108 q : all_segments(&all_segments_), segments_per_idx(&segments_per_idx_) {}
1295 virtual void build_area(bool sw_corner_inside, int32 value, bool* se_corner_inside, bool* nw_corner_inside);
1296 virtual Area_Oracle::point_status get_point_status(int32 value, double lat, double lon);
1298private:
1299 const std::vector< Point_Double >* all_segments;
1300 const std::map< uint32, std::vector< unsigned int > >* segments_per_idx;
1301 std::set< uint32 > inside_corners;
1305void RHR_Polygon_Area_Oracle::build_area(
1306 bool sw_corner_inside, int32 value, bool* se_corner_inside, bool* nw_corner_inside)
1307613387 q{
1308613387 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value);
1309613387 q if (spi_it == segments_per_idx->end())
13100 return;
1312613387 q if (sw_corner_inside)
13133174 q inside_corners.insert(value);
1315613387 q if (::lon(uint32(value)<<16) > -179.99)
1316613387 q {
1317613387 q if (nw_corner_inside)
13184796 q {
13194796 q *nw_corner_inside = sw_corner_inside;
13214796 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
132220546 q seg_it != spi_it->second.end(); ++seg_it)
132315756 q {
132415756 q int32 lhs_ilon = ::ilon((*all_segments)[*seg_it].lon) & 0xffff0000;
132515756 q int32 rhs_ilon = ::ilon((*all_segments)[*seg_it+1].lon) & 0xffff0000;
132715756 q if ((lhs_ilon < (value<<16) && rhs_ilon == (value<<16))
132815756 q || (lhs_ilon == (value<<16) && rhs_ilon < (value<<16)))
13292354 q {
13302354 q double isect_lat = (*all_segments)[*seg_it].lat
13312354 q + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat)
13322354 q *(::lon(uint32(value)<<16) - (*all_segments)[*seg_it].lon)
13332354 q /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon);
13342354 q if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000))
13351934 q *nw_corner_inside = !*nw_corner_inside;
13362354 q }
133715756 q }
13384796 q }
1340613387 q if (se_corner_inside)
13418236 q {
13428236 q *se_corner_inside = sw_corner_inside;
13448236 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
134534656 q seg_it != spi_it->second.end(); ++seg_it)
134626426 q {
134726426 q uint32 lhs_ilat = ::ilat((*all_segments)[*seg_it].lat) & 0xffff0000;
134826426 q uint32 rhs_ilat = ::ilat((*all_segments)[*seg_it+1].lat) & 0xffff0000;
135026426 q if ((lhs_ilat < (value & 0xffff0000) && rhs_ilat == (value & 0xffff0000))
135126426 q || (lhs_ilat == (value & 0xffff0000) && rhs_ilat < (value & 0xffff0000)))
13522634 q {
13532634 q double isect_lon = (*all_segments)[*seg_it].lon
13542634 q + ((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon)
13552634 q *(::lat(value & 0xffff0000) - (*all_segments)[*seg_it].lat)
13562634 q /((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat);
13572634 q if ((::ilon(isect_lon) & 0xffff0000) == (uint32(value)<<16))
13581744 q *se_corner_inside = !*se_corner_inside;
13592634 q }
136026426 q }
13618236 q }
1362613387 q }
13630 else
13640 {
13650 if (nw_corner_inside)
13660 {
13670 *nw_corner_inside = sw_corner_inside;
13690 for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
13700 seg_it != spi_it->second.end(); ++seg_it)
13710 {
13720 double lhs_lon = (*all_segments)[*seg_it].lon;
13730 lhs_lon -= lhs_lon > 0 ? 360. : 0.;
13740 double rhs_lon = (*all_segments)[*seg_it+1].lon;
13750 rhs_lon -= rhs_lon > 0 ? 360. : 0.;
13770 if (::lon(uint32(value)<<16) > -180.)
13780 {
13790 int32 lhs_ilon = ::ilon(lhs_lon) & 0xffff0000;
13800 int32 rhs_ilon = ::ilon(rhs_lon) & 0xffff0000;
13820 if ((lhs_ilon < (value<<16) && rhs_ilon == (value<<16))
13830 || (lhs_ilon == (value<<16) && rhs_ilon < (value<<16)))
13840 {
13850 double isect_lat = (*all_segments)[*seg_it].lat
13860 + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat)
13870 *(::lon(uint32(value)<<16) - (*all_segments)[*seg_it].lon)
13880 /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon);
13890 if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000))
13900 *nw_corner_inside = !*nw_corner_inside;
13910 }
13920 }
13930 else
13940 {
13950 if ((lhs_lon <= -180. && rhs_lon > -180.) || (rhs_lon <= -180. && lhs_lon > -180.))
13960 {
13970 double isect_lat = (*all_segments)[*seg_it].lat
13980 + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat)
13990 *(-180. - (*all_segments)[*seg_it].lon)
14000 /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon);
14010 if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000))
14020 *nw_corner_inside = !*nw_corner_inside;
14030 }
14040 }
14050 }
14060 }
14080 if (se_corner_inside)
14090 {
14100 *se_corner_inside = sw_corner_inside;
14120 for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
14130 seg_it != spi_it->second.end(); ++seg_it)
14140 {
14150 uint32 lhs_ilat = ::ilat((*all_segments)[*seg_it].lat) & 0xffff0000;
14160 uint32 rhs_ilat = ::ilat((*all_segments)[*seg_it+1].lat) & 0xffff0000;
14180 if ((lhs_ilat < (value & 0xffff0000) && rhs_ilat == (value & 0xffff0000))
14190 || (lhs_ilat == (value & 0xffff0000) && rhs_ilat < (value & 0xffff0000)))
14200 {
14210 double lhs_lon = (*all_segments)[*seg_it].lon;
14220 lhs_lon -= lhs_lon > 0 ? 360. : 0.;
14230 double rhs_lon = (*all_segments)[*seg_it+1].lon;
14240 rhs_lon -= rhs_lon > 0 ? 360. : 0.;
14260 double isect_lon = (*all_segments)[*seg_it].lon
14270 + ((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon)
14280 *(::lat(value & 0xffff0000) - (*all_segments)[*seg_it].lat)
14290 /((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat);
14300 if (isect_lon >= -180. && (::ilon(isect_lon) & 0xffff0000) == (uint32(value)<<16))
14310 *se_corner_inside = !*se_corner_inside;
14320 }
14330 }
14340 }
14350 }
1436613387 q}
1439Area_Oracle::point_status RHR_Polygon_Area_Oracle::get_point_status(int32 value, double lat, double lon)
1440605557 q{
1441605557 q if (value == 1)
14420 return 1;
1444605557 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value);
1445605557 q if (spi_it == segments_per_idx->end())
14460 return 0;
1448605557 q double border_lon = std::max(::lon(value<<16), -180.);
1449605557 q double border_lat = ::lat(value & 0xffff0000);
1451605557 q bool on_vertex = false;
1452605557 q bool on_segment = false;
1453605557 q bool is_inside = (inside_corners.find(value) != inside_corners.end());
1454 // is_inside is now true iff the sw corner is inside the area
1456605557 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
14572431427 q seg_it != spi_it->second.end(); ++seg_it)
14581825877 q toggle_if_inside(is_inside, on_vertex, on_segment, border_lat, border_lon, Point_Double(lat, lon),
14591825877 q (*all_segments)[*seg_it], (*all_segments)[*seg_it+1]);
1461605557 q if (on_vertex)
1462605557 q return 0x20 + 2*is_inside;
14630 if (on_segment)
14640 return 0x10 + 2*is_inside;
14660 return 2*is_inside;
1470bool strictly_west_of(double lhs, double rhs)
1471609087 q{
1472609087 q if (lhs - rhs > 180.)
14730 return lhs - 360. < rhs;
1475609087 q return lhs < rhs;
1476609087 q}
1479bool weakly_west_of(double lhs, double rhs)
1480602024 q{
1481602024 q if (lhs - rhs > 180.)
14820 return lhs - 360. <= rhs;
1484602024 q return lhs <= rhs;
1485602024 q}
1488606108 qRHR_Polygon_Geometry::RHR_Polygon_Geometry(const Free_Polygon_Geometry& rhs) : bounds(0)
1489606108 q{
1490606108 q std::vector< std::vector< Point_Double > > input(*rhs.get_multiline_geometry());
1492606108 q std::vector< Point_Double > all_segments;
1493606108 q std::vector< unsigned int > gap_positions;
1495606108 q gap_positions.push_back(0);
14961211658 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = input.begin(); iti != input.end(); ++iti)
1497605557 q {
1498605557 q all_segments.push_back((*iti)[0]);
14992434767 q for (unsigned int i = 1; i < iti->size(); ++i)
15001829217 q interpolate_segment((*iti)[i-1].lat, (*iti)[i-1].lon, (*iti)[i].lat, (*iti)[i].lon, all_segments);
1501605557 q gap_positions.push_back(all_segments.size());
1502605557 q }
1504606108 q std::map< uint32, std::vector< unsigned int > > segments_per_idx;
1505606108 q std::vector< unsigned int >::const_iterator gap_it = gap_positions.begin();
15063045318 q for (unsigned int i = 0; i < all_segments.size(); ++i)
15072439217 q {
15082439217 q if (*gap_it == i)
1509605557 q ++gap_it;
15101833667 q else
15111833667 q add_segment(segments_per_idx, all_segments[i-1], all_segments[i], i-1);
15122439217 q }
1513606108 q split_segments(all_segments, gap_positions, segments_per_idx);
1515 // divertions[2*i - 2] tells which segment follows the segment from all_segments[i+1] to all_segments[i]
1516 // divertions[2*i - 1] tells which segment follows the segment from all_segments[i] to all_segments[i+1]
1517606108 q std::vector< Line_Divertion > divertions(2*all_segments.size());
1519 // fill the gaps with invalid following segments
15201211658 q for (unsigned int i = 1; i < gap_positions.size(); ++i)
1521605557 q {
1522605557 q divertions[gap_positions[i]*2 - 2] = Line_Divertion();
1523605557 q divertions[gap_positions[i]*2 - 1] = Line_Divertion();
1524605557 q }
1526 // compute the real segment connections
1527606108 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin();
15281219488 q idx_it != segments_per_idx.end(); ++idx_it)
1529613387 q collect_divertions(all_segments, idx_it->first, idx_it->second, divertions);
1531606108 q assemble_linestrings(all_segments, divertions, linestrings);
1533606108 q RHR_Polygon_Area_Oracle area_oracle(all_segments, segments_per_idx);
1534606108 q Four_Field_Index four_field_idx(&area_oracle);
1536606108 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin();
15371219488 q idx_it != segments_per_idx.end(); ++idx_it)
1538613387 q four_field_idx.add_point(::lat(idx_it->first | 0x8000), ::lon(idx_it->first<<16 | 0x8000), idx_it->first);
1540606108 q four_field_idx.compute_inside_parts();
1542606108 q for (std::vector< std::vector< Point_Double > >::iterator lstr_it = linestrings.begin();
15431211658 q lstr_it != linestrings.end(); ++lstr_it)
1544605557 q {
1545605557 q if (lstr_it->size() > 2)
1546605557 q {
1547605557 q if (strictly_west_of((*lstr_it)[0].lon, (*lstr_it)[1].lon))
1548602024 q {
1549602024 q if (weakly_west_of((*lstr_it)[1].lon, (*lstr_it)[2].lon))
1551813 q if (four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
15520 std::reverse(lstr_it->begin(), lstr_it->end());
1554601211 q else
1555601211 q {
1556601211 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
1557601211 q ^ (((*lstr_it)[1].lat - (*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon)
1558601211 q < ((*lstr_it)[2].lat -(*lstr_it)[1].lat)/((*lstr_it)[2].lon -(*lstr_it)[1].lon)))
1559601211 q std::reverse(lstr_it->begin(), lstr_it->end());
1560601211 q }
1561602024 q }
15623536 q else if (strictly_west_of((*lstr_it)[2].lon, (*lstr_it)[1].lon))
15631605 q {
15641605 q if (!(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3))
156511 q std::reverse(lstr_it->begin(), lstr_it->end());
15661605 q }
15671934 q else if ((*lstr_it)[0].lon == (*lstr_it)[1].lon)
1569421 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
1570421 q ^ ((*lstr_it)[0].lat < (*lstr_it)[1].lat))
1571421 q std::reverse(lstr_it->begin(), lstr_it->end());
15731513 q else if ((*lstr_it)[2].lon == (*lstr_it)[1].lon)
15740 {
15750 if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
15760 ^ ((*lstr_it)[1].lat < (*lstr_it)[2].lat))
15770 std::reverse(lstr_it->begin(), lstr_it->end());
15780 }
15791513 q else
15801513 q {
15811513 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
15821513 q ^ (((*lstr_it)[1].lat -(*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon)
15831513 q < ((*lstr_it)[2].lat -(*lstr_it)[1].lat)/((*lstr_it)[2].lon -(*lstr_it)[1].lon)))
15840 std::reverse(lstr_it->begin(), lstr_it->end());
15851513 q }
1586605557 q }
1587605557 q }
1588606108 q}
1591double RHR_Polygon_Geometry::center_lat() const
15931272 q if (!bounds)
15941272 q bounds = calc_bounds(linestrings);
15961272 q return bounds->center_lat();
1600double RHR_Polygon_Geometry::center_lon() const
16021272 q if (!bounds)
16030 bounds = calc_bounds(linestrings);
16051272 q return bounds->center_lon();
1609double RHR_Polygon_Geometry::south() const
16110 if (!bounds)
16120 bounds = calc_bounds(linestrings);
16140 return bounds->south;
1618double RHR_Polygon_Geometry::north() const
16200 if (!bounds)
16210 bounds = calc_bounds(linestrings);
16230 return bounds->north;
1627double RHR_Polygon_Geometry::west() const
16290 if (!bounds)
16300 bounds = calc_bounds(linestrings);
16320 return bounds->west;
1636double RHR_Polygon_Geometry::east() const
16380 if (!bounds)
16390 bounds = calc_bounds(linestrings);
16410 return bounds->east;
1645bool RHR_Polygon_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const
16470 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
16480 iti != linestrings.end(); ++iti)
16490 {
16500 for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it)
16510 {
16520 if (bbox.contains(*it))
16530 return true;
16540 }
16550 }
16570 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
16580 iti != linestrings.end(); ++iti)
16590 {
16600 for (uint i = 1; i < iti->size(); ++i)
16610 {
16620 if (bbox.intersects((*iti)[i-1], (*iti)[i]))
16630 return true;
16640 }
16650 }
16670 bool is_inside = false;
16680 bool on_vertex = false;
16690 bool on_segment = false;
16700 Point_Double bbox_center(bbox.center_lat(), bbox.center_lon());
16720 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
16730 iti != linestrings.end(); ++iti)
16740 {
16750 for (uint i = 1; i < iti->size(); ++i)
16760 toggle_if_inside(is_inside, on_vertex, on_segment, -90., -180.,
16770 bbox_center, (*iti)[i-1], (*iti)[i]);
16780 }
16790 return (is_inside || on_vertex || on_segment);
1683Opaque_Geometry* Compound_Geometry::clone() const
16847925619 q{
16857925619 q std::vector< Opaque_Geometry* > cloned;
168678803519 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
168770877918 q cloned.push_back((*it)->clone());
16887925619 q return new Compound_Geometry(cloned);
16897925619 q}
1692Bbox_Double* calc_bounds(const std::vector< Opaque_Geometry* >& components)
1694865827 q double south = 100.0;
1695865827 q double west = 200.0;
1696865827 q double north = -100.0;
1697865827 q double east = -200.0;
1698865827 q bool wrapped = false;
1700104344627 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
1701103478826 q {
1702103478826 q if ((*it)->has_bbox())
170352103324 q {
170452103324 q south = std::min(south, (*it)->south());
170552103324 q north = std::max(north, (*it)->north());
170652103324 q west = std::min(west, (*it)->west());
170752103324 q east = std::max(east, (*it)->east());
170952103324 q wrapped |= ((*it)->east() < (*it)->west());
171052103324 q }
1711103478826 q }
1713865827 q if (north == -100.0)
171463 q return new Bbox_Double(Bbox_Double::invalid);
1715865224 q if (!wrapped && east - west <= 180.)
1716865224 q return new Bbox_Double(south, west, north, east);
17180 west = 200.;
17190 east = -200.;
17200 for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
17210 {
17220 if (!(*it)->has_bbox())
17230 continue;
17250 if ((*it)->east() <= 0)
17260 {
17270 east = std::max(east, (*it)->east());
17280 if ((*it)->west() >= 0)
17290 west = std::min(west, (*it)->west());
17300 }
17310 else if ((*it)->west() >= 0)
17320 west = std::min(west, (*it)->west());
17330 else
1734 // The components are too wildly distributed
17350 return new Bbox_Double(south, -180.0, north, 180.0);
17360 }
17380 if (west - east > 180.)
17390 return new Bbox_Double(south, west, north, east);
17410 return new Bbox_Double(south, -180.0, north, 180.0);
1745bool Compound_Geometry::has_center() const
1747948618 q if (!bounds)
1748729218 q bounds = calc_bounds(components);
1750948618 q return bounds->valid();
1754double Compound_Geometry::center_lat() const
1756582213 q if (!bounds)
17570 bounds = calc_bounds(components);
1759582213 q return bounds->center_lat();
1763double Compound_Geometry::center_lon() const
1765614414 q if (!bounds)
17660 bounds = calc_bounds(components);
1768614414 q return bounds->center_lon();
1772bool Compound_Geometry::has_bbox() const
177310857 q{
177410857 q if (!bounds)
177510857 q bounds = calc_bounds(components);
177710857 q return bounds->valid();
177810857 q}
1781double Compound_Geometry::south() const
178210857 q{
178310857 q if (!bounds)
17840 bounds = calc_bounds(components);
178610857 q return bounds->south;
178710857 q}
1790double Compound_Geometry::north() const
179110857 q{
179210857 q if (!bounds)
17930 bounds = calc_bounds(components);
179510857 q return bounds->north;
179610857 q}
1799double Compound_Geometry::west() const
180010857 q{
180110857 q if (!bounds)
18020 bounds = calc_bounds(components);
180410857 q return bounds->west;
180510857 q}
1808double Compound_Geometry::east() const
180910857 q{
181010857 q if (!bounds)
18110 bounds = calc_bounds(components);
181310857 q return bounds->east;
181410857 q}
1817bool Compound_Geometry::relation_pos_is_valid(unsigned int member_pos) const
181816704 q{
181916704 q return (member_pos < components.size() && components[member_pos]
182016704 q && components[member_pos]->has_center());
182116704 q}
1824double Compound_Geometry::relation_pos_lat(unsigned int member_pos) const
182516704 q{
182616704 q if (member_pos < components.size() && components[member_pos])
182716704 q return components[member_pos]->center_lat();
18280 return 0;
182916704 q}
1832double Compound_Geometry::relation_pos_lon(unsigned int member_pos) const
183316704 q{
183416704 q if (member_pos < components.size() && components[member_pos])
183516704 q return components[member_pos]->center_lon();
18360 return 0;
183716704 q}
1840unsigned int Compound_Geometry::relation_way_size(unsigned int member_pos) const
18412930626 q{
18422930626 q if (member_pos < components.size() && components[member_pos])
18432930626 q return components[member_pos]->way_size();
18440 return 0;
18452930626 q}
1848bool Compound_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const
18491416796 q{
18501416796 q return (member_pos < components.size() && components[member_pos]
18511416796 q && components[member_pos]->way_pos_is_valid(nd_pos));
18521416796 q}
1855double Compound_Geometry::relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const
18561408616 q{
18571408616 q if (member_pos < components.size() && components[member_pos])
18581408616 q return components[member_pos]->way_pos_lat(nd_pos);
18590 return 0;
18601408616 q}
1863double Compound_Geometry::relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const
18641408616 q{
18651408616 q if (member_pos < components.size() && components[member_pos])
18661408616 q return components[member_pos]->way_pos_lon(nd_pos);
18670 return 0;
18681408616 q}
1871void Compound_Geometry::add_component(Opaque_Geometry* component)
1872117716236 q{
1873117716236 q delete bounds;
1874117716236 q bounds = 0;
1875117716236 q components.push_back(component);
1876117716236 q}
1879bool Compound_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const
18810 for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
18820 {
18830 if ((*it)->relevant_to_bbox(bbox))
18840 return true;
18850 }
18870 return false;
1891Opaque_Geometry* Partial_Relation_Geometry::clone() const
18932614 q std::vector< Opaque_Geometry* > cloned;
18945634924 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
18955632314 q cloned.push_back((*it)->clone());
18962614 q return new Partial_Relation_Geometry(cloned);
1900bool Partial_Relation_Geometry::has_center() const
19028238 q if (!has_coords)
19035333 q return 0;
19052907 q if (!bounds)
19062107 q bounds = calc_bounds(components);
19082907 q return bounds->valid();
1912double Partial_Relation_Geometry::center_lat() const
19141516 q if (!has_coords)
19150 return 0;
19171516 q if (!bounds)
19180 bounds = calc_bounds(components);
19201516 q return bounds->center_lat();
1924double Partial_Relation_Geometry::center_lon() const
19261656 q if (!has_coords)
19270 return 0;
19291656 q if (!bounds)
19300 bounds = calc_bounds(components);
19321656 q return bounds->center_lon();
1936bool Partial_Relation_Geometry::has_bbox() const
193725698 q{
193825698 q if (!has_coords)
193924985 q return false;
1941715 q if (!bounds)
1942715 q bounds = calc_bounds(components);
1944715 q return bounds->valid();
194525698 q}
1948double Partial_Relation_Geometry::south() const
1950715 q if (!has_coords)
19510 return 0;
1953715 q if (!bounds)
19540 bounds = calc_bounds(components);
1956715 q return bounds->south;
1960double Partial_Relation_Geometry::north() const
1962715 q if (!has_coords)
19630 return 0;
1965715 q if (!bounds)
19660 bounds = calc_bounds(components);
1968715 q return bounds->north;
1972double Partial_Relation_Geometry::west() const
1974715 q if (!has_coords)
19750 return 0;
1977715 q if (!bounds)
19780 bounds = calc_bounds(components);
1980715 q return bounds->west;
1984double Partial_Relation_Geometry::east() const
1986715 q if (!has_coords)
19870 return 0;
1989715 q if (!bounds)
19900 bounds = calc_bounds(components);
1992715 q return bounds->east;
1996bool Partial_Relation_Geometry::relation_pos_is_valid(unsigned int member_pos) const
1997477956 q{
1998477956 q return (member_pos < components.size() && components[member_pos]
1999477956 q && components[member_pos]->has_center());
2000477956 q}
2003double Partial_Relation_Geometry::relation_pos_lat(unsigned int member_pos) const
20054454 q if (member_pos < components.size() && components[member_pos])
20064454 q return components[member_pos]->center_lat();
20070 return 0;
2011double Partial_Relation_Geometry::relation_pos_lon(unsigned int member_pos) const
20134454 q if (member_pos < components.size() && components[member_pos])
20144454 q return components[member_pos]->center_lon();
20150 return 0;
2019unsigned int Partial_Relation_Geometry::relation_way_size(unsigned int member_pos) const
20202825557 q{
20212825557 q if (member_pos < components.size() && components[member_pos])
20222825557 q return components[member_pos]->way_size();
20230 return 0;
20242825557 q}
2027bool Partial_Relation_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const
2028646095 q{
2029646095 q return (member_pos < components.size() && components[member_pos]
2030646095 q && components[member_pos]->way_pos_is_valid(nd_pos));
2031646095 q}
2034double Partial_Relation_Geometry::relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const
2035540965 q{
2036540965 q if (member_pos < components.size() && components[member_pos])
2037540965 q return components[member_pos]->way_pos_lat(nd_pos);
20380 return 0;
2039540965 q}
2042double Partial_Relation_Geometry::relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const
2043540965 q{
2044540965 q if (member_pos < components.size() && components[member_pos])
2045540965 q return components[member_pos]->way_pos_lon(nd_pos);
20460 return 0;
2047540965 q}
2050void Partial_Relation_Geometry::add_placeholder()
205188873320 q{
205288873320 q components.push_back(new Null_Geometry());
205388873320 q}
2056void Partial_Relation_Geometry::add_point(const Point_Double& point)
2058531115 q delete bounds;
2059531115 q bounds = 0;
2060531115 q if (point.lat < 100.)
2061531115 q {
2062531115 q has_coords = true;
2063531115 q components.push_back(new Point_Geometry(point.lat, point.lon));
2064531115 q }
20650 else
20660 components.push_back(new Null_Geometry());
2070void Partial_Relation_Geometry::start_way()
207140275619 q{
207240275619 q components.push_back(new Partial_Way_Geometry());
207340275619 q}
2076void Partial_Relation_Geometry::add_way_point(const Point_Double& point)
2077218897217 q{
2078218897217 q delete bounds;
2079218897217 q bounds = 0;
2081218897217 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back());
2082218897217 q if (geom)
2083218897217 q {
2084218897217 q has_coords = true;
2085218897217 q geom->add_point(point);
2086218897217 q }
2087218897217 q}
2090void Partial_Relation_Geometry::add_way_placeholder()
20915152914 q{
20925152914 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back());
20935152914 q if (geom)
20945152914 q geom->add_point(Point_Double(100., 200.));
20955152914 q}
2098bool Partial_Relation_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const
21000 for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
21010 {
21020 if ((*it)->relevant_to_bbox(bbox))
21030 return true;
21040 }
21060 return false;
2110double great_circle_dist(double lat1, double lon1, double lat2, double lon2)
21111393273095 q{
21121393273095 q if (lat1 == lat2 && lon1 == lon2)
2113427 q return 0;
21141393268895 q static const double deg_to_arc = acos(0)/90.;
21151393268895 q double scalar_prod = cos((lat2-lat1)*deg_to_arc)
21161393268895 q + cos(lat1*deg_to_arc)*cos(lat2*deg_to_arc)*(cos((lon2-lon1)*deg_to_arc) - 1);
21171393268895 q if (scalar_prod > 1)
21180 scalar_prod = 1;
21191393268895 q static const double arc_to_meter = 10*1000*1000/acos(0);
21201393268895 q return acos(scalar_prod)*arc_to_meter;
21211393273095 q}
2124double length(const Opaque_Geometry& geometry)
21251622711 q{
21261622711 q double result = 0;
21281622711 q if (geometry.has_components())
21291706 q {
21301706 q const std::vector< Opaque_Geometry* >* components = geometry.get_components();
2131139506 q for (std::vector< Opaque_Geometry* >::const_iterator it = components->begin(); it != components->end(); ++it)
2132137806 q result += (*it ? length(**it) : 0);
21331706 q }
21341605711 q else if (geometry.has_line_geometry())
21351349111 q {
21361349111 q const std::vector< Point_Double >* line_geometry = geometry.get_line_geometry();
213728984611 q for (unsigned int i = 1; i < line_geometry->size(); ++i)
213827635511 q result += great_circle_dist((*line_geometry)[i-1].lat, (*line_geometry)[i-1].lon,
213927635511 q (*line_geometry)[i].lat, (*line_geometry)[i].lon);
21401349111 q }
214125666 q else if (geometry.has_faithful_way_geometry())
2143191 q for (unsigned int i = 1; i < geometry.way_size(); ++i)
21440 result += (geometry.way_pos_is_valid(i-1) && geometry.way_pos_is_valid(i) ?
21450 great_circle_dist(geometry.way_pos_lat(i-1), geometry.way_pos_lon(i-1),
21460 geometry.way_pos_lat(i), geometry.way_pos_lon(i)) : 0);
21491622711 q return result;
21501622711 q}
2153void collect_components(
2154 std::vector< Point_Double >& nodes, std::vector< std::vector< Point_Double > >& linestrings,
2155 const Opaque_Geometry& geometry)
21561003928 q{
21571003928 q if (geometry.has_components())
21581583 q {
21591583 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components();
2160927103 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
2161925523 q {
2162925523 q if (*it)
2163925523 q collect_components(nodes, linestrings, **it);
2164925523 q }
21651583 q }
21661002348 q else if (geometry.has_line_geometry())
2167296624 q linestrings.push_back(*geometry.get_line_geometry());
2168705726 q else if (geometry.has_multiline_geometry())
2170931 q const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry();
2171931 q for (std::vector< std::vector< Point_Double > >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it)
21720 linestrings.push_back(*it);
2174704796 q else if (geometry.has_center())
2175417955 q nodes.push_back(Point_Double(geometry.center_lat(), geometry.center_lon()));
21761003928 q}
2179struct Linestring_Geometry_Ptr
21811437724 q Linestring_Geometry_Ptr(Linestring_Geometry* ptr_) : ptr(ptr_) {}
2182 bool operator<(Linestring_Geometry_Ptr rhs) const;
2184private:
2185 Linestring_Geometry* ptr;
2189bool Linestring_Geometry_Ptr::operator<(Linestring_Geometry_Ptr rhs_) const
219025417152 q{
219125417152 q if (!rhs_.ptr)
21920 return false;
219325417152 q if (!ptr)
21940 return true;
219625417152 q const std::vector< Point_Double >& lhs = *ptr->get_line_geometry();
219725417152 q const std::vector< Point_Double >& rhs = *rhs_.ptr->get_line_geometry();
219925417152 q if (lhs.size() != rhs.size())
2200431952 q return lhs.size() < rhs.size();
220224985202 q uint l_start = 0;
220324985202 q uint l_factor = 1;
220424985202 q if (lhs.back() < lhs.front())
220513149692 q {
220613149692 q l_start = lhs.size()-1;
220713149692 q l_factor = -1;
220813149692 q }
221024985202 q uint r_start = 0;
221124985202 q uint r_factor = 1;
221224985202 q if (rhs.back() < rhs.front())
221315754112 q {
221415754112 q r_start = rhs.size()-1;
221515754112 q r_factor = -1;
221615754112 q }
221828255122 q for (int i = 0; i < (int)lhs.size(); ++i)
221926742622 q {
222026742622 q if (lhs[l_start + l_factor*i] != rhs[r_start + r_factor*i])
222123472702 q return lhs[l_start + l_factor*i] < rhs[r_start + r_factor*i];
222226742622 q }
22231512501 q return false;
222424985202 q}
2227Opaque_Geometry* make_trace(const Opaque_Geometry& geometry)
222878408 q{
222978408 q std::vector< Point_Double > nodes;
223078408 q std::vector< std::vector< Point_Double > > linestrings;
223178408 q collect_components(nodes, linestrings, geometry);
223378408 q std::sort(nodes.begin(), nodes.end());
223478408 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end());
223678408 q std::map< Point_Double, uint > coord_count;
2237483768 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
2238405365 q ++coord_count[*it];
224078408 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin();
2241375028 q lit != linestrings.end(); ++lit)
2242296624 q {
2243296624 q if (lit->empty())
22440 continue;
2245296624 q coord_count[lit->front()] += 2;
2246296624 q coord_count[lit->back()] += 2;
22471930574 q for (uint i = 1; i < lit->size() - 1; ++i)
22481633954 q ++coord_count[(*lit)[i]];
2249296624 q }
225178408 q Compound_Geometry* result = new Compound_Geometry();
2253483768 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
2254405365 q result->add_component(new Point_Geometry(it->lat, it->lon));
225678408 q std::set< Linestring_Geometry_Ptr > lstrs;
225778408 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin();
2258375028 q lit != linestrings.end(); ++lit)
2259296624 q {
2260296624 q if (lit->empty())
22610 continue;
2263296624 q std::vector< Point_Double > points;
2264296624 q points.push_back(lit->front());
22651930574 q for (uint i = 1; i < lit->size() - 1; ++i)
22661633954 q {
22671633954 q if (coord_count[(*lit)[i]] > 1)
22681141101 q {
22691141101 q points.push_back((*lit)[i]);
22711141101 q Linestring_Geometry* lstr = new Linestring_Geometry(points);
22721141101 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second)
2273521841 q result->add_component(lstr);
2274619261 q else
2275619261 q delete lstr;
22771141101 q points.clear();
22781141101 q }
22791633954 q points.push_back((*lit)[i]);
22801633954 q }
2281296624 q points.push_back(lit->back());
2282296624 q Linestring_Geometry* lstr = new Linestring_Geometry(points);
2283296624 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second)
2284159634 q result->add_component(lstr);
2285136991 q else
2286136991 q delete lstr;
2287296624 q }
228978408 q return result;
229078408 q}
2293void collect_components(std::vector< Point_Double >& nodes, const Opaque_Geometry& geometry)
229410988 q{
229510988 q if (geometry.has_components())
229752 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components();
22985692 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
22995642 q {
23005642 q if (*it)
23015642 q collect_components(nodes, **it);
23025642 q }
230410938 q else if (geometry.has_line_geometry())
23057015 q {
23067015 q const std::vector< Point_Double >& lstrs = *geometry.get_line_geometry();
230784745 q for (std::vector< Point_Double >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it)
230877735 q nodes.push_back(*it);
23097015 q }
23103925 q else if (geometry.has_multiline_geometry())
2312441 q const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry();
2313441 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = lstrs.begin(); lit != lstrs.end(); ++lit)
23140 {
23150 for (std::vector< Point_Double >::const_iterator it = lit->begin(); it != lit->end(); ++it)
23160 nodes.push_back(*it);
23170 }
23193485 q else if (geometry.has_center())
23201223 q nodes.push_back(Point_Double(geometry.center_lat(), geometry.center_lon()));
232110988 q}
2324struct Point_Double_By_Lon
2326 bool operator()(const Point_Double& lhs, const Point_Double& rhs)
2327681695 q {
2328681695 q if (lhs.lon != rhs.lon)
2329675175 q return lhs.lon < rhs.lon;
23306525 q return lhs.lat < rhs.lat;
2331681695 q }
2335struct Spherical_Vector
233735735 q Spherical_Vector() : x(0), y(0), z(0) {}
233981785 q Spherical_Vector(const Point_Double& pt) : x(sin(pt.lat*deg_to_arc()))
234081785 q {
234181785 q double cos_lat = cos(pt.lat*deg_to_arc());
234281785 q y = cos_lat*sin(pt.lon*deg_to_arc());
234381785 q z = cos_lat*cos(pt.lon*deg_to_arc());
234481785 q }
2346 Spherical_Vector(const Spherical_Vector& lhs, const Spherical_Vector& rhs)
234762785 q : x(lhs.y*rhs.z - lhs.z*rhs.y), y(lhs.z*rhs.x - lhs.x*rhs.z), z(lhs.x*rhs.y - lhs.y*rhs.x)
234862785 q {
234962785 q double length = sqrt(x*x + y*y + z*z);
235062785 q x /= length;
235162785 q y /= length;
235262785 q z /= length;
235362785 q }
2355 double x;
2356 double y;
2357 double z;
23590 double operator==(const Spherical_Vector& rhs) const { return x == rhs.x && y == rhs.y && z == rhs.z; }
2360625215 q double operator*(const Spherical_Vector& rhs) const { return x*rhs.x + y*rhs.y + z*rhs.z; }
2362 static double deg_to_arc()
2363327125 q {
2364327125 q static const double result = acos(0)/90.;
2365327125 q return result;
2366327125 q }
2370struct Proto_Hull
2372 struct Hull_Segment
23748685 q Hull_Segment(const Point_Double& pt) : ll_pt(pt), s_pt(pt) {}
237527055 q Hull_Segment(const Point_Double& pt, const Spherical_Vector& s) : ll_pt(pt), s_pt(s) {}
2377 Point_Double ll_pt;
2378 Spherical_Vector s_pt;
2379 Spherical_Vector edge;
2380 };
2382 Proto_Hull(const Point_Double& s, const Point_Double& w, const Point_Double& n, const Point_Double& e);
2383 void enhance(const Point_Double& rhs);
2384 std::vector< Point_Double > get_line_geometry() const;
2386private:
2387 std::vector< Hull_Segment > segments;
2391Proto_Hull::Proto_Hull(const Point_Double& s, const Point_Double& w, const Point_Double& n, const Point_Double& e)
23934345 q if (w.lon != e.lon)
23944345 q {
23954345 q segments.push_back(Hull_Segment(w));
23964345 q segments.push_back(Hull_Segment(e));
23974345 q }
23980 else if (n.lat != s.lat)
23990 {
24000 segments.push_back(Hull_Segment(s));
24010 segments.push_back(Hull_Segment(n));
24020 }
24044345 q if (!segments.empty())
24054345 q {
24064345 q segments[1].edge = Spherical_Vector(segments[0].s_pt, segments[1].s_pt);
24074345 q segments[0].edge = Spherical_Vector(segments[1].s_pt, segments[0].s_pt);
24084345 q }
2412void Proto_Hull::enhance(const Point_Double& rhs)
241373105 q{
241473105 q Spherical_Vector s_pt(rhs);
241673105 q std::vector< Hull_Segment >::iterator from_it = segments.begin();
2417527515 q while (from_it != segments.end() && from_it->edge*s_pt < 1e-8)
2418454415 q ++from_it;
242073105 q if (from_it != segments.end() && from_it->edge*s_pt >= 1e-8)
242127055 q {
242227055 q if (segments.begin()->edge*s_pt >= 0)
242314775 q {
242414775 q from_it = segments.end();
242514775 q --from_it;
242614775 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0)
24270 --from_it;
242814775 q ++from_it;
242914775 q segments.erase(from_it, segments.end());
243114775 q std::vector< Hull_Segment >::iterator to_it = segments.begin();
243235545 q while (to_it != segments.end() && to_it->edge*s_pt >= 0)
243320775 q ++to_it;
243414775 q --to_it;
243514775 q from_it = segments.begin();
243714775 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt);
243814775 q if (from_it == to_it)
24399365 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt));
24405414 q else
24415414 q {
24425414 q ++from_it;
24435414 q from_it = segments.erase(from_it, to_it);
24445414 q --from_it;
24455414 q *from_it = Hull_Segment(rhs, s_pt);
24465414 q }
244714775 q }
244812285 q else
244912285 q {
245012285 q std::vector< Hull_Segment >::iterator to_it = from_it;
245128795 q while (to_it != segments.end() && to_it->edge*s_pt >= 0)
245216515 q ++to_it;
245312285 q --to_it;
245424905 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0)
245512625 q --from_it;
245612285 q ++from_it;
245812285 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt);
245912285 q if (from_it == to_it)
24608375 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt));
24613915 q else
24623915 q {
24633915 q ++from_it;
24643915 q from_it = segments.erase(from_it, to_it);
24653915 q --from_it;
24663915 q *from_it = Hull_Segment(rhs, s_pt);
24673915 q }
246812285 q }
247027055 q if (from_it == segments.begin())
247114775 q from_it->edge = Spherical_Vector(segments.back().s_pt, s_pt);
247212285 q else
247312285 q from_it->edge = Spherical_Vector((from_it-1)->s_pt, s_pt);
247427055 q }
247573105 q}
2478std::vector< Point_Double > Proto_Hull::get_line_geometry() const
24804345 q std::vector< Point_Double > result;
24814345 q result.reserve(segments.size());
248229505 q for (std::vector< Hull_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it)
248325165 q result.push_back(it->ll_pt);
24844345 q return result;
2488Opaque_Geometry* make_hull(const Opaque_Geometry& geometry)
24905348 q std::vector< Point_Double > nodes;
24915348 q collect_components(nodes, geometry);
24935348 q std::sort(nodes.begin(), nodes.end(), Point_Double_By_Lon());
24945348 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end());
24965348 q if (nodes.empty())
249742 q return new Null_Geometry();
24985306 q if (nodes.size() == 1)
2499241 q return new Point_Geometry(nodes.front().lat, nodes.front().lon);
25005065 q if (nodes.size() == 2)
2502723 q nodes.push_back(nodes.front());
2503723 q return new Linestring_Geometry(nodes);
25064345 q Point_Double min_lat(100., 0.);
25074345 q Point_Double max_lat(-100., 0.);
250877445 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
250973105 q {
251073105 q if (it->lat < min_lat.lat)
251118305 q min_lat = *it;
251273105 q if (it->lat > max_lat.lat)
251317695 q max_lat = *it;
251473105 q }
25164345 q double max_lon_delta = nodes.front().lon + 360. - nodes.back().lon;
25174345 q Point_Double west_end = nodes.front();
25184345 q Point_Double east_end = nodes.back();
251973105 q for (uint i = 1; i < nodes.size(); ++i)
252068765 q {
252168765 q if (max_lon_delta < nodes[i].lon - nodes[i-1].lon)
25220 {
25230 max_lon_delta = nodes[i].lon - nodes[i-1].lon;
25240 west_end = nodes[i];
25250 east_end = nodes[i-1];
25260 }
252768765 q }
25294345 q Proto_Hull proto_hull(min_lat, west_end, max_lat, east_end);
253077445 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
253173105 q proto_hull.enhance(*it);
25334345 q std::vector< Point_Double > line_geom = proto_hull.get_line_geometry();
25344345 q if (line_geom.size() < 3)
2535423 q return new Linestring_Geometry(line_geom);
25373925 q return new RHR_Polygon_Geometry(Free_Polygon_Geometry(
25383925 q std::vector< std::vector< Point_Double > >(1, line_geom)));