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

overpass_api/core/geometry.cc

1151 of 1795 instrumented lines run by the suite (64.1%) 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
32701723 q{
33701723 q return (south + north) / 2;
34701723 q}
37double Bbox_Double::center_lon() const
382859623 q{
392859623 q if (west <= east)
402859623 q return (west + east) / 2;
410 else if ((west + east) / 2 <= 0)
420 return (west + east) / 2 + 180.0;
430 else
440 return (west + east) / 2 - 180.0;
452859623 q}
48bool Bbox_Double::contains(const Point_Double& point) const
4929228 q{
5029228 q if (point.lat + 5e-8 < south || point.lat - 5e-8 > north)
5112411 q return false;
5316826 q if (east >= west)
5416826 q return point.lon + 5e-8 >= west && point.lon - 5e-8 <= east;
560 return point.lon + 5e-8 >= west || point.lon - 5e-8 <= east;
5716826 q}
59bool Bbox_Double::intersects(const Point_Double& from, const Point_Double& to) const
604672105135 q{
614672105135 q double from_lon = from.lon;
624672105135 q double to_lon = to.lon;
634672105135 q if (from.lon < 0. && to.lon - from.lon > 180.)
640 from_lon += 360.;
654672105135 q if (to.lon < 0. && from.lon - to.lon > 180.)
660 to_lon += 360.;
684672105135 q double delta_from = 0;
694672105135 q if (from.lat + 5e-8 < south)
702415859129 q {
712415859129 q if (to.lat + 5e-8 < south)
722412410129 q return false;
733449125 q if (to.lat - 5e-8 > from.lat)
743449125 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
763449125 q }
772256246132 q else if (from.lat - 5e-8 > north)
781317511130 q {
791317511130 q if (to.lat - 5e-8 > north)
801313983130 q return false;
813528126 q if (to.lat + 5e-8 < from.lat)
823528126 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
843528126 q }
86945712129 q if (to.lat + 5e-8 < south && from.lat - 5e-8 > to.lat)
87 // Adjust to.lat and to.lon
882134121 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
90943578129 q else if (to.lat - 5e-8 > north && from.lat + 5e-8 < to.lat)
91 // Adjust to.lat and to.lon
922484122 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
94945712129 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.
102945712129 q double min_lon = std::min(from_lon, to_lon);
103945712129 q double max_lon = std::max(from_lon, to_lon);
104945712129 q if (west <= east)
105945712129 q {
106945712129 q if (max_lon < 180.)
107945712129 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;
112945712129 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)
13032000438 q{
13132000438 q double south = 100.0;
13232000438 q double west = 200.0;
13332000438 q double north = -100.0;
13432000438 q double east = -200.0;
136933177238 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it)
137901176838 q {
138901176838 q if (it->lat < 100.)
139901170338 q {
140901170338 q south = std::min(south, it->lat);
141901170338 q west = std::min(west, it->lon);
142901170338 q north = std::max(north, it->lat);
143901170338 q east = std::max(east, it->lon);
144901170338 q }
145901176838 q }
14732000438 q if (north == -100.0)
1480 return new Bbox_Double(Bbox_Double::invalid);
14932000438 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 }
17232000438 q else
17332000438 q return new Bbox_Double(south, west, north, east);
17432000438 q}
177double Linestring_Geometry::center_lat() const
178114810 q{
179114810 q if (!bounds)
180105810 q bounds = calc_bounds(points);
182114810 q return bounds->center_lat();
183114810 q}
186double Linestring_Geometry::center_lon() const
187196510 q{
188196510 q if (!bounds)
18912336 q bounds = calc_bounds(points);
191196510 q return bounds->center_lon();
192196510 q}
195double Linestring_Geometry::south() const
19630042028 q{
19730042028 q if (!bounds)
19829957127 q bounds = calc_bounds(points);
20030042028 q return bounds->south;
20130042028 q}
204double Linestring_Geometry::north() const
20530013728 q{
20630013728 q if (!bounds)
2070 bounds = calc_bounds(points);
20930013728 q return bounds->north;
21030013728 q}
213double Linestring_Geometry::west() const
21459022828 q{
21559022828 q if (!bounds)
2162831 q bounds = calc_bounds(points);
21859022828 q return bounds->west;
21959022828 q}
222double Linestring_Geometry::east() const
22358994528 q{
22458994528 q if (!bounds)
2250 bounds = calc_bounds(points);
22758994528 q return bounds->east;
22858994528 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
2741785913 q{
2751785913 q if (!has_coords)
2760 return 0;
2781785913 q if (!bounds)
2791785913 q bounds = calc_bounds(points);
2811785913 q return bounds->south;
2821785913 q}
285double Partial_Way_Geometry::north() const
2861785913 q{
2871785913 q if (!has_coords)
2880 return 0;
2901785913 q if (!bounds)
2910 bounds = calc_bounds(points);
2931785913 q return bounds->north;
2941785913 q}
297double Partial_Way_Geometry::west() const
2983571113 q{
2993571113 q if (!has_coords)
3000 return 0;
3023571113 q if (!bounds)
3030 bounds = calc_bounds(points);
3053571113 q return bounds->west;
3063571113 q}
309double Partial_Way_Geometry::east() const
3103571113 q{
3113571113 q if (!has_coords)
3120 return 0;
3143571113 q if (!bounds)
3150 bounds = calc_bounds(points);
3173571113 q return bounds->east;
3183571113 q}
321Partial_Way_Geometry::Partial_Way_Geometry(const std::vector< Point_Double >& points_)
32220164 q : points(points_), bounds(0), has_coords(false)
32320164 q{
32440324 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end() && !has_coords; ++it)
32520164 q has_coords |= (it->lat < 100.);
32720164 q if (has_coords)
32819984 q {
32919984 q valid_segments.push_back(std::vector< Point_Double >());
33019984 q if (!points.empty() && points.front().lat < 100.)
33119984 q valid_segments.back().push_back(points.front());
332381384 q for (unsigned int i = 1; i < points.size(); ++i)
333361404 q {
334361404 q if (points[i].lat < 100.)
335361404 q {
336361404 q if (points[i-1].lat >= 100.)
3370 valid_segments.push_back(std::vector< Point_Double >());
338361404 q valid_segments.back().push_back(points[i]);
339361404 q }
340361404 q }
34119984 q }
34220164 q}
344void Partial_Way_Geometry::add_point(const Point_Double& point)
34568371121 q{
34668371121 q delete bounds;
34768371121 q bounds = 0;
34868371121 q if (point.lat < 100.)
34962938717 q {
35062938717 q if (points.empty() || (points.back().lat >= 100.))
3512907617 q valid_segments.push_back(std::vector< Point_Double >());
35262938717 q valid_segments.back().push_back(point);
35362938717 q has_coords = true;
35462938717 q }
35568371121 q points.push_back(point);
35668371121 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)
37846083 q{
37946083 q double south = 100.0;
38046083 q double west = 200.0;
38146083 q double north = -100.0;
38246083 q double east = -200.0;
38446083 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin();
38592163 q iti != linestrings.end(); ++iti)
38646083 q {
387604783 q for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it)
388558703 q {
389558703 q if (it->lat < 100.)
390558703 q {
391558703 q south = std::min(south, it->lat);
392558703 q west = std::min(west, it->lon);
393558703 q north = std::max(north, it->lat);
394558703 q east = std::max(east, it->lon);
395558703 q }
396558703 q }
39746083 q }
39946083 q if (north == -100.0)
4000 return new Bbox_Double(Bbox_Double::invalid);
40146083 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 }
42846083 q else
42946083 q return new Bbox_Double(south, west, north, east);
43046083 q}
43346227 qFree_Polygon_Geometry::Free_Polygon_Geometry(const std::vector< std::vector< Point_Double > >& linestrings_) : linestrings(linestrings_), bounds(0)
43446227 q{
43592447 q for (std::vector< std::vector< Point_Double > >::iterator it = linestrings.begin(); it != linestrings.end();
43646227 q ++it)
43746227 q {
43846227 q if (it->front() != it->back())
43946227 q it->push_back(it->front());
44046227 q }
44146227 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)
499181904 q{
500181904 q if (linestring.size() < 2)
5010 return;
503181904 q delete bounds;
504181904 q bounds = 0;
505181904 q linestrings.push_back(linestring);
506181904 q if (linestrings.back().front() != linestrings.back().back())
507181852 q linestrings.back().push_back(linestrings.back().front());
508181904 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)
513839899 q{
514839899 q if (pt.lon < -179.9)
5150 {
5160 lhs.lon -= lhs.lon > 0 ? 360. : 0.;
5170 rhs.lon -= rhs.lon > 0 ? 360. : 0.;
5180 }
519839899 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 }
525839899 q if (lhs.lat == rhs.lat)
526176253 q {
527176253 q if (lhs.lat < pt.lat)
528101 q is_inside ^= ((lhs.lon - pt.lon)*(rhs.lon - pt.lon) < 0);
529176153 q else if (lhs.lat == pt.lat)
530176013 q {
531176013 q if (pt.lon == lhs.lon || pt.lon == rhs.lon)
532176013 q on_vertex = true;
5330 else
5340 on_segment |= ((lhs.lon - pt.lon)*(rhs.lon - pt.lon) < 0);
535176013 q }
536 // no else required -- such a segment does not play a role
537176253 q }
538663649 q else if (lhs.lon == rhs.lon)
539176133 q {
540176133 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon))
54132 q on_vertex = true;
542176102 q else if (pt.lon == lhs.lon && (pt.lat - lhs.lat)*(pt.lat - rhs.lat) < 0)
5430 on_segment = true;
544176102 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
547176133 q }
548487519 q else if ((lhs.lat - rhs.lat)*(lhs.lon - rhs.lon) < 0)
549 // The segment runs from nw to se or se to nw
550163367 q {
551163367 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon))
55253026 q on_vertex = true;
553110347 q else if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0)
55417023 q {
55517023 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon);
55617023 q if (isect_lat < pt.lat)
55712153 q is_inside = !is_inside;
5584871 q else if (isect_lat == pt.lat)
5590 on_segment = true;
56017023 q }
56193327 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon)
5620 {
5630 if (lhs.lat < pt.lat)
5640 is_inside = !is_inside;
5650 }
56693327 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon)
56811 q if (rhs.lat < pt.lat)
56911 q is_inside = !is_inside;
57193317 q else if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0)
5727163 q {
5737163 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat);
5747163 q if (border_lon < isect_lon && isect_lon < pt.lon)
5751131 q is_inside = !is_inside;
5767163 q }
577163367 q }
578324158 q else
579 // The segment runs from sw to ne or ne to sw
580324158 q {
581324158 q if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0)
58210294 q {
58310294 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat);
58410294 q if ((lhs.lon <= pt.lon || rhs.lon <= pt.lon) && (border_lon < isect_lon))
5857524 q is_inside = !is_inside;
58610294 q }
587324158 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon))
588215224 q {
589215224 q on_vertex = true;
590215224 q return;
591215224 q }
593108936 q if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0)
59418325 q {
59518325 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon);
59618325 q if (isect_lat < pt.lat)
59713315 q is_inside = !is_inside;
5985012 q else if (isect_lat == pt.lat)
5990 on_segment = true;
60018325 q }
60190616 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon)
6020 {
6030 if (lhs.lat < pt.lat)
6040 is_inside = !is_inside;
6050 }
60690616 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon)
60821 q if (rhs.lat < pt.lat)
60921 q is_inside = !is_inside;
611108936 q }
612839899 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)
644106236 q{
645106236 q static const double deg_to_arc = acos(0)/90.;
647106236 q double sin_factor = sin(factor);
648106236 q double cos_factor = cos(factor);
649106236 q double new_x = sin_factor * orth_x + cos_factor * lhs_gc_x;
650106236 q double new_y = sin_factor * orth_y;
651106236 q double new_z = sin_factor * orth_z + cos_factor * lhs_gc_z;
653106236 q double new_lat = asin(new_x)/deg_to_arc;
654106236 q double new_lon = atan2(new_y, new_z)/deg_to_arc + lhs_lon;
655106236 q if (new_lon < -180.)
6560 new_lon += 360.;
657106236 q else if (new_lon > 180.)
6580 new_lon -= 360.;
660106236 q return Point_Double(new_lat, new_lon);
661106236 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_)
69227926 q : orth_x(orth_x_), orth_y(orth_y_), orth_z(orth_z_),
69327926 q lhs_gc_x(lhs_gc_x_), lhs_gc_z(lhs_gc_z_), lhs_lon(lhs_lon_),
69427926 q center(lhs_gc_x == 0 ? dist : atan(orth_x/lhs_gc_x)),
69527926 q ex_pt(interpolation_point(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z,
69627926 q std::max(std::min(center, dist), 0.), lhs_lon)),
69727926 q ex_gc_pt_lat(ex_pt.lat),
69827926 q target(&target_)
69927926 q {
70027926 q static const double deg_to_arc = acos(0)/90.;
70127926 q acceptable_max_length = .0065536*cos(ex_pt.lat * deg_to_arc);
70327926 q double bounded_center = std::max(std::min(center, dist), 0.);
70427926 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 }
71027926 q center = bounded_center;
71127926 q }
714 void Interpolation_Collector::collect_single_point(double dist)
71642 q target->push_back(interpolation_point(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, dist, lhs_lon));
720 void Interpolation_Collector::collect_sequence(double from, double to, double max_step)
72155846 q {
72255846 q int num_sections = (int)((to - from)/max_step)+1;
723134116 q for (int j = 1; j < num_sections; ++j)
72478276 q target->push_back(interpolation_point(orth_x, orth_y, orth_z,
72578276 q lhs_gc_x, lhs_gc_z, from + (to - from)*j/num_sections, lhs_lon));
72655846 q }
729 void Interpolation_Collector::collect_center(double from, double to, int divisor)
73027926 q {
73127926 q static const double deg_to_arc = acos(0)/90.;
73327926 q double max_length_threshold = .0065536/divisor;
73427926 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 }
75027926 q else
75127926 q {
75227926 q if (acceptable_max_length < 1e-9)
7530 acceptable_max_length = 1e-9;
75427926 q acceptable_max_length *= deg_to_arc;
75627926 q collect_sequence(from, center, acceptable_max_length);
75727926 q if (center > from && center < to)
75842 q collect_single_point(center);
75927926 q collect_sequence(center, to, acceptable_max_length);
76027926 q }
76127926 q }
765void interpolate_segment(double lhs_lat, double lhs_lon, double rhs_lat, double rhs_lon,
766 std::vector< Point_Double >& target)
7679967411 q{
7689967411 q static const double deg_to_arc = acos(0)/90.;
7709967411 q if (fabs(rhs_lat - lhs_lat) < .0065536 && fabs(rhs_lon - lhs_lon) < .0065536)
7719688211 q target.push_back(Point_Double(rhs_lat, rhs_lon));
77227926 q else
77327926 q {
77427926 q if (fabs(rhs_lon - lhs_lon) > 180.)
7750 rhs_lon = (lhs_lon < 0 ? rhs_lon - 360. : rhs_lon + 360.);
77727926 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 }
79527926 q else
79627926 q {
79727926 q double lhs_cos = cos(lhs_lat * deg_to_arc);
79827926 q double lhs_gc_x = sin(lhs_lat * deg_to_arc);
79927926 q double lhs_gc_z = lhs_cos;
80127926 q double rhs_cos = cos(rhs_lat * deg_to_arc);
80227926 q double rhs_gc_x = sin(rhs_lat * deg_to_arc);
80327926 q double rhs_gc_y = sin((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos;
80427926 q double rhs_gc_z = cos((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos;
80627926 q double prod = lhs_gc_x * rhs_gc_x + lhs_gc_z * rhs_gc_z;
80727926 q double dist = acos(prod);
80927926 q double orth_x = rhs_gc_x - prod * lhs_gc_x;
81027926 q double orth_y = rhs_gc_y;
81127926 q double orth_z = rhs_gc_z - prod * lhs_gc_z;
81227926 q double lg_orth = sqrt(orth_x * orth_x + orth_y * orth_y + orth_z * orth_z);
81327926 q orth_x /= lg_orth;
81427926 q orth_y /= lg_orth;
81527926 q orth_z /= lg_orth;
81727926 q Interpolation_Collector collector(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, dist, lhs_lon, target);
81827926 q collector.collect_center(0, dist, 4);
81927926 q }
82127926 q if (rhs_lon < -180.)
8220 rhs_lon += 360.;
82327926 q else if (rhs_lon > 180.)
8240 rhs_lon -= 360.;
82527926 q target.push_back(Point_Double(rhs_lat, rhs_lon));
82627926 q }
8279967411 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)
83210750511 q{
83310750511 q uint32 lhs_ilat = ::ilat(from.lat);
83410750511 q int32 lhs_ilon = ::ilon(from.lon);
83510750511 q uint32 rhs_ilat = ::ilat(to.lat);
83610750511 q int32 rhs_ilon = ::ilon(to.lon);
83810750511 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos);
83910750511 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000))
84099446 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)].push_back(pos);
84110750511 q if ((lhs_ilat & 0xffff0000) != (rhs_ilat & 0xffff0000))
84275806 q {
84375806 q segments_per_idx[(rhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos);
84475806 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000))
84528506 q segments_per_idx[(rhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)].push_back(pos);
84675806 q }
84710750511 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)
90328974710 q{
90428974710 q double rfmlt_lat = rhs_from.lat - lhs_to.lat;
90528974710 q double rfmlt_lon = rhs_from.lon - lhs_to.lon;
907 //The two segments are connected by a vertex
90828974710 q if (!rfmlt_lat && !rfmlt_lon)
9098611110 q return false;
9102036369 q if (lhs_from == rhs_to || lhs_from == rhs_from || lhs_to == rhs_to)
911224539 q return false;
9131811837 q double lfmlt_lat = lhs_from.lat - lhs_to.lat;
9141811837 q double lfmlt_lon = lhs_from.lon - lhs_to.lon;
9151811837 q double rfmrt_lat = rhs_from.lat - rhs_to.lat;
9161811837 q double rfmrt_lon = rhs_from.lon - rhs_to.lon;
9171811837 q double det = lfmlt_lat * rfmrt_lon - rfmrt_lat * lfmlt_lon;
9191811837 q if (det == 0)
920642 q {
921 // Segments on parallel but distinct beams
922642 q if (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat != 0)
9230 return false;
925642 q if (fabs(rfmlt_lat) > fabs(rfmlt_lon))
92741 q if (lhs_from.lat < lhs_to.lat)
92941 q if (rhs_from.lat < rhs_to.lat)
931 // Segments are non-overlapping
93241 q if (lhs_to.lat < rhs_from.lat || rhs_to.lat < lhs_from.lat)
93341 q 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 }
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 }
978601 q else
979601 q {
980601 q if (lhs_from.lon < lhs_to.lon)
981601 q {
982601 q if (rhs_from.lon < rhs_to.lon)
983601 q {
984 // Segments are non-overlapping
985601 q if (lhs_to.lon < rhs_from.lon || rhs_to.lon < lhs_from.lon)
986601 q 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 }
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 }
10310 return true;
10341811197 q double x = (rfmrt_lon * rfmlt_lat - rfmrt_lat * rfmlt_lon)/det;
10351811197 q double y = (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat)/det;
10371811197 q if (x <= -1e-7 || x >= 1+1e-7 || y <= -1e-7 || y >= 1+1e-7)
10381811197 q return false;
10400 isect.lat = lhs_to.lat + x * lfmlt_lat;
10410 isect.lon = lhs_to.lon + x * lfmlt_lon;
10420 return true;
10431811197 q}
1046struct Idx_Per_Point_Double
1048 Idx_Per_Point_Double(const Point_Double& pt_, unsigned int idx_, unsigned int remote_idx_)
104921501011 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
105662407411 q {
105762407411 q if (pt.lat != rhs.pt.lat)
105838137510 q return pt.lat < rhs.pt.lat;
105924269911 q if (pt.lon != rhs.pt.lon)
1060882844 q return pt.lon < rhs.pt.lon;
106215441511 q return idx < rhs.idx;
106324269911 q }
1067struct Line_Divertion
106930625811 q Line_Divertion() : to_idx(std::numeric_limits< uint >::max()),
107030625811 q to_remote_idx(std::numeric_limits< uint >::max()) {}
107121501011 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)
10822281211 q{
10832281211 q for (std::map< uint32, std::vector< unsigned int > >::iterator idx_it = segments_per_idx.begin();
10845903111 q idx_it != segments_per_idx.end(); ++idx_it)
10853621911 q {
10863621911 q Point_Double isect(100, 0);
108712787911 q for (unsigned int i = 1; i < idx_it->second.size(); ++i)
10889166010 q {
108938140710 q for (unsigned int j = 0; j < i; ++j)
109028974710 q {
109128974710 q if (try_intersect(all_segments[idx_it->second[j]], all_segments[idx_it->second[j]+1],
109228974710 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 }
114428974710 q }
11459166010 q }
11463621911 q }
11472281211 q}
1150unsigned int line_divertion_idx(const Idx_Per_Point_Double& rhs)
115121501011 q{
115221501011 q return rhs.idx*2 + ((int)rhs.remote_idx - (int)rhs.idx - 1)/2;
115321501011 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)
11593621911 q{
11603621911 q std::vector< Idx_Per_Point_Double > pos_per_pt;
11623621911 q for (std::vector< unsigned int >::const_iterator seg_it = segments.begin();
116316409811 q seg_it != segments.end(); ++seg_it)
116412787911 q {
116512787911 q uint32 lhs_ilat = ::ilat(all_segments[*seg_it].lat);
116612787911 q int32 lhs_ilon = ::ilon(all_segments[*seg_it].lon);
116712787911 q if (((lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)) == idx)
116810750511 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it], *seg_it, *seg_it+1));
117012787911 q uint32 rhs_ilat = ::ilat(all_segments[*seg_it+1].lat);
117112787911 q int32 rhs_ilon = ::ilon(all_segments[*seg_it+1].lon);
117212787911 q if (((rhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)) == idx)
117310750511 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it+1], *seg_it+1, *seg_it));
117412787911 q }
11763621911 q if (pos_per_pt.empty())
117734336 q return;
11793278611 q std::sort(pos_per_pt.begin(), pos_per_pt.end());
11813278611 q unsigned int same_since = 0;
11823278611 q unsigned int i = 0;
118328058211 q while (i <= pos_per_pt.size())
118424779611 q {
118524779611 q if (i == pos_per_pt.size() || pos_per_pt[i].pt != pos_per_pt[same_since].pt)
118610750511 q {
118710750511 q if (i - same_since == 2)
118810750511 q {
118910750511 q divertions[line_divertion_idx(pos_per_pt[same_since])] = Line_Divertion(pos_per_pt[same_since+1]);
119010750511 q divertions[line_divertion_idx(pos_per_pt[same_since+1])] = Line_Divertion(pos_per_pt[same_since]);
119110750511 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 }
122810750511 q same_since = i;
122910750511 q }
123024779611 q ++i;
123124779611 q }
12323278611 q}
1235void assemble_linestrings(const std::vector< Point_Double >& all_segments,
1236 std::vector< Line_Divertion >& divertions, std::vector< std::vector< Point_Double > >& linestrings)
12372281211 q{
1238 // the function changes divertions because divertions is no longer needed after the function
12402281211 q unsigned int pos = 0;
12412281211 q unsigned int count = 0;
124215312911 q for (unsigned int i = 0; i < divertions.size(); i += 2)
124313031711 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()
12464562411 q while (count < all_segments.size())
12472281211 q {
12482281211 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 }
12552281211 q linestrings.push_back(std::vector< Point_Double >());
12562281211 q std::vector< Point_Double >& cur_target = linestrings.back();
12582281211 q int dir = 1;
125913031711 q while (divertions[2*pos + dir].to_idx != std::numeric_limits< uint >::max())
126010750511 q {
126110750511 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();
126410750511 q else if (cur_target.empty() || !all_segments[pos].epsilon_equal(cur_target.back()))
126510750511 q cur_target.push_back(all_segments[pos]);
126710750511 q Line_Divertion& old_div = divertions[2*(int)pos + dir];
126810750511 q Line_Divertion& divertion = divertions[2*(int)pos + (3*dir-1)/2];
126910750511 q dir = divertion.to_remote_idx - divertion.to_idx;
127010750511 q pos = divertion.to_idx;
127210750511 q ++count;
127310750511 q old_div.to_idx = std::numeric_limits< uint >::max();
127410750511 q }
12762281211 q ++pos;
12772281211 q if (pos == all_segments.size()-1)
127831 q pos = 0;
12802281211 q if (cur_target.size() <= 2)
12815982 q linestrings.pop_back();
1282222149 q else
1283222149 q cur_target.push_back(cur_target.front());
12842281211 q }
12852281211 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_)
12932281211 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)
13073621911 q{
13083621911 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value);
13093621911 q if (spi_it == segments_per_idx->end())
13100 return;
13123621911 q if (sw_corner_inside)
131331326 q inside_corners.insert(value);
13153621911 q if (::lon(uint32(value)<<16) > -179.99)
13163621911 q {
13173621911 q if (nw_corner_inside)
131862439 q {
131962439 q *nw_corner_inside = sw_corner_inside;
132162439 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
1322297229 q seg_it != spi_it->second.end(); ++seg_it)
1323234799 q {
1324234799 q int32 lhs_ilon = ::ilon((*all_segments)[*seg_it].lon) & 0xffff0000;
1325234799 q int32 rhs_ilon = ::ilon((*all_segments)[*seg_it+1].lon) & 0xffff0000;
1327234799 q if ((lhs_ilon < (value<<16) && rhs_ilon == (value<<16))
1328234799 q || (lhs_ilon == (value<<16) && rhs_ilon < (value<<16)))
132942606 q {
133042606 q double isect_lat = (*all_segments)[*seg_it].lat
133142606 q + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat)
133242606 q *(::lon(uint32(value)<<16) - (*all_segments)[*seg_it].lon)
133342606 q /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon);
133442606 q if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000))
133533246 q *nw_corner_inside = !*nw_corner_inside;
133642606 q }
1337234799 q }
133862439 q }
13403621911 q if (se_corner_inside)
1341120089 q {
1342120089 q *se_corner_inside = sw_corner_inside;
1344120089 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
1345588069 q seg_it != spi_it->second.end(); ++seg_it)
1346467989 q {
1347467989 q uint32 lhs_ilat = ::ilat((*all_segments)[*seg_it].lat) & 0xffff0000;
1348467989 q uint32 rhs_ilat = ::ilat((*all_segments)[*seg_it+1].lat) & 0xffff0000;
1350467989 q if ((lhs_ilat < (value & 0xffff0000) && rhs_ilat == (value & 0xffff0000))
1351467989 q || (lhs_ilat == (value & 0xffff0000) && rhs_ilat < (value & 0xffff0000)))
135269266 q {
135369266 q double isect_lon = (*all_segments)[*seg_it].lon
135469266 q + ((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon)
135569266 q *(::lat(value & 0xffff0000) - (*all_segments)[*seg_it].lat)
135669266 q /((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat);
135769266 q if ((::ilon(isect_lon) & 0xffff0000) == (uint32(value)<<16))
135850526 q *se_corner_inside = !*se_corner_inside;
135969266 q }
1360467989 q }
1361120089 q }
13623621911 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 }
14363621911 q}
1439Area_Oracle::point_status RHR_Polygon_Area_Oracle::get_point_status(int32 value, double lat, double lon)
1440222149 q{
1441222149 q if (value == 1)
14420 return 1;
1444222149 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value);
1445222149 q if (spi_it == segments_per_idx->end())
14460 return 0;
1448222149 q double border_lon = std::max(::lon(value<<16), -180.);
1449222149 q double border_lat = ::lat(value & 0xffff0000);
1451222149 q bool on_vertex = false;
1452222149 q bool on_segment = false;
1453222149 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
1456222149 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin();
14571062039 q seg_it != spi_it->second.end(); ++seg_it)
1458839899 q toggle_if_inside(is_inside, on_vertex, on_segment, border_lat, border_lon, Point_Double(lat, lon),
1459839899 q (*all_segments)[*seg_it], (*all_segments)[*seg_it+1]);
1461222149 q if (on_vertex)
1462222149 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)
1471258349 q{
1472258349 q if (lhs - rhs > 180.)
14730 return lhs - 360. < rhs;
1475258349 q return lhs < rhs;
1476258349 q}
1479bool weakly_west_of(double lhs, double rhs)
1480185943 q{
1481185943 q if (lhs - rhs > 180.)
14820 return lhs - 360. <= rhs;
1484185943 q return lhs <= rhs;
1485185943 q}
14882281211 qRHR_Polygon_Geometry::RHR_Polygon_Geometry(const Free_Polygon_Geometry& rhs) : bounds(0)
14892281211 q{
14902281211 q std::vector< std::vector< Point_Double > > input(*rhs.get_multiline_geometry());
14922281211 q std::vector< Point_Double > all_segments;
14932281211 q std::vector< unsigned int > gap_positions;
14952281211 q gap_positions.push_back(0);
14964562411 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = input.begin(); iti != input.end(); ++iti)
14972281211 q {
14982281211 q all_segments.push_back((*iti)[0]);
149912248611 q for (unsigned int i = 1; i < iti->size(); ++i)
15009967411 q interpolate_segment((*iti)[i-1].lat, (*iti)[i-1].lon, (*iti)[i].lat, (*iti)[i].lon, all_segments);
15012281211 q gap_positions.push_back(all_segments.size());
15022281211 q }
15042281211 q std::map< uint32, std::vector< unsigned int > > segments_per_idx;
15052281211 q std::vector< unsigned int >::const_iterator gap_it = gap_positions.begin();
150615312911 q for (unsigned int i = 0; i < all_segments.size(); ++i)
150713031711 q {
150813031711 q if (*gap_it == i)
15092281211 q ++gap_it;
151010750511 q else
151110750511 q add_segment(segments_per_idx, all_segments[i-1], all_segments[i], i-1);
151213031711 q }
15132281211 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]
15172281211 q std::vector< Line_Divertion > divertions(2*all_segments.size());
1519 // fill the gaps with invalid following segments
15204562411 q for (unsigned int i = 1; i < gap_positions.size(); ++i)
15212281211 q {
15222281211 q divertions[gap_positions[i]*2 - 2] = Line_Divertion();
15232281211 q divertions[gap_positions[i]*2 - 1] = Line_Divertion();
15242281211 q }
1526 // compute the real segment connections
15272281211 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin();
15285903111 q idx_it != segments_per_idx.end(); ++idx_it)
15293621911 q collect_divertions(all_segments, idx_it->first, idx_it->second, divertions);
15312281211 q assemble_linestrings(all_segments, divertions, linestrings);
15332281211 q RHR_Polygon_Area_Oracle area_oracle(all_segments, segments_per_idx);
15342281211 q Four_Field_Index four_field_idx(&area_oracle);
15362281211 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin();
15375903111 q idx_it != segments_per_idx.end(); ++idx_it)
15383621911 q four_field_idx.add_point(::lat(idx_it->first | 0x8000), ::lon(idx_it->first<<16 | 0x8000), idx_it->first);
15402281211 q four_field_idx.compute_inside_parts();
15422281211 q for (std::vector< std::vector< Point_Double > >::iterator lstr_it = linestrings.begin();
15434502611 q lstr_it != linestrings.end(); ++lstr_it)
1544222149 q {
1545222149 q if (lstr_it->size() > 2)
1546222149 q {
1547222149 q if (strictly_west_of((*lstr_it)[0].lon, (*lstr_it)[1].lon))
1548185943 q {
1549185943 q if (weakly_west_of((*lstr_it)[1].lon, (*lstr_it)[2].lon))
155010042 q {
155110042 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());
155310042 q }
1554175901 q else
1555175901 q {
1556175901 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
1557175901 q ^ (((*lstr_it)[1].lat - (*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon)
1558175901 q < ((*lstr_it)[2].lat -(*lstr_it)[1].lat)/((*lstr_it)[2].lon -(*lstr_it)[1].lon)))
1559175901 q std::reverse(lstr_it->begin(), lstr_it->end());
1560175901 q }
1561185943 q }
156236208 q else if (strictly_west_of((*lstr_it)[2].lon, (*lstr_it)[1].lon))
156329237 q {
156429237 q if (!(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3))
156591 q std::reverse(lstr_it->begin(), lstr_it->end());
156629237 q }
15676972 q else if ((*lstr_it)[0].lon == (*lstr_it)[1].lon)
156921 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
157021 q ^ ((*lstr_it)[0].lat < (*lstr_it)[1].lat))
157121 q std::reverse(lstr_it->begin(), lstr_it->end());
15736951 q else if ((*lstr_it)[2].lon == (*lstr_it)[1].lon)
157511 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
157611 q ^ ((*lstr_it)[1].lat < (*lstr_it)[2].lat))
157711 q std::reverse(lstr_it->begin(), lstr_it->end());
15796941 q else
15806941 q {
15816941 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)
15826941 q ^ (((*lstr_it)[1].lat -(*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon)
15836941 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());
15856941 q }
1586222149 q }
1587222149 q }
15882281211 q}
1591double RHR_Polygon_Geometry::center_lat() const
159246083 q{
159346083 q if (!bounds)
159446083 q bounds = calc_bounds(linestrings);
159646083 q return bounds->center_lat();
159746083 q}
1600double RHR_Polygon_Geometry::center_lon() const
160146083 q{
160246083 q if (!bounds)
16030 bounds = calc_bounds(linestrings);
160546083 q return bounds->center_lon();
160646083 q}
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
16842716320 q{
16852716320 q std::vector< Opaque_Geometry* > cloned;
168613652520 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
168710936219 q cloned.push_back((*it)->clone());
16882716320 q return new Compound_Geometry(cloned);
16892716320 q}
1692Bbox_Double* calc_bounds(const std::vector< Opaque_Geometry* >& components)
16932677726 q{
16942677726 q double south = 100.0;
16952677726 q double west = 200.0;
16962677726 q double north = -100.0;
16972677726 q double east = -200.0;
16982677726 q bool wrapped = false;
170045457526 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
170142779826 q {
170242779826 q if ((*it)->has_bbox())
170333872825 q {
170433872825 q south = std::min(south, (*it)->south());
170533872825 q north = std::max(north, (*it)->north());
170633872825 q west = std::min(west, (*it)->west());
170733872825 q east = std::max(east, (*it)->east());
170933872825 q wrapped |= ((*it)->east() < (*it)->west());
171033872825 q }
171142779826 q }
17132677726 q if (north == -100.0)
171432 q return new Bbox_Double(Bbox_Double::invalid);
17152677425 q if (!wrapped && east - west <= 180.)
17162677425 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
17462110718 q{
17472110718 q if (!bounds)
17482110618 q bounds = calc_bounds(components);
17502110718 q return bounds->valid();
17512110718 q}
1754double Compound_Geometry::center_lat() const
175674515 q if (!bounds)
17570 bounds = calc_bounds(components);
175974515 q return bounds->center_lat();
1763double Compound_Geometry::center_lon() const
17642104711 q{
17652104711 q if (!bounds)
17660 bounds = calc_bounds(components);
17682104711 q return bounds->center_lon();
17692104711 q}
1772bool Compound_Geometry::has_bbox() const
177349799 q{
177449799 q if (!bounds)
177549799 q bounds = calc_bounds(components);
177749799 q return bounds->valid();
177849799 q}
1781double Compound_Geometry::south() const
178249799 q{
178349799 q if (!bounds)
17840 bounds = calc_bounds(components);
178649799 q return bounds->south;
178749799 q}
1790double Compound_Geometry::north() const
179149799 q{
179249799 q if (!bounds)
17930 bounds = calc_bounds(components);
179549799 q return bounds->north;
179649799 q}
1799double Compound_Geometry::west() const
180049819 q{
180149819 q if (!bounds)
18020 bounds = calc_bounds(components);
180449819 q return bounds->west;
180549819 q}
1808double Compound_Geometry::east() const
180949819 q{
181049819 q if (!bounds)
18110 bounds = calc_bounds(components);
181349819 q return bounds->east;
181449819 q}
1817bool Compound_Geometry::relation_pos_is_valid(unsigned int member_pos) const
181843936 q{
181943936 q return (member_pos < components.size() && components[member_pos]
182043936 q && components[member_pos]->has_center());
182143936 q}
1824double Compound_Geometry::relation_pos_lat(unsigned int member_pos) const
182543936 q{
182643936 q if (member_pos < components.size() && components[member_pos])
182743936 q return components[member_pos]->center_lat();
18280 return 0;
182943936 q}
1832double Compound_Geometry::relation_pos_lon(unsigned int member_pos) const
183343936 q{
183443936 q if (member_pos < components.size() && components[member_pos])
183543936 q return components[member_pos]->center_lon();
18360 return 0;
183743936 q}
1840unsigned int Compound_Geometry::relation_way_size(unsigned int member_pos) const
184128977238 q{
184228977238 q if (member_pos < components.size() && components[member_pos])
184328977238 q return components[member_pos]->way_size();
18440 return 0;
184528977238 q}
1848bool Compound_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const
184920941358 q{
185020941358 q return (member_pos < components.size() && components[member_pos]
185120941358 q && components[member_pos]->way_pos_is_valid(nd_pos));
185220941358 q}
1855double Compound_Geometry::relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const
185614255528 q{
185714255528 q if (member_pos < components.size() && components[member_pos])
185814255528 q return components[member_pos]->way_pos_lat(nd_pos);
18590 return 0;
186014255528 q}
1863double Compound_Geometry::relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const
186414255528 q{
186514255528 q if (member_pos < components.size() && components[member_pos])
186614255528 q return components[member_pos]->way_pos_lon(nd_pos);
18670 return 0;
186814255528 q}
1871void Compound_Geometry::add_component(Opaque_Geometry* component)
187245788340 q{
187345788340 q delete bounds;
187445788340 q bounds = 0;
187545788340 q components.push_back(component);
187645788340 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
1893504 q std::vector< Opaque_Geometry* > cloned;
189471324 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
189570824 q cloned.push_back((*it)->clone());
1896504 q return new Partial_Relation_Geometry(cloned);
1900bool Partial_Relation_Geometry::has_center() const
190110287 q{
190210287 q if (!has_coords)
19035452 q return 0;
19054836 q if (!bounds)
19064806 q bounds = calc_bounds(components);
19084836 q return bounds->valid();
190910287 q}
1912double Partial_Relation_Geometry::center_lat() const
1914165 q if (!has_coords)
19150 return 0;
1917165 q if (!bounds)
19180 bounds = calc_bounds(components);
1920165 q return bounds->center_lat();
1924double Partial_Relation_Geometry::center_lon() const
19264764 q if (!has_coords)
19270 return 0;
19294764 q if (!bounds)
19300 bounds = calc_bounds(components);
19324764 q return bounds->center_lon();
1936bool Partial_Relation_Geometry::has_bbox() const
19382868 q if (!has_coords)
1939744 q return false;
19412126 q if (!bounds)
19422126 q bounds = calc_bounds(components);
19442126 q return bounds->valid();
1948double Partial_Relation_Geometry::south() const
19502126 q if (!has_coords)
19510 return 0;
19532126 q if (!bounds)
19540 bounds = calc_bounds(components);
19562126 q return bounds->south;
1960double Partial_Relation_Geometry::north() const
19622126 q if (!has_coords)
19630 return 0;
19652126 q if (!bounds)
19660 bounds = calc_bounds(components);
19682126 q return bounds->north;
1972double Partial_Relation_Geometry::west() const
19742126 q if (!has_coords)
19750 return 0;
19772126 q if (!bounds)
19780 bounds = calc_bounds(components);
19802126 q return bounds->west;
1984double Partial_Relation_Geometry::east() const
19862126 q if (!has_coords)
19870 return 0;
19892126 q if (!bounds)
19900 bounds = calc_bounds(components);
19922126 q return bounds->east;
1996bool Partial_Relation_Geometry::relation_pos_is_valid(unsigned int member_pos) const
19987306 q return (member_pos < components.size() && components[member_pos]
19997306 q && components[member_pos]->has_center());
2003double Partial_Relation_Geometry::relation_pos_lat(unsigned int member_pos) const
20051653 q if (member_pos < components.size() && components[member_pos])
20061653 q return components[member_pos]->center_lat();
20070 return 0;
2011double Partial_Relation_Geometry::relation_pos_lon(unsigned int member_pos) const
20131653 q if (member_pos < components.size() && components[member_pos])
20141653 q return components[member_pos]->center_lon();
20150 return 0;
2019unsigned int Partial_Relation_Geometry::relation_way_size(unsigned int member_pos) const
20202208118 q{
20212208118 q if (member_pos < components.size() && components[member_pos])
20222208118 q return components[member_pos]->way_size();
20230 return 0;
20242208118 q}
2027bool Partial_Relation_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const
20281283966 q{
20291283966 q return (member_pos < components.size() && components[member_pos]
20301283966 q && components[member_pos]->way_pos_is_valid(nd_pos));
20311283966 q}
2034double Partial_Relation_Geometry::relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const
20351045746 q{
20361045746 q if (member_pos < components.size() && components[member_pos])
20371045746 q return components[member_pos]->way_pos_lat(nd_pos);
20380 return 0;
20391045746 q}
2042double Partial_Relation_Geometry::relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const
20431045746 q{
20441045746 q if (member_pos < components.size() && components[member_pos])
20451045746 q return components[member_pos]->way_pos_lon(nd_pos);
20460 return 0;
20471045746 q}
2050void Partial_Relation_Geometry::add_placeholder()
205121259119 q{
205221259119 q components.push_back(new Null_Geometry());
205321259119 q}
2056void Partial_Relation_Geometry::add_point(const Point_Double& point)
205853812 q delete bounds;
205953812 q bounds = 0;
206053812 q if (point.lat < 100.)
206253812 q has_coords = true;
206353812 q components.push_back(new Point_Geometry(point.lat, point.lon));
20650 else
20660 components.push_back(new Null_Geometry());
2070void Partial_Relation_Geometry::start_way()
20714056418 q{
20724056418 q components.push_back(new Partial_Way_Geometry());
20734056418 q}
2076void Partial_Relation_Geometry::add_way_point(const Point_Double& point)
207762910116 q{
207862910116 q delete bounds;
207962910116 q bounds = 0;
208162910116 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back());
208262910116 q if (geom)
208362910116 q {
208462910116 q has_coords = true;
208562910116 q geom->add_point(point);
208662910116 q }
208762910116 q}
2090void Partial_Relation_Geometry::add_way_placeholder()
20911150310 q{
20921150310 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back());
20931150310 q if (geom)
20941150310 q geom->add_point(Point_Double(100., 200.));
20951150310 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)
21111400417673 q{
21121400417673 q if (lat1 == lat2 && lon1 == lon2)
2113276 q return 0;
21141400414973 q static const double deg_to_arc = acos(0)/90.;
21151400414973 q double scalar_prod = cos((lat2-lat1)*deg_to_arc)
21161400414973 q + cos(lat1*deg_to_arc)*cos(lat2*deg_to_arc)*(cos((lon2-lon1)*deg_to_arc) - 1);
21171400414973 q if (scalar_prod > 1)
21180 scalar_prod = 1;
21191400414973 q static const double arc_to_meter = 10*1000*1000/acos(0);
21201400414973 q return acos(scalar_prod)*arc_to_meter;
21211400417673 q}
2124double length(const Opaque_Geometry& geometry)
21255711113 q{
21265711113 q double result = 0;
21285711113 q if (geometry.has_components())
212910906 q {
213010906 q const std::vector< Opaque_Geometry* >* components = geometry.get_components();
2131535456 q for (std::vector< Opaque_Geometry* >::const_iterator it = components->begin(); it != components->end(); ++it)
2132524556 q result += (*it ? length(**it) : 0);
213310906 q }
21345602113 q else if (geometry.has_line_geometry())
21352906013 q {
21362906013 q const std::vector< Point_Double >* line_geometry = geometry.get_line_geometry();
213782226613 q for (unsigned int i = 1; i < line_geometry->size(); ++i)
213879320613 q result += great_circle_dist((*line_geometry)[i-1].lat, (*line_geometry)[i-1].lon,
213979320613 q (*line_geometry)[i].lat, (*line_geometry)[i].lon);
21402906013 q }
2141269615 q else if (geometry.has_faithful_way_geometry())
2143731 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);
21495711113 q return result;
21505711113 q}
2153void collect_components(
2154 std::vector< Point_Double >& nodes, std::vector< std::vector< Point_Double > >& linestrings,
2155 const Opaque_Geometry& geometry)
2156433938 q{
2157433938 q if (geometry.has_components())
2159454 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components();
2160407994 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
2161407544 q {
2162407544 q if (*it)
2163407544 q collect_components(nodes, linestrings, **it);
2164407544 q }
2166433488 q else if (geometry.has_line_geometry())
2167227175 q linestrings.push_back(*geometry.get_line_geometry());
2168206315 q else if (geometry.has_multiline_geometry())
21690 {
21700 const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry();
21710 for (std::vector< std::vector< Point_Double > >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it)
21720 linestrings.push_back(*it);
21730 }
2174206315 q else if (geometry.has_center())
2175206074 q nodes.push_back(Point_Double(geometry.center_lat(), geometry.center_lon()));
2176433938 q}
2179struct Linestring_Geometry_Ptr
2181692035 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
219011151184 q{
219111151184 q if (!rhs_.ptr)
21920 return false;
219311151184 q if (!ptr)
21940 return true;
219611151184 q const std::vector< Point_Double >& lhs = *ptr->get_line_geometry();
219711151184 q const std::vector< Point_Double >& rhs = *rhs_.ptr->get_line_geometry();
219911151184 q if (lhs.size() != rhs.size())
2200852824 q return lhs.size() < rhs.size();
220210298363 q uint l_start = 0;
220310298363 q uint l_factor = 1;
220410298363 q if (lhs.back() < lhs.front())
22054312552 q {
22064312552 q l_start = lhs.size()-1;
22074312552 q l_factor = -1;
22084312552 q }
221010298363 q uint r_start = 0;
221110298363 q uint r_factor = 1;
221210298363 q if (rhs.back() < rhs.front())
22135359612 q {
22145359612 q r_start = rhs.size()-1;
22155359612 q r_factor = -1;
22165359612 q }
221811083703 q for (int i = 0; i < (int)lhs.size(); ++i)
221910749443 q {
222010749443 q if (lhs[l_start + l_factor*i] != rhs[r_start + r_factor*i])
22219964103 q return lhs[l_start + l_factor*i] < rhs[r_start + r_factor*i];
222210749443 q }
2223334261 q return false;
222410298363 q}
2227Opaque_Geometry* make_trace(const Opaque_Geometry& geometry)
222826398 q{
222926398 q std::vector< Point_Double > nodes;
223026398 q std::vector< std::vector< Point_Double > > linestrings;
223126398 q collect_components(nodes, linestrings, geometry);
223326398 q std::sort(nodes.begin(), nodes.end());
223426398 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end());
223626398 q std::map< Point_Double, uint > coord_count;
2237232168 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
2238205774 q ++coord_count[*it];
224026398 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin();
2241253568 q lit != linestrings.end(); ++lit)
2242227175 q {
2243227175 q if (lit->empty())
22440 continue;
2245227175 q coord_count[lit->front()] += 2;
2246227175 q coord_count[lit->back()] += 2;
22472515265 q for (uint i = 1; i < lit->size() - 1; ++i)
22482288095 q ++coord_count[(*lit)[i]];
2249227175 q }
225126398 q Compound_Geometry* result = new Compound_Geometry();
2253232168 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
2254205774 q result->add_component(new Point_Geometry(it->lat, it->lon));
225626398 q std::set< Linestring_Geometry_Ptr > lstrs;
225726398 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin();
2258253568 q lit != linestrings.end(); ++lit)
2259227175 q {
2260227175 q if (lit->empty())
22610 continue;
2263227175 q std::vector< Point_Double > points;
2264227175 q points.push_back(lit->front());
22652515265 q for (uint i = 1; i < lit->size() - 1; ++i)
22662288095 q {
22672288095 q if (coord_count[(*lit)[i]] > 1)
2268464863 q {
2269464863 q points.push_back((*lit)[i]);
2271464863 q Linestring_Geometry* lstr = new Linestring_Geometry(points);
2272464863 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second)
2273309323 q result->add_component(lstr);
2274155541 q else
2275155541 q delete lstr;
2277464863 q points.clear();
2278464863 q }
22792288095 q points.push_back((*lit)[i]);
22802288095 q }
2281227175 q points.push_back(lit->back());
2282227175 q Linestring_Geometry* lstr = new Linestring_Geometry(points);
2283227175 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second)
2284215585 q result->add_component(lstr);
228511591 q else
228611591 q delete lstr;
2287227175 q }
228926398 q return result;
229026398 q}
2293void collect_components(std::vector< Point_Double >& nodes, const Opaque_Geometry& geometry)
229467309 q{
229567309 q if (geometry.has_components())
2297364 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components();
229814474 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it)
229914114 q {
230014114 q if (*it)
230114114 q collect_components(nodes, **it);
230214114 q }
230466949 q else if (geometry.has_line_geometry())
230559007 q {
230659007 q const std::vector< Point_Double >& lstrs = *geometry.get_line_geometry();
23071870647 q for (std::vector< Point_Double >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it)
23081811647 q nodes.push_back(*it);
230959007 q }
23107944 q else if (geometry.has_multiline_geometry())
231291 q const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry();
231391 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 }
23197854 q else if (geometry.has_center())
23201383 q nodes.push_back(Point_Double(geometry.center_lat(), geometry.center_lon()));
232167309 q}
2324struct Point_Double_By_Lon
2326 bool operator()(const Point_Double& lhs, const Point_Double& rhs)
232712758547 q {
232812758547 q if (lhs.lon != rhs.lon)
232912723347 q return lhs.lon < rhs.lon;
233035207 q return lhs.lat < rhs.lat;
233112758547 q }
2335struct Spherical_Vector
2337963957 q Spherical_Vector() : x(0), y(0), z(0) {}
23391882567 q Spherical_Vector(const Point_Double& pt) : x(sin(pt.lat*deg_to_arc()))
23401882567 q {
23411882567 q double cos_lat = cos(pt.lat*deg_to_arc());
23421882567 q y = cos_lat*sin(pt.lon*deg_to_arc());
23431882567 q z = cos_lat*cos(pt.lon*deg_to_arc());
23441882567 q }
2346 Spherical_Vector(const Spherical_Vector& lhs, const Spherical_Vector& rhs)
23471832467 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)
23481832467 q {
23491832467 q double length = sqrt(x*x + y*y + z*z);
23501832467 q x /= length;
23511832467 q y /= length;
23521832467 q z /= length;
23531832467 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; }
236021868267 q double operator*(const Spherical_Vector& rhs) const { return x*rhs.x + y*rhs.y + z*rhs.z; }
2362 static double deg_to_arc()
23637530247 q {
23647530247 q static const double result = acos(0)/90.;
23657530247 q return result;
23667530247 q }
2370struct Proto_Hull
2372 struct Hull_Segment
237495447 q Hull_Segment(const Point_Double& pt) : ll_pt(pt), s_pt(pt) {}
2375868517 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)
239247727 q{
239347727 q if (w.lon != e.lon)
239447727 q {
239547727 q segments.push_back(Hull_Segment(w));
239647727 q segments.push_back(Hull_Segment(e));
239747727 q }
23980 else if (n.lat != s.lat)
23990 {
24000 segments.push_back(Hull_Segment(s));
24010 segments.push_back(Hull_Segment(n));
24020 }
240447727 q if (!segments.empty())
240547727 q {
240647727 q segments[1].edge = Spherical_Vector(segments[0].s_pt, segments[1].s_pt);
240747727 q segments[0].edge = Spherical_Vector(segments[1].s_pt, segments[0].s_pt);
240847727 q }
240947727 q}
2412void Proto_Hull::enhance(const Point_Double& rhs)
24131787127 q{
24141787127 q Spherical_Vector s_pt(rhs);
24161787127 q std::vector< Hull_Segment >::iterator from_it = segments.begin();
241717985787 q while (from_it != segments.end() && from_it->edge*s_pt < 1e-8)
241816198667 q ++from_it;
24201787127 q if (from_it != segments.end() && from_it->edge*s_pt >= 1e-8)
2421868517 q {
2422868517 q if (segments.begin()->edge*s_pt >= 0)
2423422137 q {
2424422137 q from_it = segments.end();
2425422137 q --from_it;
2426422137 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0)
24270 --from_it;
2428422137 q ++from_it;
2429422137 q segments.erase(from_it, segments.end());
2431422137 q std::vector< Hull_Segment >::iterator to_it = segments.begin();
24321081647 q while (to_it != segments.end() && to_it->edge*s_pt >= 0)
2433659517 q ++to_it;
2434422137 q --to_it;
2435422137 q from_it = segments.begin();
2437422137 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt);
2438422137 q if (from_it == to_it)
2439217817 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt));
2440204327 q else
2441204327 q {
2442204327 q ++from_it;
2443204327 q from_it = segments.erase(from_it, to_it);
2444204327 q --from_it;
2445204327 q *from_it = Hull_Segment(rhs, s_pt);
2446204327 q }
2447422137 q }
2448446387 q else
2449446387 q {
2450446387 q std::vector< Hull_Segment >::iterator to_it = from_it;
24511141277 q while (to_it != segments.end() && to_it->edge*s_pt >= 0)
2452694897 q ++to_it;
2453446387 q --to_it;
2454910777 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0)
2455464397 q --from_it;
2456446387 q ++from_it;
2458446387 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt);
2459446387 q if (from_it == to_it)
2460219027 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt));
2461227366 q else
2462227366 q {
2463227366 q ++from_it;
2464227366 q from_it = segments.erase(from_it, to_it);
2465227366 q --from_it;
2466227366 q *from_it = Hull_Segment(rhs, s_pt);
2467227366 q }
2468446387 q }
2470868517 q if (from_it == segments.begin())
2471422137 q from_it->edge = Spherical_Vector(segments.back().s_pt, s_pt);
2472446387 q else
2473446387 q from_it->edge = Spherical_Vector((from_it-1)->s_pt, s_pt);
2474868517 q }
24751787127 q}
2478std::vector< Point_Double > Proto_Hull::get_line_geometry() const
247947727 q{
248047727 q std::vector< Point_Double > result;
248147727 q result.reserve(segments.size());
2482507777 q for (std::vector< Hull_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it)
2483460057 q result.push_back(it->ll_pt);
248447727 q return result;
248547727 q}
2488Opaque_Geometry* make_hull(const Opaque_Geometry& geometry)
248953199 q{
249053199 q std::vector< Point_Double > nodes;
249153199 q collect_components(nodes, geometry);
249353199 q std::sort(nodes.begin(), nodes.end(), Point_Double_By_Lon());
249453199 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end());
249653199 q if (nodes.empty())
249711 q return new Null_Geometry();
249853188 q if (nodes.size() == 1)
2499241 q return new Point_Geometry(nodes.front().lat, nodes.front().lon);
250052947 q if (nodes.size() == 2)
25015221 q {
25025221 q nodes.push_back(nodes.front());
25035221 q return new Linestring_Geometry(nodes);
25045221 q }
250647727 q Point_Double min_lat(100., 0.);
250747727 q Point_Double max_lat(-100., 0.);
25081834847 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
25091787127 q {
25101787127 q if (it->lat < min_lat.lat)
2511613347 q min_lat = *it;
25121787127 q if (it->lat > max_lat.lat)
2513537437 q max_lat = *it;
25141787127 q }
251647727 q double max_lon_delta = nodes.front().lon + 360. - nodes.back().lon;
251747727 q Point_Double west_end = nodes.front();
251847727 q Point_Double east_end = nodes.back();
25191787127 q for (uint i = 1; i < nodes.size(); ++i)
25201739407 q {
25211739407 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 }
25271739407 q }
252947727 q Proto_Hull proto_hull(min_lat, west_end, max_lat, east_end);
25301834847 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
25311787127 q proto_hull.enhance(*it);
253347727 q std::vector< Point_Double > line_geom = proto_hull.get_line_geometry();
253447727 q if (line_geom.size() < 3)
25351501 q return new Linestring_Geometry(line_geom);
253746227 q return new RHR_Polygon_Geometry(Free_Polygon_Geometry(
253846227 q std::vector< std::vector< Point_Double > >(1, line_geom)));
253947727 q}