forked from GenericMappingTools/pygmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle geopandas and shapely geometries via __geo_interface__ link (G…
…enericMappingTools#1000) Initial stab at allowing PyGMT to accept Python objects that implement __geo_interface__, i.e. a GeoJSON-like format. Works by conversion to a temporary OGR_GMT file, which can then be read natively by GMT . This is currently tested via `pygmt.info` in test_geopandas.py on a geopandas.GeoDataFrame object only. Will handle raw GeoJSON dict-like objects properly via fiona in subsequent iterations. * Add intersphinx mapping to geopandas docs * Create tempfile_from_geojson function to handle __geo_interface__ * Add geopandas.GeoDataFrame to list of allowed table inputs to info * Handle generic __geo_interface__ objects via fiona and geopandas Hacky attempt to handle non-geopandas objects (e.g. shapely.geometry) that have a __geo_interface__ dictionary property associated with it. Still assumes that geopandas is installed (along with fiona), so not a perfect solution. Also added another test with different geometry types. * Install geopandas on the Python 3.9/NumPy 1.20 test build only * Mention optional geopandas dependency in install and maintenance docs * Tweak tempfile_from_geojson docstring and remove unused fiona code * Add geopandas.GeoDataFrame to standardized table-classes filler text Co-authored-by: Michael Grund <[email protected]>
- Loading branch information
1 parent
054fddf
commit 567cead
Showing
10 changed files
with
145 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Tests on integration with geopandas. | ||
""" | ||
import numpy.testing as npt | ||
import pytest | ||
from pygmt import info | ||
|
||
gpd = pytest.importorskip("geopandas") | ||
shapely = pytest.importorskip("shapely") | ||
|
||
|
||
@pytest.fixture(scope="module", name="gdf") | ||
def fixture_gdf(): | ||
""" | ||
Create a sample geopandas GeoDataFrame object with shapely geometries of | ||
different types. | ||
""" | ||
linestring = shapely.geometry.LineString([(20, 15), (30, 15)]) | ||
polygon = shapely.geometry.Polygon([(20, 10), (23, 10), (23, 14), (20, 14)]) | ||
multipolygon = shapely.geometry.shape( | ||
{ | ||
"type": "MultiPolygon", | ||
"coordinates": [ | ||
[ | ||
[[0, 0], [20, 0], [10, 20], [0, 0]], # Counter-clockwise | ||
[[3, 2], [10, 16], [17, 2], [3, 2]], # Clockwise | ||
], | ||
[[[6, 4], [14, 4], [10, 12], [6, 4]]], # Counter-clockwise | ||
[[[25, 5], [30, 10], [35, 5], [25, 5]]], | ||
], | ||
} | ||
) | ||
# Multipolygon first so the OGR_GMT file has @GMULTIPOLYGON in the header | ||
gdf = gpd.GeoDataFrame( | ||
index=["multipolygon", "polygon", "linestring"], | ||
geometry=[multipolygon, polygon, linestring], | ||
) | ||
|
||
return gdf | ||
|
||
|
||
def test_geopandas_info_geodataframe(gdf): | ||
""" | ||
Check that info can return the bounding box region from a | ||
geopandas.GeoDataFrame. | ||
""" | ||
output = info(table=gdf, per_column=True) | ||
npt.assert_allclose(actual=output, desired=[0.0, 35.0, 0.0, 20.0]) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"geomtype,desired", | ||
[ | ||
("multipolygon", [0.0, 35.0, 0.0, 20.0]), | ||
("polygon", [20.0, 23.0, 10.0, 14.0]), | ||
("linestring", [20.0, 30.0, 15.0, 15.0]), | ||
], | ||
) | ||
def test_geopandas_info_shapely(gdf, geomtype, desired): | ||
""" | ||
Check that info can return the bounding box region from a shapely.geometry | ||
object that has a __geo_interface__ property. | ||
""" | ||
geom = gdf.loc[geomtype].geometry | ||
output = info(table=geom, per_column=True) | ||
npt.assert_allclose(actual=output, desired=desired) |