Skip to content

Integration with geopandas to plot shapely geometries #608

Closed
@weiji14

Description

@weiji14

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 GenericMappingTools/foss4g2019oceania#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

Metadata

Metadata

Assignees

Labels

feature requestNew feature wantedhelp wantedHelping hands are appreciatedscipy-sprintThings to work on during the SciPy sprint!

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions