|
| 1 | +# ruff: noqa: RUF003 |
| 2 | +""" |
| 3 | +Roads |
| 4 | +===== |
| 5 | +
|
| 6 | +The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such |
| 7 | +as lines which are stored in a :class:`geopandas.GeoDataFrame` object. Use |
| 8 | +:func:`geopandas.read_file` to load data from any supported OGR format such as |
| 9 | +a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. Then, pass the |
| 10 | +:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of |
| 11 | +:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter. |
| 12 | +""" |
| 13 | + |
| 14 | +# %% |
| 15 | +import geopandas as gpd |
| 16 | +import pygmt |
| 17 | + |
| 18 | +# Read shapefile data using geopandas |
| 19 | +gdf = gpd.read_file( |
| 20 | + "http://www2.census.gov/geo/tiger/TIGER2015/PRISECROADS/tl_2015_15_prisecroads.zip" |
| 21 | +) |
| 22 | +# The dataset contains different road types listed in the RTTYP column, |
| 23 | +# here we select the following ones to plot: |
| 24 | +roads_common = gdf[gdf.RTTYP == "M"] # Common name roads |
| 25 | +roads_state = gdf[gdf.RTTYP == "S"] # State recognized roads |
| 26 | +roads_interstate = gdf[gdf.RTTYP == "I"] # Interstate roads |
| 27 | + |
| 28 | +fig = pygmt.Figure() |
| 29 | + |
| 30 | +# Define target region around Oʻahu (Hawaiʻi) |
| 31 | +region = [-158.3, -157.6, 21.2, 21.75] # xmin, xmax, ymin, ymax |
| 32 | + |
| 33 | +title = "Main roads of O`ahu (Hawai`i)" # Approximating the Okina letter ʻ with ` |
| 34 | +fig.basemap(region=region, projection="M12c", frame=["af", f"WSne+t{title}"]) |
| 35 | +fig.coast(land="gray", water="dodgerblue4", shorelines="1p,black") |
| 36 | + |
| 37 | +# Plot the individual road types with different pen settings and assign labels |
| 38 | +# which are displayed in the legend |
| 39 | +fig.plot(data=roads_common, pen="5p,dodgerblue", label="CommonName") |
| 40 | +fig.plot(data=roads_state, pen="2p,gold", label="StateRecognized") |
| 41 | +fig.plot(data=roads_interstate, pen="2p,red", label="Interstate") |
| 42 | + |
| 43 | +# Add legend |
| 44 | +fig.legend() |
| 45 | + |
| 46 | +fig.show() |
0 commit comments