-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrodek.cpp
319 lines (273 loc) · 9.57 KB
/
srodek.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
///////////////////////////////////////////////////////////////////////
// various include declarations ///////////////////////////////////////
#include <cstdio>
#include <exception>
#include <mpi.h>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include "shapefil.h"
#include <GeographicLib/DMS.hpp>
#include <GeographicLib/Geocentric.hpp>
#include <GeographicLib/Geodesic.hpp>
#include <GeographicLib/Gnomonic.hpp>
#include <GeographicLib/PolygonArea.hpp>
#include <GeographicLib/AlbersEqualArea.hpp>
using GeographicLib::DMS;
using GeographicLib::Geocentric;
using GeographicLib::Geodesic;
using GeographicLib::PolygonArea;
///////////////////////////////////////////////////////////////////////
// internal type definitions //////////////////////////////////////////
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::ring<Point> Ring;
typedef boost::geometry::model::polygon<Point> Polygon;
typedef boost::geometry::model::multi_polygon<Polygon> Shape;
typedef boost::geometry::model::box<Point> Rectangle;
typedef std::unique_ptr<SHPObject, decltype(&SHPDestroyObject)> ShapeObjectHandle;
///////////////////////////////////////////////////////////////////////
// geographic constants for data set //////////////////////////////////
const double GRS80_A = 6378137.0;
const double GRS80_F = 1.0/298.257222101;
const double CENTRAL_MERIDIAN = 19.0;
const double CENTRAL_PARALLEL = 52.0;
///////////////////////////////////////////////////////////////////////
// parallel MPI environment ///////////////////////////////////////////
int rankMPI, sizeMPI;
///////////////////////////////////////////////////////////////////////
// helper routines and wrappers ///////////////////////////////////////
// C++ style wrapper for Shapefile
class ShapeFileHandle {
SHPHandle handle;
public:
ShapeFileHandle(const char* filepath) {
handle = SHPOpen(filepath, "rb");
if (!handle) {
throw std::runtime_error("file does not exist");
}
}
~ShapeFileHandle(void) {
if (handle) SHPClose(handle);
}
ShapeObjectHandle readObject(int index) {
return ShapeObjectHandle(
SHPReadObject(handle, index),
SHPDestroyObject
);
}
};
// routine to read vertices from Shapefile to boost-style polygon
Shape readShapefile(const char* filepath, int index) {
ShapeFileHandle handle(filepath);
ShapeObjectHandle shape = handle.readObject(index);
if (!shape || shape->nSHPType != SHPT_POLYGON) {
throw std::runtime_error("invalid file contents");
}
Shape result;
std::list<Ring> innerRings;
const double *x = shape->padfX, *y = shape->padfY;
for (int part=0; part<shape->nParts; ++part) {
int vStart = (shape->nParts > 1) ? shape->panPartStart[part] : 0;
int vEnd = (shape->nParts > 1 && part+1<shape->nParts) ? shape->panPartStart[part+1] : shape->nVertices;
Ring ring;
for (int v=vStart; v<vEnd; ++v) {
boost::geometry::append(ring, Point(x[v], y[v]));
}
if (boost::geometry::area(ring) < 0) {
innerRings.push_back(ring);
} else {
result.push_back({ring});
}
}
for (const Ring& ring : innerRings) {
bool found = false;
for (Polygon& polygon : result) {
if (boost::geometry::within(ring, polygon)) {
if (found) {
throw std::runtime_error("found inner ring with multiple matching outer rings");
}
polygon.inners().push_back(ring);
found = true;
}
}
if (!found) {
throw std::runtime_error("found inner ring with no matching outer ring");
}
}
return result;
}
// wrapper for projection classes
class Projection {
public:
virtual void Forward(double lat, double lon, double& x, double& y) const =0;
virtual void Reverse(double x, double y, double& lat, double& lon) const =0;
void addPointToPolygonArea(PolygonArea& polygonArea, double x, double y) const {
double lat, lon;
Reverse(x, y, lat, lon);
polygonArea.AddPoint(lat, lon);
}
};
// wrapper for a simple no-op latitude/longitude projection
class TrivialProjection : public Projection {
public:
void Forward(double lat, double lon, double& x, double& y) const {
x = lon;
y = lat;
}
void Reverse(double x, double y, double& lat, double& lon) const {
lon = x;
lat = y;
}
};
// wrapper for Gnomonic projection
class GnomonicProjection : public Projection {
GeographicLib::Gnomonic internal;
public:
GnomonicProjection(const Geodesic& geodesic)
: internal(geodesic) { }
void Forward(double lat, double lon, double& x, double& y) const {
internal.Forward(CENTRAL_PARALLEL, CENTRAL_MERIDIAN, lat, lon, x, y);
}
void Reverse(double x, double y, double& lat, double& lon) const {
internal.Reverse(CENTRAL_PARALLEL, CENTRAL_MERIDIAN, x, y, lat, lon);
}
};
// wrapper for Albers equal area projection
class EqualAreaProjection : public Projection {
GeographicLib::AlbersEqualArea internal;
public:
EqualAreaProjection(const Geodesic& geodesic)
: internal(geodesic.MajorRadius(), geodesic.Flattening(), CENTRAL_PARALLEL, 1.0) { }
void Forward(double lat, double lon, double& x, double& y) const {
internal.Forward(CENTRAL_MERIDIAN, lat, lon, x, y);
}
void Reverse(double x, double y, double& lat, double& lon) const {
internal.Reverse(CENTRAL_MERIDIAN, x, y, lat, lon);
}
};
///////////////////////////////////////////////////////////////////////
// translators from one projection to another /////////////////////////
void translateRing(const Projection& srcProjection, const Projection& dstProjection, Ring& ring) {
boost::geometry::for_each_point(ring, [&srcProjection, &dstProjection](Point& point) {
double x, y, lat, lon;
srcProjection.Reverse(point.x(), point.y(), lat, lon);
dstProjection.Forward(lat, lon, x, y);
point.x(x); point.y(y);
});
}
void translatePolygon(const Projection& srcProjection, const Projection& dstProjection, Polygon& polygon) {
translateRing(srcProjection, dstProjection, polygon.outer());
for (auto& inner : polygon.inners()) {
translateRing(srcProjection, dstProjection, inner);
}
}
void translateShape(const Projection& srcProjection, const Projection& dstProjection, Shape& shape) {
for (auto& polygon : shape) {
translatePolygon(srcProjection, dstProjection, polygon);
}
}
///////////////////////////////////////////////////////////////////////
// main routines for computation of the centre ////////////////////////
void compute(const Shape& shape, const Geocentric& geocentric, const Projection& projection, int N) {
Rectangle bounds;
boost::geometry::envelope(shape, bounds);
double xMin = bounds.min_corner().x();
double xMax = bounds.max_corner().x();
double yMin = bounds.min_corner().y();
double yMax = bounds.max_corner().y();
double x[N+1], y[N+1];
for (int i=0; i<=N; ++i) {
x[i] = xMin + (xMax - xMin) * i/N;
y[i] = yMin + (yMax - yMin) * i/N;
}
double X0, Y0, Z0;
geocentric.Forward(CENTRAL_PARALLEL, CENTRAL_MERIDIAN, 0.0, X0, Y0, Z0);
Ring cell;
std::list<Polygon> intersection;
double areaNode = 0;
double XNode = 0, YNode = 0, ZNode = 0;
MPI_Barrier(MPI_COMM_WORLD);
for (int ix=rankMPI; ix<N; ix+=sizeMPI) {
for (int iy=0; iy<N; ++iy) {
cell.clear();
boost::geometry::append(cell, Point(x[ix], y[iy]));
boost::geometry::append(cell, Point(x[ix], y[iy+1]));
boost::geometry::append(cell, Point(x[ix+1], y[iy+1]));
boost::geometry::append(cell, Point(x[ix+1], y[iy]));
boost::geometry::append(cell, Point(x[ix], y[iy]));
intersection.clear();
boost::geometry::intersection(shape, cell, intersection);
for (const Polygon& part : intersection) {
Point centroid(0, 0);
double lat, lon, X, Y, Z;
boost::geometry::centroid(part, centroid);
projection.Reverse(centroid.x(), centroid.y(), lat, lon);
geocentric.Forward(lat, lon, 0.0, X, Y, Z);
double area = boost::geometry::area(part);
XNode += (X - X0) * area;
YNode += (Y - Y0) * area;
ZNode += (Z - Z0) * area;
// summation of the area
areaNode += area;
}
}
}
MPI_Barrier(MPI_COMM_WORLD);
double X, Y, Z, area;
MPI_Reduce(&areaNode, &area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&XNode, &X, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&YNode, &Y, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&ZNode, &Z, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (!rankMPI) {
X = X0 + X / area;
Y = Y0 + Y / area;
Z = Z0 + Z / area;
double lat, lon, h;
geocentric.Reverse(X, Y, Z, lat, lon, h);
double latD, latM, latS, lonD, lonM, lonS;
DMS::Encode(lat, latD, latM, latS);
DMS::Encode(lon, lonD, lonM, lonS);
printf(" %10.3f %12.10f %12.10f %.3f %.0f°%02.0f′%05.2f″ %.0f°%02.0f′%05.2f″",
1.0e-6*area, lat, lon, h, latD, latM, latS, lonD, lonM, lonS);
fflush(stdout);
}
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rankMPI);
MPI_Comm_size(MPI_COMM_WORLD, &sizeMPI);
if (argc < 3) {
if (!rankMPI) {
fprintf(stderr, "USAGE: %s N path_to_shapefile [ object_index ]\n", argv[0]);
}
return 1;
}
try {
const int N = atoi(argv[1]);
const char* path = argv[2];
const int index = argc>3 ? atoi(argv[3]) : 0;
if (N < 10) {
throw std::runtime_error("N must be at least 10");
}
Geodesic geodesic(GRS80_A, GRS80_F);
Geocentric geocentric(GRS80_A, GRS80_F);
TrivialProjection trivial;
EqualAreaProjection equalArea(geodesic);
if (!rankMPI) {
printf("%6d ", N);
fflush(stdout);
}
Shape shape = readShapefile(path, index);
translateShape(trivial, equalArea, shape);
compute(shape, geocentric, equalArea, N);
if (!rankMPI) {
putchar('\n');
}
} catch (const std::exception& ex) {
if (!rankMPI) {
fprintf(stderr, "ERROR: %s\n", ex.what());
}
return 1;
}
MPI_Finalize();
}