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
@@ -1,120 +1,163 @@
1
1
  require_relative '../ffi/ogr'
2
2
  require_relative 'feature_definition_extensions'
3
- require_relative 'field'
3
+ require_relative 'field_definition'
4
+ require_relative 'geometry_field_definition'
4
5
 
5
6
  module OGR
6
7
  class FeatureDefinition
7
8
  include FeatureDefinitionExtensions
8
9
 
9
- # @param name [String]
10
- def self.create(name)
11
- feature_defn_pointer = FFI::GDAL.OGR_FD_Create(name)
12
- return nil if feature_defn_pointer.null?
13
-
14
- new(feature_defn_pointer)
15
- end
16
-
17
- # @param feature_definition [String]
18
- def initialize(feature_definition)
19
- @feature_definition_pointer = GDAL._pointer(OGR::FeatureDefinition,
20
- feature_definition)
21
-
22
- close_me = -> { FFI::GDAL.OGR_FD_Destroy(@feature_definition_pointer) }
10
+ # @return [FFI::Pointer] C pointer of the C FeatureDefn.
11
+ attr_reader :c_pointer
12
+
13
+ # @param name_or_pointer [String, FFI::Pointer] When given a String, it will
14
+ # create a new FeatureDefinition with that name. When given an
15
+ # FFI::Pointer, the new object will simply wrap the C FeatureDefinition
16
+ # at that address.
17
+ def initialize(name_or_pointer)
18
+ @c_pointer = if name_or_pointer.is_a? String
19
+ FFI::OGR::API.OGR_FD_Create(name_or_pointer)
20
+ else
21
+ name_or_pointer
22
+ end
23
+
24
+ if !@c_pointer.is_a?(FFI::Pointer) || @c_pointer.null?
25
+ fail OGR::InvalidFeatureDefinition, "Unable to create #{self.class.name} from #{name_or_pointer}"
26
+ end
27
+
28
+ close_me = -> { FFI::OGR::API.OGR_FD_Destroy(@c_pointer) }
23
29
  ObjectSpace.define_finalizer self, close_me
24
30
  end
25
31
 
26
- def c_pointer
27
- @feature_definition_pointer
32
+ def release!
33
+ FFI::OGR::API.OGR_FD_Release(@c_pointer)
28
34
  end
29
35
 
30
36
  # @return [String]
31
37
  def name
32
- FFI::GDAL.OGR_FD_GetName(@feature_definition_pointer)
38
+ FFI::OGR::API.OGR_FD_GetName(@c_pointer)
33
39
  end
34
40
 
35
41
  # @return [Fixnum]
36
42
  def field_count
37
- FFI::GDAL.OGR_FD_GetFieldCount(@feature_definition_pointer)
43
+ FFI::OGR::API.OGR_FD_GetFieldCount(@c_pointer)
38
44
  end
39
45
 
40
46
  # @param index [Fixnum]
41
- # @return [OGR::Field]
42
- def field(index)
43
- field_ptr = FFI::GDAL.OGR_FD_GetFieldDefn(@feature_definition_pointer, index)
44
- return nil if field_ptr.null?
47
+ # @return [OGR::FieldDefinition]
48
+ def field_definition(index)
49
+ field_definition_ptr =
50
+ FFI::OGR::API.OGR_FD_GetFieldDefn(@c_pointer, index)
51
+ return nil if field_definition_ptr.null?
45
52
 
46
- OGR::Field.new(field_ptr)
53
+ OGR::FieldDefinition.new(field_definition_ptr, nil)
54
+ end
55
+
56
+ # @param field_definition [OGR::FieldDefinition, FFI::Pointer]
57
+ def add_field_definition(field_definition)
58
+ field_definition_ptr = GDAL._pointer(OGR::FieldDefinition, field_definition)
59
+
60
+ if field_definition_ptr.nil?
61
+ fail OGR::InvalidFieldDefinition, "Unable to add OGR::FieldDefinition: '#{field_definition}'"
62
+ end
63
+
64
+ FFI::OGR::API.OGR_FD_AddFieldDefn(@c_pointer, field_definition_ptr)
65
+ end
66
+
67
+ # @param index [Fixnum] Index of the field definition to delete.
68
+ # @return [Boolean]
69
+ def delete_field_definition(index)
70
+ ogr_err = FFI::OGR::API.OGR_FD_DeleteFieldDefn(
71
+ @c_pointer,
72
+ index)
73
+
74
+ ogr_err.handle_result "Unable to delete field definition at index #{index}"
47
75
  end
48
76
 
49
77
  # @param name [String]
50
- # @return [Fixnum] -1 if no match found
78
+ # @return [Fixnum] nil if no match found
51
79
  def field_index(name)
52
- FFI::GDAL.OGR_FD_GetFieldIndex(@feature_definition_pointer, name)
80
+ result = FFI::OGR::API.OGR_FD_GetFieldIndex(@c_pointer, name)
81
+
82
+ result < 0 ? nil : result
53
83
  end
54
84
 
55
- # @return [FFI::GDAL::OGRwkbGeometryType]
85
+ # @return [FFI::OGR::API::WKBGeometryType]
56
86
  def geometry_type
57
- FFI::GDAL.OGR_FD_GetGeomType(@feature_definition_pointer)
87
+ FFI::OGR::API.OGR_FD_GetGeomType(@c_pointer)
58
88
  end
59
89
 
60
- # @param new_type [FFI::GDAL::OGRwkbGeometryType]
90
+ # @param new_type [FFI::OGR::API::WKBGeometryType]
61
91
  def geometry_type=(new_type)
62
- FFI::GDAL.OGR_FD_SetGeomType(@feature_definition_pointer, new_type)
92
+ FFI::OGR::API.OGR_FD_SetGeomType(@c_pointer, new_type)
63
93
  end
64
94
 
65
95
  # @return [Boolean]
66
96
  def geometry_ignored?
67
- FFI::GDAL.OGR_FD_IsGeometryIgnored(@feature_definition_pointer)
97
+ FFI::OGR::API.OGR_FD_IsGeometryIgnored(@c_pointer)
68
98
  end
69
99
 
70
100
  # @param ignore [Boolean]
71
- def ignore_geometry!(ignore)
72
- FFI::GDAL.OGR_FD_SetGeometryIgnored(@feature_definition_pointer, ignore)
101
+ def ignore_geometry!(ignore = true)
102
+ FFI::OGR::API.OGR_FD_SetGeometryIgnored(@c_pointer, ignore)
73
103
  end
74
104
 
75
105
  # @return [Boolean]
76
106
  def style_ignored?
77
- FFI::GDAL.OGR_FD_IsStyleIgnored(@feature_definition_pointer)
107
+ FFI::OGR::API.OGR_FD_IsStyleIgnored(@c_pointer)
78
108
  end
79
109
 
80
110
  # @param ignore [Boolean]
81
- def ignore_style!(ignore)
82
- FFI::GDAL.OGR_FD_SetStyleIgnored(@feature_definition_pointer, ignore)
111
+ def ignore_style!(ignore = true)
112
+ FFI::OGR::API.OGR_FD_SetStyleIgnored(@c_pointer, ignore)
83
113
  end
84
114
 
85
115
  # @return [Fixnum]
86
116
  def geometry_field_count
87
- FFI::GDAL.OGR_FD_GetGeomFieldCount(@feature_definition_pointer)
117
+ FFI::OGR::API.OGR_FD_GetGeomFieldCount(@c_pointer)
88
118
  end
89
119
 
90
120
  # @param index [Fixnum]
91
- # @return [OGR::Field]
121
+ # @return [OGR::GeometryFieldDefinition]
92
122
  def geometry_field_definition(index)
93
- field_ptr = FFI::GDAL.OGR_FD_GetGeomFieldDefn(@feature_definition_pointer, index)
94
- return nil if field_ptr.null?
123
+ geometry_field_definition_ptr =
124
+ FFI::OGR::API.OGR_FD_GetGeomFieldDefn(@c_pointer, index)
125
+ return nil if geometry_field_definition_ptr.null?
95
126
 
96
- OGR::Field.new(field_ptr)
127
+ OGR::GeometryFieldDefinition.new(geometry_field_definition_ptr)
97
128
  end
98
129
 
99
130
  # @param name [String]
100
131
  # @return [Fixnum]
101
132
  def geometry_field_index(name)
102
- FFI::GDAL.OGR_FD_GetGeomFieldIndex(@feature_definition_pointer, name)
133
+ result = FFI::OGR::API.OGR_FD_GetGeomFieldIndex(@c_pointer, name)
134
+
135
+ result < 0 ? nil : result
103
136
  end
104
137
 
105
- # @param name [String]
106
- # @return [OGR::Field]
107
- def geometry_field_by_name(name)
108
- geometry_field_definition(geometry_field_index(name))
138
+ # @param geometry_field_definition [OGR::GeometryFieldDefinition, FFI::Pointer]
139
+ def add_geometry_field_definition(geometry_field_definition)
140
+ geometry_field_definition_ptr = GDAL._pointer(OGR::GeometryFieldDefinition,
141
+ geometry_field_definition)
142
+ FFI::OGR::API.OGR_FD_AddGeomFieldDefn(@c_pointer,
143
+ geometry_field_definition_ptr)
144
+ end
145
+
146
+ # @param index [Fixnum]
147
+ # @return [Boolean]
148
+ def delete_geometry_field_definition(index)
149
+ ogr_err = FFI::OGR::API.OGR_FD_DeleteGeomFieldDefn(@c_pointer,
150
+ index)
151
+
152
+ ogr_err.handle_result "Unable to delete geometry field definition at index #{index}"
109
153
  end
110
154
 
111
- # @param other_feature_defintion [OGR::Feature, FFI::Pointer]
155
+ # @param other_feature_definition [OGR::Feature, FFI::Pointer]
112
156
  # @return [Boolean]
113
- def same?(other_feature_defintion)
114
- fd_ptr = GDAL._pointer(OGR::FeatureDefinition, other_feature_defintion)
157
+ def same?(other_feature_definition)
158
+ fd_ptr = GDAL._pointer(OGR::FeatureDefinition, other_feature_definition)
115
159
 
116
- FFI::GDAL.OGR_FD_IsSame(@feature_definition_pointer, fd_ptr)
160
+ FFI::OGR::API.OGR_FD_IsSame(@c_pointer, fd_ptr)
117
161
  end
118
- alias_method :==, :same?
119
162
  end
120
163
  end
@@ -2,35 +2,58 @@ require 'json'
2
2
 
3
3
  module OGR
4
4
  module FeatureDefinitionExtensions
5
+ # @return [Array<OGR::FieldDefinition>]
6
+ def field_definitions
7
+ return [] if field_count.zero?
5
8
 
6
- # @return [Array<OGR::Field>]
7
- def fields
8
- 0.upto(field_count - 1).map do |i|
9
- field(i)
9
+ field_count.times.map do |i|
10
+ field_definition(i)
11
+ end
12
+ end
13
+
14
+ # @return [Array<OGR::GeometryFieldDefinition>]
15
+ def geometry_field_definitions
16
+ return [] if geometry_field_count.zero?
17
+
18
+ geometry_field_count.times.map do |i|
19
+ geometry_field_definition(i)
10
20
  end
11
21
  end
12
22
 
13
23
  # @param name [String]
14
- # @return [OGR::Field]
15
- def field_by_name(name)
16
- field(field_index(name))
24
+ # @return [OGR::FieldDefinition]
25
+ def field_definition_by_name(name)
26
+ i = field_index(name)
27
+ return unless i
28
+
29
+ field_definition(i)
30
+ end
31
+
32
+ # @param name [String]
33
+ # @return [OGR::GeometryFieldDefinition]
34
+ def geometry_field_definition_by_name(name)
35
+ g = geometry_field_index(name)
36
+ return unless g
37
+
38
+ geometry_field_definition(g)
17
39
  end
18
40
 
19
41
  # @return [Hash]
20
- def as_json
42
+ def as_json(_options = nil)
21
43
  {
22
44
  field_count: field_count,
23
- fields: fields.map(&:as_json),
45
+ field_definitions: field_definitions.map(&:as_json),
24
46
  geometry_field_count: geometry_field_count,
25
47
  geometry_type: geometry_type,
26
- name: name,
27
- is_style_ignored: style_ignored?
48
+ is_geometry_ignored: geometry_ignored?,
49
+ is_style_ignored: style_ignored?,
50
+ name: name
28
51
  }
29
52
  end
30
53
 
31
54
  # @return [String]
32
- def to_json
33
- as_json.to_json
55
+ def to_json(options = nil)
56
+ as_json(options).to_json
34
57
  end
35
58
  end
36
59
  end
@@ -2,30 +2,81 @@ require 'json'
2
2
 
3
3
  module OGR
4
4
  module FeatureExtensions
5
-
6
- # @return [Array<OGR::Field>]
5
+ # @return [Array] Uses each FieldDefinition to determine the field type at
6
+ # each index and returns maps the field as that value type.
7
7
  def fields
8
- 0.upto(field_count - 1).map do |i|
9
- field(i)
8
+ field_count.times.map do |i|
9
+ case field_definition(i).type
10
+ when :OFTInteger then field_as_integer(i)
11
+ when :OFTIntegerList then field_as_integer_list(i)
12
+ when :OFTReal then field_as_double(i)
13
+ when :OFTRealList then field_as_double_list(i)
14
+ when :OFTString then field_as_string(i)
15
+ when :OFTStringList then field_as_string_list(i)
16
+ when :OFTWideString then field_as_string(i)
17
+ when :OFTWideStringList then field_as_string_list(i)
18
+ when :OFTBinary then field_as_binary(i)
19
+ when :OFTDate, :OFTTime, :OFTDateTime then field_as_date_time(i)
20
+ when :OFTInteger64 then field_as_integer(i)
21
+ when :OFTInteger64List then field_as_integer_lsit(i)
22
+ when :OFTMaxType then field_as_date_time(i)
23
+ else fail "not sure about field type: #{fd.type}"
24
+ end
25
+ end
26
+ end
27
+
28
+ # @return [Array<OGR::Geometry>]
29
+ def geometry_fields
30
+ geometry_field_count.times.map do |i|
31
+ geometry_field(i)
32
+ end
33
+ end
34
+
35
+ # TODO: this seems really wonky...
36
+ def valid_raw_field?(index, raw_field)
37
+ field_def = field_definition(index)
38
+
39
+ case field_def.type
40
+ when :OFTInteger then !raw_field[:integer].nil?
41
+ when :OFTIntegerList then !raw_field[:integer_list].nil?
42
+ when :OFTInteger64 then !raw_field[:integer64].nil?
43
+ when :OFTInteger64List then !raw_field[:integer64_list].nil?
44
+ when :OFTReal then !raw_field[:real].nil?
45
+ when :OFTRealList then !raw_field[:real_list].nil?
46
+ when :OFTString then !raw_field[:string].nil?
47
+ when :OFTStringList then !raw_field[:string_list].nil?
48
+ when :OFTBinary then !raw_field[:binary].nil?
49
+ when :OFTDate then !raw_field[:date].nil?
50
+ else
51
+ fail OGR::Failure, "Not sure how to set raw type: #{field_def.type}"
10
52
  end
11
53
  end
12
54
 
13
55
  # @return [Hash]
14
- def as_json
56
+ def as_json(options = nil)
57
+ fields_with_nested_json = fields.map do |f|
58
+ {
59
+ field_definition: f[:field_definition].as_json(options),
60
+ value_as_string: f[:value_as_string]
61
+ }
62
+ end
63
+
15
64
  {
16
- definition: definition,
65
+ definition: definition.as_json(options),
17
66
  fid: fid,
18
67
  field_count: field_count,
19
- fields: fields.map(&:as_json),
20
- geometry: geometry.as_json,
68
+ fields: fields_with_nested_json,
69
+ geometry: geometry ? geometry.as_json(options) : nil,
21
70
  geometry_field_count: geometry_field_count,
22
- style_string: style_string
71
+ geometry_field_definitions: geometry_field_definitions.map(&:as_json),
72
+ style_string: style_string,
73
+ style_table: style_table ? style_table.as_json(options) : nil
23
74
  }
24
75
  end
25
76
 
26
77
  # @return [String]
27
- def to_json
28
- as_json.to_json
78
+ def to_json(options = nil)
79
+ as_json(options).to_json
29
80
  end
30
81
  end
31
82
  end
data/lib/ogr/field.rb CHANGED
@@ -1,91 +1,212 @@
1
- require_relative '../ffi/ogr'
2
- require_relative 'field_extensions'
1
+ require 'date'
3
2
 
4
3
  module OGR
5
4
  class Field
6
- include FieldExtensions
7
-
8
- # @param name [String]
9
- # @param type [FFI::GDAL::OGRFieldType]
10
- # @return [OGR::Field]
11
- def self.create(name, type)
12
- field_ptr = FFI::GDAL.OGR_Fld_Create(name, type)
13
- return nil if field_ptr.null?
5
+ # @return [FFI::OGR::Point]
6
+ attr_reader :c_struct
14
7
 
15
- new(field_ptr)
8
+ # @param ogr_field_struct [FFI::OGR::Point, FFI::Pointer]
9
+ def initialize(ogr_field_struct = nil)
10
+ @c_struct = ogr_field_struct || ::FFI::OGR::Field.new
16
11
  end
17
12
 
18
- # @param field [OGR::Field, FFI::Pointer]
19
- def initialize(field)
20
- @field_pointer = GDAL._pointer(OGR::Field, field)
13
+ # @return [FFI::Pointer]
14
+ def c_pointer
15
+ @c_struct.to_ptr
21
16
  end
22
17
 
23
- def c_pointer
24
- @field_pointer
18
+ # @return [Fixnum]
19
+ def integer
20
+ @c_struct[:integer]
25
21
  end
22
+ alias_method :to_i, :integer
26
23
 
27
- def destroy!
28
- FFI::GDAL.OGR_Fld_Destroy(@field_pointer)
24
+ # @param new_int [Fixnum]
25
+ def integer=(new_int)
26
+ @c_struct[:integer] = new_int
29
27
  end
30
28
 
31
- # @return [String]
32
- def name
33
- FFI::GDAL.OGR_Fld_GetNameRef(@field_pointer)
29
+ # @return [Bignum]
30
+ def integer64
31
+ @c_struct[:integer64]
34
32
  end
35
33
 
36
- # @param new_value [String]
37
- def name=(new_value)
38
- FFI::GDAL.OGR_Fld_SetName(@field_pointer, new_value)
34
+ # @param new_int64 [Bignum]
35
+ def integer64=(new_int64)
36
+ @c_struct[:integer64] = new_int64
39
37
  end
40
38
 
41
- # @return [FFI::GDAL::OGRJustification]
42
- def justification
43
- FFI::GDAL.OGR_Fld_GetJustify(@field_pointer)
39
+ # @return [Float]
40
+ def real
41
+ @c_struct[:real]
44
42
  end
43
+ alias_method :to_f, :real
45
44
 
46
- # @param new_value [FFI::GDAL::OGRJustification]
47
- def justification=(new_value)
48
- FFI::GDAL.OGR_Fld_SetJustify(@field_pointer, new_value)
45
+ # @param new_real [Float]
46
+ def real=(new_real)
47
+ @c_struct[:real] = new_real
49
48
  end
50
49
 
51
- # @return [Fixnum]
52
- def precision
53
- FFI::GDAL.OGR_Fld_GetPrecision(@field_pointer)
50
+ # TODO: This blows up when another value type has been set.
51
+ def string
52
+ return '' if @c_struct[:string] && @c_struct[:string].null?
53
+
54
+ @c_struct[:string].read_string
54
55
  end
55
56
 
56
- # @param new_value [Fixnum]
57
- def precision=(new_value)
58
- FFI::GDAL.OGR_Fld_SetPrecision(@field_pointer, new_value)
57
+ def string=(new_string)
58
+ @c_struct[:string] = FFI::MemoryPointer.from_string(new_string)
59
59
  end
60
60
 
61
- # @return [FFI::GDAL::OGRFieldType]
62
- def type
63
- FFI::GDAL.OGR_Fld_GetType(@field_pointer)
61
+ # @return [Array<Fixnum>]
62
+ def integer_list
63
+ il = @c_struct[:integer_list]
64
+ return [] if il[:count].zero?
65
+
66
+ il[:list].read_array_of_int(il[:count])
64
67
  end
65
68
 
66
- # @param new_value [FFI::GDAL::OGRFieldType]
67
- def type=(new_value)
68
- FFI::GDAL.OGR_Fld_SetType(@field_pointer, new_value)
69
+ # @param new_integer_list [Array<Fixnum>]
70
+ def integer_list=(new_integer_list)
71
+ list_ptr = FFI::MemoryPointer.new(:int, new_integer_list.size)
72
+ list_ptr.write_array_of_int(new_integer_list)
73
+
74
+ il = FFI::OGR::FieldTypes::IntegerList.new
75
+ il[:count] = new_integer_list.size
76
+ il[:list] = list_ptr
77
+
78
+ @c_struct[:integer_list] = il
69
79
  end
70
80
 
71
- # @return [Fixnum]
72
- def width
73
- FFI::GDAL.OGR_Fld_GetWidth(@field_pointer)
81
+ # @return [Array<Bignum>]
82
+ def integer64_list
83
+ il = @c_struct[:integer_list]
84
+ return [] if il[:count].zero?
85
+
86
+ il[:list].read_array_of_int64(il[:count])
87
+ end
88
+ alias_method :to_bignum, :integer64_list
89
+
90
+ # @param new_integer64_list [Array<Bignum>]
91
+ def integer64_list=(new_integer64_list)
92
+ list_ptr = FFI::MemoryPointer.new(:int64, new_integer64_list.size)
93
+ list_ptr.write_array_of_int64(new_integer64_list)
94
+
95
+ il = FFI::OGR::FieldTypes::Integer64List.new
96
+ il[:count] = new_integer64_list.size
97
+ il[:list] = list_ptr
98
+
99
+ @c_struct[:integer64_list] = il
100
+ end
101
+ alias_method :bignum_list=, :integer64_list=
102
+
103
+ # @return [Array<Float>]
104
+ def real_list
105
+ rl = @c_struct[:real_list]
106
+ return [] if rl[:count].zero?
107
+
108
+ rl[:list].read_array_of_double(rl[:count])
109
+ end
110
+ alias_method :float_list, :real_list
111
+
112
+ # @param new_real_list [Array<Float>]
113
+ def real_list=(new_real_list)
114
+ list_ptr = FFI::MemoryPointer.new(:double, new_real_list.size)
115
+ list_ptr.write_array_of_double(new_real_list)
116
+
117
+ rl = FFI::OGR::FieldTypes::RealList.new
118
+ rl[:count] = new_real_list.size
119
+ rl[:list] = list_ptr
120
+
121
+ @c_struct[:real_list] = rl
74
122
  end
123
+ alias_method :float_list=, :real_list=
75
124
 
76
- # @param new_value [Fixnum]
77
- def width=(new_value)
78
- FFI::GDAL.OGR_Fld_SetWidth(@field_pointer, new_value)
125
+ # @return [Array<String>]
126
+ def string_list
127
+ sl = @c_struct[:string_list]
128
+ return [] if sl[:count].zero?
129
+
130
+ sl[:list].read_array_of_pointer(sl[:count]).map(&:read_string)
131
+ end
132
+
133
+ # @param new_string_list [Array<String>]
134
+ def string_list=(new_string_list)
135
+ list_ptr = GDAL._string_array_to_pointer(new_string_list)
136
+
137
+ sl = FFI::OGR::FieldTypes::StringList.new
138
+ sl[:count] = new_string_list.size
139
+ sl[:list] = list_ptr
140
+
141
+ @c_struct[:string_list] = sl
79
142
  end
80
143
 
81
- # @return [Boolean]
82
- def ignored?
83
- FFI::GDAL.OGR_Fld_IsIgnored(@field_pointer)
144
+ # @return [String] 8-bit, unsigned data (uchar). Unpack with
145
+ # String#unpack('C*').
146
+ def binary
147
+ b = @c_struct[:binary]
148
+
149
+ b[:count] > 0 ? b[:data].read_bytes(b[:count]) : ''
84
150
  end
85
151
 
86
- # @param new_value [Boolean]
87
- def ignore=(new_value)
88
- FFI::GDAL.OGR_Fld_SetIgnored(@field_pointer, new_value)
152
+ # @param new_binary [String] Binary string of 8-bit, unsigned data (uchar).
153
+ # Pack with Array#pack('C*').
154
+ def binary=(new_binary)
155
+ data = FFI::MemoryPointer.new(:uchar, new_binary.length)
156
+ data.put_bytes(0, new_binary)
157
+
158
+ b = FFI::OGR::FieldTypes::Binary.new
159
+ b[:data] = data
160
+ b[:count] = new_binary.length
161
+
162
+ @c_struct[:binary] = b
163
+ end
164
+
165
+ # @return [Hash]
166
+ def set
167
+ { marker1: @c_struct[:set][:marker1], marker2: @c_struct[:set][:marker2] }
168
+ end
169
+
170
+ # @param new_set [Hash{marker1 => Fixnum, marker2 => Fixnum}]
171
+ def set=(new_set)
172
+ set = FFI::OGR::FieldTypes::Set.new
173
+ set[:marker1] = new_set[:marker1]
174
+ set[:marker2] = new_set[:marker2]
175
+
176
+ @c_struct[:set] = set
177
+ end
178
+
179
+ # @return [DateTime]
180
+ def date
181
+ c_date = @c_struct[:date]
182
+ return if c_date[:year].zero? || c_date[:month].zero? || c_date[:day].zero?
183
+
184
+ formatted_tz = OGR._format_time_zone_for_ruby(c_date[:tz_flag].to_i)
185
+
186
+ DateTime.new(c_date[:year],
187
+ c_date[:month],
188
+ c_date[:day],
189
+ c_date[:hour],
190
+ c_date[:minute],
191
+ c_date[:second],
192
+ formatted_tz)
193
+ end
194
+
195
+ # @param new_date [Date, Time, DateTime]
196
+ def date=(new_date)
197
+ time = new_date.to_time
198
+ zone = OGR._format_time_zone_for_ogr(time.zone)
199
+
200
+ date = FFI::OGR::FieldTypes::Date.new
201
+ date[:year] = time.year
202
+ date[:month] = time.month
203
+ date[:day] = time.day
204
+ date[:hour] = time.hour
205
+ date[:minute] = time.min
206
+ date[:second] = time.sec
207
+ date[:tz_flag] = zone
208
+
209
+ @c_struct[:date] = date
89
210
  end
90
211
  end
91
212
  end