Skip to content

Commit 091bd3c

Browse files
authored
geopandas: Use io.StringIO to read geojson data and handle compatibility with geopandas v0.x and v1.x (#3247)
1 parent 861f454 commit 091bd3c

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

pygmt/helpers/tempfile.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
Utilities for dealing with temporary file management.
33
"""
44

5+
import io
56
import uuid
67
from contextlib import contextmanager
78
from pathlib import Path
89
from tempfile import NamedTemporaryFile
910

1011
import numpy as np
12+
from packaging.version import Version
1113

1214

1315
def unique_name():
@@ -139,29 +141,33 @@ def tempfile_from_geojson(geojson):
139141
# 32-bit integer overflow issue. Related issues:
140142
# https://github.com/geopandas/geopandas/issues/967#issuecomment-842877704
141143
# 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)
151163
# Using geopandas.to_file to directly export to OGR_GMT format
152164
geojson.to_file(**ogrgmt_kwargs)
153165
except AttributeError:
154166
# Other 'geo' formats which implement __geo_interface__
155167
import json
156168

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)
165171

166172
yield tmpfile.name
167173

0 commit comments

Comments
 (0)