-
Notifications
You must be signed in to change notification settings - Fork 224
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
Add gallery example for plotting an RGB image from an xarray.DataArray #2641
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d38316
Add gallery example for plotting an RGB image from an xarray.DataArray
weiji14 60d1be3
Add abbreviation for Cloud-Optimized GeoTIFF
weiji14 b516c19
Set title font to Times-Roman
weiji14 e7b66e6
Change apostrophe to Okina symbol ʻ
weiji14 9085f65
Inline comment on map scale projection
weiji14 4fd90be
Use ¯ and ` instead of \225 and \140
weiji14 7586ef7
Merge branch 'main' into gallery/rgb_image
weiji14 3c0712d
Force loading dataarray into memory
weiji14 00b427b
Apply suggestions from code review
weiji14 2553cce
Lint
weiji14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
RGB Image | ||
--------- | ||
The :meth:`pygmt.Figure.grdimage` method can be used to plot Red, Green, Blue | ||
(RGB) images, or any 3-band false color combination. Here, we'll use | ||
:py:func:`rioxarray.open_rasterio` to read a GeoTIFF file into an | ||
:class:`xarray.DataArray` format, and plot it on a map. | ||
|
||
The example below shows a Worldview 2 satellite image over | ||
`Lāhainā, Hawaiʻi during the August 2023 wildfires | ||
<https://en.wikipedia.org/wiki/2023_Hawaii_wildfires#L%C4%81hain%C4%81>`_. | ||
Data is sourced from a Cloud-Optimized GeoTIFF (COG) file hosted on | ||
`OpenAerialMap <https://map.openaerialmap.org>`_ under a | ||
`CC BY-NC 4.0 <https://creativecommons.org/licenses/by-nc/4.0/>`_ license. | ||
""" | ||
import pygmt | ||
import rioxarray | ||
|
||
############################################################################### | ||
# Read 3-band data from GeoTIFF into an xarray.DataArray object: | ||
with rioxarray.open_rasterio( | ||
filename="https://oin-hotosm.s3.us-east-1.amazonaws.com/64d6a49a19cb3a000147a65b/0/64d6a49a19cb3a000147a65c.tif", | ||
overview_level=5, | ||
) as img: | ||
# Subset to area of Lāhainā in EPSG:32604 coordinates | ||
image = img.rio.clip_box(minx=738000, maxx=755000, miny=2300000, maxy=2318000) | ||
image = image.load() # Force loading the DataArray into memory | ||
image | ||
|
||
############################################################################### | ||
# Plot the RGB imagery: | ||
fig = pygmt.Figure() | ||
with pygmt.config(FONT_TITLE="Times-Roman"): # Set title font to Times-Roman | ||
fig.grdimage( | ||
grid=image, | ||
# Use a map scale where 1 cm on the map equals 1 km on the ground | ||
projection="x1:100000", | ||
frame=[r"WSne+tL@!a¯hain@!a¯, Hawai`i on 9 Aug 2023", "af"], | ||
) | ||
fig.show() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 GeoTiff file is almost 1 GB, which is too big to download.
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.
The next line
overview_level=5
gets a reduced resolution version. Original image has shape (y: 115712, x: 99328), but at overview_level=5, the shape is (y: 1808, x: 1552).