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/ogr/layer.rb CHANGED
@@ -1,226 +1,105 @@
1
1
  require_relative '../ffi/ogr'
2
- require_relative 'layer_extensions'
2
+ require_relative '../gdal/major_object'
3
3
  require_relative 'envelope'
4
- require_relative 'geometry'
5
- require_relative 'feature'
6
- require_relative 'feature_definition'
7
- require_relative 'spatial_reference'
8
- require_relative 'style_table'
4
+ require_relative 'layer_mixins/extensions'
5
+ require_relative 'layer_mixins/capability_methods'
6
+ require_relative 'layer_mixins/ogr_feature_methods'
7
+ require_relative 'layer_mixins/ogr_field_methods'
8
+ require_relative 'layer_mixins/ogr_layer_method_methods'
9
+ require_relative 'layer_mixins/ogr_query_filter_methods'
10
+ require_relative 'layer_mixins/ogr_sql_methods'
9
11
 
10
12
  module OGR
11
13
  class Layer
12
14
  include GDAL::MajorObject
13
- include LayerExtensions
14
-
15
- def initialize(layer)
16
- @ogr_layer_pointer = GDAL._pointer(OGR::Layer, layer)
15
+ include LayerMixins::Extensions
16
+ include LayerMixins::CapabilityMethods
17
+ include LayerMixins::OGRFeatureMethods
18
+ include LayerMixins::OGRFieldMethods
19
+ include LayerMixins::OGRLayerMethodMethods
20
+ include LayerMixins::OGRQueryFilterMethods
21
+ include LayerMixins::OGRSQLMethods
22
+
23
+ class_eval FFI::OGR::Core::OGR_ALTER.to_ruby
24
+
25
+ # @return [FFI::Pointer] C pointer to the C Layer.
26
+ attr_reader :c_pointer
27
+
28
+ # @param layer_ptr [FFI::Pointer]
29
+ def initialize(layer_ptr)
30
+ @c_pointer = layer_ptr
17
31
  @features = []
18
32
  end
19
33
 
20
- def c_pointer
21
- @ogr_layer_pointer
22
- end
23
-
24
34
  # @return [String]
25
35
  def name
26
- FFI::GDAL.OGR_L_GetName(@ogr_layer_pointer)
27
- end
28
-
29
- # @return [Symbol] One of OGRwkbGeometryType.
30
- def geometry_type
31
- FFI::GDAL.OGR_L_GetGeomType(@ogr_layer_pointer)
32
- end
33
-
34
- # TODO: per the gdal docs: "The returned pointer is to an internally owned
35
- # object, and should not be altered or deleted by the caller."
36
- #
37
- # @return [OGR::Geometry]
38
- def spatial_filter
39
- return @spatial_filter if @spatial_filter
40
-
41
- filter_pointer = FFI::GDAL.OGR_L_GetSpatialFilter(@ogr_layer_pointer)
42
- return nil if filter_pointer.null?
43
-
44
- @spatial_filter = OGR::Geometry.new(filter_pointer)
45
- end
46
-
47
- # The number of features in this layer. If +force+ is false and it would be
48
- # expensive to determine the feature count, -1 may be returned.
49
- #
50
- # @param force [Boolean] Force the calculation even if it's
51
- # expensive.
52
- # @return [Fixnum]
53
- def feature_count(force=true)
54
- FFI::GDAL.OGR_L_GetFeatureCount(@ogr_layer_pointer, force)
55
- end
56
-
57
- # @param index [Fixnum] The 0-based index of the feature to get. It should
58
- # be <= +feature_count+, but not checking is done to ensure.
59
- # @return [OGR::Feature, nil]
60
- def feature(index)
61
- @features.fetch(index) do
62
- feature_pointer = FFI::GDAL.OGR_L_GetFeature(@ogr_layer_pointer, index)
63
- return nil if feature_pointer.null?
64
-
65
- feature = OGR::Feature.new(feature_pointer)
66
- @features.insert(index, feature)
67
-
68
- feature
69
- end
70
- end
71
-
72
- # The next available feature in this layer. Only features matching the
73
- # current spatial filter will be returned. Use +reset_reading+ to start at
74
- # the beginning again.
75
- #
76
- # @return [OGR::Feature, nil]
77
- def next_feature
78
- feature_pointer = FFI::GDAL.OGR_L_GetNextFeature(@ogr_layer_pointer)
79
- return nil if feature_pointer.null?
80
-
81
- OGR::Feature.new(feature_pointer)
82
- end
83
-
84
- # @return [Fixnum]
85
- def features_read
86
- FFI::GDAL.OGR_L_GetFeaturesRead(@ogr_layer_pointer)
87
- end
88
-
89
- # Uses the already-defined FeatureDefinition to create then write a new feature
90
- # to the layer.
91
- #
92
- # @return [OGR::Feature]
93
- def create_feature
94
- feature_def = feature_definition
95
- feature = OGR::Feature.create(feature_def)
96
- ogr_err = FFI::GDAL.OGR_L_CreateFeature(@ogr_layer_pointer, feature.c_pointer)
97
- ogr_err.to_ruby
98
-
99
- feature
100
- end
101
-
102
- # Deletes the feature from the layer.
103
- #
104
- # TODO: Use OGR_L_TestCapability before trying to delete.
105
- # @return +true+ if successful, otherwise raises an OGR exception.
106
- def delete_feature(feature_id)
107
- ogr_err = FFI::GDAL.OGR_L_DeleteFeature(@ogr_layer_pointer, feature_id)
108
-
109
- ogr_err.to_ruby
110
- end
111
-
112
- # Creates and writes a new field to the layer.
113
- #
114
- # @param name [String]
115
- # @param type [FFI::GDAL::OGRFieldType]
116
- # @param approx_ok [Boolean] If +true+ the field may be created in a slightly
117
- # different form, depending on the limitations of the format driver.
118
- # @return [OGR::Field]
119
- def create_field(name, type, approx_ok=false)
120
- field = OGR::Field.create(name, type)
121
- ogr_err = FFI::GDAL.OGR_L_CreateField(@ogr_layer_pointer, field.c_pointer, approx_ok)
122
- ogr_err.to_ruby
123
-
124
- field
36
+ FFI::OGR::API.OGR_L_GetName(@c_pointer)
125
37
  end
126
38
 
127
- # @param field [OGR::Field, FFI::Pointer]
128
- # @param approx_ok [Boolean] If +true+ the field may be created in a slightly
129
- # different form, depending on the limitations of the format driver.
130
- # @return +true+ if successful, otherwise raises an OGR exception.
131
- def add_field(field, approx_ok=false)
132
- field_ptr = GDAL._pointer(OGR::Field, field)
133
- ogr_err = FFI::GDAL.OGR_L_CreateField(@ogr_layer_pointer, field_ptr, approx_ok)
39
+ # @return [Boolean]
40
+ # TODO: This seems to occasionally lead to: 28352 illegal hardware
41
+ # instruction, and sometimes full crashes.
42
+ def sync_to_disk
43
+ ogr_err = FFI::OGR::API.OGR_L_SyncToDisk(@c_pointer)
134
44
 
135
- ogr_err.to_ruby
45
+ ogr_err.handle_result
136
46
  end
137
47
 
138
- # Deletes the field definition from the layer.
48
+ # Tests if this layer supports the given capability. Must be in the list
49
+ # of available capabilities. See http://www.gdal.org/ogr__api_8h.html#a480adc8b839b04597f49583371d366fd.
139
50
  #
140
- # TODO: Use OGR_L_TestCapability before trying to delete.
141
- # @return +true+ if successful, otherwise raises an OGR exception.
142
- def delete_field(field_id)
143
- ogr_err = FFI::GDAL.OGR_L_DeleteField(@ogr_layer_pointer, field_id)
144
-
145
- ogr_err.to_ruby
146
- end
147
-
148
- # # Creates and writes a new geometry to the layer.
149
- # #
150
- # # @return [OGR::GeometryField]
151
- # def create_geometry_field(approx_ok=false)
152
- # geometry_field_definition_ptr = FFI::MemoryPointer.new(:OGRGeomFieldDefnH)
153
- # ogr_err = OGR_L_CreateGeomField(@ogr_layer_pointer, geometry_field_definition_ptr)
154
- #
155
- # OGR::GeometryFieldDefinition.new(geometry_field_definition_ptr)
156
- # end
157
-
158
- # Resets the sequential reading of features for this layer.
159
- def reset_reading
160
- FFI::GDAL.OGR_L_ResetReading(@ogr_layer_pointer)
161
- end
162
-
163
- # The schema information for this layer.
164
- #
165
- # @return [OGR::FeatureDefinition,nil]
166
- def feature_definition
167
- return @feature_definition if @feature_definition
168
-
169
- feature_defn_pointer = FFI::GDAL.OGR_L_GetLayerDefn(@ogr_layer_pointer)
170
- return nil if feature_defn_pointer.null?
171
-
172
- @feature_definition = OGR::FeatureDefinition.new(feature_defn_pointer)
51
+ # @param capability [String]
52
+ # @return [Boolean]
53
+ # @see http://www.gdal.org/ogr__api_8h.html#a480adc8b839b04597f49583371d366fd
54
+ def test_capability(capability)
55
+ FFI::OGR::API.OGR_L_TestCapability(@c_pointer, capability.to_s)
173
56
  end
174
57
 
175
58
  # @return [OGR::SpatialReference]
176
59
  def spatial_reference
177
- return @spatial_reference if @spatial_reference
178
-
179
- spatial_ref_pointer = FFI::GDAL.OGR_L_GetSpatialRef(@ogr_layer_pointer)
60
+ spatial_ref_pointer = FFI::OGR::API.OGR_L_GetSpatialRef(@c_pointer)
180
61
  return nil if spatial_ref_pointer.null?
181
62
 
182
- @spatial_reference = OGR::SpatialReference.new(spatial_ref_pointer)
63
+ OGR::SpatialReference.new(spatial_ref_pointer)
183
64
  end
184
65
 
185
66
  # @return [OGR::Envelope]
186
- def extent(force=true)
187
- return @envelope if @envelope
188
-
189
- envelope = FFI::GDAL::OGREnvelope.new
190
- FFI::GDAL.OGR_L_GetExtent(@ogr_layer_pointer, envelope, force)
67
+ def extent(force = true)
68
+ envelope = FFI::OGR::Envelope.new
69
+ FFI::OGR::API.OGR_L_GetExtent(@c_pointer, envelope, force)
191
70
  return nil if envelope.null?
192
71
 
193
- @envelope = OGR::Envelope.new(envelope)
72
+ OGR::Envelope.new(envelope)
194
73
  end
195
74
 
196
75
  # @return [OGR::Envelope]
197
- def extent_by_geometry(geometry_field_index, force=true)
198
- envelope = FFI::GDAL::OGREnvelope.new
199
- FFI::GDAL.OGR_L_GetExtentEx(@ogr_layer_pointer, geometry_field_index, envelope, force)
76
+ def extent_by_geometry(geometry_field_index, force = true)
77
+ envelope = FFI::OGR::Envelope.new
78
+ FFI::OGR::API.OGR_L_GetExtentEx(@c_pointer, geometry_field_index, envelope, force)
200
79
  return nil if envelope.null?
201
80
 
202
81
  OGR::Envelope.new(envelope)
203
82
  end
204
83
 
205
- # The name of the underlying database column. '' if not supported.
206
- # @return [String]
207
- def fid_column
208
- FFI::GDAL.OGR_L_GetFIDColumn(@ogr_layer_pointer)
209
- end
210
-
211
- # @return [String]
212
- def geometry_column
213
- FFI::GDAL.OGR_L_GetGeometryColumn(@ogr_layer_pointer)
84
+ # @return [Symbol] One of OGRwkbGeometryType.
85
+ def geometry_type
86
+ FFI::OGR::API.OGR_L_GetGeomType(@c_pointer)
214
87
  end
215
88
 
216
89
  # @return [OGR::StyleTable, nil]
217
90
  def style_table
218
- return @style_table if @style_table
219
-
220
- style_table_pointer = FFI::GDAL.OGR_L_GetStyleTable(@ogr_layer_pointer)
91
+ style_table_pointer = FFI::OGR::API.OGR_L_GetStyleTable(@c_pointer)
221
92
  return nil if style_table_pointer.null?
222
93
 
223
- @style_table = OGR::StyleTable.new(style_table_pointer)
94
+ OGR::StyleTable.new(style_table_pointer)
95
+ end
96
+
97
+ # @param new_style_table [OGR::StyleTable, FFI::pointer]
98
+ def style_table=(new_style_table)
99
+ style_table_ptr = GDAL._pointer(OGR::StyleTable, new_style_table)
100
+ fail OGR::Failure if style_table_ptr.nil? || style_table_ptr.null?
101
+
102
+ FFI::OGR::API.OGR_L_SetStyleTable(@c_pointer, style_table_ptr)
224
103
  end
225
104
  end
226
105
  end
@@ -0,0 +1,102 @@
1
+ module OGR
2
+ module LayerMixins
3
+ # Helper methods for checking what the Layer is capable of.
4
+ module CapabilityMethods
5
+ # @return [Boolean] +true+ if #feature() is implemented in an optimized
6
+ # manner for this layer (as opposed to using #next_feature and
7
+ # #reset_reading).
8
+ def can_random_read?
9
+ test_capability('RandomRead')
10
+ end
11
+
12
+ # @return [Boolean] +true+ if #create_feature is allowed on this layer.
13
+ def can_sequential_write?
14
+ test_capability('SequentialWrite')
15
+ end
16
+
17
+ # @return [Boolean] +true+ if #feature= is allowed on this layer.
18
+ def can_random_write?
19
+ test_capability('RandomWrite')
20
+ end
21
+
22
+ # @return [Boolean] +true+ if this layer implements spatial filtering
23
+ # efficiently. This can be used as a clue by the application whether
24
+ # it should build and maintain its own spatial index for features in
25
+ # this layer.
26
+ def can_fast_spatial_filter?
27
+ test_capability('FastSpatialFilter')
28
+ end
29
+
30
+ # @return [Boolean] +true+ if this layer can return a feature count
31
+ # efficiently (i.e. without counting all of the features).
32
+ def can_fast_feature_count?
33
+ test_capability('FastFeatureCount')
34
+ end
35
+
36
+ # @return [Boolean] +true+ if this Layer can return its extent
37
+ # efficiently (i.e. without scanning all of the features.).
38
+ def can_fast_get_extent?
39
+ test_capability('FastGetExtent')
40
+ end
41
+
42
+ # @return [Boolean] +true+ if this layer can perform the
43
+ # #next_feature_index= call efficiently.
44
+ def can_fast_set_next_by_index?
45
+ test_capability('FastSetNextByIndex')
46
+ end
47
+
48
+ # @return [Boolean] +true+ if new Fields can be created on this Layer.
49
+ def can_create_field?
50
+ test_capability('CreateField')
51
+ end
52
+
53
+ # @return [Boolean] +true+ if the Layer supports creating new geometry
54
+ # fields on the current layer.
55
+ def can_create_geometry_field?
56
+ test_capability('CreateGeomField')
57
+ end
58
+
59
+ # @return [Boolean] +true+ if the Layer supports deleting existing fields
60
+ # on the current layer.
61
+ def can_delete_field?
62
+ test_capability('DeleteField')
63
+ end
64
+
65
+ # @return [Boolean] +true+ if the Layer supports reording fields on the
66
+ # current layer.
67
+ def can_reorder_fields?
68
+ test_capability('ReorderFields')
69
+ end
70
+
71
+ # @return [Boolean] +true+ if the Layer supports altering the defintition
72
+ # of an existing field on the current layer.
73
+ def can_alter_field_definition?
74
+ test_capability('AlterFieldDefn')
75
+ end
76
+
77
+ # @return [Boolean] +true+ if the Layer supports deleting Features.
78
+ def can_delete_feature?
79
+ test_capability('DeleteFeature')
80
+ end
81
+
82
+ # @return [Boolean] +true+ if :OFTString fields are guaranteed to be in
83
+ # UTF-8.
84
+ def strings_are_utf_8?
85
+ test_capability('StringsAsUTF8')
86
+ end
87
+
88
+ # @return [Boolean] +true+ if this Layer supports transactions. If not,
89
+ # #start_transaction, #commit_transaction, and #rollback_transaction
90
+ # will not work in a meaningful manner.
91
+ def supports_transactions?
92
+ test_capability('Transactions')
93
+ end
94
+
95
+ # @return [Boolean] +true+ if this Layer supports reading or writing
96
+ # curve geometries.
97
+ def supports_curve_geometries?
98
+ test_capability('CurveGeometries')
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,59 @@
1
+ require 'json'
2
+
3
+ module OGR
4
+ module LayerMixins
5
+ # Methods not part of the C Layer API.
6
+ module Extensions
7
+ # @return [Array<OGR::Feature>]
8
+ def features
9
+ return [] if feature_count.zero?
10
+
11
+ feature_list = feature_count.times.map do |i|
12
+ feature(i)
13
+ end
14
+
15
+ @features = feature_list
16
+ end
17
+
18
+ # @return [OGR::Geometry] A convex hull geometry derived from a LineString
19
+ # that connects the 4 bounding box points (from the extent).
20
+ def geometry_from_extent
21
+ sr = spatial_reference
22
+ geometry = OGR::Geometry.create(:wkbLineString)
23
+ geometry.spatial_reference = sr
24
+
25
+ geometry.add_point(extent.x_min, extent.y_min)
26
+ geometry.add_point(extent.x_min, extent.y_max)
27
+ geometry.add_point(extent.x_max, extent.y_max)
28
+ geometry.add_point(extent.x_max, extent.y_min)
29
+ geometry.add_point(extent.x_min, extent.y_min)
30
+
31
+ geometry.convex_hull
32
+ end
33
+
34
+ # @return [Hash]
35
+ def as_json(options = nil)
36
+ {
37
+ layer: {
38
+ extent: extent.as_json(options),
39
+ feature_count: feature_count,
40
+ feature_definition: feature_definition.as_json(options),
41
+ features: features.map(&:as_json),
42
+ fid_column: fid_column,
43
+ geometry_column: geometry_column,
44
+ geometry_type: geometry_type,
45
+ name: name,
46
+ spatial_reference: spatial_reference ? spatial_reference.as_json(options) : nil,
47
+ style_table: style_table ? style_table.as_json(options) : nil
48
+ },
49
+ metadata: nil # all_metadata
50
+ }
51
+ end
52
+
53
+ # @return [String]
54
+ def to_json(options = nil)
55
+ as_json(options).to_json
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,127 @@
1
+ require_relative '../feature'
2
+ require_relative '../feature_definition'
3
+
4
+ module OGR
5
+ module LayerMixins
6
+ module OGRFeatureMethods
7
+ # The schema information for this layer.
8
+ #
9
+ # @return [OGR::FeatureDefinition,nil]
10
+ def definition
11
+ feature_defn_pointer = FFI::OGR::API.OGR_L_GetLayerDefn(@c_pointer)
12
+ return nil if feature_defn_pointer.null?
13
+
14
+ # This object should not be modified.
15
+ OGR::FeatureDefinition.new(feature_defn_pointer)
16
+ end
17
+ alias_method :feature_definition, :definition
18
+
19
+ # Adds the new OGR::Feature to the Layer. The feature should have been
20
+ # created using the Layer's FeatureDefintion.
21
+ #
22
+ # feature = OGR::Feature.new(layer.feature_definition)
23
+ # feature.set_field_integer(123)
24
+ # layer.create_feature(feature)
25
+ #
26
+ # @param feature [OGR::Feature] [description]
27
+ # @return [Boolean]
28
+ def create_feature(feature)
29
+ unless can_sequential_write?
30
+ fail OGR::UnsupportedOperation, 'This layer does not support feature creation.'
31
+ end
32
+
33
+ ogr_err = FFI::OGR::API.OGR_L_CreateFeature(@c_pointer, feature.c_pointer)
34
+
35
+ ogr_err.handle_result
36
+ end
37
+
38
+ # Deletes the feature from the layer.
39
+ #
40
+ # @param feature_id [Fixnum] ID of the Feature to delete.
41
+ # @return +true+ if successful, otherwise raises an OGR exception.
42
+ # @raise [OGR::Failure] When trying to delete a feature with an ID that
43
+ # does not exist.
44
+ def delete_feature(feature_id)
45
+ unless can_delete_feature?
46
+ fail OGR::UnsupportedOperation, 'This layer does not support feature deletion.'
47
+ end
48
+
49
+ ogr_err = FFI::OGR::API.OGR_L_DeleteFeature(@c_pointer, feature_id)
50
+
51
+ ogr_err.handle_result "Unable to delete feature with ID '#{feature_id}'"
52
+ end
53
+
54
+ # The number of features in this layer. If +force+ is false and it would be
55
+ # expensive to determine the feature count, -1 may be returned.
56
+ #
57
+ # @param force [Boolean] Force the calculation even if it's expensive.
58
+ # @return [Fixnum]
59
+ def feature_count(force = true)
60
+ FFI::OGR::API.OGR_L_GetFeatureCount(@c_pointer, force)
61
+ end
62
+
63
+ # Rewrites an existing feature using the ID within the given Feature.
64
+ #
65
+ # @param new_feature [OGR::Feature, FFI::Pointer]
66
+ def feature=(new_feature)
67
+ unless can_random_write?
68
+ fail OGR::UnsupportedOperation, '#feature= not supported by this Layer'
69
+ end
70
+
71
+ new_feature_ptr = GDAL._pointer(OGR::Feature, new_feature)
72
+ fail OGR::InvalidFeature if new_feature_ptr.nil? || new_feature_ptr.null?
73
+
74
+ ogr_err = FFI::OGR::API.OGR_L_SetFeature(@c_pointer, new_feature_ptr)
75
+
76
+ ogr_err.handle_result
77
+ end
78
+
79
+ # @param index [Fixnum] The 0-based index of the feature to get. It should
80
+ # be <= +feature_count+, but no checking is done to ensure.
81
+ # @return [OGR::Feature, nil]
82
+ def feature(index)
83
+ unless can_random_read?
84
+ fail OGR::UnsupportedOperation, '#feature(index) not supported by this Layer'
85
+ end
86
+
87
+ feature_pointer = FFI::OGR::API.OGR_L_GetFeature(@c_pointer, index)
88
+ return nil if feature_pointer.null?
89
+
90
+ OGR::Feature.new(feature_pointer)
91
+ end
92
+
93
+ # The next available feature in this layer. Only features matching the
94
+ # current spatial filter will be returned. Use +reset_reading+ to start at
95
+ # the beginning again.
96
+ #
97
+ # @return [OGR::Feature, nil]
98
+ def next_feature
99
+ feature_pointer = FFI::OGR::API.OGR_L_GetNextFeature(@c_pointer)
100
+ return nil if feature_pointer.null?
101
+
102
+ OGR::Feature.new(feature_pointer)
103
+ end
104
+
105
+ # Sets the index for #next_feature.
106
+ #
107
+ # @param feature_index [Fixnum]
108
+ # @return [Boolean]
109
+ def next_feature_index=(feature_index)
110
+ ogr_err = FFI::OGR::API.OGR_L_SetNextByIndex(@c_pointer, feature_index)
111
+
112
+ ogr_err.handle_result "Unable to set next feature index to #{feature_index}"
113
+ end
114
+ alias_method :set_next_by_index, :next_feature_index=
115
+
116
+ # @return [Fixnum]
117
+ def features_read
118
+ FFI::OGR::API.OGR_L_GetFeaturesRead(@c_pointer)
119
+ end
120
+
121
+ # Resets the sequential reading of features for this layer.
122
+ def reset_reading
123
+ FFI::OGR::API.OGR_L_ResetReading(@c_pointer)
124
+ end
125
+ end
126
+ end
127
+ end