pyogrio 0.9.0__cp39-cp39-macosx_12_0_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyogrio might be problematic. Click here for more details.

Files changed (239) hide show
  1. pyogrio/.dylibs/libgdal.34.3.8.5.dylib +0 -0
  2. pyogrio/__init__.py +48 -0
  3. pyogrio/_compat.py +41 -0
  4. pyogrio/_env.py +61 -0
  5. pyogrio/_err.cpython-39-darwin.so +0 -0
  6. pyogrio/_err.pxd +4 -0
  7. pyogrio/_err.pyx +250 -0
  8. pyogrio/_geometry.cpython-39-darwin.so +0 -0
  9. pyogrio/_geometry.pxd +4 -0
  10. pyogrio/_geometry.pyx +129 -0
  11. pyogrio/_io.cpython-39-darwin.so +0 -0
  12. pyogrio/_io.pxd +0 -0
  13. pyogrio/_io.pyx +2742 -0
  14. pyogrio/_ogr.cpython-39-darwin.so +0 -0
  15. pyogrio/_ogr.pxd +444 -0
  16. pyogrio/_ogr.pyx +346 -0
  17. pyogrio/_version.py +21 -0
  18. pyogrio/_vsi.cpython-39-darwin.so +0 -0
  19. pyogrio/_vsi.pxd +4 -0
  20. pyogrio/_vsi.pyx +140 -0
  21. pyogrio/arrow_bridge.h +115 -0
  22. pyogrio/core.py +320 -0
  23. pyogrio/errors.py +32 -0
  24. pyogrio/gdal_data/GDAL-targets-release.cmake +19 -0
  25. pyogrio/gdal_data/GDAL-targets.cmake +105 -0
  26. pyogrio/gdal_data/GDALConfig.cmake +25 -0
  27. pyogrio/gdal_data/GDALConfigVersion.cmake +85 -0
  28. pyogrio/gdal_data/GDALLogoBW.svg +138 -0
  29. pyogrio/gdal_data/GDALLogoColor.svg +126 -0
  30. pyogrio/gdal_data/GDALLogoGS.svg +126 -0
  31. pyogrio/gdal_data/LICENSE.TXT +467 -0
  32. pyogrio/gdal_data/bag_template.xml +201 -0
  33. pyogrio/gdal_data/copyright +467 -0
  34. pyogrio/gdal_data/cubewerx_extra.wkt +48 -0
  35. pyogrio/gdal_data/default.rsc +0 -0
  36. pyogrio/gdal_data/ecw_cs.wkt +1453 -0
  37. pyogrio/gdal_data/eedaconf.json +23 -0
  38. pyogrio/gdal_data/epsg.wkt +1 -0
  39. pyogrio/gdal_data/esri_StatePlane_extra.wkt +631 -0
  40. pyogrio/gdal_data/gdalicon.png +0 -0
  41. pyogrio/gdal_data/gdalinfo_output.schema.json +346 -0
  42. pyogrio/gdal_data/gdalmdiminfo_output.schema.json +321 -0
  43. pyogrio/gdal_data/gdalvrt.xsd +772 -0
  44. pyogrio/gdal_data/gfs.xsd +246 -0
  45. pyogrio/gdal_data/gml_registry.xml +117 -0
  46. pyogrio/gdal_data/gml_registry.xsd +66 -0
  47. pyogrio/gdal_data/gmlasconf.xml +169 -0
  48. pyogrio/gdal_data/gmlasconf.xsd +1066 -0
  49. pyogrio/gdal_data/grib2_center.csv +251 -0
  50. pyogrio/gdal_data/grib2_process.csv +102 -0
  51. pyogrio/gdal_data/grib2_subcenter.csv +63 -0
  52. pyogrio/gdal_data/grib2_table_4_2_0_0.csv +261 -0
  53. pyogrio/gdal_data/grib2_table_4_2_0_1.csv +261 -0
  54. pyogrio/gdal_data/grib2_table_4_2_0_13.csv +261 -0
  55. pyogrio/gdal_data/grib2_table_4_2_0_14.csv +261 -0
  56. pyogrio/gdal_data/grib2_table_4_2_0_15.csv +261 -0
  57. pyogrio/gdal_data/grib2_table_4_2_0_16.csv +261 -0
  58. pyogrio/gdal_data/grib2_table_4_2_0_17.csv +11 -0
  59. pyogrio/gdal_data/grib2_table_4_2_0_18.csv +261 -0
  60. pyogrio/gdal_data/grib2_table_4_2_0_19.csv +261 -0
  61. pyogrio/gdal_data/grib2_table_4_2_0_190.csv +261 -0
  62. pyogrio/gdal_data/grib2_table_4_2_0_191.csv +261 -0
  63. pyogrio/gdal_data/grib2_table_4_2_0_2.csv +261 -0
  64. pyogrio/gdal_data/grib2_table_4_2_0_20.csv +261 -0
  65. pyogrio/gdal_data/grib2_table_4_2_0_21.csv +261 -0
  66. pyogrio/gdal_data/grib2_table_4_2_0_3.csv +261 -0
  67. pyogrio/gdal_data/grib2_table_4_2_0_4.csv +261 -0
  68. pyogrio/gdal_data/grib2_table_4_2_0_5.csv +261 -0
  69. pyogrio/gdal_data/grib2_table_4_2_0_6.csv +261 -0
  70. pyogrio/gdal_data/grib2_table_4_2_0_7.csv +261 -0
  71. pyogrio/gdal_data/grib2_table_4_2_10_0.csv +261 -0
  72. pyogrio/gdal_data/grib2_table_4_2_10_1.csv +261 -0
  73. pyogrio/gdal_data/grib2_table_4_2_10_191.csv +261 -0
  74. pyogrio/gdal_data/grib2_table_4_2_10_2.csv +261 -0
  75. pyogrio/gdal_data/grib2_table_4_2_10_3.csv +261 -0
  76. pyogrio/gdal_data/grib2_table_4_2_10_4.csv +261 -0
  77. pyogrio/gdal_data/grib2_table_4_2_1_0.csv +261 -0
  78. pyogrio/gdal_data/grib2_table_4_2_1_1.csv +261 -0
  79. pyogrio/gdal_data/grib2_table_4_2_1_2.csv +261 -0
  80. pyogrio/gdal_data/grib2_table_4_2_20_0.csv +261 -0
  81. pyogrio/gdal_data/grib2_table_4_2_20_1.csv +261 -0
  82. pyogrio/gdal_data/grib2_table_4_2_20_2.csv +261 -0
  83. pyogrio/gdal_data/grib2_table_4_2_2_0.csv +261 -0
  84. pyogrio/gdal_data/grib2_table_4_2_2_3.csv +261 -0
  85. pyogrio/gdal_data/grib2_table_4_2_2_4.csv +261 -0
  86. pyogrio/gdal_data/grib2_table_4_2_2_5.csv +261 -0
  87. pyogrio/gdal_data/grib2_table_4_2_2_6.csv +261 -0
  88. pyogrio/gdal_data/grib2_table_4_2_3_0.csv +261 -0
  89. pyogrio/gdal_data/grib2_table_4_2_3_1.csv +261 -0
  90. pyogrio/gdal_data/grib2_table_4_2_3_2.csv +28 -0
  91. pyogrio/gdal_data/grib2_table_4_2_3_3.csv +8 -0
  92. pyogrio/gdal_data/grib2_table_4_2_3_4.csv +14 -0
  93. pyogrio/gdal_data/grib2_table_4_2_3_5.csv +11 -0
  94. pyogrio/gdal_data/grib2_table_4_2_3_6.csv +11 -0
  95. pyogrio/gdal_data/grib2_table_4_2_4_0.csv +261 -0
  96. pyogrio/gdal_data/grib2_table_4_2_4_1.csv +261 -0
  97. pyogrio/gdal_data/grib2_table_4_2_4_10.csv +261 -0
  98. pyogrio/gdal_data/grib2_table_4_2_4_2.csv +261 -0
  99. pyogrio/gdal_data/grib2_table_4_2_4_3.csv +261 -0
  100. pyogrio/gdal_data/grib2_table_4_2_4_4.csv +261 -0
  101. pyogrio/gdal_data/grib2_table_4_2_4_5.csv +261 -0
  102. pyogrio/gdal_data/grib2_table_4_2_4_6.csv +261 -0
  103. pyogrio/gdal_data/grib2_table_4_2_4_7.csv +261 -0
  104. pyogrio/gdal_data/grib2_table_4_2_4_8.csv +261 -0
  105. pyogrio/gdal_data/grib2_table_4_2_4_9.csv +261 -0
  106. pyogrio/gdal_data/grib2_table_4_2_local_Canada.csv +5 -0
  107. pyogrio/gdal_data/grib2_table_4_2_local_HPC.csv +2 -0
  108. pyogrio/gdal_data/grib2_table_4_2_local_MRMS.csv +175 -0
  109. pyogrio/gdal_data/grib2_table_4_2_local_NCEP.csv +401 -0
  110. pyogrio/gdal_data/grib2_table_4_2_local_NDFD.csv +38 -0
  111. pyogrio/gdal_data/grib2_table_4_2_local_index.csv +7 -0
  112. pyogrio/gdal_data/grib2_table_4_5.csv +261 -0
  113. pyogrio/gdal_data/grib2_table_versions.csv +3 -0
  114. pyogrio/gdal_data/gt_datum.csv +229 -0
  115. pyogrio/gdal_data/gt_ellips.csv +24 -0
  116. pyogrio/gdal_data/header.dxf +1124 -0
  117. pyogrio/gdal_data/inspire_cp_BasicPropertyUnit.gfs +57 -0
  118. pyogrio/gdal_data/inspire_cp_CadastralBoundary.gfs +60 -0
  119. pyogrio/gdal_data/inspire_cp_CadastralParcel.gfs +81 -0
  120. pyogrio/gdal_data/inspire_cp_CadastralZoning.gfs +161 -0
  121. pyogrio/gdal_data/jpfgdgml_AdmArea.gfs +59 -0
  122. pyogrio/gdal_data/jpfgdgml_AdmBdry.gfs +49 -0
  123. pyogrio/gdal_data/jpfgdgml_AdmPt.gfs +59 -0
  124. pyogrio/gdal_data/jpfgdgml_BldA.gfs +54 -0
  125. pyogrio/gdal_data/jpfgdgml_BldL.gfs +54 -0
  126. pyogrio/gdal_data/jpfgdgml_Cntr.gfs +54 -0
  127. pyogrio/gdal_data/jpfgdgml_CommBdry.gfs +49 -0
  128. pyogrio/gdal_data/jpfgdgml_CommPt.gfs +59 -0
  129. pyogrio/gdal_data/jpfgdgml_Cstline.gfs +54 -0
  130. pyogrio/gdal_data/jpfgdgml_ElevPt.gfs +54 -0
  131. pyogrio/gdal_data/jpfgdgml_GCP.gfs +94 -0
  132. pyogrio/gdal_data/jpfgdgml_LeveeEdge.gfs +49 -0
  133. pyogrio/gdal_data/jpfgdgml_RailCL.gfs +54 -0
  134. pyogrio/gdal_data/jpfgdgml_RdASL.gfs +44 -0
  135. pyogrio/gdal_data/jpfgdgml_RdArea.gfs +54 -0
  136. pyogrio/gdal_data/jpfgdgml_RdCompt.gfs +59 -0
  137. pyogrio/gdal_data/jpfgdgml_RdEdg.gfs +59 -0
  138. pyogrio/gdal_data/jpfgdgml_RdMgtBdry.gfs +49 -0
  139. pyogrio/gdal_data/jpfgdgml_RdSgmtA.gfs +59 -0
  140. pyogrio/gdal_data/jpfgdgml_RvrMgtBdry.gfs +49 -0
  141. pyogrio/gdal_data/jpfgdgml_SBAPt.gfs +49 -0
  142. pyogrio/gdal_data/jpfgdgml_SBArea.gfs +54 -0
  143. pyogrio/gdal_data/jpfgdgml_SBBdry.gfs +44 -0
  144. pyogrio/gdal_data/jpfgdgml_WA.gfs +54 -0
  145. pyogrio/gdal_data/jpfgdgml_WL.gfs +54 -0
  146. pyogrio/gdal_data/jpfgdgml_WStrA.gfs +54 -0
  147. pyogrio/gdal_data/jpfgdgml_WStrL.gfs +54 -0
  148. pyogrio/gdal_data/netcdf_config.xsd +143 -0
  149. pyogrio/gdal_data/nitf_spec.xml +3306 -0
  150. pyogrio/gdal_data/nitf_spec.xsd +189 -0
  151. pyogrio/gdal_data/ogrinfo_output.schema.json +505 -0
  152. pyogrio/gdal_data/ogrvrt.xsd +543 -0
  153. pyogrio/gdal_data/osmconf.ini +132 -0
  154. pyogrio/gdal_data/ozi_datum.csv +131 -0
  155. pyogrio/gdal_data/ozi_ellips.csv +35 -0
  156. pyogrio/gdal_data/pci_datum.txt +463 -0
  157. pyogrio/gdal_data/pci_ellips.txt +77 -0
  158. pyogrio/gdal_data/pdfcomposition.xsd +721 -0
  159. pyogrio/gdal_data/pds4_template.xml +65 -0
  160. pyogrio/gdal_data/plscenesconf.json +1985 -0
  161. pyogrio/gdal_data/ruian_vf_ob_v1.gfs +1455 -0
  162. pyogrio/gdal_data/ruian_vf_st_uvoh_v1.gfs +86 -0
  163. pyogrio/gdal_data/ruian_vf_st_v1.gfs +1489 -0
  164. pyogrio/gdal_data/ruian_vf_v1.gfs +2126 -0
  165. pyogrio/gdal_data/s57agencies.csv +249 -0
  166. pyogrio/gdal_data/s57attributes.csv +484 -0
  167. pyogrio/gdal_data/s57expectedinput.csv +1008 -0
  168. pyogrio/gdal_data/s57objectclasses.csv +287 -0
  169. pyogrio/gdal_data/seed_2d.dgn +0 -0
  170. pyogrio/gdal_data/seed_3d.dgn +0 -0
  171. pyogrio/gdal_data/stateplane.csv +259 -0
  172. pyogrio/gdal_data/template_tiles.mapml +28 -0
  173. pyogrio/gdal_data/tms_LINZAntarticaMapTileGrid.json +190 -0
  174. pyogrio/gdal_data/tms_MapML_APSTILE.json +268 -0
  175. pyogrio/gdal_data/tms_MapML_CBMTILE.json +346 -0
  176. pyogrio/gdal_data/tms_NZTM2000.json +243 -0
  177. pyogrio/gdal_data/trailer.dxf +434 -0
  178. pyogrio/gdal_data/usage +4 -0
  179. pyogrio/gdal_data/vcpkg-cmake-wrapper.cmake +23 -0
  180. pyogrio/gdal_data/vcpkg.spdx.json +264 -0
  181. pyogrio/gdal_data/vcpkg_abi_info.txt +41 -0
  182. pyogrio/gdal_data/vdv452.xml +367 -0
  183. pyogrio/gdal_data/vdv452.xsd +63 -0
  184. pyogrio/gdal_data/vicar.json +164 -0
  185. pyogrio/geopandas.py +675 -0
  186. pyogrio/proj_data/CH +22 -0
  187. pyogrio/proj_data/GL27 +23 -0
  188. pyogrio/proj_data/ITRF2000 +24 -0
  189. pyogrio/proj_data/ITRF2008 +94 -0
  190. pyogrio/proj_data/ITRF2014 +55 -0
  191. pyogrio/proj_data/copyright +34 -0
  192. pyogrio/proj_data/deformation_model.schema.json +582 -0
  193. pyogrio/proj_data/nad.lst +142 -0
  194. pyogrio/proj_data/nad27 +810 -0
  195. pyogrio/proj_data/nad83 +745 -0
  196. pyogrio/proj_data/other.extra +53 -0
  197. pyogrio/proj_data/proj-config-version.cmake +44 -0
  198. pyogrio/proj_data/proj-config.cmake +79 -0
  199. pyogrio/proj_data/proj-targets-release.cmake +19 -0
  200. pyogrio/proj_data/proj-targets.cmake +107 -0
  201. pyogrio/proj_data/proj.db +0 -0
  202. pyogrio/proj_data/proj.ini +51 -0
  203. pyogrio/proj_data/proj4-targets-release.cmake +19 -0
  204. pyogrio/proj_data/proj4-targets.cmake +107 -0
  205. pyogrio/proj_data/projjson.schema.json +1174 -0
  206. pyogrio/proj_data/triangulation.schema.json +214 -0
  207. pyogrio/proj_data/usage +4 -0
  208. pyogrio/proj_data/vcpkg.spdx.json +198 -0
  209. pyogrio/proj_data/vcpkg_abi_info.txt +27 -0
  210. pyogrio/proj_data/world +214 -0
  211. pyogrio/raw.py +871 -0
  212. pyogrio/tests/__init__.py +0 -0
  213. pyogrio/tests/conftest.py +204 -0
  214. pyogrio/tests/fixtures/README.md +89 -0
  215. pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.cpg +1 -0
  216. pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.dbf +0 -0
  217. pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.prj +1 -0
  218. pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.shp +0 -0
  219. pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.shx +0 -0
  220. pyogrio/tests/fixtures/poly_not_enough_points.shp.zip +0 -0
  221. pyogrio/tests/fixtures/sample.osm.pbf +0 -0
  222. pyogrio/tests/fixtures/test_datetime.geojson +7 -0
  223. pyogrio/tests/fixtures/test_datetime_tz.geojson +8 -0
  224. pyogrio/tests/fixtures/test_fgdb.gdb.zip +0 -0
  225. pyogrio/tests/fixtures/test_gpkg_nulls.gpkg +0 -0
  226. pyogrio/tests/fixtures/test_multisurface.gpkg +0 -0
  227. pyogrio/tests/fixtures/test_nested.geojson +18 -0
  228. pyogrio/tests/fixtures/test_ogr_types_list.geojson +12 -0
  229. pyogrio/tests/test_arrow.py +1041 -0
  230. pyogrio/tests/test_core.py +588 -0
  231. pyogrio/tests/test_geopandas_io.py +2174 -0
  232. pyogrio/tests/test_path.py +352 -0
  233. pyogrio/tests/test_raw_io.py +1404 -0
  234. pyogrio/util.py +223 -0
  235. pyogrio-0.9.0.dist-info/LICENSE +21 -0
  236. pyogrio-0.9.0.dist-info/METADATA +100 -0
  237. pyogrio-0.9.0.dist-info/RECORD +239 -0
  238. pyogrio-0.9.0.dist-info/WHEEL +5 -0
  239. pyogrio-0.9.0.dist-info/top_level.txt +1 -0
pyogrio/raw.py ADDED
@@ -0,0 +1,871 @@
1
+ from io import BytesIO
2
+ import warnings
3
+
4
+ from pyogrio._env import GDALEnv
5
+ from pyogrio._compat import HAS_ARROW_API, HAS_ARROW_WRITE_API, HAS_PYARROW
6
+ from pyogrio.core import detect_write_driver
7
+ from pyogrio.errors import DataSourceError
8
+ from pyogrio.util import (
9
+ _mask_to_wkb,
10
+ _preprocess_options_key_value,
11
+ get_vsi_path_or_buffer,
12
+ vsi_path,
13
+ )
14
+
15
+ with GDALEnv():
16
+ from pyogrio._io import ogr_open_arrow, ogr_read, ogr_write, ogr_write_arrow
17
+ from pyogrio._ogr import (
18
+ _get_driver_metadata_item,
19
+ get_gdal_version,
20
+ get_gdal_version_string,
21
+ ogr_driver_supports_write,
22
+ ogr_driver_supports_vsi,
23
+ )
24
+
25
+
26
+ DRIVERS_NO_MIXED_SINGLE_MULTI = {
27
+ "FlatGeobuf",
28
+ "GPKG",
29
+ }
30
+
31
+ DRIVERS_NO_MIXED_DIMENSIONS = {
32
+ "FlatGeobuf",
33
+ }
34
+
35
+
36
+ def read(
37
+ path_or_buffer,
38
+ /,
39
+ layer=None,
40
+ encoding=None,
41
+ columns=None,
42
+ read_geometry=True,
43
+ force_2d=False,
44
+ skip_features=0,
45
+ max_features=None,
46
+ where=None,
47
+ bbox=None,
48
+ mask=None,
49
+ fids=None,
50
+ sql=None,
51
+ sql_dialect=None,
52
+ return_fids=False,
53
+ datetime_as_string=False,
54
+ **kwargs,
55
+ ):
56
+ """Read OGR data source into numpy arrays.
57
+
58
+ IMPORTANT: non-linear geometry types (e.g., MultiSurface) are converted
59
+ to their linear approximations.
60
+
61
+ Parameters
62
+ ----------
63
+ path_or_buffer : pathlib.Path or str, or bytes buffer
64
+ A dataset path or URI, or raw buffer.
65
+ layer : int or str, optional (default: first layer)
66
+ If an integer is provided, it corresponds to the index of the layer
67
+ with the data source. If a string is provided, it must match the name
68
+ of the layer in the data source. Defaults to first layer in data source.
69
+ encoding : str, optional (default: None)
70
+ If present, will be used as the encoding for reading string values from
71
+ the data source. By default will automatically try to detect the native
72
+ encoding and decode to ``UTF-8``.
73
+ columns : list-like, optional (default: all columns)
74
+ List of column names to import from the data source. Column names must
75
+ exactly match the names in the data source, and will be returned in
76
+ the order they occur in the data source. To avoid reading any columns,
77
+ pass an empty list-like. If combined with ``where`` parameter, must
78
+ include columns referenced in the ``where`` expression or the data may
79
+ not be correctly read; the data source may return empty results or
80
+ raise an exception (behavior varies by driver).
81
+ read_geometry : bool, optional (default: True)
82
+ If True, will read geometry into WKB. If False, geometry will be None.
83
+ force_2d : bool, optional (default: False)
84
+ If the geometry has Z values, setting this to True will cause those to
85
+ be ignored and 2D geometries to be returned
86
+ skip_features : int, optional (default: 0)
87
+ Number of features to skip from the beginning of the file before
88
+ returning features. If greater than available number of features, an
89
+ empty DataFrame will be returned. Using this parameter may incur
90
+ significant overhead if the driver does not support the capability to
91
+ randomly seek to a specific feature, because it will need to iterate
92
+ over all prior features.
93
+ max_features : int, optional (default: None)
94
+ Number of features to read from the file.
95
+ where : str, optional (default: None)
96
+ Where clause to filter features in layer by attribute values. If the data source
97
+ natively supports SQL, its specific SQL dialect should be used (eg. SQLite and
98
+ GeoPackage: `SQLITE`_, PostgreSQL). If it doesn't, the `OGRSQL WHERE`_ syntax
99
+ should be used. Note that it is not possible to overrule the SQL dialect, this
100
+ is only possible when you use the SQL parameter.
101
+ Examples: ``"ISO_A3 = 'CAN'"``, ``"POP_EST > 10000000 AND POP_EST < 100000000"``
102
+ bbox : tuple of (xmin, ymin, xmax, ymax), optional (default: None)
103
+ If present, will be used to filter records whose geometry intersects this
104
+ box. This must be in the same CRS as the dataset. If GEOS is present
105
+ and used by GDAL, only geometries that intersect this bbox will be
106
+ returned; if GEOS is not available or not used by GDAL, all geometries
107
+ with bounding boxes that intersect this bbox will be returned.
108
+ Cannot be combined with ``mask`` keyword.
109
+ mask : Shapely geometry, optional (default: None)
110
+ If present, will be used to filter records whose geometry intersects
111
+ this geometry. This must be in the same CRS as the dataset. If GEOS is
112
+ present and used by GDAL, only geometries that intersect this geometry
113
+ will be returned; if GEOS is not available or not used by GDAL, all
114
+ geometries with bounding boxes that intersect the bounding box of this
115
+ geometry will be returned. Requires Shapely >= 2.0.
116
+ Cannot be combined with ``bbox`` keyword.
117
+ fids : array-like, optional (default: None)
118
+ Array of integer feature id (FID) values to select. Cannot be combined
119
+ with other keywords to select a subset (``skip_features``,
120
+ ``max_features``, ``where``, ``bbox``, or ``mask``). Note that the
121
+ starting index is driver and file specific (e.g. typically 0 for
122
+ Shapefile and 1 for GeoPackage, but can still depend on the specific
123
+ file). The performance of reading a large number of features usings FIDs
124
+ is also driver specific.
125
+ sql : str, optional (default: None)
126
+ The SQL statement to execute. Look at the sql_dialect parameter for more
127
+ information on the syntax to use for the query. When combined with other
128
+ keywords like ``columns``, ``skip_features``, ``max_features``,
129
+ ``where``, ``bbox``, or ``mask``, those are applied after the SQL query.
130
+ Be aware that this can have an impact on performance, (e.g. filtering
131
+ with the ``bbox`` or ``mask`` keywords may not use spatial indexes).
132
+ Cannot be combined with the ``layer`` or ``fids`` keywords.
133
+ sql_dialect : str, optional (default: None)
134
+ The SQL dialect the ``sql`` statement is written in. Possible values:
135
+
136
+ - **None**: if the data source natively supports SQL, its specific SQL dialect
137
+ will be used by default (eg. SQLite and Geopackage: `SQLITE`_, PostgreSQL).
138
+ If the data source doesn't natively support SQL, the `OGRSQL`_ dialect is
139
+ the default.
140
+ - '`OGRSQL`_': can be used on any data source. Performance can suffer
141
+ when used on data sources with native support for SQL.
142
+ - '`SQLITE`_': can be used on any data source. All spatialite_
143
+ functions can be used. Performance can suffer on data sources with
144
+ native support for SQL, except for Geopackage and SQLite as this is
145
+ their native SQL dialect.
146
+
147
+ return_fids : bool, optional (default: False)
148
+ If True, will return the FIDs of the feature that were read.
149
+ datetime_as_string : bool, optional (default: False)
150
+ If True, will return datetime dtypes as detected by GDAL as a string
151
+ array (which can be used to extract timezone info), instead of
152
+ a datetime64 array.
153
+
154
+ **kwargs
155
+ Additional driver-specific dataset open options passed to OGR. Invalid
156
+ options will trigger a warning.
157
+
158
+ Returns
159
+ -------
160
+ (dict, fids, geometry, data fields)
161
+ Returns a tuple of meta information about the data source in a dict,
162
+ an ndarray of FIDs corresponding to the features that were read or None
163
+ (if return_fids is False),
164
+ an ndarray of geometry objects or None (if data source does not include
165
+ geometry or read_geometry is False), a tuple of ndarrays for each field
166
+ in the data layer.
167
+
168
+ Meta is: {
169
+ "crs": "<crs>",
170
+ "fields": <ndarray of field names>,
171
+ "dtypes": <ndarray of numpy dtypes corresponding to fields>
172
+ "encoding": "<encoding>",
173
+ "geometry_type": "<geometry type>"
174
+ }
175
+
176
+ .. _OGRSQL:
177
+
178
+ https://gdal.org/user/ogr_sql_dialect.html#ogr-sql-dialect
179
+
180
+ .. _OGRSQL WHERE:
181
+
182
+ https://gdal.org/user/ogr_sql_dialect.html#where
183
+
184
+ .. _SQLITE:
185
+
186
+ https://gdal.org/user/sql_sqlite_dialect.html#sql-sqlite-dialect
187
+
188
+ .. _spatialite:
189
+
190
+ https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html
191
+
192
+ """
193
+
194
+ dataset_kwargs = _preprocess_options_key_value(kwargs) if kwargs else {}
195
+
196
+ return ogr_read(
197
+ get_vsi_path_or_buffer(path_or_buffer),
198
+ layer=layer,
199
+ encoding=encoding,
200
+ columns=columns,
201
+ read_geometry=read_geometry,
202
+ force_2d=force_2d,
203
+ skip_features=skip_features,
204
+ max_features=max_features or 0,
205
+ where=where,
206
+ bbox=bbox,
207
+ mask=_mask_to_wkb(mask),
208
+ fids=fids,
209
+ sql=sql,
210
+ sql_dialect=sql_dialect,
211
+ return_fids=return_fids,
212
+ dataset_kwargs=dataset_kwargs,
213
+ datetime_as_string=datetime_as_string,
214
+ )
215
+
216
+
217
+ def read_arrow(
218
+ path_or_buffer,
219
+ /,
220
+ layer=None,
221
+ encoding=None,
222
+ columns=None,
223
+ read_geometry=True,
224
+ force_2d=False,
225
+ skip_features=0,
226
+ max_features=None,
227
+ where=None,
228
+ bbox=None,
229
+ mask=None,
230
+ fids=None,
231
+ sql=None,
232
+ sql_dialect=None,
233
+ return_fids=False,
234
+ **kwargs,
235
+ ):
236
+ """
237
+ Read OGR data source into a pyarrow Table.
238
+
239
+ See docstring of `read` for parameters.
240
+
241
+ Returns
242
+ -------
243
+ (dict, pyarrow.Table)
244
+
245
+ Returns a tuple of meta information about the data source in a dict,
246
+ and a pyarrow Table with data.
247
+
248
+ Meta is: {
249
+ "crs": "<crs>",
250
+ "fields": <ndarray of field names>,
251
+ "encoding": "<encoding>",
252
+ "geometry_type": "<geometry_type>",
253
+ "geometry_name": "<name of geometry column in arrow table>",
254
+ }
255
+ """
256
+ if not HAS_PYARROW:
257
+ raise RuntimeError(
258
+ "pyarrow required to read using 'read_arrow'. You can use 'open_arrow' "
259
+ "to read data with an alternative Arrow implementation"
260
+ )
261
+
262
+ from pyarrow import Table
263
+
264
+ gdal_version = get_gdal_version()
265
+
266
+ if skip_features < 0:
267
+ raise ValueError("'skip_features' must be >= 0")
268
+
269
+ if max_features is not None and max_features < 0:
270
+ raise ValueError("'max_features' must be >= 0")
271
+
272
+ # limit batch size to max_features if set
273
+ if "batch_size" in kwargs:
274
+ batch_size = kwargs.pop("batch_size")
275
+ else:
276
+ batch_size = 65_536
277
+
278
+ if max_features is not None and max_features < batch_size:
279
+ batch_size = max_features
280
+
281
+ # handle skip_features internally within open_arrow if GDAL >= 3.8.0
282
+ gdal_skip_features = 0
283
+ if gdal_version >= (3, 8, 0):
284
+ gdal_skip_features = skip_features
285
+ skip_features = 0
286
+
287
+ with open_arrow(
288
+ path_or_buffer,
289
+ layer=layer,
290
+ encoding=encoding,
291
+ columns=columns,
292
+ read_geometry=read_geometry,
293
+ force_2d=force_2d,
294
+ where=where,
295
+ bbox=bbox,
296
+ mask=mask,
297
+ fids=fids,
298
+ sql=sql,
299
+ sql_dialect=sql_dialect,
300
+ return_fids=return_fids,
301
+ skip_features=gdal_skip_features,
302
+ batch_size=batch_size,
303
+ use_pyarrow=True,
304
+ **kwargs,
305
+ ) as source:
306
+ meta, reader = source
307
+
308
+ if max_features is not None:
309
+ batches = []
310
+ count = 0
311
+ while True:
312
+ try:
313
+ batch = reader.read_next_batch()
314
+ batches.append(batch)
315
+
316
+ count += len(batch)
317
+ if count >= (skip_features + max_features):
318
+ break
319
+
320
+ except StopIteration:
321
+ break
322
+
323
+ # use combine_chunks to release the original memory that included
324
+ # too many features
325
+ table = (
326
+ Table.from_batches(batches, schema=reader.schema)
327
+ .slice(skip_features, max_features)
328
+ .combine_chunks()
329
+ )
330
+
331
+ elif skip_features > 0:
332
+ table = reader.read_all().slice(skip_features).combine_chunks()
333
+
334
+ else:
335
+ table = reader.read_all()
336
+
337
+ return meta, table
338
+
339
+
340
+ def open_arrow(
341
+ path_or_buffer,
342
+ /,
343
+ layer=None,
344
+ encoding=None,
345
+ columns=None,
346
+ read_geometry=True,
347
+ force_2d=False,
348
+ skip_features=0,
349
+ max_features=None,
350
+ where=None,
351
+ bbox=None,
352
+ mask=None,
353
+ fids=None,
354
+ sql=None,
355
+ sql_dialect=None,
356
+ return_fids=False,
357
+ batch_size=65_536,
358
+ use_pyarrow=False,
359
+ **kwargs,
360
+ ):
361
+ """
362
+ Open OGR data source as a stream of Arrow record batches.
363
+
364
+ See docstring of `read` for parameters.
365
+
366
+ The returned object is reading from a stream provided by OGR and must not be
367
+ accessed after the OGR dataset has been closed, i.e. after the context manager has
368
+ been closed.
369
+
370
+ By default this functions returns a generic stream object implementing
371
+ the `Arrow PyCapsule Protocol`_ (i.e. having an ``__arrow_c_stream__``
372
+ method). This object can then be consumed by your Arrow implementation
373
+ of choice that supports this protocol.
374
+ Optionally, you can specify ``use_pyarrow=True`` to directly get the
375
+ stream as a `pyarrow.RecordBatchReader`.
376
+
377
+ .. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
378
+
379
+ Other Parameters
380
+ ----------------
381
+ batch_size : int (default: 65_536)
382
+ Maximum number of features to retrieve in a batch.
383
+ use_pyarrow : bool (default: False)
384
+ If True, return a pyarrow RecordBatchReader instead of a generic
385
+ ArrowStream object. In the default case, this stream object needs
386
+ to be passed to another library supporting the Arrow PyCapsule
387
+ Protocol to consume the stream of data.
388
+
389
+ Examples
390
+ --------
391
+
392
+ >>> from pyogrio.raw import open_arrow
393
+ >>> import pyarrow as pa
394
+ >>> import shapely
395
+ >>>
396
+ >>> with open_arrow(path) as source:
397
+ >>> meta, stream = source
398
+ >>> # wrap the arrow stream object in a pyarrow RecordBatchReader
399
+ >>> reader = pa.RecordBatchReader.from_stream(stream)
400
+ >>> for batch in reader:
401
+ >>> geometries = shapely.from_wkb(batch[meta["geometry_name"] or "wkb_geometry"])
402
+
403
+ The returned `stream` object needs to be consumed by a library implementing
404
+ the Arrow PyCapsule Protocol. In the above example, pyarrow is used through
405
+ its RecordBatchReader. For this case, you can also specify ``use_pyarrow=True``
406
+ to directly get this result as a short-cut:
407
+
408
+ >>> with open_arrow(path, use_pyarrow=True) as source:
409
+ >>> meta, reader = source
410
+ >>> for batch in reader:
411
+ >>> geometries = shapely.from_wkb(batch[meta["geometry_name"] or "wkb_geometry"])
412
+
413
+ Returns
414
+ -------
415
+ (dict, pyarrow.RecordBatchReader or ArrowStream)
416
+
417
+ Returns a tuple of meta information about the data source in a dict,
418
+ and a data stream object (a generic ArrowStream object, or a pyarrow
419
+ RecordBatchReader if `use_pyarrow` is set to True).
420
+
421
+ Meta is: {
422
+ "crs": "<crs>",
423
+ "fields": <ndarray of field names>,
424
+ "encoding": "<encoding>",
425
+ "geometry_type": "<geometry_type>",
426
+ "geometry_name": "<name of geometry column in arrow table>",
427
+ }
428
+ """
429
+ if not HAS_ARROW_API:
430
+ raise RuntimeError("GDAL>= 3.6 required to read using arrow")
431
+
432
+ dataset_kwargs = _preprocess_options_key_value(kwargs) if kwargs else {}
433
+
434
+ return ogr_open_arrow(
435
+ get_vsi_path_or_buffer(path_or_buffer),
436
+ layer=layer,
437
+ encoding=encoding,
438
+ columns=columns,
439
+ read_geometry=read_geometry,
440
+ force_2d=force_2d,
441
+ skip_features=skip_features,
442
+ max_features=max_features or 0,
443
+ where=where,
444
+ bbox=bbox,
445
+ mask=_mask_to_wkb(mask),
446
+ fids=fids,
447
+ sql=sql,
448
+ sql_dialect=sql_dialect,
449
+ return_fids=return_fids,
450
+ dataset_kwargs=dataset_kwargs,
451
+ batch_size=batch_size,
452
+ use_pyarrow=use_pyarrow,
453
+ )
454
+
455
+
456
+ def _parse_options_names(xml):
457
+ """Convert metadata xml to list of names"""
458
+ # Based on Fiona's meta.py
459
+ # (https://github.com/Toblerity/Fiona/blob/91c13ad8424641557a4e5f038f255f9b657b1bc5/fiona/meta.py)
460
+ import xml.etree.ElementTree as ET
461
+
462
+ options = []
463
+ if xml:
464
+ root = ET.fromstring(xml)
465
+ for option in root.iter("Option"):
466
+ # some options explicitly have scope='raster'
467
+ if option.attrib.get("scope", "vector") != "raster":
468
+ options.append(option.attrib["name"])
469
+
470
+ return options
471
+
472
+
473
+ def _validate_metadata(dataset_metadata, layer_metadata, metadata):
474
+ """ """
475
+ if metadata is not None:
476
+ if layer_metadata is not None:
477
+ raise ValueError("Cannot pass both metadata and layer_metadata")
478
+ layer_metadata = metadata
479
+
480
+ # validate metadata types
481
+ for metadata in [dataset_metadata, layer_metadata]:
482
+ if metadata is not None:
483
+ for k, v in metadata.items():
484
+ if not isinstance(k, str):
485
+ raise ValueError(f"metadata key {k} must be a string")
486
+
487
+ if not isinstance(v, str):
488
+ raise ValueError(f"metadata value {v} must be a string")
489
+ return dataset_metadata, layer_metadata
490
+
491
+
492
+ def _preprocess_options_kwargs(driver, dataset_options, layer_options, kwargs):
493
+ """
494
+ Preprocess kwargs and split in dataset and layer creation options.
495
+ """
496
+
497
+ dataset_kwargs = _preprocess_options_key_value(dataset_options or {})
498
+ layer_kwargs = _preprocess_options_key_value(layer_options or {})
499
+ if kwargs:
500
+ kwargs = _preprocess_options_key_value(kwargs)
501
+ dataset_option_names = _parse_options_names(
502
+ _get_driver_metadata_item(driver, "DMD_CREATIONOPTIONLIST")
503
+ )
504
+ layer_option_names = _parse_options_names(
505
+ _get_driver_metadata_item(driver, "DS_LAYER_CREATIONOPTIONLIST")
506
+ )
507
+ for k, v in kwargs.items():
508
+ if k in dataset_option_names:
509
+ dataset_kwargs[k] = v
510
+ elif k in layer_option_names:
511
+ layer_kwargs[k] = v
512
+ else:
513
+ raise ValueError(f"unrecognized option '{k}' for driver '{driver}'")
514
+
515
+ return dataset_kwargs, layer_kwargs
516
+
517
+
518
+ def _get_write_path_driver(path, driver, append=False):
519
+ """Validate and return path and driver
520
+
521
+ Parameters
522
+ ----------
523
+ path : str or io.BytesIO
524
+ path to output file on writeable file system or an io.BytesIO object to
525
+ allow writing to memory
526
+ driver : str, optional (default: None)
527
+ The OGR format driver used to write the vector file. By default attempts
528
+ to infer driver from path. Must be provided to write to a file-like
529
+ object.
530
+ append : bool, optional (default: False)
531
+ True if path and driver is being tested for append support
532
+
533
+ Returns
534
+ -------
535
+ (path, driver)
536
+ """
537
+
538
+ if isinstance(path, BytesIO):
539
+ if driver is None:
540
+ raise ValueError("driver must be provided to write to in-memory file")
541
+
542
+ # blacklist certain drivers known not to work in current memory implementation
543
+ # because they create multiple files
544
+ if driver in {"ESRI Shapefile", "OpenFileGDB"}:
545
+ raise ValueError(f"writing to in-memory file is not supported for {driver}")
546
+
547
+ # verify that driver supports VSI methods
548
+ if not ogr_driver_supports_vsi(driver):
549
+ raise DataSourceError(
550
+ f"{driver} does not support ability to write in-memory in GDAL "
551
+ f"{get_gdal_version_string()}"
552
+ )
553
+
554
+ if append:
555
+ raise NotImplementedError("append is not supported for in-memory files")
556
+
557
+ else:
558
+ path = vsi_path(str(path))
559
+
560
+ if driver is None:
561
+ driver = detect_write_driver(path)
562
+
563
+ # verify that driver supports writing
564
+ if not ogr_driver_supports_write(driver):
565
+ raise DataSourceError(
566
+ f"{driver} does not support write functionality in GDAL "
567
+ f"{get_gdal_version_string()}"
568
+ )
569
+
570
+ # prevent segfault from: https://github.com/OSGeo/gdal/issues/5739
571
+ if append and driver == "FlatGeobuf" and get_gdal_version() <= (3, 5, 0):
572
+ raise RuntimeError(
573
+ "append to FlatGeobuf is not supported for GDAL <= 3.5.0 due to segfault"
574
+ )
575
+
576
+ return path, driver
577
+
578
+
579
+ def write(
580
+ path,
581
+ geometry,
582
+ field_data,
583
+ fields,
584
+ field_mask=None,
585
+ layer=None,
586
+ driver=None,
587
+ # derived from meta if roundtrip
588
+ geometry_type=None,
589
+ crs=None,
590
+ encoding=None,
591
+ promote_to_multi=None,
592
+ nan_as_null=True,
593
+ append=False,
594
+ dataset_metadata=None,
595
+ layer_metadata=None,
596
+ metadata=None,
597
+ dataset_options=None,
598
+ layer_options=None,
599
+ gdal_tz_offsets=None,
600
+ **kwargs,
601
+ ):
602
+ """Write geometry and field data to an OGR file format.
603
+
604
+ Parameters
605
+ ----------
606
+ path : str or io.BytesIO
607
+ path to output file on writeable file system or an io.BytesIO object to
608
+ allow writing to memory
609
+ NOTE: support for writing to memory is limited to specific drivers.
610
+ geometry : ndarray of WKB encoded geometries or None
611
+ If None, geometries will not be written to output file
612
+ field_data : list-like of shape (num_fields, num_records)
613
+ contains one record per field to be written in same order as fields
614
+ fields : list-like
615
+ contains field names
616
+ field_mask : list-like of ndarrays or None, optional (default: None)
617
+ contains mask arrays indicating null values of the field at the same
618
+ position in the outer list, or None to indicate field does not have
619
+ a mask array
620
+ layer : str, optional (default: None)
621
+ layer name to create. If writing to memory and layer name is not
622
+ provided, it layer name will be set to a UUID4 value.
623
+ driver : string, optional (default: None)
624
+ The OGR format driver used to write the vector file. By default attempts
625
+ to infer driver from path. Must be provided to write to memory.
626
+ geometry_type : str, optional (default: None)
627
+ Possible values are: "Unknown", "Point", "LineString", "Polygon",
628
+ "MultiPoint", "MultiLineString", "MultiPolygon" or "GeometryCollection".
629
+
630
+ This parameter does not modify the geometry, but it will try to force
631
+ the layer type of the output file to this value. Use this parameter with
632
+ caution because using a wrong layer geometry type may result in errors
633
+ when writing the file, may be ignored by the driver, or may result in
634
+ invalid files.
635
+ crs : str, optional (default: None)
636
+ WKT-encoded CRS of the geometries to be written.
637
+ encoding : str, optional (default: None)
638
+ If present, will be used as the encoding for writing string values to
639
+ the file. Use with caution, only certain drivers support encodings
640
+ other than UTF-8.
641
+ promote_to_multi : bool, optional (default: None)
642
+ If True, will convert singular geometry types in the data to their
643
+ corresponding multi geometry type for writing. By default, will convert
644
+ mixed singular and multi geometry types to multi geometry types for
645
+ drivers that do not support mixed singular and multi geometry types. If
646
+ False, geometry types will not be promoted, which may result in errors
647
+ or invalid files when attempting to write mixed singular and multi
648
+ geometry types to drivers that do not support such combinations.
649
+ nan_as_null : bool, default True
650
+ For floating point columns (float32 / float64), whether NaN values are
651
+ written as "null" (missing value). Defaults to True because in pandas
652
+ NaNs are typically used as missing value. Note that when set to False,
653
+ behaviour is format specific: some formats don't support NaNs by
654
+ default (e.g. GeoJSON will skip this property) or might treat them as
655
+ null anyway (e.g. GeoPackage).
656
+ append : bool, optional (default: False)
657
+ If True, the data source specified by path already exists, and the
658
+ driver supports appending to an existing data source, will cause the
659
+ data to be appended to the existing records in the data source. Not
660
+ supported for writing to in-memory files.
661
+ NOTE: append support is limited to specific drivers and GDAL versions.
662
+ dataset_metadata : dict, optional (default: None)
663
+ Metadata to be stored at the dataset level in the output file; limited
664
+ to drivers that support writing metadata, such as GPKG, and silently
665
+ ignored otherwise. Keys and values must be strings.
666
+ layer_metadata : dict, optional (default: None)
667
+ Metadata to be stored at the layer level in the output file; limited to
668
+ drivers that support writing metadata, such as GPKG, and silently
669
+ ignored otherwise. Keys and values must be strings.
670
+ metadata : dict, optional (default: None)
671
+ alias of layer_metadata
672
+ dataset_options : dict, optional
673
+ Dataset creation options (format specific) passed to OGR. Specify as
674
+ a key-value dictionary.
675
+ layer_options : dict, optional
676
+ Layer creation options (format specific) passed to OGR. Specify as
677
+ a key-value dictionary.
678
+ gdal_tz_offsets : dict, optional (default: None)
679
+ Used to handle GDAL timezone offsets for each field contained in dict.
680
+ """
681
+ # if dtypes is given, remove it from kwargs (dtypes is included in meta returned by
682
+ # read, and it is convenient to pass meta directly into write for round trip tests)
683
+ kwargs.pop("dtypes", None)
684
+
685
+ path, driver = _get_write_path_driver(path, driver, append=append)
686
+
687
+ dataset_metadata, layer_metadata = _validate_metadata(
688
+ dataset_metadata, layer_metadata, metadata
689
+ )
690
+
691
+ if geometry is not None and promote_to_multi is None:
692
+ promote_to_multi = (
693
+ geometry_type.startswith("Multi")
694
+ and driver in DRIVERS_NO_MIXED_SINGLE_MULTI
695
+ )
696
+
697
+ if geometry is not None and crs is None:
698
+ warnings.warn(
699
+ "'crs' was not provided. The output dataset will not have "
700
+ "projection information defined and may not be usable in other "
701
+ "systems."
702
+ )
703
+
704
+ # preprocess kwargs and split in dataset and layer creation options
705
+ dataset_kwargs, layer_kwargs = _preprocess_options_kwargs(
706
+ driver, dataset_options, layer_options, kwargs
707
+ )
708
+
709
+ ogr_write(
710
+ path,
711
+ layer=layer,
712
+ driver=driver,
713
+ geometry=geometry,
714
+ geometry_type=geometry_type,
715
+ field_data=field_data,
716
+ field_mask=field_mask,
717
+ fields=fields,
718
+ crs=crs,
719
+ encoding=encoding,
720
+ promote_to_multi=promote_to_multi,
721
+ nan_as_null=nan_as_null,
722
+ append=append,
723
+ dataset_metadata=dataset_metadata,
724
+ layer_metadata=layer_metadata,
725
+ dataset_kwargs=dataset_kwargs,
726
+ layer_kwargs=layer_kwargs,
727
+ gdal_tz_offsets=gdal_tz_offsets,
728
+ )
729
+
730
+
731
+ def write_arrow(
732
+ arrow_obj,
733
+ path,
734
+ layer=None,
735
+ driver=None,
736
+ geometry_name=None,
737
+ geometry_type=None,
738
+ crs=None,
739
+ encoding=None,
740
+ append=False,
741
+ dataset_metadata=None,
742
+ layer_metadata=None,
743
+ metadata=None,
744
+ dataset_options=None,
745
+ layer_options=None,
746
+ **kwargs,
747
+ ):
748
+ """
749
+ Write an Arrow-compatible data source to an OGR file format.
750
+
751
+ .. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
752
+
753
+ Parameters
754
+ ----------
755
+ arrow_obj
756
+ The Arrow data to write. This can be any Arrow-compatible tabular data
757
+ object that implements the `Arrow PyCapsule Protocol`_ (i.e. has an
758
+ ``__arrow_c_stream__`` method), for example a pyarrow Table or
759
+ RecordBatchReader.
760
+ path : str or io.BytesIO
761
+ path to output file on writeable file system or an io.BytesIO object to
762
+ allow writing to memory
763
+ NOTE: support for writing to memory is limited to specific drivers.
764
+ layer : str, optional (default: None)
765
+ layer name to create. If writing to memory and layer name is not
766
+ provided, it layer name will be set to a UUID4 value.
767
+ driver : string, optional (default: None)
768
+ The OGR format driver used to write the vector file. By default attempts
769
+ to infer driver from path. Must be provided to write to memory.
770
+ geometry_name : str, optional (default: None)
771
+ The name of the column in the input data that will be written as the
772
+ geometry field. Will be inferred from the input data if the geometry
773
+ column is annotated as an "geoarrow.wkb" or "ogc.wkb" extension type.
774
+ Otherwise needs to be specified explicitly.
775
+ geometry_type : str
776
+ The geometry type of the written layer. Currently, this needs to be
777
+ specified explicitly when creating a new layer with geometries.
778
+ Possible values are: "Unknown", "Point", "LineString", "Polygon",
779
+ "MultiPoint", "MultiLineString", "MultiPolygon" or "GeometryCollection".
780
+
781
+ This parameter does not modify the geometry, but it will try to force the layer
782
+ type of the output file to this value. Use this parameter with caution because
783
+ using a wrong layer geometry type may result in errors when writing the
784
+ file, may be ignored by the driver, or may result in invalid files.
785
+ encoding : str, optional (default: None)
786
+ Only used for the .dbf file of ESRI Shapefiles. If not specified,
787
+ uses the default locale.
788
+ append : bool, optional (default: False)
789
+ If True, the data source specified by path already exists, and the
790
+ driver supports appending to an existing data source, will cause the
791
+ data to be appended to the existing records in the data source. Not
792
+ supported for writing to in-memory files.
793
+ NOTE: append support is limited to specific drivers and GDAL versions.
794
+ dataset_metadata : dict, optional (default: None)
795
+ Metadata to be stored at the dataset level in the output file; limited
796
+ to drivers that support writing metadata, such as GPKG, and silently
797
+ ignored otherwise. Keys and values must be strings.
798
+ layer_metadata : dict, optional (default: None)
799
+ Metadata to be stored at the layer level in the output file; limited to
800
+ drivers that support writing metadata, such as GPKG, and silently
801
+ ignored otherwise. Keys and values must be strings.
802
+ metadata : dict, optional (default: None)
803
+ alias of layer_metadata
804
+ dataset_options : dict, optional
805
+ Dataset creation options (format specific) passed to OGR. Specify as
806
+ a key-value dictionary.
807
+ layer_options : dict, optional
808
+ Layer creation options (format specific) passed to OGR. Specify as
809
+ a key-value dictionary.
810
+ **kwargs
811
+ Additional driver-specific dataset or layer creation options passed
812
+ to OGR. pyogrio will attempt to automatically pass those keywords
813
+ either as dataset or as layer creation option based on the known
814
+ options for the specific driver. Alternatively, you can use the
815
+ explicit `dataset_options` or `layer_options` keywords to manually
816
+ do this (for example if an option exists as both dataset and layer
817
+ option).
818
+
819
+ """
820
+ if not HAS_ARROW_WRITE_API:
821
+ raise RuntimeError("GDAL>=3.8 required to write using arrow")
822
+
823
+ if not hasattr(arrow_obj, "__arrow_c_stream__"):
824
+ raise ValueError(
825
+ "The provided data is not recognized as Arrow data. The object "
826
+ "should implement the Arrow PyCapsule Protocol (i.e. have a "
827
+ "'__arrow_c_stream__' method)."
828
+ )
829
+
830
+ path, driver = _get_write_path_driver(path, driver, append=append)
831
+
832
+ if "promote_to_multi" in kwargs:
833
+ raise ValueError(
834
+ "The 'promote_to_multi' option is not supported when writing using Arrow"
835
+ )
836
+
837
+ if geometry_name is not None:
838
+ if geometry_type is None:
839
+ raise ValueError("'geometry_type' keyword is required")
840
+ if crs is None:
841
+ # TODO: does GDAL infer CRS automatically from geometry metadata?
842
+ warnings.warn(
843
+ "'crs' was not provided. The output dataset will not have "
844
+ "projection information defined and may not be usable in other "
845
+ "systems."
846
+ )
847
+
848
+ dataset_metadata, layer_metadata = _validate_metadata(
849
+ dataset_metadata, layer_metadata, metadata
850
+ )
851
+
852
+ # preprocess kwargs and split in dataset and layer creation options
853
+ dataset_kwargs, layer_kwargs = _preprocess_options_kwargs(
854
+ driver, dataset_options, layer_options, kwargs
855
+ )
856
+
857
+ ogr_write_arrow(
858
+ path,
859
+ layer=layer,
860
+ driver=driver,
861
+ arrow_obj=arrow_obj,
862
+ geometry_type=geometry_type,
863
+ geometry_name=geometry_name,
864
+ crs=crs,
865
+ encoding=encoding,
866
+ append=append,
867
+ dataset_metadata=dataset_metadata,
868
+ layer_metadata=layer_metadata,
869
+ dataset_kwargs=dataset_kwargs,
870
+ layer_kwargs=layer_kwargs,
871
+ )