ffi-gdal 1.0.0.beta3 → 1.0.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (286) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +62 -0
  4. data/Gemfile +1 -1
  5. data/History.md +53 -28
  6. data/README.md +6 -0
  7. data/Rakefile +23 -1
  8. data/examples/extract_and_colorize.rb +21 -22
  9. data/examples/geometries.rb +2 -2
  10. data/examples/gridding.rb +106 -0
  11. data/examples/ogr_layer_to_layer.rb +1 -1
  12. data/examples/points.txt +127 -0
  13. data/examples/testing_gdal.rb +3 -4
  14. data/ffi-gdal.gemspec +3 -2
  15. data/lib/ext/error_symbols.rb +2 -57
  16. data/lib/ext/ffi_library_function_checks.rb +26 -0
  17. data/lib/ext/float_ext.rb +2 -2
  18. data/lib/ext/narray_ext.rb +2 -0
  19. data/lib/ext/numeric_as_data_type.rb +19 -0
  20. data/lib/ext/to_bool.rb +4 -4
  21. data/lib/ffi/cpl/conv.rb +132 -0
  22. data/lib/ffi/cpl/error.rb +67 -0
  23. data/lib/ffi/cpl/hash_set.rb +39 -0
  24. data/lib/ffi/cpl/http.rb +25 -0
  25. data/lib/ffi/cpl/http_result.rb +18 -0
  26. data/lib/ffi/cpl/list.rb +38 -0
  27. data/lib/ffi/cpl/mime_part.rb +11 -0
  28. data/lib/ffi/cpl/minixml.rb +47 -0
  29. data/lib/ffi/cpl/port.rb +23 -0
  30. data/lib/ffi/cpl/quad_tree.rb +51 -0
  31. data/lib/ffi/{ogr/ogr_envelope.rb → cpl/rect_obj.rb} +3 -3
  32. data/lib/ffi/cpl/string.rb +95 -0
  33. data/lib/ffi/cpl/vsi.rb +115 -0
  34. data/lib/ffi/cpl/xml_node.rb +6 -6
  35. data/lib/ffi/gdal/alg.rb +385 -0
  36. data/lib/ffi/gdal/{gdal_color_entry.rb → color_entry.rb} +1 -2
  37. data/lib/ffi/gdal/exceptions.rb +6 -0
  38. data/lib/ffi/gdal/{gdal_gcp.rb → gcp.rb} +1 -3
  39. data/lib/ffi/gdal/{gdal_h.rb → gdal.rb} +253 -252
  40. data/lib/ffi/gdal/grid.rb +58 -0
  41. data/lib/ffi/gdal/{gdal_grid_data_metrics_options.rb → grid_data_metrics_options.rb} +1 -2
  42. data/lib/ffi/gdal/{gdal_grid_inverse_distance_to_a_power_options.rb → grid_inverse_distance_to_a_power_options.rb} +1 -2
  43. data/lib/ffi/gdal/{gdal_grid_moving_average_options.rb → grid_moving_average_options.rb} +1 -2
  44. data/lib/ffi/gdal/{gdal_grid_nearest_neighbor_options.rb → grid_nearest_neighbor_options.rb} +1 -2
  45. data/lib/ffi/gdal/matching.rb +20 -0
  46. data/lib/ffi/gdal/{gdal_rpc_info.rb → rpc_info.rb} +1 -2
  47. data/lib/ffi/gdal/{gdal_transformer_info.rb → transformer_info.rb} +1 -2
  48. data/lib/ffi/gdal/version.rb +1 -1
  49. data/lib/ffi/gdal/vrt.rb +92 -0
  50. data/lib/ffi/gdal/{gdal_warp_options.rb → warp_options.rb} +8 -4
  51. data/lib/ffi/gdal/warper.rb +70 -0
  52. data/lib/ffi/gdal.rb +56 -69
  53. data/lib/ffi/ogr/api.rb +567 -0
  54. data/lib/ffi/ogr/{ogr_contour_writer_info.rb → contour_writer_info.rb} +2 -3
  55. data/lib/ffi/ogr/core.rb +181 -0
  56. data/lib/ffi/ogr/envelope.rb +12 -0
  57. data/lib/ffi/ogr/{ogr_envelope_3d.rb → envelope_3d.rb} +2 -2
  58. data/lib/ffi/ogr/featurestyle.rb +29 -0
  59. data/lib/ffi/ogr/field.rb +63 -0
  60. data/lib/ffi/ogr/geocoding.rb +30 -0
  61. data/lib/ffi/ogr/srs_api.rb +407 -0
  62. data/lib/ffi/ogr/style_param.rb +13 -0
  63. data/lib/ffi/ogr/{ogr_style_value.rb → style_value.rb} +4 -3
  64. data/lib/ffi/ogr.rb +7 -7
  65. data/lib/ffi-gdal.rb +7 -145
  66. data/lib/gdal/color_entry.rb +18 -14
  67. data/lib/gdal/color_entry_mixins/extensions.rb +32 -0
  68. data/lib/gdal/color_interpretation.rb +2 -2
  69. data/lib/gdal/color_table.rb +37 -32
  70. data/lib/gdal/color_table_mixins/extensions.rb +48 -0
  71. data/lib/gdal/cpl_error_handler.rb +119 -0
  72. data/lib/gdal/data_type.rb +7 -7
  73. data/lib/gdal/dataset.rb +131 -238
  74. data/lib/gdal/dataset_mixins/algorithm_methods.rb +182 -0
  75. data/lib/gdal/dataset_mixins/extensions.rb +542 -0
  76. data/lib/gdal/dataset_mixins/matching.rb +26 -0
  77. data/lib/gdal/driver.rb +68 -92
  78. data/lib/gdal/driver_mixins/extensions.rb +93 -0
  79. data/lib/gdal/exceptions.rb +32 -4
  80. data/lib/gdal/geo_transform.rb +63 -43
  81. data/lib/gdal/geo_transform_mixins/extensions.rb +57 -0
  82. data/lib/gdal/grid.rb +144 -0
  83. data/lib/gdal/grid_types/data_metrics_base.rb +14 -0
  84. data/lib/gdal/grid_types/inverse_distance_to_a_power.rb +19 -0
  85. data/lib/gdal/grid_types/metric_average_distance.rb +12 -0
  86. data/lib/gdal/grid_types/metric_average_distance_pts.rb +12 -0
  87. data/lib/gdal/grid_types/metric_count.rb +12 -0
  88. data/lib/gdal/grid_types/metric_maximum.rb +12 -0
  89. data/lib/gdal/grid_types/metric_minimum.rb +12 -0
  90. data/lib/gdal/grid_types/metric_range.rb +12 -0
  91. data/lib/gdal/grid_types/moving_average.rb +19 -0
  92. data/lib/gdal/grid_types/nearest_neighbor.rb +19 -0
  93. data/lib/gdal/grid_types.rb +22 -0
  94. data/lib/gdal/internal_helpers.rb +94 -0
  95. data/lib/gdal/major_object.rb +4 -7
  96. data/lib/gdal/options.rb +14 -7
  97. data/lib/gdal/raster_attribute_table.rb +38 -47
  98. data/lib/gdal/raster_attribute_table_mixins/extensions.rb +41 -0
  99. data/lib/gdal/raster_band.rb +193 -227
  100. data/lib/gdal/raster_band_classifier.rb +107 -0
  101. data/lib/gdal/raster_band_mixins/algorithm_methods.rb +292 -0
  102. data/lib/gdal/raster_band_mixins/extensions.rb +238 -0
  103. data/lib/gdal/rpc_info.rb +35 -0
  104. data/lib/gdal/transformer.rb +15 -0
  105. data/lib/gdal/transformers/approximate_transformer.rb +48 -0
  106. data/lib/gdal/transformers/base_general_image_projection_transformer.rb +45 -0
  107. data/lib/gdal/transformers/gcp_transformer.rb +55 -0
  108. data/lib/gdal/transformers/general_image_projection_transformer.rb +31 -0
  109. data/lib/gdal/transformers/general_image_projection_transformer2.rb +52 -0
  110. data/lib/gdal/transformers/general_image_projection_transformer3.rb +25 -0
  111. data/lib/gdal/transformers/geolocation_transformer.rb +42 -0
  112. data/lib/gdal/transformers/reprojection_transformer.rb +39 -0
  113. data/lib/gdal/transformers/rpc_transformer.rb +56 -0
  114. data/lib/gdal/transformers/tps_transformer.rb +40 -0
  115. data/lib/gdal/virtual_dataset.rb +96 -0
  116. data/lib/gdal/warp_operation.rb +20 -23
  117. data/lib/gdal.rb +17 -0
  118. data/lib/ogr/coordinate_transformation.rb +16 -41
  119. data/lib/ogr/data_source.rb +103 -58
  120. data/lib/ogr/data_source_extensions.rb +5 -6
  121. data/lib/ogr/data_source_mixins/capability_methods.rb +27 -0
  122. data/lib/ogr/driver.rb +61 -33
  123. data/lib/ogr/driver_mixins/capability_methods.rb +16 -0
  124. data/lib/ogr/envelope.rb +29 -18
  125. data/lib/ogr/envelope_extensions.rb +70 -49
  126. data/lib/ogr/error_handling.rb +46 -0
  127. data/lib/ogr/exceptions.rb +58 -12
  128. data/lib/ogr/feature.rb +334 -86
  129. data/lib/ogr/feature_definition.rb +94 -51
  130. data/lib/ogr/feature_definition_extensions.rb +36 -13
  131. data/lib/ogr/feature_extensions.rb +62 -11
  132. data/lib/ogr/field.rb +175 -54
  133. data/lib/ogr/field_definition.rb +110 -0
  134. data/lib/ogr/{field_extensions.rb → field_definition_extensions.rb} +4 -5
  135. data/lib/ogr/{geocoding_session.rb → geocoder.rb} +14 -13
  136. data/lib/ogr/geometries/geometry_collection.rb +13 -0
  137. data/lib/ogr/geometries/line_string.rb +35 -0
  138. data/lib/ogr/geometries/linear_ring.rb +11 -0
  139. data/lib/ogr/geometries/multi_line_string.rb +16 -0
  140. data/lib/ogr/geometries/multi_point.rb +14 -0
  141. data/lib/ogr/geometries/multi_polygon.rb +21 -0
  142. data/lib/ogr/geometries/none_geometry.rb +13 -0
  143. data/lib/ogr/geometries/point.rb +65 -0
  144. data/lib/ogr/geometries/point_extensions.rb +32 -0
  145. data/lib/ogr/geometries/polygon.rb +14 -0
  146. data/lib/ogr/geometries/unknown_geometry.rb +10 -0
  147. data/lib/ogr/geometry.rb +270 -242
  148. data/lib/ogr/geometry_extensions.rb +8 -9
  149. data/lib/ogr/geometry_field_definition.rb +99 -0
  150. data/lib/ogr/geometry_field_definition_extensions.rb +19 -0
  151. data/lib/ogr/geometry_types/container.rb +75 -0
  152. data/lib/ogr/geometry_types/curve.rb +25 -28
  153. data/lib/ogr/geometry_types/surface.rb +13 -4
  154. data/lib/ogr/internal_helpers.rb +57 -0
  155. data/lib/ogr/layer.rb +60 -181
  156. data/lib/ogr/layer_mixins/capability_methods.rb +102 -0
  157. data/lib/ogr/layer_mixins/extensions.rb +59 -0
  158. data/lib/ogr/layer_mixins/ogr_feature_methods.rb +127 -0
  159. data/lib/ogr/layer_mixins/ogr_field_methods.rb +143 -0
  160. data/lib/ogr/layer_mixins/ogr_layer_method_methods.rb +163 -0
  161. data/lib/ogr/layer_mixins/ogr_query_filter_methods.rb +89 -0
  162. data/lib/ogr/layer_mixins/ogr_sql_methods.rb +48 -0
  163. data/lib/ogr/spatial_reference.rb +108 -589
  164. data/lib/ogr/spatial_reference_extensions.rb +29 -3
  165. data/lib/ogr/spatial_reference_mixins/coordinate_system_getter_setters.rb +494 -0
  166. data/lib/ogr/spatial_reference_mixins/exporters.rb +134 -0
  167. data/lib/ogr/spatial_reference_mixins/importers.rb +243 -0
  168. data/lib/ogr/spatial_reference_mixins/morphers.rb +25 -0
  169. data/lib/ogr/spatial_reference_mixins/parameter_getter_setters.rb +122 -0
  170. data/lib/ogr/spatial_reference_mixins/type_checks.rb +62 -0
  171. data/lib/ogr/style_table.rb +53 -5
  172. data/lib/ogr/style_table_extensions.rb +21 -5
  173. data/lib/ogr/style_tool.rb +118 -0
  174. data/lib/ogr.rb +8 -0
  175. data/spec/ffi-gdal_spec.rb +1 -2
  176. data/spec/integration/{color_table_info_spec.rb → gdal/color_table_info_spec.rb} +12 -12
  177. data/spec/integration/{dataset_info_spec.rb → gdal/dataset_info_spec.rb} +2 -6
  178. data/spec/integration/{driver_info_spec.rb → gdal/driver_info_spec.rb} +5 -5
  179. data/spec/integration/{geo_transform_info_spec.rb → gdal/geo_transform_info_spec.rb} +1 -14
  180. data/spec/integration/{raster_attribute_table_info_spec.rb → gdal/raster_attribute_table_info_spec.rb} +1 -2
  181. data/spec/integration/{raster_band_info_spec.rb → gdal/raster_band_info_spec.rb} +30 -42
  182. data/spec/integration/ogr/layer_spec.rb +97 -0
  183. data/spec/spec_helper.rb +94 -5
  184. data/spec/support/integration_help.rb +1 -0
  185. data/spec/support/shared_contexts.rb +26 -0
  186. data/spec/support/shared_examples/{major_object_examples.rb → gdal/major_object_examples.rb} +5 -5
  187. data/spec/support/shared_examples/ogr/a_25D_geometry.rb +7 -0
  188. data/spec/support/shared_examples/ogr/a_container_geometry.rb +47 -0
  189. data/spec/support/shared_examples/ogr/a_geometry.rb +404 -0
  190. data/spec/support/shared_examples/ogr/a_line_string.rb +16 -0
  191. data/spec/support/test_style_table.txt +3 -0
  192. data/spec/unit/ext/error_symbols_spec.rb +41 -53
  193. data/spec/unit/ext/numeric_as_data_type_spec.rb +113 -0
  194. data/spec/unit/ffi/gdal_spec.rb +70 -0
  195. data/spec/unit/gdal/color_entry_spec.rb +5 -0
  196. data/spec/unit/gdal/color_interpretation_spec.rb +5 -0
  197. data/spec/unit/gdal/{color_table_extensions_spec.rb → color_table_mixins/extensions_spec.rb} +3 -3
  198. data/spec/unit/gdal/color_table_spec.rb +14 -16
  199. data/spec/unit/gdal/data_type_spec.rb +10 -17
  200. data/spec/unit/gdal/dataset_spec.rb +4 -7
  201. data/spec/unit/gdal/driver_mixins/extensions_spec.rb +22 -0
  202. data/spec/unit/gdal/driver_spec.rb +49 -0
  203. data/spec/unit/gdal/environment_methods_spec.rb +6 -0
  204. data/spec/unit/gdal/geo_transform_spec.rb +276 -0
  205. data/spec/unit/gdal/grid_spec.rb +5 -0
  206. data/spec/unit/gdal/internal_helpers_spec.rb +112 -0
  207. data/spec/unit/gdal/major_object_spec.rb +6 -0
  208. data/spec/unit/gdal/options_spec.rb +5 -0
  209. data/spec/unit/gdal/raster_attribute_table_spec.rb +5 -0
  210. data/spec/unit/gdal/raster_band_classifier_spec.rb +134 -0
  211. data/spec/unit/gdal/raster_band_spec.rb +5 -0
  212. data/spec/unit/gdal/rpc_info_spec.rb +5 -0
  213. data/spec/unit/gdal/version_info_spec.rb +6 -0
  214. data/spec/unit/gdal/virtual_dataset_spec.rb +32 -0
  215. data/spec/unit/gdal/warp_operation_spec.rb +5 -0
  216. data/spec/unit/ogr/data_source_mixins/capability_methods_spec.rb +30 -0
  217. data/spec/unit/ogr/data_source_spec.rb +209 -0
  218. data/spec/unit/ogr/driver_mixins/capability_methods_spec.rb +18 -0
  219. data/spec/unit/ogr/driver_spec.rb +150 -0
  220. data/spec/unit/ogr/envelope_spec.rb +322 -0
  221. data/spec/unit/ogr/feature_definition_spec.rb +313 -0
  222. data/spec/unit/ogr/feature_spec.rb +379 -0
  223. data/spec/unit/ogr/field_definition_spec.rb +135 -0
  224. data/spec/unit/ogr/field_spec.rb +193 -0
  225. data/spec/unit/ogr/geometries/geometry_collection_spec.rb +186 -0
  226. data/spec/unit/ogr/geometries/line_string_spec.rb +105 -0
  227. data/spec/unit/ogr/{linear_ring_spec.rb → geometries/linear_ring_spec.rb} +10 -31
  228. data/spec/unit/ogr/geometries/multi_line_string_spec.rb +14 -0
  229. data/spec/unit/ogr/geometries/multi_point_spec.rb +14 -0
  230. data/spec/unit/ogr/geometries/multi_polygon_spec.rb +41 -0
  231. data/spec/unit/ogr/geometries/none_geometry_spec.rb +12 -0
  232. data/spec/unit/ogr/{point_spec.rb → geometries/point_spec.rb} +19 -25
  233. data/spec/unit/ogr/geometries/polygon_spec.rb +17 -0
  234. data/spec/unit/ogr/geometries/unknown_geometry_spec.rb +10 -0
  235. data/spec/unit/ogr/geometry_field_definition_spec.rb +87 -0
  236. data/spec/unit/ogr/geometry_spec.rb +542 -0
  237. data/spec/unit/ogr/internal_helpers_spec.rb +57 -0
  238. data/spec/unit/ogr/layer_mixins/capability_methods_spec.rb +88 -0
  239. data/spec/unit/ogr/layer_mixins/ogr_feature_methods_spec.rb +145 -0
  240. data/spec/unit/ogr/layer_mixins/ogr_field_methods_spec.rb +432 -0
  241. data/spec/unit/ogr/layer_mixins/ogr_layer_method_methods_spec.rb +20 -0
  242. data/spec/unit/ogr/layer_mixins/ogr_query_filter_methods_spec.rb +42 -0
  243. data/spec/unit/ogr/layer_mixins/ogr_sql_methods_spec.rb +12 -0
  244. data/spec/unit/ogr/layer_spec.rb +66 -67
  245. data/spec/unit/ogr/spatial_reference_mixins/coordinate_system_getter_setters_spec.rb +46 -0
  246. data/spec/unit/ogr/spatial_reference_mixins/exporters_spec.rb +139 -0
  247. data/spec/unit/ogr/spatial_reference_mixins/importers_spec.rb +38 -0
  248. data/spec/unit/ogr/spatial_reference_mixins/morphers_spec.rb +36 -0
  249. data/spec/unit/ogr/spatial_reference_mixins/parameter_getter_setters_spec.rb +102 -0
  250. data/spec/unit/ogr/spatial_reference_mixins/type_checks_spec.rb +157 -0
  251. data/spec/unit/ogr/spatial_reference_spec.rb +42 -0
  252. data/spec/unit/ogr/style_table_spec.rb +132 -0
  253. data/spec/unit/ogr/style_tool_spec.rb +157 -0
  254. data/spec/unit/version_info_spec.rb +1 -1
  255. metadata +285 -75
  256. data/lib/ffi/cpl/conv_h.rb +0 -143
  257. data/lib/ffi/cpl/error_h.rb +0 -63
  258. data/lib/ffi/cpl/minixml_h.rb +0 -14
  259. data/lib/ffi/cpl/string_h.rb +0 -81
  260. data/lib/ffi/cpl/vsi_h.rb +0 -112
  261. data/lib/ffi/gdal/alg_h.rb +0 -127
  262. data/lib/ffi/gdal/grid_h.rb +0 -51
  263. data/lib/ffi/gdal/warper_h.rb +0 -48
  264. data/lib/ffi/ogr/api_h.rb +0 -553
  265. data/lib/ffi/ogr/core_h.rb +0 -148
  266. data/lib/ffi/ogr/featurestyle_h.rb +0 -22
  267. data/lib/ffi/ogr/geocoding_h.rb +0 -21
  268. data/lib/ffi/ogr/ogr_field.rb +0 -50
  269. data/lib/ffi/ogr/ogr_style_param.rb +0 -12
  270. data/lib/ffi/ogr/srs_api_h.rb +0 -325
  271. data/lib/gdal/color_entry_extensions.rb +0 -30
  272. data/lib/gdal/color_table_extensions.rb +0 -47
  273. data/lib/gdal/dataset_extensions.rb +0 -496
  274. data/lib/gdal/driver_extensions.rb +0 -56
  275. data/lib/gdal/geo_transform_extensions.rb +0 -90
  276. data/lib/gdal/raster_attribute_table_extensions.rb +0 -40
  277. data/lib/gdal/raster_band_extensions.rb +0 -198
  278. data/lib/ogr/geometry_types/collection.rb +0 -45
  279. data/lib/ogr/layer_extensions.rb +0 -55
  280. data/lib/ogr/line_string.rb +0 -7
  281. data/lib/ogr/linear_ring.rb +0 -6
  282. data/lib/ogr/multi_line_string.rb +0 -9
  283. data/lib/ogr/multi_point.rb +0 -7
  284. data/lib/ogr/multi_polygon.rb +0 -14
  285. data/lib/ogr/point.rb +0 -89
  286. data/lib/ogr/polygon.rb +0 -9
@@ -0,0 +1,57 @@
1
+ require 'json'
2
+
3
+ module GDAL
4
+ module GeoTransformMixins
5
+ module Extensions
6
+ # Calculates the pixel and line location of a geospatial coordinate. Used
7
+ # for converting from world coordinates to to image pixels.
8
+ #
9
+ # @param x_geo [Fixnum]
10
+ # @param y_geo [Fixnum]
11
+ # @return [Hash{pixel => Fixnum, line Fixnum}]
12
+ def world_to_pixel(x_geo, y_geo)
13
+ pixel = (x_geo - x_origin) / pixel_width
14
+ line = (y_origin - y_geo) / pixel_height
15
+
16
+ { pixel: pixel.round.to_i, line: line.round.to_i }
17
+ end
18
+
19
+ # All attributes as an Array, in the order the C-Struct describes them:
20
+ # * x_origin
21
+ # * pixel_width
22
+ # * x_rotation
23
+ # * y_origin
24
+ # * y_rotation
25
+ # * pixel_height
26
+ #
27
+ # @return [Array]
28
+ def to_a
29
+ [
30
+ x_origin,
31
+ pixel_width,
32
+ x_rotation,
33
+ y_origin,
34
+ y_rotation,
35
+ pixel_height
36
+ ]
37
+ end
38
+
39
+ # @return [Hash]
40
+ def as_json(_options = nil)
41
+ {
42
+ x_origin: x_origin,
43
+ x_rotation: x_rotation,
44
+ pixel_width: pixel_width,
45
+ y_origin: y_origin,
46
+ y_rotation: y_rotation,
47
+ pixel_height: pixel_height
48
+ }
49
+ end
50
+
51
+ # @return [String]
52
+ def to_json(options = nil)
53
+ as_json(options).to_json
54
+ end
55
+ end
56
+ end
57
+ end
data/lib/gdal/grid.rb ADDED
@@ -0,0 +1,144 @@
1
+ require 'forwardable'
2
+ require 'narray'
3
+ require_relative '../ffi/gdal/alg'
4
+ require_relative 'grid_types'
5
+ require_relative 'geo_transform'
6
+
7
+ module GDAL
8
+ # Wrapper for GDAL's [Grid API](http://www.gdal.org/grid_tutorial.html).
9
+ class Grid
10
+ extend Forwardable
11
+ include GDAL::Logger
12
+
13
+ # @return [NArray]
14
+ attr_accessor :points
15
+
16
+ # @return [GDAL::GeoTransform]
17
+ attr_reader :geo_transform
18
+
19
+ # @return [FFI::GDAL::DataType]
20
+ attr_accessor :data_type
21
+
22
+ def_delegator :@gridder, :options, :options
23
+
24
+ # @param algorithm [Symbol]
25
+ # @param data_type [FFI::GDAL::DataType]
26
+ def initialize(algorithm, data_type: :GDT_Byte)
27
+ @data_type = data_type
28
+ @gridder = init_gridder(algorithm)
29
+ @points = nil
30
+ @geo_transform = GDAL::GeoTransform.new
31
+ end
32
+
33
+ # @return [FFI::MemoryPointer] Pointer to the grid data.
34
+ def create(&progress_block)
35
+ update_geo_transform
36
+
37
+ log "Number of points: #{point_count}"
38
+ x_coordinates_ptr = FFI::MemoryPointer.new(:double, point_count)
39
+ y_coordinates_ptr = FFI::MemoryPointer.new(:double, point_count)
40
+ x_coordinates_ptr.write_array_of_float(@points[0, true].to_a)
41
+ y_coordinates_ptr.write_array_of_float(@points[1, true].to_a)
42
+
43
+ if @points.shape.first == 3
44
+ z_coordinates_ptr = FFI::MemoryPointer.new(:double, point_count)
45
+ z_coordinates_ptr.write_array_of_float(@points[2, true].to_a)
46
+ else
47
+ z_coordinates_ptr = nil
48
+ end
49
+
50
+ log "x_min, y_min: #{x_min}, #{y_min}"
51
+ log "x_max, y_max: #{x_max}, #{y_max}"
52
+ log "x_size, y_size: #{x_size}, #{y_size}"
53
+ log "pixel_width: #{@geo_transform.pixel_width}"
54
+ log "pixel_height: #{@geo_transform.pixel_height}"
55
+ data_ptr = FFI::MemoryPointer.new(:buffer_inout, x_size * y_size)
56
+
57
+ # gdal_grid.cpp lists this as the corner coordinates, which ends up being
58
+ # larger than my x_max/y_min test values. That seems odd.
59
+ # x_end = x_max + (@geo_transform.x_size(x_max) / 2)
60
+ # y_end = y_min - (@geo_transform.y_size(y_max) / 2)
61
+ max_pixel = @geo_transform.world_to_pixel(x_max, y_max)
62
+ x_end = x_max + (max_pixel[:pixel] / 2)
63
+ y_end = y_min - (max_pixel[:line] / 2)
64
+ log "corner x1 (gdal_grid.cpp): #{x_end}"
65
+ log "corner y1 (gdal_grid.cpp): #{y_end}"
66
+
67
+ FFI::GDAL::Alg.GDALGridCreate(
68
+ @gridder.algorithm, # eAlgorithm
69
+ @gridder.options.to_ptr, # poOptions
70
+ point_count, # nPoints
71
+ x_coordinates_ptr, # padfX
72
+ y_coordinates_ptr, # padfY
73
+ z_coordinates_ptr, # padfZ
74
+ x_min, # dfXMin
75
+ x_max, # dfXMax
76
+ y_min, # dfYMin
77
+ y_max, # dfYMax
78
+ x_size, # nXSize
79
+ y_size, # nYSize
80
+ @data_type, # eType
81
+ data_ptr, # pData,
82
+ progress_block, # pfnProgress
83
+ nil # pProgressArg
84
+ )
85
+
86
+ data_ptr
87
+ end
88
+
89
+ # @return [Fixnum] The number of points (x,y,z) in the grid.
90
+ def point_count
91
+ @points.shape.last
92
+ end
93
+
94
+ def x_min
95
+ @points.nil? ? nil : @points[0, true].to_a.compact.min
96
+ end
97
+
98
+ def x_max
99
+ @points.nil? ? nil : @points[0, true].to_a.compact.max
100
+ end
101
+
102
+ def x_size
103
+ @points.nil? ? nil : (x_max - x_min)
104
+ end
105
+
106
+ def y_min
107
+ @points.nil? ? nil : @points[1, true].to_a.compact.min
108
+ end
109
+
110
+ def y_max
111
+ @points.nil? ? nil : @points[1, true].to_a.compact.max
112
+ end
113
+
114
+ def y_size
115
+ @points.nil? ? nil : (y_max - y_min)
116
+ end
117
+
118
+ private
119
+
120
+ def init_gridder(algorithm)
121
+ case algorithm
122
+ when :inverse_distance_to_a_power then GDAL::GridTypes::InverseDistanceToAPower.new
123
+ when :moving_average then GDAL::GridTypes::MovingAverage.new
124
+ when :nearest_neighbor then GDAL::GridTypes::NearestNeighbor.new
125
+ when :metric_average_distance then GDAL::GridTypes::MetricAverageDistance.new
126
+ when :metric_average_distance_pts then GDAL::GridTypes::MetricAverageDistancePts.new
127
+ when :metric_count then GDAL::GridTypes::MetricCount.new
128
+ when :metric_maximum then GDAL::GridTypes::MetricMaximum.new
129
+ when :metric_minimum then GDAL::GridTypes::MetricMinimum.new
130
+ when :metric_range then GDAL::GridTypes::MetricRange.new
131
+ else fail GDAL::UnknownGridAlgorithm, algorithm
132
+ end
133
+ end
134
+
135
+ def update_geo_transform
136
+ return if @points.nil?
137
+
138
+ @geo_transform.x_origin = x_min
139
+ @geo_transform.y_origin = y_max
140
+ @geo_transform.pixel_width = (x_max - x_min) / x_size
141
+ @geo_transform.pixel_height = (y_max - y_min) / y_size
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,14 @@
1
+ require_relative '../../ffi/gdal/gdal_grid_data_metrics_options'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class DataMetricsBase
6
+ # @return [FFI::GDAL::GridDataMetricsOptions]
7
+ attr_reader :options
8
+
9
+ def initialize
10
+ @options = FFI::GDAL::GridDataMetricsOptions.new
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../../ffi/gdal/gdal_grid_inverse_distance_to_a_power_options'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class InverseDistanceToAPower
6
+ # @return [FFI::GDAL::GridInverseDistanceToAPowerOptions]
7
+ attr_reader :options
8
+
9
+ def initialize
10
+ @options = FFI::GDAL::GridInverseDistanceToAPowerOptions.new
11
+ end
12
+
13
+ # @return [Symbol]
14
+ def algorithm
15
+ :GGA_InverseDistanceToAPower
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'data_metrics_base'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class MetricAverageDistance < DataMetricsBase
6
+ # @return [Symbol]
7
+ def algorithm
8
+ :GGA_MetricAverageDistance
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'data_metrics_base'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class MetricAverageDistancePts < DataMetricsBase
6
+ # @return [Symbol]
7
+ def algorithm
8
+ :GGA_MetricAverageDistancePts
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'data_metrics_base'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class MetricCount < DataMetricsBase
6
+ # @return [Symbol]
7
+ def algorithm
8
+ :GGA_MetricCount
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'data_metrics_base'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class MetricMaximum < DataMetricsBase
6
+ # @return [Symbol]
7
+ def algorithm
8
+ :GGA_MetricMaximum
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'data_metrics_base'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class MetricMinimum < DataMetricsBase
6
+ # @return [Symbol]
7
+ def algorithm
8
+ :GGA_MetricMinimum
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'data_metrics_base'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class MetricRange < DataMetricsBase
6
+ # @return [Symbol]
7
+ def algorithm
8
+ :GGA_MetricRange
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../../ffi/gdal/gdal_grid_moving_average_options'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class MovingAverage
6
+ # @return [FFI::GDAL::GridMovingAverageOptions]
7
+ attr_reader :options
8
+
9
+ def initialize
10
+ @options = FFI::GDAL::GridMovingAverageOptions.new
11
+ end
12
+
13
+ # @return [Symbol]
14
+ def algorithm
15
+ :GGA_MovingAverage
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../../ffi/gdal/gdal_grid_nearest_neighbor_options'
2
+
3
+ module GDAL
4
+ module GridTypes
5
+ class NearestNeighbor
6
+ # @return [FFI::GDAL::GridNearestNeighborOptions]
7
+ attr_reader :options
8
+
9
+ def initialize
10
+ @options = FFI::GDAL::GridNearestNeighborOptions.new
11
+ end
12
+
13
+ # @return [Symbol]
14
+ def algorithm
15
+ :GGA_NearestNeighbor
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module GDAL
2
+ module GridTypes
3
+ autoload :InverseDistanceToAPower,
4
+ File.expand_path('grid_types/inverse_distance_to_a_power', __dir__)
5
+ autoload :MetricAverageDistance,
6
+ File.expand_path('grid_types/metric_average_distance', __dir__)
7
+ autoload :MetricAverageDistancePts,
8
+ File.expand_path('grid_types/metric_average_distance_pts', __dir__)
9
+ autoload :MetricCount,
10
+ File.expand_path('grid_types/metric_count', __dir__)
11
+ autoload :MetricMaximum,
12
+ File.expand_path('grid_types/metric_maximum', __dir__)
13
+ autoload :MetricMinimum,
14
+ File.expand_path('grid_types/metric_minimum', __dir__)
15
+ autoload :MetricRange,
16
+ File.expand_path('grid_types/metric_range', __dir__)
17
+ autoload :MovingAverage,
18
+ File.expand_path('grid_types/moving_average', __dir__)
19
+ autoload :NearestNeighbor,
20
+ File.expand_path('grid_types/nearest_neighbor', __dir__)
21
+ end
22
+ end
@@ -0,0 +1,94 @@
1
+ require 'log_switch'
2
+
3
+ module GDAL
4
+ module Logger
5
+ include LogSwitch
6
+ end
7
+
8
+ module InternalHelpers
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+ # Internal factory method for returning a pointer from +variable+, which could
15
+ # be either of +klass+ class or a type of FFI::Pointer.
16
+ def _pointer(klass, variable, warn_on_nil = true)
17
+ if variable.is_a?(klass)
18
+ variable.c_pointer.autorelease = true
19
+ variable.c_pointer
20
+ elsif variable.is_a? FFI::Pointer
21
+ variable.autorelease = true
22
+ variable
23
+ else
24
+ if warn_on_nil && Logger.logging_enabled
25
+ Logger.logger.debug "<#{name}._pointer> #{variable.inspect} is not a valid #{klass} or FFI::Pointer."
26
+ Logger.logger.debug "<#{name}._pointer> Called at:"
27
+ caller(1, 2).each { |line| Logger.logger.debug "<#{name}._pointer>\t#{line}" }
28
+ end
29
+
30
+ nil
31
+ end
32
+ end
33
+
34
+ # @param data_type [FFI::GDAL::DataType]
35
+ # @return [Symbol] The FFI Symbol that represents a data type.
36
+ def _pointer_from_data_type(data_type, size = nil)
37
+ pointer_type = _gdal_data_type_to_ffi(data_type)
38
+
39
+ if size
40
+ FFI::MemoryPointer.new(pointer_type, size)
41
+ else
42
+ FFI::MemoryPointer.new(pointer_type)
43
+ end
44
+ end
45
+
46
+ # Takes an array of strings (or things that should be converted to
47
+ # strings) and creates a char**.
48
+ #
49
+ # @param strings [Array<String>]
50
+ # @return [FFI::MemoryPointer]
51
+ def _string_array_to_pointer(strings)
52
+ string_pointers = strings.map do |string|
53
+ FFI::MemoryPointer.from_string(string.to_s)
54
+ end
55
+
56
+ string_pointers << nil
57
+ array_pointer = FFI::MemoryPointer.new(:pointer, strings.size + 1)
58
+
59
+ string_pointers.each_with_index do |ptr, i|
60
+ array_pointer[i].put_pointer(0, ptr)
61
+ end
62
+
63
+ array_pointer
64
+ end
65
+
66
+ # Maps GDAL DataTypes to FFI types.
67
+ #
68
+ # @param data_type [FFI::GDAL::DataType]
69
+ def _gdal_data_type_to_ffi(data_type)
70
+ case data_type
71
+ when :GDT_Byte then :uchar
72
+ when :GDT_UInt16 then :uint16
73
+ when :GDT_Int16 then :int16
74
+ when :GDT_UInt32 then :uint32
75
+ when :GDT_Int32 then :int32
76
+ when :GDT_Float32 then :float
77
+ when :GDT_Float64 then :double
78
+ else
79
+ :float
80
+ end
81
+ end
82
+
83
+ # Check to see if the function is supported in the version of GDAL that we're
84
+ # using.
85
+ #
86
+ # @param function_name [Symbol]
87
+ # @return [Boolean]
88
+ def _supported?(function_name)
89
+ !FFI::GDAL.unsupported_gdal_functions.include?(function_name) &&
90
+ FFI::GDAL.respond_to?(function_name)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -1,6 +1,5 @@
1
1
  require_relative '../ffi/gdal'
2
2
 
3
-
4
3
  module GDAL
5
4
  module MajorObject
6
5
  # @return [Array<String>]
@@ -20,7 +19,7 @@ module GDAL
20
19
 
21
20
  # @param domain [String] Name of the domain to get metadata for.
22
21
  # @return [Hash]
23
- def metadata(domain='')
22
+ def metadata(domain = '')
24
23
  m = FFI::GDAL.GDALGetMetadata(c_pointer, domain)
25
24
  return {} if m.null?
26
25
 
@@ -40,14 +39,12 @@ module GDAL
40
39
  # @param name [String]
41
40
  # @param domain [String]
42
41
  # @return [String]
43
- def metadata_item(name, domain='')
42
+ def metadata_item(name, domain = '')
44
43
  FFI::GDAL.GDALGetMetadataItem(c_pointer, name, domain)
45
44
  end
46
45
 
47
- def set_metadata_item(name, value, domain='')
48
- cpl_err = FFI::GDAL.GDALSetMetadataItem(c_pointer, name, value.to_s, domain)
49
-
50
- cpl_err.to_bool
46
+ def set_metadata_item(name, value, domain = '')
47
+ FFI::GDAL.GDALSetMetadataItem(c_pointer, name, value.to_s, domain)
51
48
  end
52
49
 
53
50
  # @return [Hash{domain => Array<String>}]
data/lib/gdal/options.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'ffi'
2
-
2
+ require_relative '../ffi/cpl/string'
3
3
 
4
4
  module GDAL
5
5
  # A wrapper for the way GDAL does key/value pair options for methods.
@@ -8,12 +8,18 @@ module GDAL
8
8
  # them.
9
9
  #
10
10
  # @param hash [Hash]
11
+ # @param nil_on_empty [Boolean] When +true+, if +hash+ is empty, return
12
+ # +nil+. If +false+, creates a 0-size pointer.
11
13
  # @return [FFI::MemoryPointer, nil]
12
- def self.pointer(hash)
13
- hash.empty? ? nil : new(hash).c_pointer
14
+ def self.pointer(hash, nil_on_empty: true)
15
+ if nil_on_empty
16
+ hash.empty? ? nil : new(hash).c_pointer
17
+ else
18
+ new(hash).c_pointer
19
+ end
14
20
  end
15
21
 
16
- def initialize(hash={})
22
+ def initialize(hash = {})
17
23
  super()
18
24
  capitalize_keys!(hash)
19
25
  end
@@ -21,10 +27,10 @@ module GDAL
21
27
  # @return [FFI::MemoryPointer] The double-char pointer that contains the
22
28
  # options for GDAL to use.
23
29
  def c_pointer
24
- options_ptr = FFI::MemoryPointer.new(:pointer, self.size)
30
+ options_ptr = FFI::MemoryPointer.new(:pointer, size)
25
31
 
26
- self.each do |key, value|
27
- options_ptr = FFI::GDAL.CSLSetNameValue(options_ptr, key, value)
32
+ each do |key, value|
33
+ options_ptr = FFI::CPL::String.CSLSetNameValue(options_ptr, key, value)
28
34
  end
29
35
 
30
36
  options_ptr
@@ -38,6 +44,7 @@ module GDAL
38
44
  # options_array[i].first.read_string
39
45
  # end
40
46
  # end
47
+
41
48
  private
42
49
 
43
50
  def capitalize_keys!(hash)