|
2 | 2 | Utilities for dealing with temporary file management.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import io |
5 | 6 | import uuid
|
6 | 7 | from contextlib import contextmanager
|
7 | 8 | from pathlib import Path
|
8 | 9 | from tempfile import NamedTemporaryFile
|
9 | 10 |
|
10 | 11 | import numpy as np
|
| 12 | +from packaging.version import Version |
11 | 13 |
|
12 | 14 |
|
13 | 15 | def unique_name():
|
@@ -139,29 +141,33 @@ def tempfile_from_geojson(geojson):
|
139 | 141 | # 32-bit integer overflow issue. Related issues:
|
140 | 142 | # https://github.com/geopandas/geopandas/issues/967#issuecomment-842877704
|
141 | 143 | # https://github.com/GenericMappingTools/pygmt/issues/2497
|
142 |
| - if geojson.index.name is None: |
143 |
| - geojson.index.name = "index" |
144 |
| - geojson = geojson.reset_index(drop=False) |
145 |
| - schema = gpd.io.file.infer_schema(geojson) |
146 |
| - for col, dtype in schema["properties"].items(): |
147 |
| - if dtype in ("int", "int64"): |
148 |
| - overflow = geojson[col].abs().max() > 2**31 - 1 |
149 |
| - schema["properties"][col] = "float" if overflow else "int32" |
150 |
| - ogrgmt_kwargs["schema"] = schema |
| 144 | + if Version(gpd.__version__).major < 1: # GeoPandas v0.x |
| 145 | + # The default engine 'fiona' supports the 'schema' parameter. |
| 146 | + if geojson.index.name is None: |
| 147 | + geojson.index.name = "index" |
| 148 | + geojson = geojson.reset_index(drop=False) |
| 149 | + schema = gpd.io.file.infer_schema(geojson) |
| 150 | + for col, dtype in schema["properties"].items(): |
| 151 | + if dtype in ("int", "int64"): |
| 152 | + overflow = geojson[col].abs().max() > 2**31 - 1 |
| 153 | + schema["properties"][col] = "float" if overflow else "int32" |
| 154 | + ogrgmt_kwargs["schema"] = schema |
| 155 | + else: # GeoPandas v1.x. |
| 156 | + # The default engine "pyogrio" doesn't support the 'schema' parameter |
| 157 | + # but we can change the dtype directly. |
| 158 | + for col in geojson.columns: |
| 159 | + if geojson[col].dtype in ("int", "int64", "Int64"): |
| 160 | + overflow = geojson[col].abs().max() > 2**31 - 1 |
| 161 | + dtype = "float" if overflow else "int32" |
| 162 | + geojson[col] = geojson[col].astype(dtype) |
151 | 163 | # Using geopandas.to_file to directly export to OGR_GMT format
|
152 | 164 | geojson.to_file(**ogrgmt_kwargs)
|
153 | 165 | except AttributeError:
|
154 | 166 | # Other 'geo' formats which implement __geo_interface__
|
155 | 167 | import json
|
156 | 168 |
|
157 |
| - import fiona |
158 |
| - |
159 |
| - with fiona.Env(): |
160 |
| - jsontext = json.dumps(geojson.__geo_interface__) |
161 |
| - # Do Input/Output via Fiona virtual memory |
162 |
| - with fiona.io.MemoryFile(file_or_bytes=jsontext.encode()) as memfile: |
163 |
| - geoseries = gpd.GeoSeries.from_file(filename=memfile) |
164 |
| - geoseries.to_file(**ogrgmt_kwargs) |
| 169 | + jsontext = json.dumps(geojson.__geo_interface__) |
| 170 | + gpd.read_file(filename=io.StringIO(jsontext)).to_file(**ogrgmt_kwargs) |
165 | 171 |
|
166 | 172 | yield tmpfile.name
|
167 | 173 |
|
|
0 commit comments