question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integration with geopandas to plot shapely geometries

See original GitHub issue

Description of the desired feature

Geopandas is somewhat of a defacto geographic data structure in Python nowadays, and is used to hold vector shapes like Polygons, Lines and Points. What might be useful is if we can have PyGMT plot geopandas Data Structures (e.g. geopandas.GeoDataFrames) directly. Something like so:

import geopandas as gpd
import pygmt

cities: gpd.GeoDataFrame = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
cities.head()
# 	name	geometry
# 0	Vatican City	POINT (12.45339 41.90328)
# 1	San Marino	POINT (12.44177 43.93610)
# 2	Vaduz	POINT (9.51667 47.13372)
# 3	Luxembourg	POINT (6.13000 49.61166)
# 4	Palikir	POINT (158.14997 6.91664)

fig = pygmt.Figure()
fig.plot(data=cities, style="s0.2c", color="black")
fig.show()

One way would be to change plot to accept geopandas type objects, look for the ‘geometry’ column, and plot the ‘Shapely’ geometries.

Currently, users need to go through a convoluted process such as with the following (adapted from https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/21, also xref https://github.com/GenericMappingTools/foss4g2019oceania/pull/7):

fig = pygmt.Figure()
fig.coast(region="g", land="gray", water="lightblue")
points = [point for point in cities.geometry[:10]]  # plot 10 cities only
for point in points:
    x, y = point.coords.xy
    fig.plot(x=x, y=y, style="s0.2c", color="black")
fig.show()

produces:

cities

Using data=geodataframe seems like the easiest, but we could consider alternative ways of implementing the geopandas integration. There might need to be separate code pathways for handling Points, Lines, and Polygons.

👋 Would be good to hear from the geo-community: What is your preferred way of putting data into a plot function?

  1. The plot-function-first-then-data way, i.e. fig.plot(data=geodata)
  2. The data-first-then-plot accessor way geodata.gmt.plot(style=...)

Are you willing to help implement and maintain this feature? Yes, but discuss first 😄

Related issues that may benefit from this: #392, #493

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
weiji14commented, Feb 26, 2021

FYI, I’m working on #961 which will lay the groundwork for geopandas integration (by providing a single I/O entrypoint into GMT virtualfiles). Still need to work on the implementation details, but hopefully 🤞 this will mean that any PyGMT function which accepts a table-like input will be able to take in geopandas.GeoDataFrame too (i.e. not just fig.plot), but will need more testing on points/lines/polygons to see if this does actually work!

1reaction
steo85itcommented, Feb 20, 2021

Hi! This would be super-useful, and your “hack” already works great for plotting geoDataFrames of Polygons imported from .shp files on top of .grd “images/maps”.

image

LMK if you need help with testing and such!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plotting with Geoplot and GeoPandas
We start out by replicating the basic GeoPandas world plot using Geoplot. Geoplot can re-project data into any of the map projections provided...
Read more >
Using GeoPandas for Spatial Visualization | by Paul Torres
Geometric operations are performed by shapely. GeoPandas further depends on fiona for file access and descartes and matplotlib for plotting.
Read more >
Mapping in Python with geopandas - Dani Arribas-Bel
The easiest way to get from a file to a quick visualization of the data is by loading it as a GeoDataFrame and...
Read more >
Geospatial Python | Plot Shapely Polygon with Matplotlib
We can plot Shapely polygons by resorting to Geopandas plot() function, or directly, extracting the polygon's boundaries.
Read more >
Geopandas Plotting Geometries and Spatial Operations
That's the power of GeoPanda. It's integrating Fiona as well as Shapely and other tools that we have, and Pandas of course. Let's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found