contrek 1.4.4 → 1.4.5
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 +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +62 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/Tests.cpp +7 -3
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/GeoJsonStreamingMerger.h +1 -1
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Merger.cpp +31 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Merger.h +4 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/shared/OpencvConverter.cpp +51 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/shared/OpencvConverter.h +205 -0
- data/ext/cpp_polygon_finder/cpp_polygon_finder.cpp +1 -0
- data/lib/contrek/finder/concurrent/merger.rb +25 -1
- data/lib/contrek/shared/opencv_converter.rb +2 -0
- data/lib/contrek/version.rb +1 -1
- data/lib/contrek.rb +0 -1
- metadata +3 -2
- data/lib/contrek/map/mercator_projection.rb +0 -78
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9350d60698a69dda805ede85e42b80fcc38022a361e52674fd2780d696e0f9ff
|
|
4
|
+
data.tar.gz: '08d164d2e53bb3c96e7212ac6bc9d46bb673de8f10bb950d43f3d66d0c86d793'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1c6d0b73ab8affa0ed17aa3fd803f7aed81cec846db6edd3de5dfcec8103bf23c9e23484819c4a981a4cb0c46da18f0b169122124213a9b4417df108f54c468a
|
|
7
|
+
data.tar.gz: 57817011679288782915a50ca1adf4155b71732ab73e330e2ac696ea504d7204c08935a6b83487000faac68b37f38aff5b5761d78fc85aab7703752f2700d65f
|
data/CHANGELOG.md
CHANGED
|
@@ -167,3 +167,6 @@ All notable changes to this project will be documented in this file.
|
|
|
167
167
|
- Fixed `VerticalMerger` handling of polygon orientation and starting points.
|
|
168
168
|
- Added `detach()` to `RawBitmap` on the C++ side.
|
|
169
169
|
- Added an OpenCV coordinate converter helper for converting contours to Contrek-compatible polygons.
|
|
170
|
+
|
|
171
|
+
## [1.4.5] - 2026-10-11
|
|
172
|
+
- Added tile boundary validation for external geometries, with support for bypassing checks via `unsafe_mode: true`.
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -676,6 +676,68 @@ result = Contrek.contour!(
|
|
|
676
676
|
|
|
677
677
|
The choice mainly affects how diagonally touching regions are interpreted.
|
|
678
678
|
|
|
679
|
+
## Merging External Geometry
|
|
680
|
+
|
|
681
|
+
Contrek's merger can process polygon data produced by external contour
|
|
682
|
+
extractors, provided that the input geometry follows the requirements below.
|
|
683
|
+
|
|
684
|
+
### The Laws of the Tiles
|
|
685
|
+
|
|
686
|
+
1. **Adjacent tiles must share an overlap and have identical exposed boundary sequences.**
|
|
687
|
+
Adjacent tiles must share a single scanline of overlap. The exposed contour
|
|
688
|
+
segments on their shared boundary must have identical coordinate sequences,
|
|
689
|
+
including all intermediate vertices, expressed in the same coordinate system.
|
|
690
|
+
Any mismatch between the two sequences makes the tiles incompatible for merging.
|
|
691
|
+
|
|
692
|
+
2. **Coordinates must describe cell boundaries.**
|
|
693
|
+
A cell is the square area occupied by a pixel in the raster. Coordinates
|
|
694
|
+
must describe the boundaries of these cells, rather than representing a
|
|
695
|
+
pixel by its center or by a single vertex.
|
|
696
|
+
|
|
697
|
+
3. **Contours must follow cell boundaries.**
|
|
698
|
+
Contour segments supplied to the merger must follow the horizontal and
|
|
699
|
+
vertical edges of the raster cells. Diagonal segments are not allowed in
|
|
700
|
+
the geometry passed to the merger.
|
|
701
|
+
|
|
702
|
+
4. **Polygon orientation (`versus`) must be consistent across all tiles.**
|
|
703
|
+
All polygons must follow the orientation declared by the result, either
|
|
704
|
+
clockwise or counterclockwise. The same orientation must be used by every
|
|
705
|
+
tile submitted to the merger.
|
|
706
|
+
|
|
707
|
+
5. **Every contour must start at the canonical point of its first scanline.**
|
|
708
|
+
A *scanline* is a horizontal raster row scanned from left to right, like
|
|
709
|
+
the electron beam of a CRT display. Consider the topmost scanline
|
|
710
|
+
intersecting the contour. On that scanline, a **counterclockwise** contour
|
|
711
|
+
must start from the **leftmost** point, while a **clockwise** contour must
|
|
712
|
+
start from the **rightmost** point.
|
|
713
|
+
|
|
714
|
+
6. **The bounding box must match the contour geometry.**
|
|
715
|
+
`min_x`, `max_x`, `min_y`, and `max_y` must correctly describe the spatial
|
|
716
|
+
extent of the contour.
|
|
717
|
+
|
|
718
|
+
7. **Tile metadata must match the supplied geometry.**
|
|
719
|
+
`width`, `height`, and `versus` must correctly describe the tile. In
|
|
720
|
+
particular, `height` must represent the actual height of the result.
|
|
721
|
+
|
|
722
|
+
8. **Polygons must be sorted in raster scan order.**
|
|
723
|
+
Polygons must be ordered by increasing **Y**, and then by increasing **X**
|
|
724
|
+
when they have the same Y: top to bottom, and left to right at the same
|
|
725
|
+
vertical position.
|
|
726
|
+
|
|
727
|
+
### Boundary Validation
|
|
728
|
+
|
|
729
|
+
By default, Contrek validates the shared boundaries of adjacent tiles when
|
|
730
|
+
merging externally supplied geometries. Boundary coordinates must match
|
|
731
|
+
exactly, including intermediate vertices. Otherwise, the merger raises a
|
|
732
|
+
`Tile boundary mismatch` error.
|
|
733
|
+
|
|
734
|
+
This validation can be disabled by setting `unsafe_mode: true`, avoiding
|
|
735
|
+
validation overhead when the input geometries are known to be valid.
|
|
736
|
+
|
|
737
|
+
**Important:** Boundary validation does not verify all the Laws of the Tiles.
|
|
738
|
+
Users supplying external geometries remain responsible for satisfying every
|
|
739
|
+
requirement listed above.
|
|
740
|
+
|
|
679
741
|
# Results
|
|
680
742
|
|
|
681
743
|
The tracing result contains two kinds of information:
|
|
@@ -294,7 +294,9 @@ void Tests::test_h()
|
|
|
294
294
|
PolygonFinder pl_right(&b_right, &matcher, nullptr, options);
|
|
295
295
|
ProcessResult *right_result = pl_right.process_info();
|
|
296
296
|
|
|
297
|
-
Options merger_options = {
|
|
297
|
+
Options merger_options = {
|
|
298
|
+
{"unsafe_mode", true}
|
|
299
|
+
};
|
|
298
300
|
HorizontalMerger hmerger(1, merger_options);
|
|
299
301
|
hmerger.add_tile(*left_result);
|
|
300
302
|
hmerger.add_tile(*right_result);
|
|
@@ -326,7 +328,9 @@ double get_peak_rss() {
|
|
|
326
328
|
|
|
327
329
|
void stream_png_image(const std::string& filepath, uint32_t stripe_height, bool generate_svg = false, bool generate_png = false) {
|
|
328
330
|
std::vector<ProcessResult*> result_clones;
|
|
329
|
-
Options options = {
|
|
331
|
+
Options options = {
|
|
332
|
+
{"unsafe_mode", true}
|
|
333
|
+
};
|
|
330
334
|
VerticalMerger vmerger(0, options);
|
|
331
335
|
|
|
332
336
|
PngSource source(filepath);
|
|
@@ -409,7 +413,7 @@ void stream_progressive_png_image(const std::string& filepath, uint32_t stripe_h
|
|
|
409
413
|
|
|
410
414
|
SvgStreamingMerger merger(
|
|
411
415
|
0,
|
|
412
|
-
{{"bounds", true}},
|
|
416
|
+
{{"bounds", true},{"unsafe_mode", true}},
|
|
413
417
|
&stream,
|
|
414
418
|
source.width(),
|
|
415
419
|
source.height());
|
data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/GeoJsonStreamingMerger.h
CHANGED
|
@@ -34,7 +34,7 @@ class GeoJsonStreamingMerger : public StreamingMerger {
|
|
|
34
34
|
if (!transform || !crs) throw std::invalid_argument("Invalid geo_localization.");
|
|
35
35
|
|
|
36
36
|
const std::string authority = crs->get<std::string>("authority", "");
|
|
37
|
-
const auto code = crs->get("code",
|
|
37
|
+
const auto code = crs->get("code", std::int64_t{0});
|
|
38
38
|
if (authority != "EPSG" || code != 4326) throw std::invalid_argument("Unsupported CRS: " + authority + ":" + std::to_string(code));
|
|
39
39
|
const double x_origin = transform->get<double>("x_origin", 0.0);
|
|
40
40
|
const double y_origin = transform->get<double>("y_origin", 0.0);
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
#include <iostream>
|
|
11
11
|
#include <string>
|
|
12
12
|
#include <vector>
|
|
13
|
+
#include <algorithm>
|
|
14
|
+
#include <list>
|
|
13
15
|
#include "Merger.h"
|
|
14
16
|
|
|
15
17
|
Merger::Merger(int number_of_threads, const Options& options)
|
|
@@ -49,12 +51,17 @@ void Merger::add_tile(ProcessResult& result)
|
|
|
49
51
|
}
|
|
50
52
|
}
|
|
51
53
|
}
|
|
54
|
+
if (safe()) validate(result);
|
|
52
55
|
|
|
53
56
|
int end_x = this->current_x + result.width;
|
|
54
57
|
Tile* tile = new Tile(this, this->current_x, end_x, std::to_string(tiles_.size()), tiles_.size(), Benchmarks {0, 0});
|
|
55
58
|
tile->assign_raw_polygons(result.polygons, result.treemap);
|
|
56
59
|
tiles_.queue_push(tile);
|
|
57
60
|
|
|
61
|
+
if (safe()) {
|
|
62
|
+
previous_boundary_ = boundary_coordinates(result.polygons, end_x);
|
|
63
|
+
}
|
|
64
|
+
|
|
58
65
|
this->maximum_width_ = end_x;
|
|
59
66
|
this->current_x = end_x - 1;
|
|
60
67
|
}
|
|
@@ -76,3 +83,27 @@ void Merger::translate(ProcessResult& result, int offset) {
|
|
|
76
83
|
polygon.bounds.max_x += offset;
|
|
77
84
|
}
|
|
78
85
|
}
|
|
86
|
+
|
|
87
|
+
std::vector<int> Merger::boundary_coordinates(const std::list<Polygon>& polygons, int boundary_x) {
|
|
88
|
+
std::vector<int> coordinates;
|
|
89
|
+
|
|
90
|
+
for (const auto& polygon : polygons) {
|
|
91
|
+
for (const auto& point : polygon.outer) {
|
|
92
|
+
if (point.x == boundary_x) {
|
|
93
|
+
coordinates.push_back(point.y);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
std::sort(coordinates.begin(), coordinates.end());
|
|
99
|
+
return coordinates;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
void Merger::validate(const ProcessResult& result) {
|
|
103
|
+
if (tiles_.size() == 0) return;
|
|
104
|
+
|
|
105
|
+
const auto current = boundary_coordinates(result.polygons, current_x);
|
|
106
|
+
if (previous_boundary_ != current) {
|
|
107
|
+
throw std::invalid_argument("Tile boundary mismatch: adjacent tiles have different edge coordinates");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
#pragma once
|
|
11
11
|
#include <string>
|
|
12
12
|
#include <vector>
|
|
13
|
+
#include <list>
|
|
13
14
|
#include "Finder.h"
|
|
14
15
|
#include "../Options.h"
|
|
15
16
|
|
|
@@ -23,4 +24,7 @@ class Merger : public Finder {
|
|
|
23
24
|
protected:
|
|
24
25
|
void translate(ProcessResult& result, int offset);
|
|
25
26
|
int current_x = 0;
|
|
27
|
+
void validate(const ProcessResult& result);
|
|
28
|
+
static std::vector<int> boundary_coordinates(const std::list<Polygon>& polygons, int boundary_x);
|
|
29
|
+
std::vector<int> previous_boundary_;
|
|
26
30
|
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* OpencvConverter.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 "OpencvConverter.h"
|
|
11
|
+
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
#include <functional>
|
|
14
|
+
#include <limits>
|
|
15
|
+
#include <utility>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
std::size_t OpencvConverter::EdgeKeyHash::operator()(const EdgeKey& key) const {
|
|
19
|
+
return std::hash<uint64_t>{}(key.a) ^
|
|
20
|
+
(std::hash<uint64_t>{}(key.b) << 1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
uint64_t OpencvConverter::point_key(const Point& p) {
|
|
24
|
+
return (static_cast<uint64_t>(static_cast<uint32_t>(p.x)) << 32) |
|
|
25
|
+
static_cast<uint32_t>(p.y);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
OpencvConverter::EdgeKey OpencvConverter::edge_key(const Point& a, const Point& b) {
|
|
29
|
+
uint64_t ka = point_key(a);
|
|
30
|
+
uint64_t kb = point_key(b);
|
|
31
|
+
if (ka > kb) {
|
|
32
|
+
std::swap(ka, kb);
|
|
33
|
+
}
|
|
34
|
+
return {ka, kb};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void OpencvConverter::rotate_to_scanline_start(std::vector<Point>& points, const RectBounds& bounds) {
|
|
38
|
+
if (points.empty()) return;
|
|
39
|
+
|
|
40
|
+
std::size_t index = points.size();
|
|
41
|
+
int min_x = std::numeric_limits<int>::max();
|
|
42
|
+
for (std::size_t i = 0; i < points.size(); ++i) {
|
|
43
|
+
if (points[i].y == bounds.min_y && points[i].x < min_x) {
|
|
44
|
+
min_x = points[i].x;
|
|
45
|
+
index = i;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (index != points.size() && index > 0) {
|
|
49
|
+
std::rotate(points.begin(), points.begin() + index, points.end());
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* OpencvConverter.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 <vector>
|
|
13
|
+
#include <unordered_map>
|
|
14
|
+
#include <unordered_set>
|
|
15
|
+
#include <cstdint>
|
|
16
|
+
#include <stdexcept>
|
|
17
|
+
#include <cstdlib>
|
|
18
|
+
|
|
19
|
+
#include "polygon/finder/Node.h"
|
|
20
|
+
#include "RectBounds.h"
|
|
21
|
+
|
|
22
|
+
class OpencvConverter {
|
|
23
|
+
private:
|
|
24
|
+
static constexpr int chain_dir[3][3] = {
|
|
25
|
+
{3, 4, 5},
|
|
26
|
+
{2, -1, 6},
|
|
27
|
+
{1, 0, 7}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
static constexpr int corner_x[4] = {0, 1, 1, 0};
|
|
31
|
+
static constexpr int corner_y[4] = {1, 1, 0, 0};
|
|
32
|
+
|
|
33
|
+
struct BoundaryEdge {
|
|
34
|
+
Point a;
|
|
35
|
+
Point b;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
struct EdgeKey {
|
|
39
|
+
uint64_t a;
|
|
40
|
+
uint64_t b;
|
|
41
|
+
bool operator==(const EdgeKey& other) const {
|
|
42
|
+
return a == other.a && b == other.b;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
struct EdgeKeyHash {
|
|
47
|
+
std::size_t operator()(const EdgeKey& key) const;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
static uint64_t point_key(const Point& p);
|
|
51
|
+
static EdgeKey edge_key(const Point& a, const Point& b);
|
|
52
|
+
static void rotate_to_scanline_start(std::vector<Point>& points, const RectBounds& bounds);
|
|
53
|
+
|
|
54
|
+
template<typename P>
|
|
55
|
+
static int chain_direction(const P& from, const P& to) {
|
|
56
|
+
const int dx = to.x - from.x;
|
|
57
|
+
const int dy = to.y - from.y;
|
|
58
|
+
|
|
59
|
+
if (dx == 0 && dy == 0)
|
|
60
|
+
throw std::invalid_argument("Zero-length contour step");
|
|
61
|
+
|
|
62
|
+
if (std::abs(dx) > 1 || std::abs(dy) > 1)
|
|
63
|
+
throw std::invalid_argument("Non-adjacent contour step");
|
|
64
|
+
|
|
65
|
+
return chain_dir[dx + 1][dy + 1];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public:
|
|
69
|
+
template<typename P>
|
|
70
|
+
static std::vector<Point> contour_to_cell_boundary(const std::vector<P>& contour, const RectBounds& bounds) {
|
|
71
|
+
std::vector<Point> raw;
|
|
72
|
+
const std::size_t n = contour.size();
|
|
73
|
+
|
|
74
|
+
if (n == 0) {
|
|
75
|
+
return raw;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (n == 1) {
|
|
79
|
+
const int x = contour[0].x;
|
|
80
|
+
const int y = contour[0].y;
|
|
81
|
+
return {
|
|
82
|
+
Point(x, y + 1),
|
|
83
|
+
Point(x + 1, y + 1),
|
|
84
|
+
Point(x + 1, y),
|
|
85
|
+
Point(x, y)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
raw.reserve(n * 2);
|
|
90
|
+
|
|
91
|
+
for (std::size_t i = 0; i < n; ++i) {
|
|
92
|
+
const P& previous = contour[(i + n - 1) % n];
|
|
93
|
+
const P& current = contour[i];
|
|
94
|
+
const P& next = contour[(i + 1) % n];
|
|
95
|
+
const int incoming = chain_direction(previous, current);
|
|
96
|
+
const int outgoing = chain_direction(current, next);
|
|
97
|
+
int corner = incoming >> 1;
|
|
98
|
+
const int last_corner = ((outgoing + 1) >> 1) & 3;
|
|
99
|
+
for (;;) {
|
|
100
|
+
raw.emplace_back(current.x + corner_x[corner], current.y + corner_y[corner]);
|
|
101
|
+
if (corner == last_corner) {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
corner = (corner + 1) & 3;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
std::vector<BoundaryEdge> edges;
|
|
109
|
+
edges.reserve(raw.size());
|
|
110
|
+
|
|
111
|
+
for (std::size_t i = 0; i < raw.size(); ++i) {
|
|
112
|
+
const Point& a = raw[i];
|
|
113
|
+
const Point& b = raw[(i + 1) % raw.size()];
|
|
114
|
+
if (a.x == b.x && a.y == b.y) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
edges.push_back({a, b});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
std::unordered_map<EdgeKey, int, EdgeKeyHash> counts;
|
|
121
|
+
|
|
122
|
+
for (const auto& edge : edges) {
|
|
123
|
+
++counts[edge_key(edge.a, edge.b)];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
std::unordered_set<EdgeKey, EdgeKeyHash> kept;
|
|
127
|
+
std::vector<BoundaryEdge> filtered;
|
|
128
|
+
filtered.reserve(edges.size());
|
|
129
|
+
|
|
130
|
+
for (const auto& edge : edges) {
|
|
131
|
+
const EdgeKey key = edge_key(edge.a, edge.b);
|
|
132
|
+
if (((counts[key] & 1) == 0) ||
|
|
133
|
+
(kept.find(key) != kept.end())) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
kept.insert(key);
|
|
137
|
+
filtered.push_back(edge);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (filtered.empty()) {
|
|
141
|
+
return {};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
std::unordered_map<uint64_t, std::vector<std::size_t>> outgoing;
|
|
145
|
+
for (std::size_t i = 0; i < filtered.size(); ++i) {
|
|
146
|
+
outgoing[point_key(filtered[i].a)].push_back(i);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
std::vector<bool> used(filtered.size(), false);
|
|
150
|
+
std::size_t used_count = 0;
|
|
151
|
+
std::vector<Point> result;
|
|
152
|
+
result.reserve(filtered.size());
|
|
153
|
+
|
|
154
|
+
auto first_unused = [&]() {
|
|
155
|
+
for (std::size_t i = 0; i < used.size(); ++i) {
|
|
156
|
+
if (!used[i])
|
|
157
|
+
return i;
|
|
158
|
+
}
|
|
159
|
+
return used.size();
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
std::size_t current = 0;
|
|
163
|
+
|
|
164
|
+
while (used_count < filtered.size()) {
|
|
165
|
+
if (used[current]) {
|
|
166
|
+
current = first_unused();
|
|
167
|
+
if (current == used.size()) {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const BoundaryEdge& edge = filtered[current];
|
|
173
|
+
|
|
174
|
+
used[current] = true;
|
|
175
|
+
++used_count;
|
|
176
|
+
|
|
177
|
+
result.push_back(edge.a);
|
|
178
|
+
|
|
179
|
+
const uint64_t finish = point_key(edge.b);
|
|
180
|
+
|
|
181
|
+
auto it = outgoing.find(finish);
|
|
182
|
+
std::size_t next_edge = filtered.size();
|
|
183
|
+
|
|
184
|
+
if (it != outgoing.end()) {
|
|
185
|
+
for (std::size_t candidate : it->second) {
|
|
186
|
+
if (!used[candidate]) {
|
|
187
|
+
next_edge = candidate;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (next_edge != filtered.size()) {
|
|
194
|
+
current = next_edge;
|
|
195
|
+
} else {
|
|
196
|
+
current = first_unused();
|
|
197
|
+
if (current == used.size())
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
rotate_to_scanline_start(result, bounds);
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
@@ -571,6 +571,7 @@ void Init_cpp_polygon_finder() {
|
|
|
571
571
|
rb_cstd_ofstream.define_method("closed?", [](OfstreamWrapper& self) { return self.closed(); });
|
|
572
572
|
rb_cstd_ofstream.define_method("rewind", [](OfstreamWrapper& self) { self.rewind(); });
|
|
573
573
|
rb_cstd_ofstream.define_method("read", [](OfstreamWrapper& self) { return self.read(); });
|
|
574
|
+
rb_cstd_ofstream.define_method("path", [](OfstreamWrapper& self) { return self.path; });
|
|
574
575
|
|
|
575
576
|
Data_Type<StreamingMerger> rb_cstreaming_merger = define_class<StreamingMerger, VerticalMerger>("CPPStreamingMerger");
|
|
576
577
|
rb_cstreaming_merger.define_method("add_tile", [](StreamingMerger& self, Object rb_result, bool flush) {
|
|
@@ -13,6 +13,7 @@ module Contrek
|
|
|
13
13
|
@current_x = 0
|
|
14
14
|
@tiles = Queue.new
|
|
15
15
|
@whole_tile = nil
|
|
16
|
+
@previous_boundary = nil
|
|
16
17
|
@user_options = options
|
|
17
18
|
if (warn_options = @user_options.keys & IGNORE_OPTIONS).any?
|
|
18
19
|
warn "[Contrek WARNING] The given options '#{warn_options.join(",")}' will be ignored."
|
|
@@ -28,12 +29,13 @@ module Contrek
|
|
|
28
29
|
raise ArgumentError, "All results must have the same height" if @height != result.metadata[:height] && safe?
|
|
29
30
|
@options[:versus] ||= result.metadata[:versus]
|
|
30
31
|
raise ArgumentError, "All results must have the same versus option" if @options[:versus] != result.metadata[:versus] && safe?
|
|
31
|
-
if safe? && result.metadata[:options]
|
|
32
|
+
if safe? && result.metadata[:options]&.key?(:compress)
|
|
32
33
|
if (result.metadata[:options][:compress].keys - [:uniq, :linear]).any?
|
|
33
34
|
raise ArgumentError, "Result with not supported postprocessing compression mode"
|
|
34
35
|
end
|
|
35
36
|
end
|
|
36
37
|
|
|
38
|
+
validate(result) if safe?
|
|
37
39
|
end_x = @current_x + result.metadata[:width]
|
|
38
40
|
|
|
39
41
|
tile = Tile.new(
|
|
@@ -45,6 +47,9 @@ module Contrek
|
|
|
45
47
|
tile.assign_raw_polygons!(result[:polygons])
|
|
46
48
|
|
|
47
49
|
@tiles << tile
|
|
50
|
+
|
|
51
|
+
@previous_boundary = boundary_coordinates(result.polygons, end_x) if safe?
|
|
52
|
+
|
|
48
53
|
@maximum_width = end_x
|
|
49
54
|
@current_x = end_x - 1
|
|
50
55
|
end
|
|
@@ -60,6 +65,25 @@ module Contrek
|
|
|
60
65
|
|
|
61
66
|
private
|
|
62
67
|
|
|
68
|
+
def validate(result)
|
|
69
|
+
return unless @previous_boundary
|
|
70
|
+
|
|
71
|
+
current = boundary_coordinates(result.polygons, @current_x)
|
|
72
|
+
unless @previous_boundary == current
|
|
73
|
+
raise ArgumentError, "Tile boundary mismatch: adjacent tiles have different edge coordinates"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def boundary_coordinates(polygons, boundary_x)
|
|
78
|
+
coordinates = []
|
|
79
|
+
polygons.each do |polygon|
|
|
80
|
+
polygon[:outer].each do |point|
|
|
81
|
+
coordinates << point[:y] if point[:x] == boundary_x
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
coordinates.sort!
|
|
85
|
+
end
|
|
86
|
+
|
|
63
87
|
def translate(result, offset)
|
|
64
88
|
result.polygons.each do |polygon|
|
|
65
89
|
polygon[:outer].each { |p| p[:x] += offset }
|
data/lib/contrek/version.rb
CHANGED
data/lib/contrek.rb
CHANGED
|
@@ -51,7 +51,6 @@ require "contrek/finder/concurrent/streaming_merger"
|
|
|
51
51
|
require "contrek/finder/concurrent/svg_streaming_merger"
|
|
52
52
|
require "contrek/finder/concurrent/geo_json_streaming_merger"
|
|
53
53
|
require "contrek/finder/concurrent/cursor"
|
|
54
|
-
require "contrek/map/mercator_projection"
|
|
55
54
|
require "contrek/matchers/matcher"
|
|
56
55
|
require "contrek/matchers/matcher_hsb"
|
|
57
56
|
require "contrek/matchers/value_not_matcher"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: contrek
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.4.
|
|
4
|
+
version: 1.4.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Emanuele Cesaroni
|
|
@@ -263,6 +263,8 @@ files:
|
|
|
263
263
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/UniqReducer.h
|
|
264
264
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/VisvalingamReducer.cpp
|
|
265
265
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/reducers/VisvalingamReducer.h
|
|
266
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/shared/OpencvConverter.cpp
|
|
267
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/shared/OpencvConverter.h
|
|
266
268
|
- ext/cpp_polygon_finder/cpp_polygon_finder.cpp
|
|
267
269
|
- ext/cpp_polygon_finder/extconf.rb
|
|
268
270
|
- lib/LICENSE-MIT.md
|
|
@@ -323,7 +325,6 @@ files:
|
|
|
323
325
|
- lib/contrek/finder/node_cluster.rb
|
|
324
326
|
- lib/contrek/finder/polygon_finder.rb
|
|
325
327
|
- lib/contrek/finder/result.rb
|
|
326
|
-
- lib/contrek/map/mercator_projection.rb
|
|
327
328
|
- lib/contrek/matchers/matcher.rb
|
|
328
329
|
- lib/contrek/matchers/matcher_hsb.rb
|
|
329
330
|
- lib/contrek/matchers/value_not_matcher.rb
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Map
|
|
4
|
-
class MercatorProjection
|
|
5
|
-
MERCATOR_RANGE = 256
|
|
6
|
-
|
|
7
|
-
class LatLng
|
|
8
|
-
attr_reader :lat, :lng
|
|
9
|
-
def initialize(lat:, lng:)
|
|
10
|
-
@lat = lat
|
|
11
|
-
@lng = lng
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
class Point
|
|
16
|
-
attr_accessor :x, :y
|
|
17
|
-
def initialize(x: 0, y: 0)
|
|
18
|
-
@x = x
|
|
19
|
-
@y = y
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def initialize
|
|
24
|
-
@pixel_origin = Point.new(x: MERCATOR_RANGE / 2, y: MERCATOR_RANGE / 2)
|
|
25
|
-
@pixels_per_lon_degree = MERCATOR_RANGE.to_f / 360
|
|
26
|
-
@pixels_per_lon_radian = MERCATOR_RANGE.to_f / (2 * Math::PI)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def self.get_corners(center, zoom, map_width, map_height)
|
|
30
|
-
scale = 2**zoom
|
|
31
|
-
proj = Map::MercatorProjection.new
|
|
32
|
-
center_px = proj.from_lat_lng_to_point(lat_lng: center)
|
|
33
|
-
sw_point = Point.new(x: center_px.x - ((map_width / 2).to_f / scale), y: center_px.y + ((map_height / 2).to_f / scale))
|
|
34
|
-
sw_lat_lon = proj.from_point_to_lat_lng(sw_point)
|
|
35
|
-
ne_point = Point.new(x: center_px.x + ((map_width / 2).to_f / scale), y: center_px.y - ((map_height / 2).to_f / scale))
|
|
36
|
-
ne_lat_lon = proj.from_point_to_lat_lng(ne_point)
|
|
37
|
-
{
|
|
38
|
-
N: ne_lat_lon.lat,
|
|
39
|
-
E: ne_lat_lon.lng,
|
|
40
|
-
S: sw_lat_lon.lat,
|
|
41
|
-
W: sw_lat_lon.lng
|
|
42
|
-
}
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def from_lat_lng_to_point(lat_lng:, opt_point: nil)
|
|
46
|
-
point = opt_point.nil? ? Point.new(x: 0, y: 0) : opt_point
|
|
47
|
-
origin = @pixel_origin
|
|
48
|
-
point.x = origin.x + (lat_lng.lng * @pixels_per_lon_degree)
|
|
49
|
-
siny = bound(Math.sin(degreesToRadians(lat_lng.lat)), -0.9999, 0.9999)
|
|
50
|
-
point.y = origin.y + (0.5 * Math.log((1 + siny) / (1 - siny)) * -@pixels_per_lon_radian)
|
|
51
|
-
point
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def from_point_to_lat_lng(point)
|
|
55
|
-
origin = @pixel_origin
|
|
56
|
-
lng = (point.x - origin.x) / @pixels_per_lon_degree
|
|
57
|
-
lat_radians = (point.y - origin.y) / -@pixels_per_lon_radian
|
|
58
|
-
lat = radiansToDegrees(2 * Math.atan(Math.exp(lat_radians)) - (Math::PI / 2))
|
|
59
|
-
LatLng.new(lat: lat, lng: lng)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
private
|
|
63
|
-
|
|
64
|
-
def degreesToRadians(deg)
|
|
65
|
-
deg * (Math::PI / 180)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def radiansToDegrees(rad)
|
|
69
|
-
rad / (Math::PI / 180)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def bound(value, opt_min, opt_max)
|
|
73
|
-
value = [value, opt_min].max if !opt_min.nil?
|
|
74
|
-
value = [value, opt_max].min if !opt_max.nil?
|
|
75
|
-
value
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|