Skip to content

Commit 4f1238e

Browse files
authored
GeoPandas: Improve the interger overflow check using np.iinfo (#3518)
1 parent 9c47b38 commit 4f1238e

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pygmt/helpers/tempfile.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ def tempfile_from_geojson(geojson):
143143
# 32-bit integer overflow issue. Related issues:
144144
# https://github.com/geopandas/geopandas/issues/967#issuecomment-842877704
145145
# https://github.com/GenericMappingTools/pygmt/issues/2497
146+
147+
int32_info = np.iinfo(np.int32)
148+
146149
if Version(gpd.__version__).major < 1: # GeoPandas v0.x
147150
# The default engine 'fiona' supports the 'schema' parameter.
148151
if geojson.index.name is None:
@@ -151,7 +154,10 @@ def tempfile_from_geojson(geojson):
151154
schema = gpd.io.file.infer_schema(geojson)
152155
for col, dtype in schema["properties"].items():
153156
if dtype in {"int", "int64"}:
154-
overflow = geojson[col].abs().max() > 2**31 - 1
157+
overflow = (
158+
geojson[col].max() > int32_info.max
159+
or geojson[col].min() < int32_info.min
160+
)
155161
schema["properties"][col] = "float" if overflow else "int32"
156162
geojson[col] = geojson[col].astype(schema["properties"][col])
157163
ogrgmt_kwargs["schema"] = schema
@@ -160,7 +166,10 @@ def tempfile_from_geojson(geojson):
160166
# but we can change the dtype directly.
161167
for col in geojson.columns:
162168
if geojson[col].dtype.name in {"int", "int64", "Int64"}:
163-
overflow = geojson[col].abs().max() > 2**31 - 1
169+
overflow = (
170+
geojson[col].max() > int32_info.max
171+
or geojson[col].min() < int32_info.min
172+
)
164173
dtype = "float" if overflow else "int32"
165174
geojson[col] = geojson[col].astype(dtype)
166175
# Using geopandas.to_file to directly export to OGR_GMT format

0 commit comments

Comments
 (0)