contrek 1.4.8 → 1.4.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a01ebf8c61e86b070d300b47d3e0f82cc8a4912bbaf738af24c080bd81d2763d
4
- data.tar.gz: 5661528101dc8492975b3a4dc7765f02b0ae884d6ba7fecbaf132676b53f4b9f
3
+ metadata.gz: c5621bef598d115df881a8f9f7feee207416031ec0cc6d2c1e2035987386c925
4
+ data.tar.gz: d1936f5a1a98ef42476e8037e8063daa7cacefce5e52ce6610703d92c88e001e
5
5
  SHA512:
6
- metadata.gz: 702ea4857ad48e5817d4baa3c33b1183094720a1a8ffa80b7830e85f8b7a80f4e1a4b994f71eb1f5f1ccc91bf71cab874e90e7196f768dd6fca9e28222e7a106
7
- data.tar.gz: 1bec64c8a85057c87b0b46920ff09503c815cfef8992c0e06d1eace83c43ae8def25dab9529464f12c7c65085479514756fe924b0e1ad9c1e0a8f8fbd56fb3a7
6
+ metadata.gz: a12042f6e90d1abef2d41411e4b705f73beb19f93e19fa9105695fde518fd50bf447ff65e1f99d99a973a5969b1056f7bed35742de1cf9d05ae6c6d43c64671d
7
+ data.tar.gz: de06793e38f5bb28633fd00e8b447aff188e93e52a031ecfd2f492fe63c77d0cf41d1b1cc9ff03ce7a69d63a0a20d169cf6126393c911a51268b555c03c684d8
data/CHANGELOG.md CHANGED
@@ -176,3 +176,10 @@ All notable changes to this project will be documented in this file.
176
176
 
177
177
  ## [1.4.7] - 2026-11-07
178
178
  - Added multiclass raster polygonization support, preserving class IDs throughout polygon extraction and tile merging, including streaming mergers.
179
+
180
+ ## [1.4.8] - 2026-11-11
181
+ - Fixed premature inner contour termination with 8-connectivity.
182
+ - Added TIFF and GeoTIFF build dependencies.
183
+
184
+ ## [1.4.9] - 2026-11-21
185
+ - Support for georeferenced GeoJSON output for EPSG:5070 (NAD83 / Conus Albers).
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- contrek (1.4.8)
4
+ contrek (1.4.9)
5
5
  chunky_png (~> 1.4)
6
6
  concurrent-ruby (~> 1.3.5)
7
7
  ffi (~> 1.17)
@@ -10,6 +10,7 @@
10
10
  #pragma once
11
11
 
12
12
  #include <string>
13
+ #include "../../finder/Options.h"
13
14
 
14
15
  struct GeoTransform {
15
16
  double x_origin = 0.0;
@@ -29,4 +30,23 @@ struct GeoLocalization {
29
30
  bool valid = false;
30
31
  GeoTransform transform;
31
32
  GeoCrs crs;
33
+ Options to_options() const {
34
+ if (!valid) {
35
+ throw std::runtime_error("Invalid geolocalization");
36
+ }
37
+ return Options{
38
+ {"transform", Options{
39
+ {"x_origin", transform.x_origin},
40
+ {"y_origin", transform.y_origin},
41
+ {"x_pixel_size", transform.x_pixel_size},
42
+ {"y_pixel_size", transform.y_pixel_size},
43
+ {"x_row_offset", transform.x_row_offset},
44
+ {"y_column_offset", transform.y_column_offset}
45
+ }},
46
+ {"crs", Options{
47
+ {"authority", crs.authority},
48
+ {"code", std::int64_t{crs.code}}
49
+ }}
50
+ };
51
+ }
32
52
  };
@@ -8,90 +8,145 @@
8
8
  */
9
9
 
10
10
  #pragma once
11
+
11
12
  #include <vector>
12
13
  #include <string>
13
- #include <functional>
14
14
  #include <stdexcept>
15
- #include <iomanip>
16
- #include <limits>
15
+ #include <cstdint>
16
+ #include <charconv>
17
+ #include <system_error>
18
+ #include <fstream>
19
+
17
20
  #include "StreamingMerger.h"
18
21
 
19
22
  class GeoJsonStreamingMerger : public StreamingMerger {
20
23
  private:
21
24
  unsigned int target_value;
22
25
  bool is_first_feature = true;
23
- std::function<void(const Point&)> point_writer;
26
+ bool use_geo_localization = false;
24
27
 
25
- void configure_point_writer(const Options& options) {
28
+ double x_origin = 0.0;
29
+ double y_origin = 0.0;
30
+ double x_pixel_size = 1.0;
31
+ double y_pixel_size = 1.0;
32
+ double x_row_offset = 0.0;
33
+ double y_column_offset = 0.0;
34
+
35
+ void configure_geo(const Options& options) {
26
36
  const Options* geo_localization = options.get_options("geo_localization");
27
37
  if (!geo_localization) {
28
- point_writer = [this](const Point& point) { *stream << "[" << point.y << "," << point.x << "]"; };
38
+ use_geo_localization = false;
29
39
  return;
30
40
  }
31
41
 
32
42
  const Options* transform = geo_localization->get_options("transform");
33
43
  const Options* crs = geo_localization->get_options("crs");
34
- if (!transform || !crs) throw std::invalid_argument("Invalid geo_localization.");
44
+ if (!transform || !crs) {
45
+ throw std::invalid_argument("Invalid geo_localization.");
46
+ }
35
47
 
36
48
  const std::string authority = crs->get<std::string>("authority", "");
37
49
  const auto code = crs->get("code", std::int64_t{0});
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
- };
50
+ if (authority != "EPSG" || code <= 0) {
51
+ throw std::invalid_argument("Unsupported CRS: " + authority + ":" + std::to_string(code));
52
+ }
53
+
54
+ x_origin = transform->get<double>("x_origin", 0.0);
55
+ y_origin = transform->get<double>("y_origin", 0.0);
56
+ x_pixel_size = transform->get<double>("x_pixel_size", 1.0);
57
+ y_pixel_size = transform->get<double>("y_pixel_size", 1.0);
58
+ x_row_offset = transform->get<double>("x_row_offset", 0.0);
59
+ y_column_offset = transform->get<double>("y_column_offset", 0.0);
60
+ use_geo_localization = true;
53
61
  }
54
62
 
55
63
  void write_point(const Point& point) {
56
- point_writer(point);
64
+ if (!stream) return;
65
+
66
+ if (!use_geo_localization) {
67
+ char buffer[128];
68
+ char* ptr = buffer;
69
+ char* const end = buffer + sizeof(buffer);
70
+ *ptr++ = '[';
71
+
72
+ auto r1 = std::to_chars(ptr, end, point.y);
73
+ if (r1.ec != std::errc{}) throw std::runtime_error("Failed to format pixel x coordinate");
74
+ ptr = r1.ptr;
75
+ *ptr++ = ',';
76
+
77
+ auto r2 = std::to_chars(ptr, end, point.x);
78
+ if (r2.ec != std::errc{}) throw std::runtime_error("Failed to format pixel y coordinate");
79
+ ptr = r2.ptr;
80
+ *ptr++ = ']';
81
+
82
+ stream->write(buffer, static_cast<std::streamsize>(ptr - buffer));
83
+ return;
84
+ }
85
+
86
+ const double x = point.y;
87
+ const double y = point.x;
88
+ const double geo_x = x_origin + x * x_pixel_size + y * x_row_offset;
89
+ const double geo_y = y_origin + x * y_column_offset + y * y_pixel_size;
90
+
91
+ char buffer[128];
92
+ char* ptr = buffer;
93
+ char* const end = buffer + sizeof(buffer);
94
+ *ptr++ = '[';
95
+
96
+ auto r1 = std::to_chars(ptr, end, geo_x, std::chars_format::fixed, 7);
97
+ if (r1.ec != std::errc{}) {
98
+ throw std::runtime_error("Failed to format geo x coordinate");
99
+ }
100
+
101
+ ptr = r1.ptr;
102
+ *ptr++ = ',';
103
+
104
+ auto r2 = std::to_chars(ptr, end, geo_y, std::chars_format::fixed, 7);
105
+ if (r2.ec != std::errc{}) {
106
+ throw std::runtime_error("Failed to format geo y coordinate");
107
+ }
108
+
109
+ ptr = r2.ptr;
110
+ *ptr++ = ']';
111
+ stream->write(buffer, static_cast<std::streamsize>(ptr - buffer));
57
112
  }
58
113
 
59
114
  protected:
60
115
  void write_header() override {
61
- if (stream) {
62
- *stream << "{\"type\":\"FeatureCollection\",\"features\":[";
63
- }
116
+ if (stream) *stream << "{\"type\":\"FeatureCollection\",\"features\":[";
64
117
  }
65
118
 
66
119
  void write_footer() override {
67
- if (stream) {
68
- *stream << "]}";
69
- }
120
+ if (stream) *stream << "]}";
70
121
  }
122
+
71
123
  void write_outer_polygon_start() override {}
72
124
  void write_outer_polygon_end() override {}
73
125
  void write_inner_polygon_start() override {}
74
126
  void write_inner_polygon_end() override {}
75
127
 
76
128
  public:
77
- GeoJsonStreamingMerger(int number_of_threads,
78
- const Options& options,
79
- std::ofstream* stream_to)
129
+ GeoJsonStreamingMerger(int number_of_threads, const Options& options, std::ofstream* stream_to)
80
130
  : StreamingMerger(number_of_threads, options, stream_to) {
81
- configure_point_writer(options);
131
+ configure_geo(options);
82
132
  }
83
133
 
84
134
  void stream_raw_polygon(const Polygon& polygon) override {
85
135
  if (!stream) return;
86
- if (!is_first_feature) {
87
- *stream << ",";
88
- }
136
+
137
+ if (!is_first_feature) *stream << ",";
89
138
  is_first_feature = false;
90
139
 
91
- *stream << "{\"type\":\"Feature\",\"properties\":{\"PixelVal\":" << polygon.class_id
92
- << "},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[";
140
+ *stream << "{\"type\":\"Feature\",\"properties\":{\"PixelVal\":";
141
+ char class_buffer[32];
142
+ auto class_result = std::to_chars(class_buffer, class_buffer + sizeof(class_buffer), polygon.class_id);
143
+ if (class_result.ec != std::errc{}) throw std::runtime_error("Failed to format PixelVal");
144
+ stream->write(class_buffer, static_cast<std::streamsize>(class_result.ptr - class_buffer));
145
+ *stream << "},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[";
146
+
93
147
  const std::vector<Point>& points = polygon.outer;
94
148
  const size_t points_size = points.size();
149
+
95
150
  if (points_size > 0) {
96
151
  for (size_t i = 0; i < points_size; ++i) {
97
152
  write_point(points[i]);
@@ -117,6 +172,7 @@ class GeoJsonStreamingMerger : public StreamingMerger {
117
172
  *stream << "]";
118
173
  }
119
174
  }
175
+
120
176
  *stream << "]}}";
121
177
  }
122
178
  };
@@ -20,6 +20,7 @@ module Contrek
20
20
  TIFFTAG_PHOTOMETRIC = 262
21
21
  TIFFTAG_SAMPLESPERPIXEL = 277
22
22
  TIFFTAG_PLANARCONFIG = 284
23
+ TIFFTAG_COLORMAP = 320
23
24
  TIFFTAG_SAMPLEFORMAT = 339
24
25
  TIFFTAG_MODELPIXELSCALE = 33550
25
26
  TIFFTAG_MODELTIEPOINT = 33922
@@ -28,6 +29,7 @@ module Contrek
28
29
  PHOTOMETRIC_MINISWHITE = 0
29
30
  PHOTOMETRIC_MINISBLACK = 1
30
31
  PHOTOMETRIC_RGB = 2
32
+ PHOTOMETRIC_PALETTE = 3
31
33
 
32
34
  PLANARCONFIG_CONTIG = 1
33
35
 
@@ -77,6 +79,7 @@ module Contrek
77
79
  @sample_format = read_uint16(LibTiff::TIFFTAG_SAMPLEFORMAT, defaulted: true)
78
80
 
79
81
  validate_format
82
+ read_palette if @photometric == LibTiff::PHOTOMETRIC_PALETTE
80
83
 
81
84
  @source_row_size = LibTiff.TIFFScanlineSize64(@tiff)
82
85
  @source_row = FFI::MemoryPointer.new(:uint8, @source_row_size)
@@ -144,13 +147,50 @@ module Contrek
144
147
  unless [
145
148
  LibTiff::PHOTOMETRIC_MINISWHITE,
146
149
  LibTiff::PHOTOMETRIC_MINISBLACK,
147
- LibTiff::PHOTOMETRIC_RGB
150
+ LibTiff::PHOTOMETRIC_RGB,
151
+ LibTiff::PHOTOMETRIC_PALETTE
148
152
  ].include?(@photometric)
149
153
  raise "Unsupported TIFF photometric: #{@photometric}"
150
154
  end
155
+
156
+ if @photometric == LibTiff::PHOTOMETRIC_PALETTE &&
157
+ (@samples_per_pixel != 1 || @bits_per_sample != 8)
158
+ raise "Unsupported TIFF palette format"
159
+ end
160
+ end
161
+
162
+ def read_palette
163
+ red = FFI::MemoryPointer.new(:pointer)
164
+ green = FFI::MemoryPointer.new(:pointer)
165
+ blue = FFI::MemoryPointer.new(:pointer)
166
+
167
+ ret = LibTiff.TIFFGetField(
168
+ @tiff, LibTiff::TIFFTAG_COLORMAP,
169
+ :pointer, red, :pointer, green, :pointer, blue
170
+ )
171
+ raise "Unable to read TIFF color map" unless ret == 1
172
+
173
+ red_ptr = red.read_pointer
174
+ green_ptr = green.read_pointer
175
+ blue_ptr = blue.read_pointer
176
+
177
+ @palette = Array.new(256) do |i|
178
+ offset = i * 2
179
+ [
180
+ red_ptr.get_uint16(offset) >> 8,
181
+ green_ptr.get_uint16(offset) >> 8,
182
+ blue_ptr.get_uint16(offset) >> 8,
183
+ 255
184
+ ]
185
+ end
151
186
  end
152
187
 
153
188
  def convert_row(destination)
189
+ if @photometric == LibTiff::PHOTOMETRIC_PALETTE
190
+ convert_palette8_row(destination)
191
+ return
192
+ end
193
+
154
194
  case [@samples_per_pixel, @bits_per_sample]
155
195
  when [1, 8]
156
196
  convert_gray8_row(destination)
@@ -169,6 +209,19 @@ module Contrek
169
209
  end
170
210
  end
171
211
 
212
+ def convert_palette8_row(dest)
213
+ @width.times do |x|
214
+ index = @source_row.get_uint8(x)
215
+ rgba = @palette[index]
216
+ dest_offset = x * 4
217
+
218
+ dest.put_uint8(dest_offset, rgba[0])
219
+ dest.put_uint8(dest_offset + 1, rgba[1])
220
+ dest.put_uint8(dest_offset + 2, rgba[2])
221
+ dest.put_uint8(dest_offset + 3, rgba[3])
222
+ end
223
+ end
224
+
172
225
  def convert_rgba8_row(dest)
173
226
  dest.put_bytes(0, @source_row.read_bytes(@width * 4))
174
227
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Contrek
4
- VERSION = "1.4.8"
4
+ VERSION = "1.4.9"
5
5
  end
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.8
4
+ version: 1.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emanuele Cesaroni