Skip to content
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/gallery/maps/roads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Roads
-----
The :meth:`pygmt.Figure.plot` method allows to plot geographical data which is
stored in a :class:`geopandas.GeoDataFrame` object.
Comment thread
michaelgrund marked this conversation as resolved.
Outdated
"""

import geopandas as gpd
import pygmt

# Read shapefile data using geopandas
gdf = gpd.read_file(
"http://www2.census.gov/geo/tiger/TIGER2015/PRISECROADS/tl_2015_15_prisecroads.zip"
)
# The dataset contains different road types listed in the RTTYP column,
# here we select the following ones to plot:
roads_common = gdf[gdf.RTTYP == "M"] # Common name roads
roads_state = gdf[gdf.RTTYP == "S"] # State recognized roads
roads_interstate = gdf[gdf.RTTYP == "I"] # Interstate roads

fig = pygmt.Figure()

# Define target region around O'ahu (Hawai'i)
region = [-158.3, -157.6, 21.2, 21.75] # minx, maxx, miny, maxy
Comment thread
michaelgrund marked this conversation as resolved.
Outdated

fig.basemap(
region=region,
projection="M12c",
frame=["af", 'WSne+t"Main roads of Oahu (Hawaii)"'],
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've figured it out! We can use the octal code for apostrophe (') which is 047, see https://docs.generic-mapping-tools.org/6.2/cookbook/octal-codes.html.

Suggested change
fig.basemap(
region=region,
projection="M12c",
frame=["af", 'WSne+t"Main roads of Oahu (Hawaii)"'],
)
title = r"Main roads of O\047ahu (Hawai\047i)" # \047 is octal code for '
fig.basemap(region=region, projection="M12c", frame=["af", f'WSne+t"{title}"'])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great @weiji14!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realizing two years later that I used the wrong symbol 😅 It should be an Okina ʻ (U+02BB) instead of an apostrophe. Looks more like a 6 than a 9.

fig.coast(land="gray", water="dodgerblue4", shorelines="1p,black")

# Plot the individual road types with different pen settings and assgin labels
Comment thread
michaelgrund marked this conversation as resolved.
Outdated
# which are displayed in the legend
fig.plot(data=roads_common, pen="5p,dodgerblue", label="CommonName")
fig.plot(data=roads_state, pen="2p,gold", label="StateRecognized")
fig.plot(data=roads_interstate, pen="2p,red", label="Interstate")

# Add legend
fig.legend()

fig.show()