ffi-gdal 0.0.4 → 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +7 -1
- data/Rakefile +12 -1
- data/TODO.md +11 -0
- data/ffi-gdal.gemspec +2 -2
- data/lib/ext/error_symbols.rb +59 -0
- data/lib/ext/float_ext.rb +15 -0
- data/lib/ext/narray_ext.rb +16 -0
- data/lib/ext/to_bool.rb +2 -0
- data/lib/ffi-gdal.rb +139 -4
- data/lib/ffi/{gdal/cpl_conv.rb → cpl/conv_h.rb} +2 -3
- data/lib/ffi/{gdal/cpl_error.rb → cpl/error_h.rb} +1 -25
- data/lib/ffi/cpl/minixml_h.rb +14 -0
- data/lib/ffi/{gdal/cpl_string.rb → cpl/string_h.rb} +0 -25
- data/lib/ffi/{gdal/cpl_vsi.rb → cpl/vsi_h.rb} +0 -0
- data/lib/ffi/cpl/xml_node.rb +13 -0
- data/lib/ffi/gdal.rb +57 -593
- data/lib/ffi/gdal/alg_h.rb +127 -0
- data/lib/ffi/gdal/gdal_grid_data_metrics_options.rb +14 -0
- data/lib/ffi/gdal/gdal_grid_inverse_distance_to_a_power_options.rb +19 -0
- data/lib/ffi/gdal/gdal_grid_moving_average_options.rb +14 -0
- data/lib/ffi/gdal/gdal_grid_nearest_neighbor_options.rb +13 -0
- data/lib/ffi/gdal/gdal_h.rb +683 -0
- data/lib/ffi/gdal/gdal_rpc_info.rb +27 -0
- data/lib/ffi/gdal/gdal_transformer_info.rb +14 -0
- data/lib/ffi/gdal/gdal_warp_options.rb +43 -0
- data/lib/ffi/gdal/grid_h.rb +51 -0
- data/lib/ffi/gdal/version.rb +1 -1
- data/lib/ffi/gdal/warper_h.rb +48 -0
- data/lib/ffi/ogr.rb +12 -0
- data/lib/ffi/ogr/api_h.rb +553 -0
- data/lib/ffi/ogr/core_h.rb +148 -0
- data/lib/ffi/ogr/featurestyle_h.rb +22 -0
- data/lib/ffi/ogr/geocoding_h.rb +21 -0
- data/lib/ffi/ogr/ogr_contour_writer_info.rb +14 -0
- data/lib/ffi/ogr/ogr_envelope.rb +12 -0
- data/lib/ffi/ogr/ogr_envelope_3d.rb +14 -0
- data/lib/ffi/ogr/ogr_field.rb +50 -0
- data/lib/ffi/ogr/ogr_style_param.rb +12 -0
- data/lib/ffi/ogr/ogr_style_value.rb +13 -0
- data/lib/ffi/ogr/srs_api_h.rb +325 -0
- data/lib/gdal/color_entry.rb +47 -0
- data/lib/gdal/color_entry_extensions.rb +30 -0
- data/lib/gdal/color_interpretation.rb +15 -0
- data/lib/gdal/color_table.rb +146 -0
- data/lib/gdal/color_table_extensions.rb +47 -0
- data/lib/gdal/color_table_types/cmyk.rb +25 -0
- data/lib/gdal/color_table_types/gray.rb +9 -0
- data/lib/gdal/color_table_types/hls.rb +21 -0
- data/lib/gdal/color_table_types/rgb.rb +25 -0
- data/lib/gdal/data_type.rb +38 -0
- data/lib/gdal/dataset.rb +437 -0
- data/lib/gdal/dataset_extensions.rb +496 -0
- data/lib/gdal/driver.rb +244 -0
- data/lib/gdal/driver_extensions.rb +56 -0
- data/lib/gdal/environment_methods.rb +43 -0
- data/lib/{ffi-gdal → gdal}/exceptions.rb +4 -1
- data/lib/gdal/geo_transform.rb +188 -0
- data/lib/gdal/geo_transform_extensions.rb +90 -0
- data/lib/gdal/logger.rb +7 -0
- data/lib/{ffi-gdal → gdal}/major_object.rb +15 -14
- data/lib/gdal/options.rb +49 -0
- data/lib/gdal/raster_attribute_table.rb +185 -0
- data/lib/gdal/raster_attribute_table_extensions.rb +40 -0
- data/lib/{ffi-gdal → gdal}/raster_band.rb +227 -99
- data/lib/gdal/raster_band_extensions.rb +198 -0
- data/lib/{ffi-gdal → gdal}/version_info.rb +8 -0
- data/lib/gdal/warp_operation.rb +96 -0
- data/lib/ogr/coordinate_transformation.rb +108 -0
- data/lib/ogr/data_source.rb +172 -0
- data/lib/ogr/data_source_extensions.rb +32 -0
- data/lib/ogr/driver.rb +119 -0
- data/lib/ogr/envelope.rb +80 -0
- data/lib/ogr/envelope_extensions.rb +92 -0
- data/lib/ogr/exceptions.rb +35 -0
- data/lib/ogr/feature.rb +212 -0
- data/lib/ogr/feature_definition.rb +120 -0
- data/lib/ogr/feature_definition_extensions.rb +36 -0
- data/lib/ogr/feature_extensions.rb +31 -0
- data/lib/ogr/field.rb +91 -0
- data/lib/ogr/field_extensions.rb +23 -0
- data/lib/ogr/geocoding_session.rb +84 -0
- data/lib/ogr/geometry.rb +617 -0
- data/lib/ogr/geometry_extensions.rb +60 -0
- data/lib/ogr/geometry_types/collection.rb +45 -0
- data/lib/ogr/geometry_types/curve.rb +120 -0
- data/lib/ogr/geometry_types/surface.rb +20 -0
- data/lib/ogr/layer.rb +226 -0
- data/lib/ogr/layer_extensions.rb +55 -0
- data/lib/ogr/line_string.rb +7 -0
- data/lib/ogr/linear_ring.rb +6 -0
- data/lib/ogr/multi_line_string.rb +9 -0
- data/lib/ogr/multi_point.rb +7 -0
- data/lib/ogr/multi_polygon.rb +14 -0
- data/lib/ogr/point.rb +89 -0
- data/lib/ogr/polygon.rb +9 -0
- data/lib/ogr/spatial_reference.rb +723 -0
- data/lib/ogr/spatial_reference_extensions.rb +32 -0
- data/lib/ogr/style_table.rb +17 -0
- data/lib/ogr/style_table_extensions.rb +16 -0
- data/spec/{ffi-gdal/integration → integration}/color_table_info_spec.rb +1 -1
- data/spec/{ffi-gdal/integration → integration}/dataset_info_spec.rb +0 -0
- data/spec/{ffi-gdal/integration → integration}/driver_info_spec.rb +1 -1
- data/spec/{ffi-gdal/integration → integration}/geo_transform_info_spec.rb +0 -0
- data/spec/{ffi-gdal/integration → integration}/raster_attribute_table_info_spec.rb +1 -1
- data/spec/{ffi-gdal/integration → integration}/raster_band_info_spec.rb +5 -5
- data/spec/spec_helper.rb +4 -1
- data/spec/support/shapefiles/states_21basic/states.prj +1 -0
- data/spec/support/shapefiles/states_21basic/states.sbn +0 -0
- data/spec/support/shapefiles/states_21basic/states.sbx +0 -0
- data/spec/support/shapefiles/states_21basic/states.shp +0 -0
- data/spec/support/worldfiles/SR_50M/SR_50M.VERSION.txt +1 -0
- data/spec/support/worldfiles/SR_50M/SR_50M.prj +1 -0
- data/spec/support/worldfiles/SR_50M/SR_50M.tfw +6 -0
- data/spec/{ext/cpl_error_symbols_spec.rb → unit/ext/error_symbols_spec.rb} +1 -1
- data/spec/unit/gdal/color_table_spec.rb +146 -0
- data/spec/unit/ogr/layer_spec.rb +97 -0
- data/spec/unit/ogr/linear_ring_spec.rb +111 -0
- data/spec/unit/ogr/point_spec.rb +321 -0
- data/spec/{ffi-gdal/unit → unit}/version_info_spec.rb +1 -1
- data/testing_gdal.rb +168 -0
- data/testing_gdalwarp.rb +91 -0
- data/testing_layer_to_layer.rb +35 -0
- data/testing_ndvi.rb +76 -0
- data/testing_nir.rb +77 -0
- data/testing_ogr.rb +63 -0
- metadata +167 -59
- data/lib/ext/cpl_error_symbols.rb +0 -37
- data/lib/ffi-gdal/color_table.rb +0 -59
- data/lib/ffi-gdal/dataset.rb +0 -359
- data/lib/ffi-gdal/driver.rb +0 -151
- data/lib/ffi-gdal/geo_transform.rb +0 -137
- data/lib/ffi-gdal/raster_attribute_table.rb +0 -78
- data/lib/ffi/gdal/ogr_api.rb +0 -21
- data/lib/ffi/gdal/ogr_core.rb +0 -195
- data/lib/ffi/gdal/ogr_srs_api.rb +0 -44
- data/meow.rb +0 -144
- data/rubby.rb +0 -224
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module GDAL
|
4
|
+
module RasterBandExtensions
|
5
|
+
def overviews
|
6
|
+
0.upto(overview_count - 1).map do |i|
|
7
|
+
overview(i)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# @return [Hash{x => Fixnum, y => Fixnum}]
|
12
|
+
def block_count
|
13
|
+
x_blocks = (x_size + block_size[:x] - 1) / block_size[:x]
|
14
|
+
y_blocks = (y_size + block_size[:y] - 1) / block_size[:y]
|
15
|
+
|
16
|
+
{ x: x_blocks, y: y_blocks }
|
17
|
+
end
|
18
|
+
|
19
|
+
# The buffer size to use for block-based IO, based on #block_size.
|
20
|
+
#
|
21
|
+
# @return [Fixnum]
|
22
|
+
def block_buffer_size
|
23
|
+
block_size[:x] * block_size[:y]
|
24
|
+
end
|
25
|
+
|
26
|
+
# Reads through the raster, block-by-block and yields the pixel data that
|
27
|
+
# it gathered.
|
28
|
+
#
|
29
|
+
# @param to_data_type [FFI::GDAL::GDALDataType]
|
30
|
+
# @return [Enumerator, nil] Returns an Enumerable if no block is given,
|
31
|
+
# allowing to chain with other Enumerable methods. Returns nil if a
|
32
|
+
# block is given.
|
33
|
+
def each_by_block(to_data_type=nil)
|
34
|
+
return enum_for(:each_by_block) unless block_given?
|
35
|
+
|
36
|
+
data_type = to_data_type || self.data_type
|
37
|
+
data_pointer = GDAL._pointer_from_data_type(data_type, block_buffer_size)
|
38
|
+
|
39
|
+
0.upto(block_count[:y] - 1).each do |y_block_number|
|
40
|
+
0.upto(block_count[:x] - 1).each do |x_block_number|
|
41
|
+
read_block(x_block_number, y_block_number, data_pointer)
|
42
|
+
|
43
|
+
0.upto(block_size[:y] - 1).each do |block_index|
|
44
|
+
read_offset = block_size[:x] * block_index
|
45
|
+
pixels = if data_type == :GDT_Byte
|
46
|
+
data_pointer.get_array_of_uint8(read_offset, block_size[:x])
|
47
|
+
else
|
48
|
+
data_pointer.get_array_of_float(read_offset, block_size[:x])
|
49
|
+
end
|
50
|
+
|
51
|
+
yield(pixels)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Iterates through all lines and builds an NArray of pixels.
|
58
|
+
#
|
59
|
+
# @return [NArray]
|
60
|
+
def to_na(to_data_type=nil)
|
61
|
+
data_type = to_data_type || self.data_type
|
62
|
+
|
63
|
+
values = each_by_block(to_data_type).map do |pixels|
|
64
|
+
pixels
|
65
|
+
end
|
66
|
+
|
67
|
+
case data_type
|
68
|
+
when :GDT_Byte then NArray.to_na(values).to_type(NArray::BYTE)
|
69
|
+
when :GDT_UInt16 then NArray.to_na(values).to_type(NArray::SINT)
|
70
|
+
when :GDT_Int16 then NArray.to_na(values).to_type(NArray::SINT)
|
71
|
+
when :GDT_UInt32 then NArray.to_na(values).to_type(NArray::INT)
|
72
|
+
when :GDT_Int32 then NArray.to_na(values).to_type(NArray::INT)
|
73
|
+
when :GDT_Float32 then NArray.to_na(values).to_type(NArray::SFLOAT)
|
74
|
+
when :GDT_Float64 then NArray.to_na(values).to_type(NArray::DFLOAT)
|
75
|
+
when :GDT_CInt16 then NArray.to_na(values).to_type(NArray::SCOMPLEX)
|
76
|
+
when :GDT_CInt32 then NArray.to_na(values).to_type(NArray::DCOMPLEX)
|
77
|
+
when :GDT_CFloat32 then NArray.to_na(values).to_type(NArray::SCOMPLEX)
|
78
|
+
when :GDT_CFloat64 then NArray.to_na(values).to_type(NArray::DCOMPLEX)
|
79
|
+
else
|
80
|
+
NArray.to_na(values)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Sets the band to be a Palette band, then applies an RGB color table using
|
85
|
+
# the given colors. Colors are distributed evenly across the table based
|
86
|
+
# on the number of colors given. Note that this will overwrite any existing
|
87
|
+
# color table that may be set on this band.
|
88
|
+
#
|
89
|
+
# @param colors [Array<Fixnum, String>, Fixnum, String] Can be a single or
|
90
|
+
# many colors, given as either R, G, and B integers (0-255) or as strings
|
91
|
+
# of Hex.
|
92
|
+
#
|
93
|
+
# @example Colors as RGB values
|
94
|
+
# # This will make the first 128 values black, and the last 128, red.
|
95
|
+
# my_band.colorize!([[0, 0, 0], [255, 0, 0]])
|
96
|
+
#
|
97
|
+
# @example Colors as Hex values
|
98
|
+
# # Same as above...
|
99
|
+
# my_band.colorize!(%w[#000000 #FF0000])
|
100
|
+
def colorize!(*colors)
|
101
|
+
return if colors.empty?
|
102
|
+
|
103
|
+
table = GDAL::ColorTable.create(:GPI_RGB)
|
104
|
+
|
105
|
+
color_entry_index_count = if data_type == :GDT_Byte
|
106
|
+
256
|
107
|
+
elsif data_type == :GDT_UInt16
|
108
|
+
65536
|
109
|
+
else
|
110
|
+
raise "Can't colorize a #{data_type} band--must be :GDT_Byte or :GDT_UInt16"
|
111
|
+
end
|
112
|
+
|
113
|
+
self.color_interpretation = :GCI_PaletteIndex
|
114
|
+
table.add_color_entry(0, 0, 0, 0, 255)
|
115
|
+
bin_count = (color_entry_index_count / colors.size).to_f
|
116
|
+
|
117
|
+
1.upto(color_entry_index_count - 1) do |color_entry_index|
|
118
|
+
color_number = (color_entry_index / bin_count.to_f).to_i
|
119
|
+
|
120
|
+
color = colors[color_number]
|
121
|
+
color_array = hex_to_rgb(color) unless color.is_a?(Array)
|
122
|
+
table.add_color_entry(color_entry_index,
|
123
|
+
color_array[0], color_array[1], color_array[2], 255)
|
124
|
+
end
|
125
|
+
|
126
|
+
self.color_table = table
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
# Gets the colors from the associated ColorTable and returns an Array of
|
131
|
+
# those, where each ColorEntry is [R, G, B, A].
|
132
|
+
#
|
133
|
+
# @return [Array<Array<Fixnum>>]
|
134
|
+
def colors_as_rgb
|
135
|
+
return [] unless color_table
|
136
|
+
|
137
|
+
color_table.color_entries_as_rgb.map do |color_entry|
|
138
|
+
color_entry.to_a
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Gets the colors from the associated ColorTable and returns an Array of
|
143
|
+
# Strings, where the RGB color for each ColorEntry has been converted to
|
144
|
+
# Hex.
|
145
|
+
#
|
146
|
+
# @return [Array<String>]
|
147
|
+
def colors_as_hex
|
148
|
+
colors_as_rgb.map do |rgba|
|
149
|
+
rgb = rgba.to_a[0..2]
|
150
|
+
|
151
|
+
"##{rgb[0].to_s(16)}#{rgb[1].to_s(16)}#{rgb[2].to_s(16)}"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
# @param hex [String]
|
156
|
+
def hex_to_rgb(hex)
|
157
|
+
hex.sub!(/^#/, '')
|
158
|
+
matches = hex.match(/(?<red>[a-zA-Z0-9]{2})(?<green>[a-zA-Z0-9]{2})(?<blue>[a-zA-Z0-9]{2})/)
|
159
|
+
|
160
|
+
[matches[:red].to_i(16), matches[:green].to_i(16), matches[:blue].to_i(16)]
|
161
|
+
end
|
162
|
+
|
163
|
+
# @return [Hash]
|
164
|
+
def as_json
|
165
|
+
{
|
166
|
+
raster_band: {
|
167
|
+
block_size: block_size,
|
168
|
+
category_names: category_names,
|
169
|
+
color_interpretation: color_interpretation,
|
170
|
+
color_table: color_table,
|
171
|
+
data_type: data_type,
|
172
|
+
default_histogram: default_histogram(true),
|
173
|
+
default_raster_attribute_table: default_raster_attribute_table,
|
174
|
+
has_arbitrary_overviews: arbitrary_overviews?,
|
175
|
+
mask_flags: mask_flags,
|
176
|
+
maximum_value: maximum_value,
|
177
|
+
minimum_value: minimum_value,
|
178
|
+
no_data_value: no_data_value,
|
179
|
+
number: number,
|
180
|
+
offset: offset,
|
181
|
+
overview_count: overview_count,
|
182
|
+
overviews: overviews,
|
183
|
+
scale: scale,
|
184
|
+
statistics: statistics,
|
185
|
+
unit_type: unit_type,
|
186
|
+
x_size: x_size,
|
187
|
+
y_size: y_size
|
188
|
+
},
|
189
|
+
metadata: all_metadata
|
190
|
+
}
|
191
|
+
end
|
192
|
+
|
193
|
+
# @return [String]
|
194
|
+
def to_json
|
195
|
+
as_json.to_json
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
@@ -44,5 +44,13 @@ module GDAL
|
|
44
44
|
def long_version
|
45
45
|
FFI::GDAL.GDALVersionInfo('--version')
|
46
46
|
end
|
47
|
+
|
48
|
+
# @param major [Fixnum]
|
49
|
+
# @param minor [Fixnum]
|
50
|
+
# @return [Boolean] +true+ if the runtime GDAL library matches the given
|
51
|
+
# version params.
|
52
|
+
def check_version(major, minor)
|
53
|
+
FFI::GDAL.GDALCheckVersion(major, minor, 'FFI::GDAL')
|
54
|
+
end
|
47
55
|
end
|
48
56
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module GDAL
|
2
|
+
class WarpOperation
|
3
|
+
def initialize(warp_options)
|
4
|
+
@warp_operation_pointer = FFI::GDAL.GDALCreateWarpOperation(warp_options.ptr)
|
5
|
+
end
|
6
|
+
|
7
|
+
def c_pointer
|
8
|
+
@warp_operation_pointer
|
9
|
+
end
|
10
|
+
|
11
|
+
def destroy!
|
12
|
+
FFI::GDAL.GDALDestroyWarpOperation(@warp_operation_pointer)
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param x_offset [Fixnum] X offset of the destination image.
|
16
|
+
# @param y_offset [Fixnum] Y offset of the destination image.
|
17
|
+
# @param x_size [Fixnum] X size (width) of the destination image.
|
18
|
+
# @param y_size [Fixnum] Y size (height) of the destination image.
|
19
|
+
def chunk_and_warp_image(x_offset, y_offset, x_size, y_size)
|
20
|
+
cpl_err = FFI::GDAL.GDALChunkAndWarpImage(@warp_operation_pointer,
|
21
|
+
x_offset,
|
22
|
+
y_offset,
|
23
|
+
x_size,
|
24
|
+
y_size)
|
25
|
+
|
26
|
+
cpl_err.to_bool
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param x_offset [Fixnum] X offset of the destination image.
|
30
|
+
# @param y_offset [Fixnum] Y offset of the destination image.
|
31
|
+
# @param x_size [Fixnum] X size (width) of the destination image.
|
32
|
+
# @param y_size [Fixnum] Y size (height) of the destination image.
|
33
|
+
# @todo Implement
|
34
|
+
def chunk_and_warp_multi(x_offset, y_offset, x_size, y_size)
|
35
|
+
raise NotImplementedError, '#chunk_and_warp_multi not yet implemented.'
|
36
|
+
|
37
|
+
FFI::GDAL.GDALChunkAndWarpMulti(@warp_operation_pointer,
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
# @param destination_x_offset [Fixnum] X offset of the destination image.
|
42
|
+
# @param destination_y_offset [Fixnum] Y offset of the destination image.
|
43
|
+
# @param destination_x_size [Fixnum] X size (width) of the destination image.
|
44
|
+
# @param destination_y_size [Fixnum] Y size (height) of the destination image.
|
45
|
+
# @param source_x_offset [Fixnum] X offset of the source image.
|
46
|
+
# @param source_y_offset [Fixnum] Y offset of the source image.
|
47
|
+
# @param source_x_size [Fixnum] X size (width) of the source image.
|
48
|
+
# @param source_y_size [Fixnum] Y size (height) of the source image.
|
49
|
+
def warp_region(destination_x_offset, destination_y_offset,
|
50
|
+
destination_x_size, destination_y_size,
|
51
|
+
source_x_offset, source_y_offset,
|
52
|
+
source_x_size, source_y_size)
|
53
|
+
cpl_err = FFI::GDAL.GDALWarpRegion(@warp_operation_pointer,
|
54
|
+
destination_x_offset,
|
55
|
+
destination_y_offset,
|
56
|
+
destination_x_size,
|
57
|
+
destination_y_size,
|
58
|
+
source_x_offset,
|
59
|
+
source_y_offset,
|
60
|
+
source_x_size,
|
61
|
+
source_y_size)
|
62
|
+
|
63
|
+
cpl_err.to_bool
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param destination_x_offset [Fixnum] X offset of the destination image.
|
67
|
+
# @param destination_y_offset [Fixnum] Y offset of the destination image.
|
68
|
+
# @param destination_x_size [Fixnum] X size (width) of the destination image.
|
69
|
+
# @param destination_y_size [Fixnum] Y size (height) of the destination image.
|
70
|
+
# @param buffer [FFI::Pointer]
|
71
|
+
# @param data_type [FFI::GDAL::GDALDataType]
|
72
|
+
# @param source_x_offset [Fixnum] X offset of the source image.
|
73
|
+
# @param source_y_offset [Fixnum] Y offset of the source image.
|
74
|
+
# @param source_x_size [Fixnum] X size (width) of the source image.
|
75
|
+
# @param source_y_size [Fixnum] Y size (height) of the source image.
|
76
|
+
def warp_region_to_buffer(destination_x_offset, destination_y_offset,
|
77
|
+
destination_x_size, destination_y_size,
|
78
|
+
buffer, data_type,
|
79
|
+
source_x_offset, source_y_offset,
|
80
|
+
source_x_size, source_y_size)
|
81
|
+
cpl_err = FFI::GDAL.GDALWarpRegionToBuffer(@warp_operation_pointer,
|
82
|
+
destination_x_offset,
|
83
|
+
destination_y_offset,
|
84
|
+
destination_x_size,
|
85
|
+
destination_y_size,
|
86
|
+
buffer,
|
87
|
+
data_type,
|
88
|
+
source_x_offset,
|
89
|
+
source_y_offset,
|
90
|
+
source_x_size,
|
91
|
+
source_y_size)
|
92
|
+
|
93
|
+
cpl_err.to_bool
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'narray'
|
2
|
+
require_relative '../ffi/ogr'
|
3
|
+
|
4
|
+
module OGR
|
5
|
+
class CoordinateTransformation
|
6
|
+
|
7
|
+
# @param source_srs [OGR::SpatialReference, FFI::Pointer]
|
8
|
+
# @param destination_srs [OGR::SpatialReference, FFI::Pointer]
|
9
|
+
# @return [OGR::CoordinateTransformation]
|
10
|
+
def self.create(source_srs, destination_srs)
|
11
|
+
source_ptr = GDAL._pointer(OGR::SpatialReference, source_srs)
|
12
|
+
destination_ptr = GDAL._pointer(OGR::SpatialReference, destination_srs)
|
13
|
+
ct_ptr = FFI::GDAL.OCTNewCoordinateTransformation(source_ptr, destination_ptr)
|
14
|
+
return nil if ct_ptr.null?
|
15
|
+
|
16
|
+
source = if source_srs.is_a?(OGR::SpatialReference)
|
17
|
+
source_srs
|
18
|
+
else
|
19
|
+
OGR::SpatialReference.new(source_srs)
|
20
|
+
end
|
21
|
+
|
22
|
+
destination = if destination_srs.is_a?(OGR::SpatialReference)
|
23
|
+
destination_srs
|
24
|
+
else
|
25
|
+
OGR::SpatialReference.new(destination_srs)
|
26
|
+
end
|
27
|
+
|
28
|
+
new(ct_ptr, source, destination)
|
29
|
+
end
|
30
|
+
|
31
|
+
# @param proj4_source [String]
|
32
|
+
# @return [String]
|
33
|
+
def self.proj4_normalize(proj4_source)
|
34
|
+
if GDAL._supported?(:OCTProj4Normalize)
|
35
|
+
FFI::GDAL.OCTProj4Normalize(proj4_source)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [OGR::SpatialReference]
|
40
|
+
attr_reader :source_coordinate_system
|
41
|
+
|
42
|
+
# @return [OGR::SpatialReference]
|
43
|
+
attr_reader :destination_coordinate_system
|
44
|
+
|
45
|
+
# @param coordinate_transformation [OGR::CoordinateTransformation,
|
46
|
+
# FFI::Pointer]
|
47
|
+
def initialize(coordinate_transformation, source, destination)
|
48
|
+
@transformation_pointer = GDAL._pointer(OGR::CoordinateTransformation,
|
49
|
+
coordinate_transformation)
|
50
|
+
@source_coordinate_system = source
|
51
|
+
@destination_coordinate_system = destination
|
52
|
+
|
53
|
+
close_me = -> { destroy! }
|
54
|
+
ObjectSpace.define_finalizer self, close_me
|
55
|
+
end
|
56
|
+
|
57
|
+
def c_pointer
|
58
|
+
@transformation_pointer
|
59
|
+
end
|
60
|
+
|
61
|
+
# Deletes the object and deallocates all related resources.
|
62
|
+
def destroy!
|
63
|
+
FFI::GDAL.OCTDestroyCoordinateTransformation(@transformation_pointer)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Transforms points in the +source_srs+ space to points in the
|
67
|
+
# +destination_space+ (given in .create).
|
68
|
+
#
|
69
|
+
# @param x_vertices [Array<Float>]
|
70
|
+
# @param y_vertices [Array<Float>]
|
71
|
+
# @param z_vertices [Array<Float>]
|
72
|
+
# @return [Array<Array<Float>,Array<Float>,Array<Float>>] [[x1, y1], [x2,
|
73
|
+
# y2], etc]
|
74
|
+
def transform(x_vertices, y_vertices, z_vertices=[])
|
75
|
+
x_ptr = FFI::MemoryPointer.new(:pointer, x_vertices.size)
|
76
|
+
x_ptr.write_array_of_double(x_vertices)
|
77
|
+
y_ptr = FFI::MemoryPointer.new(:pointer, y_vertices.size)
|
78
|
+
y_ptr.write_array_of_double(y_vertices)
|
79
|
+
|
80
|
+
if z_vertices.empty?
|
81
|
+
z_ptr = nil
|
82
|
+
else
|
83
|
+
z_ptr = FFI::MemoryPointer.new(:pointer, z_vertices.size)
|
84
|
+
z_ptr.write_array_of_double(z_vertices)
|
85
|
+
end
|
86
|
+
|
87
|
+
point_count = x_vertices.size + y_vertices.size + z_vertices.size
|
88
|
+
|
89
|
+
result = FFI::GDAL.OCTTransform(@transformation_pointer, point_count,
|
90
|
+
x_ptr, y_ptr, z_ptr)
|
91
|
+
|
92
|
+
# maybe this should raise?
|
93
|
+
return false unless result
|
94
|
+
|
95
|
+
x_vals = x_ptr.read_array_of_double
|
96
|
+
y_vals = y_ptr.read_array_of_double
|
97
|
+
z_vals = z_ptr.read_array_of_double unless z_vertices.empty?
|
98
|
+
|
99
|
+
points = if z_vertices.empty?
|
100
|
+
NMatrix[x_vals, y_vals]
|
101
|
+
else
|
102
|
+
NMatrix[x_vals, y_vals, z_vals]
|
103
|
+
end
|
104
|
+
|
105
|
+
points.transpose.to_a
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'json'
|
3
|
+
require_relative '../ffi/gdal'
|
4
|
+
require_relative '../ffi/ogr'
|
5
|
+
require_relative 'data_source_extensions'
|
6
|
+
require_relative 'exceptions'
|
7
|
+
require_relative 'layer'
|
8
|
+
require_relative 'style_table'
|
9
|
+
|
10
|
+
|
11
|
+
module OGR
|
12
|
+
class DataSource
|
13
|
+
include GDAL::MajorObject
|
14
|
+
include GDAL::Logger
|
15
|
+
include DataSourceExtensions
|
16
|
+
|
17
|
+
# @param path [String] Path/URL to the file to open.
|
18
|
+
# @param access_flag [String] 'r' for read, 'w', for write.
|
19
|
+
# @return [OGR::DataSource]
|
20
|
+
def self.open(path, access_flag = 'r')
|
21
|
+
uri = URI.parse(path)
|
22
|
+
file_path = uri.scheme.nil? ? ::File.expand_path(path) : path
|
23
|
+
|
24
|
+
pointer = FFI::GDAL.OGROpen(file_path, OGR._boolean_access_flag(access_flag), nil)
|
25
|
+
raise OGR::OpenFailure.new(file_path) if pointer.null?
|
26
|
+
|
27
|
+
new(pointer)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param data_source_pointer [FFI::Pointer]
|
31
|
+
def initialize(data_source_pointer)
|
32
|
+
@data_source_pointer = data_source_pointer
|
33
|
+
@file_name = name
|
34
|
+
@layers = []
|
35
|
+
|
36
|
+
close_me = -> { destroy }
|
37
|
+
ObjectSpace.define_finalizer self, close_me
|
38
|
+
end
|
39
|
+
|
40
|
+
def c_pointer
|
41
|
+
@data_source_pointer
|
42
|
+
end
|
43
|
+
|
44
|
+
def reopen(access_flag)
|
45
|
+
if driver.name == 'Memory'
|
46
|
+
raise "Can't reopen DataSources that were created and closed using the 'Memory' driver."
|
47
|
+
end
|
48
|
+
|
49
|
+
@data_source_pointer = FFI::GDAL.OGROpen(@file_path,
|
50
|
+
OGR._boolean_access_flag(access_flag), nil)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Closes opened data source and releases allocated resources.
|
54
|
+
def destroy
|
55
|
+
FFI::GDAL.OGR_DS_Destroy(@data_source_pointer)
|
56
|
+
end
|
57
|
+
alias_method :close, :destroy
|
58
|
+
|
59
|
+
# Name of the file represented by this object.
|
60
|
+
#
|
61
|
+
# @return [String]
|
62
|
+
def name
|
63
|
+
FFI::GDAL.OGR_DS_GetName(@data_source_pointer)
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [OGR::Driver]
|
67
|
+
def driver
|
68
|
+
return @driver if @driver
|
69
|
+
|
70
|
+
driver_ptr = FFI::GDAL.OGR_DS_GetDriver(@data_source_pointer)
|
71
|
+
return nil if driver_ptr.nil?
|
72
|
+
|
73
|
+
@driver = OGR::Driver.new(driver_ptr)
|
74
|
+
end
|
75
|
+
|
76
|
+
# @return [Fixnum]
|
77
|
+
def layer_count
|
78
|
+
FFI::GDAL.OGR_DS_GetLayerCount(@data_source_pointer)
|
79
|
+
end
|
80
|
+
|
81
|
+
# @param index [Fixnum] 0-offset index of the layer to retrieve.
|
82
|
+
# @return [OGR::Layer]
|
83
|
+
def layer(index)
|
84
|
+
@layers.fetch(index) do
|
85
|
+
layer_pointer = FFI::GDAL.OGR_DS_GetLayer(@data_source_pointer, index)
|
86
|
+
return nil if layer_pointer.null?
|
87
|
+
|
88
|
+
l = OGR::Layer.new(layer_pointer)
|
89
|
+
@layers.insert(index, l)
|
90
|
+
|
91
|
+
l
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @param name [String]
|
96
|
+
# @return [OGR::Layer]
|
97
|
+
def layer_by_name(name)
|
98
|
+
layer_pointer = FFI::GDAL.OGR_DS_GetLayerByName(@data_source_pointer, name)
|
99
|
+
return nil if layer_pointer.null?
|
100
|
+
|
101
|
+
OGR::Layer.new(layer_pointer)
|
102
|
+
end
|
103
|
+
|
104
|
+
# @param name [String] The name for the new layer.
|
105
|
+
# @param geometry_type [FFI::GDAL::OGRwkbGeometryType]
|
106
|
+
# @param spatial_reference [OGR::SpatialReference] The coordinate system
|
107
|
+
# to use for the new layer or nil if none is available.
|
108
|
+
# @return [OGR::Layer]
|
109
|
+
def create_layer(name, geometry_type: :wkbUnknown, spatial_reference: nil, **options)
|
110
|
+
spatial_ref_ptr = GDAL._pointer(OGR::SpatialReference, spatial_reference)
|
111
|
+
options_obj = GDAL::Options.pointer(options)
|
112
|
+
|
113
|
+
layer_ptr = FFI::GDAL.OGR_DS_CreateLayer(@data_source_pointer, name,
|
114
|
+
spatial_ref_ptr, geometry_type, options_obj)
|
115
|
+
|
116
|
+
return nil if layer_ptr.null?
|
117
|
+
|
118
|
+
OGR::Layer.new(layer_ptr)
|
119
|
+
end
|
120
|
+
|
121
|
+
# @param source_layer [OGR::Layer, FFI::Pointer]
|
122
|
+
# @param new_name [String]
|
123
|
+
# @param options [Hash]
|
124
|
+
# @return [OGR::Layer, nil]
|
125
|
+
def copy_layer(source_layer, new_name, **options)
|
126
|
+
source_layer_ptr = GDAL._pointer(OGR::Layer, source_layer)
|
127
|
+
options_ptr = GDAL::Options.pointer(options)
|
128
|
+
|
129
|
+
layer_ptr = FFI::GDAL.OGR_DS_CopyLayer(@data_source_pointer, source_layer_ptr,
|
130
|
+
new_name, options_ptr)
|
131
|
+
return nil if layer_ptr.null?
|
132
|
+
|
133
|
+
OGR::Layer.new(layer_ptr)
|
134
|
+
end
|
135
|
+
|
136
|
+
# @param index [Fixnum]
|
137
|
+
# @return +true+ if successful, otherwise raises an OGR exception.
|
138
|
+
def delete_layer(index)
|
139
|
+
ogr_err = FFI::GDAL.OGR_DS_DeleteLayer(@data_source_pointer, index)
|
140
|
+
|
141
|
+
ogr_err.to_ruby
|
142
|
+
end
|
143
|
+
|
144
|
+
# @param command [String] The SQL to execute.
|
145
|
+
# @param spatial_filter [OGR::Geometry, FFI::Pointer]
|
146
|
+
# @param dialect [String] Can pass in 'SQLITE' to use that instead of the
|
147
|
+
# default OGRSQL dialect.
|
148
|
+
# @return [OGR::Layer, nil]
|
149
|
+
# @see http://www.gdal.org/ogr_sql.html
|
150
|
+
# TODO: not sure how to handle the call to OGR_DS_ReleaseResultSet here...
|
151
|
+
def execute_sql(command, spatial_filter=nil, dialect=nil)
|
152
|
+
geometry_ptr = GDAL._pointer(OGR::Geometry, spatial_filter)
|
153
|
+
|
154
|
+
layer_ptr = FFI::GDAL.OGR_DS_ExecuteSQL(@data_source_pointer, command, geometry_ptr,
|
155
|
+
dialect)
|
156
|
+
|
157
|
+
return nil if layer_ptr.null?
|
158
|
+
|
159
|
+
OGR::Layer.new(layer_ptr)
|
160
|
+
end
|
161
|
+
|
162
|
+
# @return [OGR::StyleTable, nil]
|
163
|
+
def style_table
|
164
|
+
return @style_table if @style_table
|
165
|
+
|
166
|
+
style_table_ptr = FFI::GDAL.OGR_DS_GetStyleTable(@data_source_pointer)
|
167
|
+
return nil if style_table_ptr.null?
|
168
|
+
|
169
|
+
@style_table = OGR::StyleTable.new(style_table_ptr)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|