-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.cpp
201 lines (162 loc) · 6.28 KB
/
geometry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "infra/geometry.h"
#include "infra/algo.h"
#include "infra/chrono.h"
#include "infra/exception.h"
#include "infra/log.h"
#include "infra/gdalgeometry.h"
#include <geos/geom/Coordinate.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/LineString.h>
#include <geos/geom/Polygon.h>
#include <geos/version.h>
#if GEOS_VERSION_MAJOR > 3 || (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 10)
#define HAVE_GEOJSON_EXPORT 1
#endif
#ifdef HAVE_GEOJSON_EXPORT
#include <geos/io/GeoJSONWriter.h>
#endif
#include <cassert>
#include <memory>
#include <vector>
namespace inf::geom {
using namespace inf;
std::unique_ptr<geos::geom::LinearRing> gdal_linear_ring_to_geos(const geos::geom::GeometryFactory& factory, gdal::LinearRingCRef ring)
{
auto coords = std::make_unique<geos::geom::CoordinateSequence>(geos::geom::CoordinateSequence::XY(ring.point_count()));
for (int i = 0; i < ring.point_count(); ++i) {
const auto point = ring.point_at(i);
coords->setAt(geos::geom::Coordinate(point.x, point.y), i);
}
return factory.createLinearRing(std::move(coords));
}
static geos::geom::Polygon::Ptr gdal_polygon_to_geos(const geos::geom::GeometryFactory& factory, gdal::PolygonCRef poly)
{
std::unique_ptr<geos::geom::LinearRing> exteriorRing;
std::vector<std::unique_ptr<geos::geom::LinearRing>> holes;
{
// Exterior ring
exteriorRing = gdal_linear_ring_to_geos(factory, poly.exterior_ring());
}
// interior rings
for (int i = 0; i < poly.interior_ring_count(); ++i) {
holes.push_back(gdal_linear_ring_to_geos(factory, poly.interior_ring(i)));
}
return factory.createPolygon(std::move(exteriorRing), std::move(holes));
}
static geos::geom::MultiPolygon::Ptr gdal_multi_polygon_to_geos(const geos::geom::GeometryFactory& factory, gdal::MultiPolygonCRef geom)
{
std::vector<std::unique_ptr<geos::geom::Geometry>> geometries;
for (int i = 0; i < geom.size(); ++i) {
geometries.push_back(gdal_polygon_to_geos(factory, geom.polygon_at(i)));
}
return factory.createMultiPolygon(std::move(geometries));
}
geos::geom::MultiPolygon::Ptr gdal_to_geos(inf::gdal::GeometryCRef geom)
{
auto factory = geos::geom::GeometryFactory::create();
if (geom.type() == gdal::Geometry::Type::Polygon) {
std::vector<std::unique_ptr<geos::geom::Geometry>> geometries;
geometries.push_back(gdal_polygon_to_geos(*factory, geom.as<gdal::PolygonCRef>()));
return factory->createMultiPolygon(std::move(geometries));
} else if (geom.type() == gdal::Geometry::Type::MultiPolygon) {
return gdal_multi_polygon_to_geos(*factory, geom.as<gdal::MultiPolygonCRef>());
}
throw RuntimeError("Geometry type not implemented");
}
static std::unique_ptr<geos::geom::LinearRing> create_linear_ring(const geos::geom::GeometryFactory& factory, inf::Point<double> p1, inf::Point<double> p2)
{
return factory.createLinearRing(geos::geom::CoordinateSequence({
geos::geom::Coordinate(p1.x, p1.y),
geos::geom::Coordinate(p2.x, p1.y),
geos::geom::Coordinate(p2.x, p2.y),
geos::geom::Coordinate(p1.x, p2.y),
geos::geom::Coordinate(p1.x, p1.y),
}));
}
geos::geom::Polygon::Ptr create_polygon(inf::Point<double> p1, inf::Point<double> p2)
{
const auto factory = geos::geom::GeometryFactory::getDefaultInstance();
return factory->createPolygon(create_linear_ring(*factory, p1, p2));
}
geos::geom::LinearRing::Ptr create_linear_ring_from_rect(inf::Point<double> p1, inf::Point<double> p2)
{
const auto factory = geos::geom::GeometryFactory::getDefaultInstance();
return create_linear_ring(*factory, p1, p2);
}
std::string to_geojson(const geos::geom::Geometry& geom)
{
#ifdef HAVE_GEOJSON_EXPORT
geos::io::GeoJSONWriter writer;
return writer.write(&geom);
#endif
(void)geom;
throw RuntimeError("Export to geojson not supported");
}
std::string to_geojson(const geos::geom::Envelope& env)
{
auto geomFactory = geos::geom::GeometryFactory::create();
geos::geom::CoordinateSequence coords({
geos::geom::Coordinate(env.getMinX(), env.getMaxY()),
geos::geom::Coordinate(env.getMaxX(), env.getMaxY()),
geos::geom::Coordinate(env.getMaxX(), env.getMinY()),
geos::geom::Coordinate(env.getMinX(), env.getMinY()),
geos::geom::Coordinate(env.getMinX(), env.getMaxY()),
});
const auto poly = geomFactory->createPolygon(geomFactory->createLinearRing(coords), {});
return to_geojson(*poly);
}
std::string to_geojson(const inf::GeoMetadata& meta)
{
auto topLeft = meta.top_left();
auto bottomRight = meta.bottom_right();
auto geomFactory = geos::geom::GeometryFactory::create();
geos::geom::CoordinateSequence coords({
geos::geom::Coordinate(topLeft.x, topLeft.y),
geos::geom::Coordinate(bottomRight.x, topLeft.y),
geos::geom::Coordinate(bottomRight.x, bottomRight.y),
geos::geom::Coordinate(topLeft.x, bottomRight.y),
geos::geom::Coordinate(topLeft.x, topLeft.y),
});
const auto poly = geomFactory->createPolygon(geomFactory->createLinearRing(coords), {});
return to_geojson(*poly);
}
void calculate_geometry_envelopes(const geos::geom::Geometry& geom)
{
const auto numGeometries = geom.getNumGeometries();
if (numGeometries == 1) {
geom.getEnvelope();
} else {
for (size_t i = 0; i < numGeometries; ++i) {
geom.getGeometryN(i)->getEnvelope();
}
}
}
CoordinateWarpFilter::CoordinateWarpFilter(int32_t sourceEpsg, int32_t destEpsg)
: _transformer(sourceEpsg, destEpsg)
{
}
CoordinateWarpFilter::CoordinateWarpFilter(const char* sourceProjection, const char* destProjection)
: _transformer(gdal::SpatialReference(sourceProjection), gdal::SpatialReference(destProjection))
{
}
void CoordinateWarpFilter::filter_rw(geos::geom::CoordinateSequence& seq, std::size_t i)
{
auto coordinate = seq.getAt(i);
Point p(coordinate.x, coordinate.y);
_transformer.transform_in_place(p);
seq.setAt(geos::geom::Coordinate(p.x, p.y), i);
}
void CoordinateWarpFilter::filter_ro(const geos::geom::CoordinateSequence& /*seq*/, std::size_t /*i*/)
{
assert(false);
}
bool CoordinateWarpFilter::isDone() const
{
return false;
}
bool CoordinateWarpFilter::isGeometryChanged() const
{
return true;
}
}