contrek 1.4.1 → 1.4.3
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 +6 -0
- data/Gemfile.lock +4 -1
- data/README.md +218 -38
- data/contrek.gemspec +1 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/ContrekApi.h +2 -2
- data/ext/cpp_polygon_finder/PolygonFinder/src/Tests.cpp +52 -148
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/RawBitmap.cpp +12 -4
- 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 +46 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/AsciiSource.h +29 -0
- 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 +103 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/PngSource.h +40 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/RasterSource.h +22 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/RasterStreamer.h +92 -0
- 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/FinderUtils.cpp +6 -8
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/PolygonFinder.cpp +7 -2
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/PolygonFinder.h +3 -1
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/Finder.cpp +6 -0
- data/ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/concurrent/GeoJsonStreamingMerger.h +51 -6
- data/ext/cpp_polygon_finder/cpp_polygon_finder.cpp +83 -0
- data/ext/cpp_polygon_finder/extconf.rb +9 -0
- data/lib/contrek/bitmaps/bitmap.rb +2 -2
- data/lib/contrek/bitmaps/chunky_bitmap.rb +23 -8
- data/lib/contrek/bitmaps/png_bitmap.rb +4 -0
- data/lib/contrek/bitmaps/rgba_bitmap.rb +50 -0
- data/lib/contrek/bitmaps/streaming/raster_streamer.rb +66 -0
- data/lib/contrek/bitmaps/streaming/sources/ascii.rb +33 -0
- data/lib/contrek/bitmaps/streaming/sources/base.rb +27 -0
- data/lib/contrek/bitmaps/streaming/sources/png.rb +105 -0
- data/lib/contrek/bitmaps/streaming/sources/tiff.rb +349 -0
- data/lib/contrek/finder/concurrent/finder.rb +3 -2
- data/lib/contrek/finder/concurrent/geo_json_streaming_merger.rb +19 -2
- data/lib/contrek/finder/polygon_finder.rb +6 -3
- data/lib/contrek/version.rb +1 -1
- data/lib/contrek.rb +6 -0
- metadata +30 -1
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ffi"
|
|
4
|
+
|
|
5
|
+
module Contrek
|
|
6
|
+
module Bitmaps
|
|
7
|
+
module Streaming
|
|
8
|
+
module Sources
|
|
9
|
+
class Tiff
|
|
10
|
+
attr_reader :width, :height, :geo_localization
|
|
11
|
+
|
|
12
|
+
module LibTiff
|
|
13
|
+
extend FFI::Library
|
|
14
|
+
|
|
15
|
+
ffi_lib "tiff"
|
|
16
|
+
|
|
17
|
+
TIFFTAG_IMAGEWIDTH = 256
|
|
18
|
+
TIFFTAG_IMAGELENGTH = 257
|
|
19
|
+
TIFFTAG_BITSPERSAMPLE = 258
|
|
20
|
+
TIFFTAG_PHOTOMETRIC = 262
|
|
21
|
+
TIFFTAG_SAMPLESPERPIXEL = 277
|
|
22
|
+
TIFFTAG_PLANARCONFIG = 284
|
|
23
|
+
TIFFTAG_SAMPLEFORMAT = 339
|
|
24
|
+
TIFFTAG_MODELPIXELSCALE = 33550
|
|
25
|
+
TIFFTAG_MODELTIEPOINT = 33922
|
|
26
|
+
TIFFTAG_MODELTRANSFORMATION = 34264
|
|
27
|
+
|
|
28
|
+
PHOTOMETRIC_MINISWHITE = 0
|
|
29
|
+
PHOTOMETRIC_MINISBLACK = 1
|
|
30
|
+
PHOTOMETRIC_RGB = 2
|
|
31
|
+
|
|
32
|
+
PLANARCONFIG_CONTIG = 1
|
|
33
|
+
|
|
34
|
+
SAMPLEFORMAT_UINT = 1
|
|
35
|
+
|
|
36
|
+
attach_function :TIFFOpen, [:string, :string], :pointer
|
|
37
|
+
attach_function :TIFFClose, [:pointer], :void
|
|
38
|
+
attach_function :TIFFGetField, [:pointer, :uint32, :varargs], :int
|
|
39
|
+
attach_function :TIFFGetFieldDefaulted, [:pointer, :uint32, :varargs], :int
|
|
40
|
+
attach_function :TIFFScanlineSize64, [:pointer], :uint64
|
|
41
|
+
attach_function :TIFFReadScanline, [:pointer, :pointer, :uint32, :uint16], :int
|
|
42
|
+
attach_function :TIFFIsTiled, [:pointer], :int
|
|
43
|
+
attach_function :TIFFSetWarningHandler, [:pointer], :pointer
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
module LibGeoTiff
|
|
47
|
+
extend FFI::Library
|
|
48
|
+
|
|
49
|
+
ffi_lib "geotiff"
|
|
50
|
+
|
|
51
|
+
GTMODELTYPEGEOKEY = 1024
|
|
52
|
+
GEOGRAPHICTYPEGEOKEY = 2048
|
|
53
|
+
PROJECTEDCSTYPEGEOKEY = 3072
|
|
54
|
+
|
|
55
|
+
MODELTYPE_PROJECTED = 1
|
|
56
|
+
MODELTYPE_GEOGRAPHIC = 2
|
|
57
|
+
|
|
58
|
+
attach_function :XTIFFOpen, [:string, :string], :pointer
|
|
59
|
+
attach_function :GTIFNew, [:pointer], :pointer
|
|
60
|
+
attach_function :GTIFFree, [:pointer], :void
|
|
61
|
+
attach_function :GTIFKeyGetSHORT, [:pointer, :int, :pointer, :int, :int], :int
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def initialize(filepath, suppress_warnings: false)
|
|
65
|
+
@suppress_warnings = suppress_warnings
|
|
66
|
+
disable_warnings! if @suppress_warnings
|
|
67
|
+
|
|
68
|
+
@tiff = LibGeoTiff.XTIFFOpen(filepath, "r")
|
|
69
|
+
raise "Unable to open TIFF: #{filepath}" if @tiff.null?
|
|
70
|
+
|
|
71
|
+
@width = read_uint32(LibTiff::TIFFTAG_IMAGEWIDTH)
|
|
72
|
+
@height = read_uint32(LibTiff::TIFFTAG_IMAGELENGTH)
|
|
73
|
+
@bits_per_sample = read_uint16(LibTiff::TIFFTAG_BITSPERSAMPLE, defaulted: true)
|
|
74
|
+
@samples_per_pixel = read_uint16(LibTiff::TIFFTAG_SAMPLESPERPIXEL, defaulted: true)
|
|
75
|
+
@photometric = read_uint16(LibTiff::TIFFTAG_PHOTOMETRIC)
|
|
76
|
+
@planar_config = read_uint16(LibTiff::TIFFTAG_PLANARCONFIG, defaulted: true)
|
|
77
|
+
@sample_format = read_uint16(LibTiff::TIFFTAG_SAMPLEFORMAT, defaulted: true)
|
|
78
|
+
|
|
79
|
+
validate_format
|
|
80
|
+
|
|
81
|
+
@source_row_size = LibTiff.TIFFScanlineSize64(@tiff)
|
|
82
|
+
@source_row = FFI::MemoryPointer.new(:uint8, @source_row_size)
|
|
83
|
+
@current_row = 0
|
|
84
|
+
@geo_localization = read_coordinates
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def get_bytes_per_pixel
|
|
88
|
+
4
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def read_next_row(destination, row_size)
|
|
92
|
+
return false if @current_row >= @height
|
|
93
|
+
|
|
94
|
+
expected_row_size = @width * get_bytes_per_pixel
|
|
95
|
+
raise "Invalid TIFF destination row size: #{row_size}" unless row_size == expected_row_size
|
|
96
|
+
|
|
97
|
+
ret = LibTiff.TIFFReadScanline(@tiff, @source_row, @current_row, 0)
|
|
98
|
+
raise "TIFFReadScanline failed at row #{@current_row}" if ret < 0
|
|
99
|
+
|
|
100
|
+
convert_row(destination)
|
|
101
|
+
|
|
102
|
+
@current_row += 1
|
|
103
|
+
true
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def close
|
|
107
|
+
unless @gtif.nil? || @gtif.null?
|
|
108
|
+
LibGeoTiff.GTIFFree(@gtif)
|
|
109
|
+
@gtif = nil
|
|
110
|
+
end
|
|
111
|
+
unless @tiff.nil? || @tiff.null?
|
|
112
|
+
LibTiff.TIFFClose(@tiff)
|
|
113
|
+
@tiff = nil
|
|
114
|
+
end
|
|
115
|
+
if @suppress_warnings
|
|
116
|
+
LibTiff.TIFFSetWarningHandler(@previous_warning_handler)
|
|
117
|
+
@previous_warning_handler = nil
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
private
|
|
122
|
+
|
|
123
|
+
def validate_format
|
|
124
|
+
if LibTiff.TIFFIsTiled(@tiff) != 0
|
|
125
|
+
raise "Tiled TIFF is not supported yet"
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
unless @planar_config == LibTiff::PLANARCONFIG_CONTIG
|
|
129
|
+
raise "Unsupported TIFF planar configuration: #{@planar_config}"
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
unless [8, 16].include?(@bits_per_sample)
|
|
133
|
+
raise "Unsupported TIFF bits per sample: #{@bits_per_sample}"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
unless [1, 3, 4].include?(@samples_per_pixel)
|
|
137
|
+
raise "Unsupported TIFF samples per pixel: #{@samples_per_pixel}"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
unless @sample_format == LibTiff::SAMPLEFORMAT_UINT
|
|
141
|
+
raise "Unsupported TIFF sample format: #{@sample_format}"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
unless [
|
|
145
|
+
LibTiff::PHOTOMETRIC_MINISWHITE,
|
|
146
|
+
LibTiff::PHOTOMETRIC_MINISBLACK,
|
|
147
|
+
LibTiff::PHOTOMETRIC_RGB
|
|
148
|
+
].include?(@photometric)
|
|
149
|
+
raise "Unsupported TIFF photometric: #{@photometric}"
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def convert_row(destination)
|
|
154
|
+
case [@samples_per_pixel, @bits_per_sample]
|
|
155
|
+
when [1, 8]
|
|
156
|
+
convert_gray8_row(destination)
|
|
157
|
+
when [1, 16]
|
|
158
|
+
convert_gray16_row(destination)
|
|
159
|
+
when [3, 8]
|
|
160
|
+
convert_rgb8_row(destination)
|
|
161
|
+
when [3, 16]
|
|
162
|
+
convert_rgb16_row(destination)
|
|
163
|
+
when [4, 8]
|
|
164
|
+
convert_rgba8_row(destination)
|
|
165
|
+
when [4, 16]
|
|
166
|
+
convert_rgba16_row(destination)
|
|
167
|
+
else
|
|
168
|
+
raise "Unsupported TIFF pixel format"
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def convert_rgba8_row(dest)
|
|
173
|
+
dest.put_bytes(0, @source_row.read_bytes(@width * 4))
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def convert_rgb8_row(dest)
|
|
177
|
+
@width.times do |x|
|
|
178
|
+
source_offset = x * 3
|
|
179
|
+
dest_offset = x * 4
|
|
180
|
+
dest.put_uint8(dest_offset, @source_row.get_uint8(source_offset))
|
|
181
|
+
dest.put_uint8(dest_offset + 1, @source_row.get_uint8(source_offset + 1))
|
|
182
|
+
dest.put_uint8(dest_offset + 2, @source_row.get_uint8(source_offset + 2))
|
|
183
|
+
dest.put_uint8(dest_offset + 3, 255)
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def convert_gray8_row(dest)
|
|
188
|
+
@width.times do |x|
|
|
189
|
+
value = @source_row.get_uint8(x)
|
|
190
|
+
value = 255 - value if @photometric == LibTiff::PHOTOMETRIC_MINISWHITE
|
|
191
|
+
dest_offset = x * 4
|
|
192
|
+
dest.put_uint8(dest_offset, value)
|
|
193
|
+
dest.put_uint8(dest_offset + 1, value)
|
|
194
|
+
dest.put_uint8(dest_offset + 2, value)
|
|
195
|
+
dest.put_uint8(dest_offset + 3, 255)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def convert_gray16_row(dest)
|
|
200
|
+
@width.times do |x|
|
|
201
|
+
value = @source_row.get_uint16(x * 2) >> 8
|
|
202
|
+
value = 255 - value if @photometric == LibTiff::PHOTOMETRIC_MINISWHITE
|
|
203
|
+
dest_offset = x * 4
|
|
204
|
+
dest.put_uint8(dest_offset, value)
|
|
205
|
+
dest.put_uint8(dest_offset + 1, value)
|
|
206
|
+
dest.put_uint8(dest_offset + 2, value)
|
|
207
|
+
dest.put_uint8(dest_offset + 3, 255)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def convert_rgb16_row(dest)
|
|
212
|
+
@width.times do |x|
|
|
213
|
+
source_offset = x * 6
|
|
214
|
+
dest_offset = x * 4
|
|
215
|
+
dest.put_uint8(dest_offset, @source_row.get_uint16(source_offset) >> 8)
|
|
216
|
+
dest.put_uint8(dest_offset + 1, @source_row.get_uint16(source_offset + 2) >> 8)
|
|
217
|
+
dest.put_uint8(dest_offset + 2, @source_row.get_uint16(source_offset + 4) >> 8)
|
|
218
|
+
dest.put_uint8(dest_offset + 3, 255)
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def convert_rgba16_row(dest)
|
|
223
|
+
@width.times do |x|
|
|
224
|
+
source_offset = x * 8
|
|
225
|
+
dest_offset = x * 4
|
|
226
|
+
dest.put_uint8(dest_offset, @source_row.get_uint16(source_offset) >> 8)
|
|
227
|
+
dest.put_uint8(dest_offset + 1, @source_row.get_uint16(source_offset + 2) >> 8)
|
|
228
|
+
dest.put_uint8(dest_offset + 2, @source_row.get_uint16(source_offset + 4) >> 8)
|
|
229
|
+
dest.put_uint8(dest_offset + 3, @source_row.get_uint16(source_offset + 6) >> 8)
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def read_uint32(tag, defaulted: false)
|
|
234
|
+
value = FFI::MemoryPointer.new(:uint32)
|
|
235
|
+
ret = if defaulted
|
|
236
|
+
LibTiff.TIFFGetFieldDefaulted(@tiff, tag, :pointer, value)
|
|
237
|
+
else
|
|
238
|
+
LibTiff.TIFFGetField(@tiff, tag, :pointer, value)
|
|
239
|
+
end
|
|
240
|
+
raise "Unable to read TIFF tag: #{tag}" unless ret == 1
|
|
241
|
+
value.read_uint32
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def read_uint16(tag, defaulted: false)
|
|
245
|
+
value = FFI::MemoryPointer.new(:uint16)
|
|
246
|
+
ret = if defaulted
|
|
247
|
+
LibTiff.TIFFGetFieldDefaulted(@tiff, tag, :pointer, value)
|
|
248
|
+
else
|
|
249
|
+
LibTiff.TIFFGetField(@tiff, tag, :pointer, value)
|
|
250
|
+
end
|
|
251
|
+
raise "Unable to read TIFF tag: #{tag}" unless ret == 1
|
|
252
|
+
value.read_uint16
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def read_coordinates
|
|
256
|
+
transform = read_model_transformation || read_pixel_scale_and_tiepoint
|
|
257
|
+
return nil unless transform
|
|
258
|
+
@gtif = LibGeoTiff.GTIFNew(@tiff)
|
|
259
|
+
|
|
260
|
+
{transform: transform,
|
|
261
|
+
crs: read_crs}
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def read_pixel_scale_and_tiepoint
|
|
265
|
+
scale_count = FFI::MemoryPointer.new(:uint16)
|
|
266
|
+
scale_data = FFI::MemoryPointer.new(:pointer)
|
|
267
|
+
ret = LibTiff.TIFFGetField(
|
|
268
|
+
@tiff, LibTiff::TIFFTAG_MODELPIXELSCALE, :pointer, scale_count, :pointer, scale_data
|
|
269
|
+
)
|
|
270
|
+
return nil unless ret == 1
|
|
271
|
+
|
|
272
|
+
tiepoint_count = FFI::MemoryPointer.new(:uint16)
|
|
273
|
+
tiepoint_data = FFI::MemoryPointer.new(:pointer)
|
|
274
|
+
|
|
275
|
+
ret = LibTiff.TIFFGetField(
|
|
276
|
+
@tiff, LibTiff::TIFFTAG_MODELTIEPOINT, :pointer, tiepoint_count, :pointer, tiepoint_data
|
|
277
|
+
)
|
|
278
|
+
return nil unless ret == 1
|
|
279
|
+
|
|
280
|
+
scale_ptr = scale_data.read_pointer
|
|
281
|
+
tiepoint_ptr = tiepoint_data.read_pointer
|
|
282
|
+
x_pixel_size = scale_ptr.get_double(0)
|
|
283
|
+
y_pixel_size = scale_ptr.get_double(8)
|
|
284
|
+
pixel_x = tiepoint_ptr.get_double(0)
|
|
285
|
+
pixel_y = tiepoint_ptr.get_double(8)
|
|
286
|
+
model_x = tiepoint_ptr.get_double(24)
|
|
287
|
+
model_y = tiepoint_ptr.get_double(32)
|
|
288
|
+
|
|
289
|
+
{x_origin: model_x - pixel_x * x_pixel_size,
|
|
290
|
+
y_origin: model_y + pixel_y * y_pixel_size,
|
|
291
|
+
x_pixel_size: x_pixel_size,
|
|
292
|
+
y_pixel_size: -y_pixel_size,
|
|
293
|
+
x_row_offset: 0.0,
|
|
294
|
+
y_column_offset: 0.0}
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def read_model_transformation
|
|
298
|
+
count = FFI::MemoryPointer.new(:uint16)
|
|
299
|
+
data = FFI::MemoryPointer.new(:pointer)
|
|
300
|
+
|
|
301
|
+
ret = LibTiff.TIFFGetField(
|
|
302
|
+
@tiff, LibTiff::TIFFTAG_MODELTRANSFORMATION, :pointer, count, :pointer, data
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
return nil unless ret == 1
|
|
306
|
+
ptr = data.read_pointer
|
|
307
|
+
return nil unless count.read_uint16 >= 16
|
|
308
|
+
|
|
309
|
+
{x_origin: ptr.get_double(3 * 8),
|
|
310
|
+
y_origin: ptr.get_double(7 * 8),
|
|
311
|
+
x_pixel_size: ptr.get_double(0 * 8),
|
|
312
|
+
y_column_offset: ptr.get_double(4 * 8),
|
|
313
|
+
x_row_offset: ptr.get_double(1 * 8),
|
|
314
|
+
y_pixel_size: ptr.get_double(5 * 8)}
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def read_crs
|
|
318
|
+
return nil if @gtif.nil? || @gtif.null?
|
|
319
|
+
|
|
320
|
+
model_type = read_geokey_short(LibGeoTiff::GTMODELTYPEGEOKEY)
|
|
321
|
+
code =
|
|
322
|
+
case model_type
|
|
323
|
+
when LibGeoTiff::MODELTYPE_PROJECTED
|
|
324
|
+
read_geokey_short(LibGeoTiff::PROJECTEDCSTYPEGEOKEY)
|
|
325
|
+
when LibGeoTiff::MODELTYPE_GEOGRAPHIC
|
|
326
|
+
read_geokey_short(LibGeoTiff::GEOGRAPHICTYPEGEOKEY)
|
|
327
|
+
end
|
|
328
|
+
return nil unless code
|
|
329
|
+
|
|
330
|
+
{authority: "EPSG",
|
|
331
|
+
code: code}
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def read_geokey_short(key)
|
|
335
|
+
value = FFI::MemoryPointer.new(:uint16)
|
|
336
|
+
ret = LibGeoTiff.GTIFKeyGetSHORT(@gtif, key, value, 0, 1)
|
|
337
|
+
|
|
338
|
+
return nil unless ret == 1
|
|
339
|
+
value.read_uint16
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def disable_warnings!
|
|
343
|
+
LibTiff.TIFFSetWarningHandler(nil)
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
end
|
|
@@ -30,7 +30,7 @@ module Contrek
|
|
|
30
30
|
@options = options
|
|
31
31
|
@clusters = []
|
|
32
32
|
@maximum_width = bitmap.w
|
|
33
|
-
@height = bitmap.h
|
|
33
|
+
@height = options[:processing_height] || bitmap.h
|
|
34
34
|
@number_of_tiles = options[:number_of_tiles] || (raise "number_of_tiles params is needed!")
|
|
35
35
|
@number_of_tiles = 1 if @number_of_tiles <= 0
|
|
36
36
|
|
|
@@ -51,6 +51,7 @@ module Contrek
|
|
|
51
51
|
bitmap: bitmap,
|
|
52
52
|
matcher: matcher,
|
|
53
53
|
options: {
|
|
54
|
+
processing_height: @options[:processing_height],
|
|
54
55
|
versus: current_versus,
|
|
55
56
|
bounds: true,
|
|
56
57
|
treemap: @options[:treemap],
|
|
@@ -71,7 +72,7 @@ module Contrek
|
|
|
71
72
|
|
|
72
73
|
x = tile_end_x - 1
|
|
73
74
|
end
|
|
74
|
-
process_tiles!(bitmap, height:
|
|
75
|
+
process_tiles!(bitmap, height: @height, deterministic: @options[:deterministic])
|
|
75
76
|
end.real
|
|
76
77
|
end
|
|
77
78
|
|
|
@@ -5,6 +5,8 @@ module Contrek
|
|
|
5
5
|
class GeoJsonStreamingMerger < StreamingMerger
|
|
6
6
|
def initialize(stream_to:, pixel_val:, options: {})
|
|
7
7
|
@pixel_val = pixel_val
|
|
8
|
+
@point_converter = self.class.coordinates_converter(options[:geo_localization])
|
|
9
|
+
|
|
8
10
|
super(stream_to:, options:)
|
|
9
11
|
end
|
|
10
12
|
|
|
@@ -23,11 +25,11 @@ module Contrek
|
|
|
23
25
|
else
|
|
24
26
|
@stream.write(",")
|
|
25
27
|
end
|
|
26
|
-
outer_ring = polygon[:outer].map { |p|
|
|
28
|
+
outer_ring = polygon[:outer].map { |p| @point_converter.call(p) }
|
|
27
29
|
outer_ring << outer_ring.first if outer_ring.first != outer_ring.last
|
|
28
30
|
polygon_coordinates = [outer_ring]
|
|
29
31
|
polygon[:inner].each do |sequence|
|
|
30
|
-
inner_ring = sequence.map { |p|
|
|
32
|
+
inner_ring = sequence.map { |p| @point_converter.call(p) }
|
|
31
33
|
inner_ring << inner_ring.first if inner_ring.first != inner_ring.last
|
|
32
34
|
polygon_coordinates << inner_ring
|
|
33
35
|
end
|
|
@@ -41,6 +43,21 @@ module Contrek
|
|
|
41
43
|
}
|
|
42
44
|
@stream.write(JSON.generate(feature_hash))
|
|
43
45
|
end
|
|
46
|
+
|
|
47
|
+
def self.coordinates_converter(geo_localization)
|
|
48
|
+
return ->(point) { [point[:y], point[:x]] } unless geo_localization
|
|
49
|
+
|
|
50
|
+
crs = geo_localization[:crs]
|
|
51
|
+
raise "Unsupported CRS: #{crs[:authority]}:#{crs[:code]}" unless crs[:authority] == "EPSG" && crs[:code] == 4326
|
|
52
|
+
|
|
53
|
+
transform = geo_localization[:transform]
|
|
54
|
+
lambda do |point|
|
|
55
|
+
x = point[:y]
|
|
56
|
+
y = point[:x]
|
|
57
|
+
[(transform[:x_origin] + x * transform[:x_pixel_size] + y * transform[:x_row_offset]).round(7),
|
|
58
|
+
(transform[:y_origin] + x * transform[:y_column_offset] + y * transform[:y_pixel_size]).round(7)]
|
|
59
|
+
end
|
|
60
|
+
end
|
|
44
61
|
end
|
|
45
62
|
end
|
|
46
63
|
end
|
|
@@ -10,10 +10,13 @@ module Contrek
|
|
|
10
10
|
@options = {versus: :a}.merge(options)
|
|
11
11
|
sanitize_options
|
|
12
12
|
@source_bitmap = bitmap
|
|
13
|
+
@processing_height = (options[:processing_height] || bitmap.h).abs
|
|
14
|
+
raise ArgumentError, "Option processing_height must be less or equal than image height!" if @processing_height > bitmap.h
|
|
15
|
+
|
|
13
16
|
@matcher = matcher
|
|
14
17
|
|
|
15
18
|
@test_bitmap = test_bitmap
|
|
16
|
-
@node_cluster = NodeCluster.new(@
|
|
19
|
+
@node_cluster = NodeCluster.new(@processing_height, @options)
|
|
17
20
|
@reports = {}
|
|
18
21
|
|
|
19
22
|
# 1 finds matching blocks
|
|
@@ -51,7 +54,7 @@ module Contrek
|
|
|
51
54
|
groups_names: @node_cluster.root_nodes.map(&:name).join,
|
|
52
55
|
benchmarks: format_benchmarks,
|
|
53
56
|
width: @source_bitmap.w,
|
|
54
|
-
height: @
|
|
57
|
+
height: @processing_height,
|
|
55
58
|
treemap: (@node_cluster.treemap if @options.has_key?(:treemap)),
|
|
56
59
|
options: @ori_options,
|
|
57
60
|
versus: @options[:versus],
|
|
@@ -103,7 +106,7 @@ module Contrek
|
|
|
103
106
|
matching = false
|
|
104
107
|
min_x = start_x
|
|
105
108
|
max_x = start_x
|
|
106
|
-
@source_bitmap.scan(start_x: start_x, end_x: end_x) do |x, y, color|
|
|
109
|
+
@source_bitmap.scan(start_x: start_x, end_x: end_x, end_y: @processing_height) do |x, y, color|
|
|
107
110
|
# puts "#{x} #{y}"
|
|
108
111
|
if @matcher.match?(color) && matching == false
|
|
109
112
|
min_x = x
|
data/lib/contrek/version.rb
CHANGED
data/lib/contrek.rb
CHANGED
|
@@ -5,11 +5,17 @@ require "contrek/bitmaps/painting"
|
|
|
5
5
|
require "contrek/bitmaps/rendering"
|
|
6
6
|
require "contrek/bitmaps/bitmap"
|
|
7
7
|
require "contrek/bitmaps/chunky_bitmap"
|
|
8
|
+
require "contrek/bitmaps/rgba_bitmap"
|
|
8
9
|
require "contrek/bitmaps/png_bitmap"
|
|
9
10
|
require "contrek/bitmaps/raw_bitmap"
|
|
10
11
|
require "contrek/bitmaps/rgb_color"
|
|
11
12
|
require "contrek/bitmaps/rgb_cpp_color"
|
|
12
13
|
require "contrek/bitmaps/sample_generator"
|
|
14
|
+
require "contrek/bitmaps/streaming/raster_streamer"
|
|
15
|
+
require "contrek/bitmaps/streaming/sources/base"
|
|
16
|
+
require "contrek/bitmaps/streaming/sources/ascii"
|
|
17
|
+
require "contrek/bitmaps/streaming/sources/png"
|
|
18
|
+
require "contrek/bitmaps/streaming/sources/tiff"
|
|
13
19
|
require "contrek/shared/result"
|
|
14
20
|
require "contrek/finder/bounds"
|
|
15
21
|
require "contrek/finder/list"
|
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.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Emanuele Cesaroni
|
|
@@ -99,6 +99,20 @@ dependencies:
|
|
|
99
99
|
- - "~>"
|
|
100
100
|
- !ruby/object:Gem::Version
|
|
101
101
|
version: 1.3.5
|
|
102
|
+
- !ruby/object:Gem::Dependency
|
|
103
|
+
name: ffi
|
|
104
|
+
requirement: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - "~>"
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '1.17'
|
|
109
|
+
type: :runtime
|
|
110
|
+
prerelease: false
|
|
111
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
112
|
+
requirements:
|
|
113
|
+
- - "~>"
|
|
114
|
+
- !ruby/object:Gem::Version
|
|
115
|
+
version: '1.17'
|
|
102
116
|
- !ruby/object:Gem::Dependency
|
|
103
117
|
name: rice
|
|
104
118
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -152,6 +166,15 @@ files:
|
|
|
152
166
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/RemoteFastPngBitmap.h
|
|
153
167
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/spng.c
|
|
154
168
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/spng.h
|
|
169
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/AsciiSource.cpp
|
|
170
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/AsciiSource.h
|
|
171
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/GeoLocalization.h
|
|
172
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/PngSource.cpp
|
|
173
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/PngSource.h
|
|
174
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/RasterSource.h
|
|
175
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/RasterStreamer.h
|
|
176
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/TiffSource.cpp
|
|
177
|
+
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/bitmaps/streaming/TiffSource.h
|
|
155
178
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/FinderUtils.cpp
|
|
156
179
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/FinderUtils.h
|
|
157
180
|
- ext/cpp_polygon_finder/PolygonFinder/src/polygon/finder/List.cpp
|
|
@@ -252,7 +275,13 @@ files:
|
|
|
252
275
|
- lib/contrek/bitmaps/rendering.rb
|
|
253
276
|
- lib/contrek/bitmaps/rgb_color.rb
|
|
254
277
|
- lib/contrek/bitmaps/rgb_cpp_color.rb
|
|
278
|
+
- lib/contrek/bitmaps/rgba_bitmap.rb
|
|
255
279
|
- lib/contrek/bitmaps/sample_generator.rb
|
|
280
|
+
- lib/contrek/bitmaps/streaming/raster_streamer.rb
|
|
281
|
+
- lib/contrek/bitmaps/streaming/sources/ascii.rb
|
|
282
|
+
- lib/contrek/bitmaps/streaming/sources/base.rb
|
|
283
|
+
- lib/contrek/bitmaps/streaming/sources/png.rb
|
|
284
|
+
- lib/contrek/bitmaps/streaming/sources/tiff.rb
|
|
256
285
|
- lib/contrek/cpp/cpp_concurrent_finder.rb
|
|
257
286
|
- lib/contrek/cpp/cpp_concurrent_horizontal_merger.rb
|
|
258
287
|
- lib/contrek/cpp/cpp_concurrent_merger.rb
|