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
data/lib/gdal/driver.rb CHANGED
@@ -1,15 +1,18 @@
1
1
  require_relative '../ffi/gdal'
2
2
  require_relative 'major_object'
3
- require_relative 'driver_extensions'
3
+ require_relative 'exceptions'
4
+ require_relative 'driver_mixins/extensions'
5
+ require_relative 'dataset'
6
+ require_relative 'options'
4
7
  require 'multi_xml'
5
- require 'log_switch'
6
-
7
8
 
8
9
  module GDAL
10
+ # Wrapper for GDAL drivers (aka "formats"). Useful for opening and working
11
+ # with GDAL datasets.
9
12
  class Driver
10
13
  include MajorObject
11
14
  include GDAL::Logger
12
- include DriverExtensions
15
+ include DriverMixins::Extensions
13
16
 
14
17
  GDAL_DOCS_URL = 'http://gdal.org'
15
18
 
@@ -20,55 +23,25 @@ module GDAL
20
23
 
21
24
  # @param name [String] Short name of the registered GDALDriver.
22
25
  # @return [GDAL::Driver]
26
+ # @raise [GDAL::InvalidDriverName] If +name+ does not represent a valid
27
+ # driver name.
23
28
  def self.by_name(name)
24
29
  driver_ptr = FFI::GDAL.GDALGetDriverByName(name)
25
- return nil if driver_ptr.null?
26
-
27
- new(driver_ptr)
28
- end
29
-
30
- # @return [Array<String>]
31
- def self.short_names
32
- return @short_names if @short_names
33
-
34
- names = 0.upto(count - 1).map do |i|
35
- driver = at_index(i)
36
- driver.short_name
37
- end
38
-
39
- @short_names = names.compact.sort
40
- end
41
-
42
- # @return [Array<String>]
43
- def self.long_names
44
- return @long_names if @long_names
45
30
 
46
- names = 0.upto(count - 1).map do |i|
47
- at_index(i).long_name
31
+ if driver_ptr.null?
32
+ fail InvalidDriverName, "'#{name}' is not a valid driver name."
48
33
  end
49
34
 
50
- @long_names = names.compact.sort
51
- end
52
-
53
- # @return [Hash{String => String}] Keys are driver short names, values are
54
- # driver long names.
55
- def self.names
56
- return @names if @names
57
-
58
- names = 0.upto(count - 1).each_with_object({}) do |i, obj|
59
- driver = at_index(i)
60
- obj[driver.short_name] = driver.long_name
61
- end
62
-
63
- @names = Hash[names.sort]
35
+ new(driver_ptr)
64
36
  end
65
37
 
66
38
  # @param index [Fixnum] Index of the registered driver. Must be less than
67
39
  # GDAL::Driver.count.
68
40
  # @return [GDAL::Driver]
41
+ # @raise [GDAL::InvalidDriverIndex] If driver at +index+ does not exist.
69
42
  def self.at_index(index)
70
43
  if index > count
71
- raise "index must be between 0 and #{count - 1}."
44
+ fail InvalidDriverIndex, "index must be between 0 and #{count - 1}."
72
45
  end
73
46
 
74
47
  driver_ptr = FFI::GDAL.GDALGetDriver(index)
@@ -77,35 +50,34 @@ module GDAL
77
50
  end
78
51
 
79
52
  # @param file_path [String] File to get the driver for.
80
- # @return [GDAL::Driver]
53
+ # @return [GDAL::Driver] Returns nil if the file is unsupported.
81
54
  def self.identify_driver(file_path)
82
55
  driver_ptr = FFI::GDAL.GDALIdentifyDriver(::File.expand_path(file_path), nil)
56
+ return nil if driver_ptr.null?
83
57
 
84
58
  new(driver_ptr)
85
59
  end
86
60
 
61
+ attr_reader :c_pointer
62
+
87
63
  # @param driver [GDAL::Driver, FFI::Pointer]
88
64
  def initialize(driver)
89
- @driver_pointer = GDAL._pointer(GDAL::Driver, driver)
90
- end
91
-
92
- def c_pointer
93
- @driver_pointer
65
+ @c_pointer = GDAL._pointer(GDAL::Driver, driver)
94
66
  end
95
67
 
96
68
  # @return [String]
97
69
  def short_name
98
- FFI::GDAL.GDALGetDriverShortName(@driver_pointer)
70
+ FFI::GDAL.GDALGetDriverShortName(@c_pointer)
99
71
  end
100
72
 
101
73
  # @return [String]
102
74
  def long_name
103
- FFI::GDAL.GDALGetDriverLongName(@driver_pointer)
75
+ FFI::GDAL.GDALGetDriverLongName(@c_pointer)
104
76
  end
105
77
 
106
78
  # @return [String]
107
79
  def help_topic
108
- "#{GDAL_DOCS_URL}/#{FFI::GDAL.GDALGetDriverHelpTopic(@driver_pointer)}"
80
+ "#{GDAL_DOCS_URL}/#{FFI::GDAL.GDALGetDriverHelpTopic(@c_pointer)}"
109
81
  end
110
82
 
111
83
  # Lists and describes the options that can be used when calling
@@ -113,9 +85,9 @@ module GDAL
113
85
  #
114
86
  # @return [Array]
115
87
  def creation_option_list
116
- return [] unless @driver_pointer
88
+ return [] unless @c_pointer
117
89
 
118
- creation_option_list_xml = FFI::GDAL.GDALGetDriverCreationOptionList(@driver_pointer)
90
+ creation_option_list_xml = FFI::GDAL.GDALGetDriverCreationOptionList(@c_pointer)
119
91
  root = MultiXml.parse(creation_option_list_xml)
120
92
  return [] if root.nil? || root.empty?
121
93
 
@@ -129,12 +101,12 @@ module GDAL
129
101
  # @return [Boolean]
130
102
  def validate_creation_options(options)
131
103
  options_pointer = if options.is_a? GDAL::Options
132
- options.c_pointer
133
- else
134
- GDAL::Options.pointer(options)
135
- end
104
+ options.c_pointer
105
+ else
106
+ GDAL::Options.pointer(options)
107
+ end
136
108
 
137
- FFI::GDAL.GDALValidateCreationOptions(@driver_pointer, options_pointer).to_bool
109
+ FFI::GDAL.GDALValidateCreationOptions(@c_pointer, options_pointer).to_bool
138
110
  end
139
111
 
140
112
  # Copy all of the associated files of a dataset from one file to another.
@@ -143,10 +115,8 @@ module GDAL
143
115
  # @param old_name [String]
144
116
  # @return true on success, false on warning.
145
117
  # @raise [GDAL::CPLErrFailure] If failures.
146
- def copy_dataset_files(new_name, old_name)
147
- cpl_err = FFI::GDAL.GDALCopyDatasetFiles(@driver_pointer, new_name, old_name)
148
-
149
- cpl_err.to_bool
118
+ def copy_dataset_files(old_name, new_name)
119
+ FFI::GDAL.GDALCopyDatasetFiles(@c_pointer, new_name, old_name)
150
120
  end
151
121
 
152
122
  # Create a new Dataset with this driver. Legal arguments depend on the
@@ -156,53 +126,46 @@ module GDAL
156
126
  # @param filename [String]
157
127
  # @param x_size [Fixnum] Width of created raster in pixels.
158
128
  # @param y_size [Fixnum] Height of created raster in pixels.
159
- # @param bands [Fixnum]
160
- # @param data_type [FFI::GDAL::GDALDataType]
161
- # @return [GDAL::Dataset] Returns the *closed* dataset. You'll need to
162
- # reopen it if you with to continue working with it.
163
- # @todo Implement options.
164
- def create_dataset(filename, x_size, y_size, bands: 1, data_type: :GDT_Byte, **options)
129
+ # @param band_count [Fixnum]
130
+ # @param data_type [FFI::GDAL::DataType]
131
+ # @return [GDAL::Dataset] Returns the *open* dataset. You'll need to
132
+ # close it.
133
+ def create_dataset(filename, x_size, y_size, band_count: 1, data_type: :GDT_Byte, **options)
165
134
  options_pointer = GDAL::Options.pointer(options)
166
135
 
167
- dataset_pointer = FFI::GDAL.GDALCreate(@driver_pointer,
136
+ dataset_pointer = FFI::GDAL.GDALCreate(
137
+ @c_pointer,
168
138
  filename,
169
139
  x_size,
170
140
  y_size,
171
- bands,
141
+ band_count,
172
142
  data_type,
173
143
  options_pointer
174
144
  )
175
145
 
176
- raise CreateFail if dataset_pointer.null?
146
+ fail CreateFail if dataset_pointer.null?
177
147
 
178
- dataset = Dataset.new(dataset_pointer)
148
+ dataset = GDAL::Dataset.new(dataset_pointer, nil)
179
149
  yield(dataset) if block_given?
180
150
 
181
151
  dataset
182
152
  end
183
153
 
184
- # @param filename [String] The name for the new dataset file.
185
154
  # @param source_dataset [GDAL::Dataset, FFI::Pointer] The dataset to copy.
155
+ # @param destination_path [String] The name for the new dataset file.
186
156
  # @param strict [Boolean] +false+ indicates the copy may adapt as needed for
187
157
  # the output format.
188
158
  # @param options [Hash]
189
159
  # @param progress [Proc] For outputting copy progress. Conforms to the
190
160
  # FFI::GDAL::GDALProgressFunc signature.
191
- def copy_dataset(filename, source_dataset, strict: true, **options, &progress)
161
+ def copy_dataset(source_dataset, destination_path, strict: true, **options, &progress)
192
162
  options_ptr = GDAL::Options.pointer(options)
163
+ source_dataset_ptr = make_dataset_pointer(source_dataset)
164
+ fail "Source dataset couldn't be read" if source_dataset_ptr.null?
193
165
 
194
- source_dataset_ptr = if source_dataset.is_a? GDAL::Dataset
195
- source_dataset.c_pointer
196
- elsif source_dataset.is_a? String
197
- GDAL::Dataset.open(source_dataset, 'r').c_pointer
198
- else
199
- source_dataset
200
- end
201
-
202
- raise "Source dataset couldn't be read" if source_dataset_ptr.null?
203
-
204
- destination_dataset_ptr = FFI::GDAL.GDALCreateCopy(@driver_pointer,
205
- filename,
166
+ destination_dataset_ptr = FFI::GDAL.GDALCreateCopy(
167
+ @c_pointer,
168
+ destination_path,
206
169
  source_dataset_ptr,
207
170
  strict,
208
171
  options_ptr,
@@ -210,9 +173,9 @@ module GDAL
210
173
  nil
211
174
  )
212
175
 
213
- raise CreateFail if destination_dataset_ptr.null?
176
+ fail CreateFail if destination_dataset_ptr.null?
214
177
 
215
- dataset = Dataset.new(destination_dataset_ptr)
178
+ dataset = Dataset.new(destination_dataset_ptr, 'w')
216
179
  yield(dataset) if block_given?
217
180
  dataset.close
218
181
 
@@ -226,9 +189,7 @@ module GDAL
226
189
  # @return true on success, false on warning.
227
190
  # @raise [GDAL::CPLErrFailure] If failures.
228
191
  def delete_dataset(file_name)
229
- cpl_err = FFI::GDAL.GDALDeleteDataset(@driver_pointer, file_name)
230
-
231
- cpl_err.to_bool
192
+ FFI::GDAL.GDALDeleteDataset(@c_pointer, file_name)
232
193
  end
233
194
 
234
195
  # @param new_name [String]
@@ -236,9 +197,24 @@ module GDAL
236
197
  # @return true on success, false on warning.
237
198
  # @raise [GDAL::CPLErrFailure] If failures.
238
199
  def rename_dataset(new_name, old_name)
239
- cpl_err = FFI::GDAL.GDALRenameDataset(@driver_pointer, new_name, old_name)
200
+ FFI::GDAL.GDALRenameDataset(@c_pointer, new_name, old_name)
201
+ end
240
202
 
241
- cpl_err.to_bool
203
+ private
204
+
205
+ # @param [GDAL::Dataset, FFI::Pointer, String] dataset Can be another
206
+ # dataset, the pointer to another dataset, or the path to a dataset.
207
+ # @return [GDAL::Dataset]
208
+ def make_dataset_pointer(dataset)
209
+ if dataset.is_a? GDAL::Dataset
210
+ dataset.c_pointer
211
+ elsif dataset.is_a? String
212
+ GDAL::Dataset.open(dataset, 'r').c_pointer
213
+ else
214
+ dataset
215
+ end
242
216
  end
243
217
  end
244
218
  end
219
+
220
+ require_relative 'dataset'
@@ -0,0 +1,93 @@
1
+ module GDAL
2
+ module DriverMixins
3
+ module Extensions
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ # @return [Array<String>]
10
+ def short_names
11
+ names = count.times.map do |i|
12
+ driver = at_index(i)
13
+ driver.short_name
14
+ end
15
+
16
+ names.compact.sort
17
+ end
18
+
19
+ # @return [Array<String>]
20
+ def long_names
21
+ names = count.times.map do |i|
22
+ at_index(i).long_name
23
+ end
24
+
25
+ names.compact.sort
26
+ end
27
+
28
+ # @return [Hash{String => String}] Keys are driver short names, values are
29
+ # driver long names.
30
+ def self.names
31
+ names = count.times.each_with_object({}) do |i, obj|
32
+ driver = at_index(i)
33
+ obj[driver.short_name] = driver.long_name
34
+ end
35
+
36
+ Hash[names.sort]
37
+ end
38
+ end
39
+
40
+ # The things that this driver can do, as reported by its metadata.
41
+ # Possibilities include:
42
+ # * :open
43
+ # * :create
44
+ # * :copy
45
+ # * :virtual_io
46
+ # * :rasters
47
+ # * :vectors
48
+ #
49
+ # @return [Array<Symbol>]
50
+ def capabilities
51
+ caps = []
52
+ caps << :open if can_open_datasets?
53
+ caps << :create if can_create_datasets?
54
+ caps << :copy if can_copy_datasets?
55
+ caps << :virtual_io if can_do_virtual_io?
56
+ caps << :rasters if can_do_rasters?
57
+ caps << :vectors if can_do_vectors?
58
+
59
+ caps
60
+ end
61
+
62
+ # @return [Boolean]
63
+ def can_open_datasets?
64
+ metadata_item('DCAP_OPEN') == 'YES'
65
+ end
66
+
67
+ # @return [Boolean]
68
+ def can_create_datasets?
69
+ metadata_item('DCAP_CREATE') == 'YES'
70
+ end
71
+
72
+ # @return [Boolean]
73
+ def can_copy_datasets?
74
+ metadata_item('DCAP_CREATECOPY') == 'YES'
75
+ end
76
+
77
+ # @return [Boolean]
78
+ def can_do_virtual_io?
79
+ metadata_item('DCAP_VIRTUALIO') == 'YES'
80
+ end
81
+
82
+ # @return [Boolean]
83
+ def can_do_rasters?
84
+ metadata_item('DCAP_RASTER') == 'YES'
85
+ end
86
+
87
+ # @return [Boolean]
88
+ def can_do_vectors?
89
+ metadata_item('DCAP_VECTOR') == 'YES'
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,20 +1,48 @@
1
1
  module GDAL
2
+ class CreateFail < StandardError
3
+ end
4
+
5
+ class Error < ::RuntimeError
6
+ end
7
+
2
8
  class OpenFailure < StandardError
3
- def initialize(file, msg=nil)
9
+ def initialize(file, msg = nil)
4
10
  message = msg || "Unable to open file '#{file}'. Perhaps an unsupported file format?"
5
11
  super(message)
6
12
  end
7
13
  end
8
14
 
9
- class CPLErrFailure < StandardError
15
+ class InvalidBandNumber < StandardError
10
16
  end
11
17
 
12
- class CreateFail < StandardError
18
+ class InvalidColorTable < StandardError
19
+ end
20
+
21
+ class InvalidDriverIndex < StandardError
22
+ end
23
+
24
+ class InvalidDriverName < StandardError
25
+ end
26
+
27
+ class InvalidRasterBand < StandardError
28
+ end
29
+
30
+ class NoWriteAccess < RuntimeError
31
+ end
32
+
33
+ class NullObject < TypeError
13
34
  end
14
35
 
15
36
  class RequiredBandNotFound < StandardError
16
37
  end
17
38
 
18
- class InvalidBandNumber < StandardError
39
+ class UnknownGridAlgorithm < StandardError
40
+ def initialize(algorithm, msg = nil)
41
+ message = msg || "Unknown Grid algorithm type '#{algorithm}'."
42
+ super(message)
43
+ end
44
+ end
45
+
46
+ class UnsupportedOperation < StandardError
19
47
  end
20
48
  end
@@ -1,24 +1,30 @@
1
1
  require_relative '../ffi/gdal'
2
- require_relative 'geo_transform_extensions'
2
+ require_relative 'geo_transform_mixins/extensions'
3
3
 
4
4
  module GDAL
5
5
  class GeoTransform
6
- include GeoTransformExtensions
6
+ include GeoTransformMixins::Extensions
7
7
 
8
+ # @return [FFI::Pointer] A pointer that can be used to hold a GeoTransform.
8
9
  def self.new_pointer
9
10
  FFI::MemoryPointer.new(:double, 6)
10
11
  end
11
12
 
12
- # @param filename [String]
13
+ # @return [FFI::Pointer] C pointer to the C geo-transform.
14
+ attr_reader :c_pointer
15
+
16
+ # @param [String] filename
17
+ # @param [String] extension The file extension to use. When nil, GDAL will
18
+ # try to derive it from the +filename+.
13
19
  # @return [GDAL::GeoTransform]
14
- def self.from_world_file(filename, extension=nil)
20
+ def self.from_world_file(filename, extension = nil)
15
21
  gt_ptr = new_pointer
16
22
 
17
23
  result = if extension
18
- FFI::GDAL.GDALReadWorldFile(filename, extension, gt_ptr)
19
- else
20
- FFI::GDAL.GDALLoadWorldFile(filename, gt_ptr)
21
- end
24
+ FFI::GDAL.GDALReadWorldFile(filename, extension, gt_ptr)
25
+ else
26
+ FFI::GDAL.GDALLoadWorldFile(filename, gt_ptr)
27
+ end
22
28
 
23
29
  return nil unless result
24
30
 
@@ -26,24 +32,25 @@ module GDAL
26
32
  end
27
33
 
28
34
  # @param geo_transform [FFI::Pointer]
29
- def initialize(geo_transform=nil)
30
- @geo_transform_pointer = if geo_transform.is_a? GDAL::GeoTransform
31
- geo_transform.c_pointer
32
- elsif geo_transform
33
- geo_transform
34
- else
35
- self.class.new_pointer
36
- end
35
+ def initialize(geo_transform = nil)
36
+ @c_pointer = if geo_transform.is_a? GDAL::GeoTransform
37
+ geo_transform.c_pointer
38
+ elsif geo_transform
39
+ geo_transform
40
+ else
41
+ self.class.new_pointer
42
+ end
43
+
44
+ self.pixel_width ||= 1.0
45
+ self.pixel_height ||= 1.0
46
+ self.x_rotation ||= 0.0
47
+ self.y_rotation ||= 0.0
37
48
 
38
49
  to_a
39
50
  end
40
51
 
41
- def c_pointer
42
- @geo_transform_pointer
43
- end
44
-
45
52
  def null?
46
- @geo_transform_pointer.null?
53
+ @c_pointer.null?
47
54
  end
48
55
 
49
56
  # X-coordinate of the center of the upper left pixel.
@@ -53,12 +60,12 @@ module GDAL
53
60
  def x_origin
54
61
  return nil if null?
55
62
 
56
- @geo_transform_pointer[0].read_double
63
+ @c_pointer[0].read_double
57
64
  end
58
65
 
59
66
  # @param new_x_origin [Float]
60
67
  def x_origin=(new_x_origin)
61
- @geo_transform_pointer[0].write_double(new_x_origin)
68
+ @c_pointer[0].write_double(new_x_origin)
62
69
  end
63
70
 
64
71
  # AKA X-pixel size.
@@ -68,12 +75,12 @@ module GDAL
68
75
  def pixel_width
69
76
  return nil if null?
70
77
 
71
- @geo_transform_pointer[1].read_double
78
+ @c_pointer[1].read_double
72
79
  end
73
80
 
74
81
  # @param new_pixel_width [Float]
75
82
  def pixel_width=(new_pixel_width)
76
- @geo_transform_pointer[1].write_double(new_pixel_width)
83
+ @c_pointer[1].write_double(new_pixel_width)
77
84
  end
78
85
 
79
86
  # Rotation about the x-axis.
@@ -83,12 +90,12 @@ module GDAL
83
90
  def x_rotation
84
91
  return nil if null?
85
92
 
86
- @geo_transform_pointer[2].read_double
93
+ @c_pointer[2].read_double
87
94
  end
88
95
 
89
96
  # @param new_x_rotation [Float]
90
97
  def x_rotation=(new_x_rotation)
91
- @geo_transform_pointer[2].write_double(new_x_rotation)
98
+ @c_pointer[2].write_double(new_x_rotation)
92
99
  end
93
100
 
94
101
  # Y-coordinate of the center of the upper left pixel.
@@ -98,12 +105,12 @@ module GDAL
98
105
  def y_origin
99
106
  return nil if null?
100
107
 
101
- @geo_transform_pointer[3].read_double
108
+ @c_pointer[3].read_double
102
109
  end
103
110
 
104
111
  # @param new_y_origin [Float]
105
112
  def y_origin=(new_y_origin)
106
- @geo_transform_pointer[3].write_double(new_y_origin)
113
+ @c_pointer[3].write_double(new_y_origin)
107
114
  end
108
115
 
109
116
  # Rotation about the y-axis.
@@ -113,12 +120,12 @@ module GDAL
113
120
  def y_rotation
114
121
  return nil if null?
115
122
 
116
- @geo_transform_pointer[4].read_double
123
+ @c_pointer[4].read_double
117
124
  end
118
125
 
119
126
  # @param new_y_rotation [Float]
120
127
  def y_rotation=(new_y_rotation)
121
- @geo_transform_pointer[4].write_double(new_y_rotation)
128
+ @c_pointer[4].write_double(new_y_rotation)
122
129
  end
123
130
 
124
131
  # AKA Y-pixel size.
@@ -128,51 +135,64 @@ module GDAL
128
135
  def pixel_height
129
136
  return nil if null?
130
137
 
131
- @geo_transform_pointer[5].read_double
138
+ @c_pointer[5].read_double
132
139
  end
133
140
 
134
141
  # @param new_pixel_height [Float]
135
142
  def pixel_height=(new_pixel_height)
136
- @geo_transform_pointer[5].write_double(new_pixel_height)
143
+ @c_pointer[5].write_double(new_pixel_height)
137
144
  end
138
145
 
139
146
  # Converts a (pixel, line) coordinate to a georeferenced (geo_x, geo_y)
140
- # location.
147
+ # location. Uses the following algorithm:
148
+ #
149
+ # geo_x = x_origin + (pixel * pixel_width) + (line * pixel_rotation)
150
+ # geo_y = y_origin + (pixel * y_rotation) + (line * pixel_height)
151
+ #
152
+ # This is also the same as doing:
153
+ #
154
+ # geo_transform.invert.world_to_pixel(pixel, line)
141
155
  #
142
156
  # @param pixel [Float] Input pixel position.
143
157
  # @param line [Float] Input line position.
144
- # @return [Hash{x_location: Float, y_location: Float}] longitude, latitude.
158
+ # @return [Hash{x_geo => Float, y_geo => Float}] +:x_geo+ is the
159
+ # # easting/longitude; +:y_geo+ is the northing/latitude.
145
160
  def apply_geo_transform(pixel, line)
146
161
  geo_x_ptr = FFI::MemoryPointer.new(:double)
147
162
  geo_y_ptr = FFI::MemoryPointer.new(:double)
148
- FFI::GDAL.GDALApplyGeoTransform(@geo_transform_pointer, pixel, line, geo_x_ptr, geo_y_ptr)
163
+ FFI::GDAL.GDALApplyGeoTransform(@c_pointer, pixel, line, geo_x_ptr, geo_y_ptr)
149
164
 
150
- { longitude: geo_x_ptr.read_double, latitude: geo_y_ptr.read_double }
165
+ { x_geo: geo_x_ptr.read_double, y_geo: geo_y_ptr.read_double }
151
166
  end
167
+ alias_method :pixel_to_world, :apply_geo_transform
152
168
 
153
- # Composes this and the give geo_transform. The result is equivalent to
154
- # applying both geotransforms to a point.
169
+ # Composes this and the given geo_transform. The resulting GeoTransform is
170
+ # equivalent to applying both GeoTransforms to a point.
155
171
  #
156
172
  # @param other_geo_transform [GDAL::GeoTransform, FFI::Pointer]
157
173
  # @return [GDAL::GeoTransform]
158
174
  def compose(other_geo_transform)
159
175
  other_ptr = GDAL._pointer(GDAL::GeoTransform, other_geo_transform)
160
176
 
177
+ unless other_ptr
178
+ fail GDAL::NullObject, "Unable to access pointer for '#{other_geo_transform}'"
179
+ end
180
+
161
181
  new_gt_ptr = self.class.new_pointer
162
- FFI::GDAL.GDALComposeGeoTransforms(@geo_transform_pointer, other_ptr, new_gt_ptr)
182
+ FFI::GDAL.GDALComposeGeoTransforms(@c_pointer, other_ptr, new_gt_ptr)
163
183
  return nil if new_gt_ptr.null?
164
184
 
165
185
  GDAL::GeoTransform.new(new_gt_ptr)
166
186
  end
167
187
 
168
188
  # Inverts the current 3x2 set of coefficients and returns a new GeoTransform.
169
- # Useful for converting from the geotransform equation from pixel to geo to
189
+ # Useful for converting from the GeoTransform equation from pixel to geo to
170
190
  # being geo to pixel.
171
191
  #
172
192
  # @return [GDAL::GeoTransform]
173
193
  def invert
174
194
  new_geo_transform_ptr = self.class.new_pointer
175
- success = FFI::GDAL.GDALInvGeoTransform(@geo_transform_pointer, new_geo_transform_ptr)
195
+ success = FFI::GDAL.GDALInvGeoTransform(@c_pointer, new_geo_transform_ptr)
176
196
  return nil unless success
177
197
 
178
198
  self.class.new(new_geo_transform_ptr)
@@ -182,7 +202,7 @@ module GDAL
182
202
  # @param world_extension [String]
183
203
  # @return [Boolean]
184
204
  def to_world_file(raster_filename, world_extension)
185
- FFI::GDAL.GDALWriteWorldFile(raster_filename, world_extension, @geo_transform_pointer)
205
+ FFI::GDAL.GDALWriteWorldFile(raster_filename, world_extension, @c_pointer)
186
206
  end
187
207
  end
188
208
  end