contrek 1.4.2 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +11 -2
- data/ext/cpp_polygon_finder/PolygonFinder/src/Tests.cpp +13 -18
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/RawBitmap.cpp +16 -6
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/RawBitmap.h +1 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/AsciiSource.cpp +10 -33
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/AsciiSource.h +4 -9
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/GeoLocalization.h +32 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/PngSource.cpp +6 -11
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/RasterStreamer.h +5 -10
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/TiffSource.cpp +301 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/TiffSource.h +51 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/PolygonFinder.h +9 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/GeoJsonStreamingMerger.h +51 -6
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Merger.cpp +6 -1
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/VerticalMerger.cpp +24 -6
- data/ext/cpp_polygon_finder/cpp_polygon_finder.cpp +39 -2
- data/ext/cpp_polygon_finder/extconf.rb +9 -0
- data/lib/contrek/bitmaps/streaming/raster_streamer.rb +1 -1
- data/lib/contrek/bitmaps/streaming/sources/tiff.rb +349 -0
- data/lib/contrek/finder/concurrent/geo_json_streaming_merger.rb +19 -2
- data/lib/contrek/finder/concurrent/merger.rb +6 -1
- data/lib/contrek/finder/concurrent/vertical_merger.rb +24 -4
- data/lib/contrek/finder/result.rb +6 -0
- data/lib/contrek/shared/opencv_converter.rb +144 -0
- data/lib/contrek/version.rb +1 -1
- data/lib/contrek.rb +2 -0
- metadata +6 -1
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* TiffSource.cpp
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025-2026 Emanuele Cesaroni
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the GNU Affero General Public License v3 (AGPLv3).
|
|
7
|
+
* See the LICENSE file in this directory for the full license text.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#include "TiffSource.h"
|
|
11
|
+
|
|
12
|
+
#include <geotiff/geotiff.h>
|
|
13
|
+
#include <geotiff/geotiffio.h>
|
|
14
|
+
#include <geotiff/geokeys.h>
|
|
15
|
+
#include <geotiff/xtiffio.h>
|
|
16
|
+
#include <tiffio.h>
|
|
17
|
+
#include <stdexcept>
|
|
18
|
+
#include <string>
|
|
19
|
+
|
|
20
|
+
static const uint32_t TIFFTAG_MODELPIXELSCALE = 33550;
|
|
21
|
+
static const uint32_t TIFFTAG_MODELTIEPOINT = 33922;
|
|
22
|
+
static const uint32_t TIFFTAG_MODELTRANSFORMATION = 34264;
|
|
23
|
+
|
|
24
|
+
TiffSource::TiffSource(const std::string& filepath, bool suppress_warnings)
|
|
25
|
+
: tiff_(nullptr),
|
|
26
|
+
width_(0),
|
|
27
|
+
height_(0),
|
|
28
|
+
bits_per_sample_(0),
|
|
29
|
+
samples_per_pixel_(0),
|
|
30
|
+
photometric_(0),
|
|
31
|
+
planar_config_(0),
|
|
32
|
+
sample_format_(0),
|
|
33
|
+
current_row_(0) {
|
|
34
|
+
if (suppress_warnings) {
|
|
35
|
+
TIFFSetWarningHandler(nullptr);
|
|
36
|
+
}
|
|
37
|
+
tiff_ = XTIFFOpen(filepath.c_str(), "r");
|
|
38
|
+
|
|
39
|
+
if (!tiff_) {
|
|
40
|
+
throw std::runtime_error("Unable to open TIFF: " + filepath);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
read_image_info();
|
|
45
|
+
read_geo_localization();
|
|
46
|
+
|
|
47
|
+
const tmsize_t scanline_size = TIFFScanlineSize(tiff_);
|
|
48
|
+
if (scanline_size <= 0) throw std::runtime_error("Invalid TIFF scanline size");
|
|
49
|
+
|
|
50
|
+
source_row_.resize(static_cast<std::size_t>(scanline_size));
|
|
51
|
+
}
|
|
52
|
+
catch (...) {
|
|
53
|
+
XTIFFClose(tiff_);
|
|
54
|
+
tiff_ = nullptr;
|
|
55
|
+
throw;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
TiffSource::~TiffSource() {
|
|
60
|
+
if (tiff_) {
|
|
61
|
+
XTIFFClose(tiff_);
|
|
62
|
+
tiff_ = nullptr;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
uint32_t TiffSource::width() const {
|
|
67
|
+
return width_;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
uint32_t TiffSource::height() const {
|
|
71
|
+
return height_;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
uint32_t TiffSource::get_bytes_per_pixel() const {
|
|
75
|
+
return 4;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const GeoLocalization& TiffSource::geo_localization() const {
|
|
79
|
+
return geo_localization_;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
void TiffSource::read_image_info() {
|
|
83
|
+
if (!TIFFGetField(tiff_, TIFFTAG_IMAGEWIDTH, &width_)) throw std::runtime_error("Unable to read TIFF width");
|
|
84
|
+
if (!TIFFGetField(tiff_, TIFFTAG_IMAGELENGTH, &height_)) throw std::runtime_error("Unable to read TIFF height");
|
|
85
|
+
|
|
86
|
+
TIFFGetFieldDefaulted(tiff_, TIFFTAG_BITSPERSAMPLE, &bits_per_sample_);
|
|
87
|
+
TIFFGetFieldDefaulted(tiff_, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel_);
|
|
88
|
+
|
|
89
|
+
if (!TIFFGetField(tiff_, TIFFTAG_PHOTOMETRIC, &photometric_)) throw std::runtime_error("Unable to read TIFF photometric");
|
|
90
|
+
|
|
91
|
+
TIFFGetFieldDefaulted(tiff_, TIFFTAG_PLANARCONFIG, &planar_config_);
|
|
92
|
+
TIFFGetFieldDefaulted(tiff_, TIFFTAG_SAMPLEFORMAT, &sample_format_);
|
|
93
|
+
|
|
94
|
+
if (TIFFIsTiled(tiff_)) throw std::runtime_error("Tiled TIFF is not supported yet");
|
|
95
|
+
if (planar_config_ != PLANARCONFIG_CONTIG) throw std::runtime_error("Unsupported TIFF planar configuration");
|
|
96
|
+
if (bits_per_sample_ != 8 && bits_per_sample_ != 16) throw std::runtime_error("Unsupported TIFF bits per sample");
|
|
97
|
+
if (samples_per_pixel_ != 1 && samples_per_pixel_ != 3 && samples_per_pixel_ != 4) throw std::runtime_error("Unsupported TIFF samples per pixel");
|
|
98
|
+
if (sample_format_ != SAMPLEFORMAT_UINT) throw std::runtime_error("Unsupported TIFF sample format");
|
|
99
|
+
|
|
100
|
+
if (photometric_ != PHOTOMETRIC_MINISWHITE &&
|
|
101
|
+
photometric_ != PHOTOMETRIC_MINISBLACK &&
|
|
102
|
+
photometric_ != PHOTOMETRIC_RGB) {
|
|
103
|
+
throw std::runtime_error("Unsupported TIFF photometric");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void TiffSource::read_geo_localization() {
|
|
108
|
+
double* matrix = nullptr;
|
|
109
|
+
uint16_t matrix_count = 0;
|
|
110
|
+
|
|
111
|
+
if (TIFFGetField(tiff_, TIFFTAG_MODELTRANSFORMATION, &matrix_count, &matrix) && matrix && matrix_count >= 16) {
|
|
112
|
+
read_model_transformation(matrix, matrix_count);
|
|
113
|
+
} else {
|
|
114
|
+
read_pixel_scale_and_tiepoint();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (geo_localization_.valid) read_crs();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
void TiffSource::read_model_transformation(double* matrix, uint16_t count) {
|
|
121
|
+
if (!matrix || count < 16) return;
|
|
122
|
+
|
|
123
|
+
GeoTransform& transform = geo_localization_.transform;
|
|
124
|
+
|
|
125
|
+
transform.x_pixel_size = matrix[0];
|
|
126
|
+
transform.x_row_offset = matrix[1];
|
|
127
|
+
transform.x_origin = matrix[3];
|
|
128
|
+
|
|
129
|
+
transform.y_column_offset = matrix[4];
|
|
130
|
+
transform.y_pixel_size = matrix[5];
|
|
131
|
+
transform.y_origin = matrix[7];
|
|
132
|
+
|
|
133
|
+
geo_localization_.valid = true;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
void TiffSource::read_pixel_scale_and_tiepoint() {
|
|
137
|
+
double* scale = nullptr;
|
|
138
|
+
double* tiepoint = nullptr;
|
|
139
|
+
|
|
140
|
+
uint16_t scale_count = 0;
|
|
141
|
+
uint16_t tiepoint_count = 0;
|
|
142
|
+
|
|
143
|
+
if (!TIFFGetField(tiff_, TIFFTAG_MODELPIXELSCALE, &scale_count, &scale)) return;
|
|
144
|
+
if (!TIFFGetField(tiff_, TIFFTAG_MODELTIEPOINT, &tiepoint_count, &tiepoint)) return;
|
|
145
|
+
if (!scale || !tiepoint || scale_count < 2 || tiepoint_count < 6) return;
|
|
146
|
+
|
|
147
|
+
const double pixel_x = tiepoint[0];
|
|
148
|
+
const double pixel_y = tiepoint[1];
|
|
149
|
+
|
|
150
|
+
const double model_x = tiepoint[3];
|
|
151
|
+
const double model_y = tiepoint[4];
|
|
152
|
+
|
|
153
|
+
const double x_pixel_size = scale[0];
|
|
154
|
+
const double y_pixel_size = scale[1];
|
|
155
|
+
|
|
156
|
+
GeoTransform& transform = geo_localization_.transform;
|
|
157
|
+
|
|
158
|
+
transform.x_origin = model_x - pixel_x * x_pixel_size;
|
|
159
|
+
transform.y_origin = model_y + pixel_y * y_pixel_size;
|
|
160
|
+
|
|
161
|
+
transform.x_pixel_size = x_pixel_size;
|
|
162
|
+
transform.y_pixel_size = -y_pixel_size;
|
|
163
|
+
|
|
164
|
+
transform.x_row_offset = 0.0;
|
|
165
|
+
transform.y_column_offset = 0.0;
|
|
166
|
+
|
|
167
|
+
geo_localization_.valid = true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
void TiffSource::read_crs() {
|
|
171
|
+
GTIF* gtif = GTIFNew(tiff_);
|
|
172
|
+
if (!gtif) return;
|
|
173
|
+
|
|
174
|
+
uint16_t model_type = 0;
|
|
175
|
+
uint16_t code = 0;
|
|
176
|
+
|
|
177
|
+
if (GTIFKeyGet(gtif, GTModelTypeGeoKey, &model_type, 0, 1) == 1) {
|
|
178
|
+
if (model_type == ModelTypeProjected) {
|
|
179
|
+
if (GTIFKeyGet(gtif, ProjectedCSTypeGeoKey, &code, 0, 1) == 1) {
|
|
180
|
+
geo_localization_.crs.authority = "EPSG";
|
|
181
|
+
geo_localization_.crs.code = static_cast<int>(code);
|
|
182
|
+
}
|
|
183
|
+
} else if (model_type == ModelTypeGeographic) {
|
|
184
|
+
if (GTIFKeyGet(gtif, GeographicTypeGeoKey, &code, 0, 1) == 1) {
|
|
185
|
+
geo_localization_.crs.authority = "EPSG";
|
|
186
|
+
geo_localization_.crs.code = static_cast<int>(code);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
GTIFFree(gtif);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
bool TiffSource::read_next_row(unsigned char* destination, std::size_t row_size) {
|
|
195
|
+
if (current_row_ >= height_) return false;
|
|
196
|
+
|
|
197
|
+
const std::size_t expected_row_size = static_cast<std::size_t>(width_) * get_bytes_per_pixel();
|
|
198
|
+
|
|
199
|
+
if (row_size != expected_row_size) {
|
|
200
|
+
throw std::runtime_error("Invalid TIFF destination row size");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (TIFFReadScanline(tiff_, source_row_.data(), current_row_, 0) < 0) {
|
|
204
|
+
throw std::runtime_error("TIFFReadScanline failed at row " + std::to_string(current_row_));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
convert_row(destination);
|
|
208
|
+
|
|
209
|
+
++current_row_;
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
void TiffSource::convert_row(unsigned char* destination) {
|
|
214
|
+
if (samples_per_pixel_ == 4 && bits_per_sample_ == 8) {
|
|
215
|
+
for (uint32_t x = 0; x < width_; ++x) {
|
|
216
|
+
const std::size_t source_offset = static_cast<std::size_t>(x) * 4;
|
|
217
|
+
const std::size_t destination_offset = static_cast<std::size_t>(x) * 4;
|
|
218
|
+
|
|
219
|
+
destination[destination_offset] = source_row_[source_offset];
|
|
220
|
+
destination[destination_offset + 1] = source_row_[source_offset + 1];
|
|
221
|
+
destination[destination_offset + 2] = source_row_[source_offset + 2];
|
|
222
|
+
destination[destination_offset + 3] = source_row_[source_offset + 3];
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (samples_per_pixel_ == 3 && bits_per_sample_ == 8) {
|
|
228
|
+
for (uint32_t x = 0; x < width_; ++x) {
|
|
229
|
+
const std::size_t source_offset = static_cast<std::size_t>(x) * 3;
|
|
230
|
+
const std::size_t destination_offset = static_cast<std::size_t>(x) * 4;
|
|
231
|
+
|
|
232
|
+
destination[destination_offset] = source_row_[source_offset];
|
|
233
|
+
destination[destination_offset + 1] = source_row_[source_offset + 1];
|
|
234
|
+
destination[destination_offset + 2] = source_row_[source_offset + 2];
|
|
235
|
+
destination[destination_offset + 3] = 255;
|
|
236
|
+
}
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (samples_per_pixel_ == 1 && bits_per_sample_ == 8) {
|
|
241
|
+
for (uint32_t x = 0; x < width_; ++x) {
|
|
242
|
+
unsigned char value = source_row_[x];
|
|
243
|
+
|
|
244
|
+
if (photometric_ == PHOTOMETRIC_MINISWHITE) value = static_cast<unsigned char>(255 - value);
|
|
245
|
+
|
|
246
|
+
const std::size_t destination_offset = static_cast<std::size_t>(x) * 4;
|
|
247
|
+
|
|
248
|
+
destination[destination_offset] = value;
|
|
249
|
+
destination[destination_offset + 1] = value;
|
|
250
|
+
destination[destination_offset + 2] = value;
|
|
251
|
+
destination[destination_offset + 3] = 255;
|
|
252
|
+
}
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const uint16_t* source16 = reinterpret_cast<const uint16_t*>(source_row_.data());
|
|
257
|
+
|
|
258
|
+
if (samples_per_pixel_ == 4 && bits_per_sample_ == 16) {
|
|
259
|
+
for (uint32_t x = 0; x < width_; ++x) {
|
|
260
|
+
const std::size_t source_offset = static_cast<std::size_t>(x) * 4;
|
|
261
|
+
const std::size_t destination_offset = static_cast<std::size_t>(x) * 4;
|
|
262
|
+
|
|
263
|
+
destination[destination_offset] = static_cast<unsigned char>(source16[source_offset] >> 8);
|
|
264
|
+
destination[destination_offset + 1] = static_cast<unsigned char>(source16[source_offset + 1] >> 8);
|
|
265
|
+
destination[destination_offset + 2] = static_cast<unsigned char>(source16[source_offset + 2] >> 8);
|
|
266
|
+
destination[destination_offset + 3] = static_cast<unsigned char>(source16[source_offset + 3] >> 8);
|
|
267
|
+
}
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (samples_per_pixel_ == 3 && bits_per_sample_ == 16) {
|
|
272
|
+
for (uint32_t x = 0; x < width_; ++x) {
|
|
273
|
+
const std::size_t source_offset = static_cast<std::size_t>(x) * 3;
|
|
274
|
+
const std::size_t destination_offset = static_cast<std::size_t>(x) * 4;
|
|
275
|
+
|
|
276
|
+
destination[destination_offset] = static_cast<unsigned char>(source16[source_offset] >> 8);
|
|
277
|
+
destination[destination_offset + 1] = static_cast<unsigned char>(source16[source_offset + 1] >> 8);
|
|
278
|
+
destination[destination_offset + 2] = static_cast<unsigned char>(source16[source_offset + 2] >> 8);
|
|
279
|
+
destination[destination_offset + 3] = 255;
|
|
280
|
+
}
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (samples_per_pixel_ == 1 && bits_per_sample_ == 16) {
|
|
285
|
+
for (uint32_t x = 0; x < width_; ++x) {
|
|
286
|
+
unsigned char value = static_cast<unsigned char>(source16[x] >> 8);
|
|
287
|
+
|
|
288
|
+
if (photometric_ == PHOTOMETRIC_MINISWHITE) value = static_cast<unsigned char>(255 - value);
|
|
289
|
+
|
|
290
|
+
const std::size_t destination_offset = static_cast<std::size_t>(x) * 4;
|
|
291
|
+
|
|
292
|
+
destination[destination_offset] = value;
|
|
293
|
+
destination[destination_offset + 1] = value;
|
|
294
|
+
destination[destination_offset + 2] = value;
|
|
295
|
+
destination[destination_offset + 3] = 255;
|
|
296
|
+
}
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
throw std::runtime_error("Unsupported TIFF pixel format");
|
|
301
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* TiffSource.h
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025-2026 Emanuele Cesaroni
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the GNU Affero General Public License v3 (AGPLv3).
|
|
7
|
+
* See the LICENSE file in this directory for the full license text.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#pragma once
|
|
11
|
+
|
|
12
|
+
#include "RasterSource.h"
|
|
13
|
+
#include "GeoLocalization.h"
|
|
14
|
+
|
|
15
|
+
#include <cstddef>
|
|
16
|
+
#include <cstdint>
|
|
17
|
+
#include <string>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
struct tiff;
|
|
21
|
+
typedef struct tiff TIFF;
|
|
22
|
+
|
|
23
|
+
class TiffSource : public RasterSource {
|
|
24
|
+
public:
|
|
25
|
+
explicit TiffSource(const std::string& filepath, bool suppress_warnings = false);
|
|
26
|
+
~TiffSource() override;
|
|
27
|
+
uint32_t width() const override;
|
|
28
|
+
uint32_t height() const override;
|
|
29
|
+
uint32_t get_bytes_per_pixel() const override;
|
|
30
|
+
bool read_next_row(unsigned char* destination, std::size_t row_size) override;
|
|
31
|
+
const GeoLocalization& geo_localization() const;
|
|
32
|
+
|
|
33
|
+
private:
|
|
34
|
+
void read_image_info();
|
|
35
|
+
void read_geo_localization();
|
|
36
|
+
void read_pixel_scale_and_tiepoint();
|
|
37
|
+
void read_model_transformation(double* matrix, uint16_t count);
|
|
38
|
+
void read_crs();
|
|
39
|
+
void convert_row(unsigned char* destination);
|
|
40
|
+
TIFF* tiff_;
|
|
41
|
+
uint32_t width_;
|
|
42
|
+
uint32_t height_;
|
|
43
|
+
uint16_t bits_per_sample_;
|
|
44
|
+
uint16_t samples_per_pixel_;
|
|
45
|
+
uint16_t photometric_;
|
|
46
|
+
uint16_t planar_config_;
|
|
47
|
+
uint16_t sample_format_;
|
|
48
|
+
uint32_t current_row_;
|
|
49
|
+
std::vector<unsigned char> source_row_;
|
|
50
|
+
GeoLocalization geo_localization_;
|
|
51
|
+
};
|
|
@@ -154,6 +154,15 @@ struct ProcessResult {
|
|
|
154
154
|
to_svg_stream(file);
|
|
155
155
|
file.close();
|
|
156
156
|
}
|
|
157
|
+
|
|
158
|
+
void sort_polygons() {
|
|
159
|
+
polygons.sort([](const Polygon& a, const Polygon& b) {
|
|
160
|
+
if (a.bounds.min_y != b.bounds.min_y) {
|
|
161
|
+
return a.bounds.min_y < b.bounds.min_y;
|
|
162
|
+
}
|
|
163
|
+
return a.bounds.min_x < b.bounds.min_x;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
157
166
|
};
|
|
158
167
|
|
|
159
168
|
class PolygonFinder {
|
data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/GeoJsonStreamingMerger.h
CHANGED
|
@@ -10,12 +10,51 @@
|
|
|
10
10
|
#pragma once
|
|
11
11
|
#include <vector>
|
|
12
12
|
#include <string>
|
|
13
|
+
#include <functional>
|
|
14
|
+
#include <stdexcept>
|
|
15
|
+
#include <iomanip>
|
|
16
|
+
#include <limits>
|
|
13
17
|
#include "StreamingMerger.h"
|
|
14
18
|
|
|
15
19
|
class GeoJsonStreamingMerger : public StreamingMerger {
|
|
16
20
|
private:
|
|
17
21
|
unsigned int target_value;
|
|
18
22
|
bool is_first_feature = true;
|
|
23
|
+
std::function<void(const Point&)> point_writer;
|
|
24
|
+
|
|
25
|
+
void configure_point_writer(const Options& options) {
|
|
26
|
+
const Options* geo_localization = options.get_options("geo_localization");
|
|
27
|
+
if (!geo_localization) {
|
|
28
|
+
point_writer = [this](const Point& point) { *stream << "[" << point.y << "," << point.x << "]"; };
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const Options* transform = geo_localization->get_options("transform");
|
|
33
|
+
const Options* crs = geo_localization->get_options("crs");
|
|
34
|
+
if (!transform || !crs) throw std::invalid_argument("Invalid geo_localization.");
|
|
35
|
+
|
|
36
|
+
const std::string authority = crs->get<std::string>("authority", "");
|
|
37
|
+
const auto code = crs->get("code", 0L);
|
|
38
|
+
if (authority != "EPSG" || code != 4326) throw std::invalid_argument("Unsupported CRS: " + authority + ":" + std::to_string(code));
|
|
39
|
+
const double x_origin = transform->get<double>("x_origin", 0.0);
|
|
40
|
+
const double y_origin = transform->get<double>("y_origin", 0.0);
|
|
41
|
+
const double x_pixel_size = transform->get<double>("x_pixel_size", 1.0);
|
|
42
|
+
const double y_pixel_size = transform->get<double>("y_pixel_size", 1.0);
|
|
43
|
+
const double x_row_offset = transform->get<double>("x_row_offset", 0.0);
|
|
44
|
+
const double y_column_offset = transform->get<double>("y_column_offset", 0.0);
|
|
45
|
+
|
|
46
|
+
*stream << std::fixed << std::setprecision(7);
|
|
47
|
+
|
|
48
|
+
point_writer = [this, x_origin, y_origin, x_pixel_size, y_pixel_size, x_row_offset, y_column_offset](const Point& point) {
|
|
49
|
+
const double x = point.y;
|
|
50
|
+
const double y = point.x;
|
|
51
|
+
*stream << "[" << x_origin + x * x_pixel_size + y * x_row_offset << "," << y_origin + x * y_column_offset + y * y_pixel_size << "]";
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void write_point(const Point& point) {
|
|
56
|
+
point_writer(point);
|
|
57
|
+
}
|
|
19
58
|
|
|
20
59
|
protected:
|
|
21
60
|
void write_header() override {
|
|
@@ -40,7 +79,9 @@ class GeoJsonStreamingMerger : public StreamingMerger {
|
|
|
40
79
|
std::ofstream* stream_to,
|
|
41
80
|
unsigned int pixel_value)
|
|
42
81
|
: StreamingMerger(number_of_threads, options, stream_to),
|
|
43
|
-
target_value(pixel_value) {
|
|
82
|
+
target_value(pixel_value) {
|
|
83
|
+
configure_point_writer(options);
|
|
84
|
+
}
|
|
44
85
|
|
|
45
86
|
void stream_raw_polygon(const Polygon& polygon) override {
|
|
46
87
|
if (!stream) return;
|
|
@@ -55,12 +96,14 @@ class GeoJsonStreamingMerger : public StreamingMerger {
|
|
|
55
96
|
const size_t points_size = points.size();
|
|
56
97
|
if (points_size > 0) {
|
|
57
98
|
for (size_t i = 0; i < points_size; ++i) {
|
|
58
|
-
|
|
99
|
+
write_point(points[i]);
|
|
59
100
|
if (i < points_size - 1) *stream << ",";
|
|
60
101
|
}
|
|
61
|
-
*stream << ",
|
|
102
|
+
*stream << ",";
|
|
103
|
+
write_point(points[0]);
|
|
104
|
+
*stream << "]";
|
|
62
105
|
} else {
|
|
63
|
-
*stream << "]
|
|
106
|
+
*stream << "]";
|
|
64
107
|
}
|
|
65
108
|
|
|
66
109
|
for (const std::vector<Point>& inner_points : polygon.inner) {
|
|
@@ -68,10 +111,12 @@ class GeoJsonStreamingMerger : public StreamingMerger {
|
|
|
68
111
|
if (inner_size > 0) {
|
|
69
112
|
*stream << ",[";
|
|
70
113
|
for (size_t i = 0; i < inner_size; ++i) {
|
|
71
|
-
|
|
114
|
+
write_point(inner_points[i]);
|
|
72
115
|
if (i < inner_size - 1) *stream << ",";
|
|
73
116
|
}
|
|
74
|
-
*stream << ",
|
|
117
|
+
*stream << ",";
|
|
118
|
+
write_point(inner_points[0]);
|
|
119
|
+
*stream << "]";
|
|
75
120
|
}
|
|
76
121
|
}
|
|
77
122
|
*stream << "]}}";
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
|
|
15
15
|
Merger::Merger(int number_of_threads, const Options& options)
|
|
16
16
|
: Finder(number_of_threads, options) {
|
|
17
|
+
if (options.contains("versus")) {
|
|
18
|
+
std::cerr << "[Contrek WARNING] The given options 'versus' will be ignored." << std::endl;
|
|
19
|
+
}
|
|
17
20
|
if (!this->safe()) {
|
|
18
21
|
std::cerr << "[Contrek WARNING] Processing tile with 'unsafe_mode: true'. "
|
|
19
22
|
<< "Incompatible result options might lead to unexpected vector geometry.\n";
|
|
@@ -58,7 +61,9 @@ void Merger::add_tile(ProcessResult& result)
|
|
|
58
61
|
|
|
59
62
|
ProcessResult* Merger::process_info() {
|
|
60
63
|
this->process_tiles();
|
|
61
|
-
|
|
64
|
+
ProcessResult* result = Finder::process_info();
|
|
65
|
+
result->versus = this->versus;
|
|
66
|
+
return result;
|
|
62
67
|
}
|
|
63
68
|
|
|
64
69
|
void Merger::translate(ProcessResult& result, int offset) {
|
|
@@ -20,10 +20,10 @@ VerticalMerger::VerticalMerger(int number_of_threads, const Options& options)
|
|
|
20
20
|
|
|
21
21
|
void VerticalMerger::add_tile(ProcessResult& result)
|
|
22
22
|
{ transpose(result);
|
|
23
|
+
adjust(result);
|
|
23
24
|
if (this->tiles_.size() > 0) {
|
|
24
25
|
translate(result, this->current_x);
|
|
25
26
|
}
|
|
26
|
-
adjust(result);
|
|
27
27
|
Merger::add_tile(result);
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -50,14 +50,32 @@ void VerticalMerger::transpose(ProcessResult& result) {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
void VerticalMerger::adjust(ProcessResult& result) {
|
|
53
|
+
const int tile_width = result.width;
|
|
54
|
+
const auto number_of_tiles = this->tiles_.size();
|
|
55
|
+
|
|
53
56
|
for (auto& polygon : result.polygons) {
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
const auto& bounds = polygon.bounds;
|
|
58
|
+
const bool needs_left = number_of_tiles > 0 && bounds.min_x == 0;
|
|
59
|
+
const bool needs_right = bounds.max_x == tile_width;
|
|
60
|
+
|
|
61
|
+
if (!needs_left && !needs_right) {
|
|
62
|
+
continue;
|
|
56
63
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
64
|
+
|
|
65
|
+
auto& sequence = polygon.outer;
|
|
66
|
+
auto best = sequence.end();
|
|
67
|
+
for (auto it = sequence.begin(); it != sequence.end(); ++it) {
|
|
68
|
+
if (it->y != bounds.min_y) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (best == sequence.end() ||
|
|
72
|
+
(result.versus == Node::A ? it->x > best->x : it->x < best->x)) {
|
|
73
|
+
best = it;
|
|
60
74
|
}
|
|
61
75
|
}
|
|
76
|
+
|
|
77
|
+
if (best != sequence.end() && best != sequence.begin()) {
|
|
78
|
+
std::rotate(sequence.begin(), best, sequence.end());
|
|
79
|
+
}
|
|
62
80
|
}
|
|
63
81
|
}
|
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
#include "PolygonFinder/src/polygon/bitmaps/streaming/RasterSource.h"
|
|
41
41
|
#include "PolygonFinder/src/polygon/bitmaps/streaming/AsciiSource.h"
|
|
42
42
|
#include "PolygonFinder/src/polygon/bitmaps/streaming/AsciiSource.cpp"
|
|
43
|
+
#ifdef CONTREK_HAS_TIFF
|
|
44
|
+
#include "PolygonFinder/src/polygon/bitmaps/streaming/TiffSource.h"
|
|
45
|
+
#include "PolygonFinder/src/polygon/bitmaps/streaming/TiffSource.cpp"
|
|
46
|
+
#endif
|
|
43
47
|
#include "PolygonFinder/src/polygon/bitmaps/streaming/PngSource.h"
|
|
44
48
|
#include "PolygonFinder/src/polygon/bitmaps/streaming/PngSource.cpp"
|
|
45
49
|
#include "PolygonFinder/src/polygon/bitmaps/streaming/RasterStreamer.h"
|
|
@@ -455,6 +459,38 @@ void Init_cpp_polygon_finder() {
|
|
|
455
459
|
.define_method("height",&PngSource::height)
|
|
456
460
|
.define_method("get_bytes_per_pixel",&PngSource::get_bytes_per_pixel);
|
|
457
461
|
|
|
462
|
+
#ifdef CONTREK_HAS_TIFF
|
|
463
|
+
Data_Type<TiffSource> rb_cTiffSource =
|
|
464
|
+
define_class<TiffSource, RasterSource>("CPPTiffSource")
|
|
465
|
+
.define_constructor(Constructor<TiffSource, std::string, bool>(),Arg("filepath"),Arg("suppress_warnings") = false)
|
|
466
|
+
.define_method("width",&TiffSource::width)
|
|
467
|
+
.define_method("height",&TiffSource::height)
|
|
468
|
+
.define_method("get_bytes_per_pixel",&TiffSource::get_bytes_per_pixel)
|
|
469
|
+
.define_method("geo_localization",[](TiffSource& self) {
|
|
470
|
+
const GeoLocalization& geo = self.geo_localization();
|
|
471
|
+
|
|
472
|
+
if (!geo.valid) return Object(Qnil);
|
|
473
|
+
|
|
474
|
+
Hash transform;
|
|
475
|
+
transform[Symbol("x_origin")] = geo.transform.x_origin;
|
|
476
|
+
transform[Symbol("y_origin")] = geo.transform.y_origin;
|
|
477
|
+
transform[Symbol("x_pixel_size")] = geo.transform.x_pixel_size;
|
|
478
|
+
transform[Symbol("y_pixel_size")] = geo.transform.y_pixel_size;
|
|
479
|
+
transform[Symbol("x_row_offset")] = geo.transform.x_row_offset;
|
|
480
|
+
transform[Symbol("y_column_offset")] = geo.transform.y_column_offset;
|
|
481
|
+
|
|
482
|
+
Hash crs;
|
|
483
|
+
crs[Symbol("authority")] = geo.crs.authority;
|
|
484
|
+
crs[Symbol("code")] = geo.crs.code;
|
|
485
|
+
|
|
486
|
+
Hash result;
|
|
487
|
+
result[Symbol("transform")] = transform;
|
|
488
|
+
result[Symbol("crs")] = crs;
|
|
489
|
+
|
|
490
|
+
return Object(result);
|
|
491
|
+
});
|
|
492
|
+
#endif
|
|
493
|
+
|
|
458
494
|
Data_Type<RasterStreamer> rb_cRasterStreamer =
|
|
459
495
|
define_class<RasterStreamer>("CPPRasterStreamer")
|
|
460
496
|
.define_constructor(Constructor<RasterStreamer, RasterSource&, uint32_t>(),Arg("source"),Arg("stripe_height"))
|
|
@@ -463,13 +499,14 @@ void Init_cpp_polygon_finder() {
|
|
|
463
499
|
VALUE block = rb_block_proc();
|
|
464
500
|
self.each(
|
|
465
501
|
buffer,
|
|
466
|
-
[block](Bitmap& bitmap,uint32_t buffer_rows,std::size_t buffer_size) {
|
|
502
|
+
[block](Bitmap& bitmap,uint32_t buffer_rows,std::size_t buffer_size, std::size_t buffer_read) {
|
|
467
503
|
Rice::Data_Object<Bitmap> rb_bitmap(&bitmap);
|
|
468
504
|
Rice::Object(block).call(
|
|
469
505
|
"call",
|
|
470
506
|
rb_bitmap,
|
|
471
507
|
buffer_rows,
|
|
472
|
-
buffer_size
|
|
508
|
+
buffer_size,
|
|
509
|
+
buffer_read
|
|
473
510
|
);
|
|
474
511
|
}
|
|
475
512
|
);
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
require "mkmf-rice"
|
|
4
4
|
|
|
5
5
|
has_tcmalloc = find_library("tcmalloc", "malloc")
|
|
6
|
+
has_tiff = have_library("tiff", "TIFFOpen", "tiffio.h")
|
|
7
|
+
has_geotiff = has_tiff && have_library("geotiff", "XTIFFOpen", "geotiff/xtiffio.h")
|
|
6
8
|
|
|
7
9
|
# rubocop:disable Style/GlobalVars
|
|
8
10
|
|
|
@@ -16,6 +18,13 @@ else
|
|
|
16
18
|
puts "tcmalloc not found; standard malloc will be used."
|
|
17
19
|
end
|
|
18
20
|
|
|
21
|
+
if has_tiff && has_geotiff
|
|
22
|
+
$CXXFLAGS << " -DCONTREK_HAS_TIFF"
|
|
23
|
+
puts "TIFF/GeoTIFF support enabled."
|
|
24
|
+
else
|
|
25
|
+
puts "TIFF/GeoTIFF support disabled."
|
|
26
|
+
end
|
|
27
|
+
|
|
19
28
|
$LDFLAGS << " -lz -lstdc++ -flto -pthread"
|
|
20
29
|
|
|
21
30
|
$objs = [
|