-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle geopandas and shapely geometries via geo_interface link #1000
Conversation
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.
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.
b3eeaba
to
2acb0ab
Compare
pygmt/helpers/tempfile.py
Outdated
# with memfile.open(driver="GeoJSON") as collection: | ||
# # Get schema from GeoJSON | ||
# schema = collection.schema | ||
# # Write to temporary OGR_GMT format file | ||
# with fiona.open( | ||
# fp=tmpfile.name, mode="w", driver="OGR_GMT", schema=schema | ||
# ) as ogrgmtfile: | ||
# ogrgmtfile.write(geojson.__geo_interface__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my failed attempt to convert a GeoJSON string to an OGR_GMT (*.gmt) format file purely using fiona
(i.e. no geopandas
installed). The problem I found was that a schema had to be set, and that requires a lot of lines of code to do, which is why I stuck to using geopandas
in the lines above.
If someone can figure out a good way to solve the schema problem, that would be fantastic!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've removed this chunk of unusable code in 34de789. Hopefully this can be revisited in the future so that GeoJSON objects can be converted into OGR_GMT purely using fiona
without the need for installing geopandas
(a bit of a big dependency).
.github/workflows/ci_tests.yaml
Outdated
@@ -45,11 +45,14 @@ jobs: | |||
# python-version: 3.7 | |||
# isDraft: true | |||
# Pair Python 3.7 with NumPy 1.17 and Python 3.9 with NumPy 1.20 | |||
# Only install geopandas on Python 3.9/NumPy 1.20 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since geopandas is an optional dependency, probably good to have a test matrix that doesn't include it.
Taking a hint from db36410.
Co-Authored-By: Michael Grund <[email protected]>
c61f3d3
to
34de789
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great.
Checking with @michaelgrund if you have any other comments on this geopandas integration PR? If not, I'll merge in the next 24 hours. |
…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]>
Description of proposed changes
Enabling PyGMT to use GeoPandas data structures! This includes plotting
geopandas.GeoDataFrame
tables and running data processing tasks. Works by converting the GeoJSON__geo_interface__
representation to an OGR_GMT file format and passing that into GMT.Preview API doc example at https://pygmt-git-geopandasintegration-gmt.vercel.app/api/generated/pygmt.info.html.
Example code (note that the should work directly in PyGMT v0.4.0 since #1000 is merged!):
References:
TODO:
gpd.to_file
geopandas
installed, would prefer a purefiona
solution)fig.plot(data=gdf)
works (will do after Refactor plot and plot3d to use virtualfile_from_data #990)Fixes #608
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Slash Commands
You can write slash commands (
/command
) in the first line of a comment to performspecific operations. Supported slash commands are:
/format
: automatically format and lint the code/test-gmt-dev
: run full tests on the latest GMT development version