-
Notifications
You must be signed in to change notification settings - Fork 371
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
Create ocean bathymetry example #2195
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6322013
create example
lgolston 8a07c56
add bbox=None to BasicReader
lgolston 660f4d5
apply isort
lgolston bec38d8
use land feature shortcut
lgolston 16ce296
move file; expand docstring
lgolston cc6d0ff
use BoundaryNorm, fig.colorbar
lgolston 9041aca
fix 0 depth color; simplify
lgolston 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,96 @@ | ||
""" | ||
Ocean bathymetry | ||
---------------- | ||
|
||
Produces a map of ocean seafloor depth, demonstrating the | ||
:class:`cartopy.io.shapereader.Reader` interface. The data is a series | ||
of 10m resolution nested polygons obtained from Natural Earth, and derived | ||
from the NASA SRTM Plus product. Since the dataset contains a zipfile with | ||
multiple shapefiles representing different depths, the example demonstrates | ||
manually downloading and reading them with the general shapereader interface, | ||
instead of the specialized `cartopy.feature.NaturalEarthFeature` interface. | ||
|
||
""" | ||
from glob import glob | ||
|
||
import matplotlib | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
import cartopy.crs as ccrs | ||
import cartopy.feature as cfeature | ||
import cartopy.io.shapereader as shpreader | ||
|
||
|
||
def load_bathymetry(zip_file_url): | ||
"""Read zip file from Natural Earth containing bathymetry shapefiles""" | ||
# Download and extract shapefiles | ||
import io | ||
import zipfile | ||
|
||
import requests | ||
r = requests.get(zip_file_url) | ||
z = zipfile.ZipFile(io.BytesIO(r.content)) | ||
z.extractall("ne_10m_bathymetry_all/") | ||
|
||
# Read shapefiles, sorted by depth | ||
shp_dict = {} | ||
files = glob('ne_10m_bathymetry_all/*.shp') | ||
assert len(files) > 0 | ||
files.sort() | ||
depths = [] | ||
for f in files: | ||
depth = '-' + f.split('_')[-1].split('.')[0] # depth from file name | ||
depths.append(depth) | ||
bbox = (90, -15, 160, 60) # (x0, y0, x1, y1) | ||
nei = shpreader.Reader(f, bbox=bbox) | ||
shp_dict[depth] = nei | ||
depths = np.array(depths)[::-1] # sort from surface to bottom | ||
return depths, shp_dict | ||
|
||
|
||
if __name__ == "__main__": | ||
# Load data (14.8 MB file) | ||
depths, shp_dict = load_bathymetry( | ||
'https://naturalearth.s3.amazonaws.com/' + | ||
'10m_physical/ne_10m_bathymetry_all.zip') | ||
|
||
# Construct colormap: | ||
# Depth levels in the colorbar are spaced as in the data | ||
norm = matplotlib.colors.Normalize(vmin=-10000, vmax=0) # in meters | ||
depth_fraction = depths.astype(float) / depths.astype(float).min() | ||
tmp = matplotlib.colormaps['Blues'] # to quantize: use .resampled(10) | ||
colors_depths = tmp(depth_fraction) # color values at fractional depths | ||
|
||
# Set up plot | ||
subplot_kw = {'projection': ccrs.LambertCylindrical()} | ||
fig, ax = plt.subplots(subplot_kw=subplot_kw, figsize=(9, 7)) | ||
ax.set_extent([90, 160, -15, 60], crs=ccrs.PlateCarree()) # x0, x1, y0, y1 | ||
|
||
# Iterate and plot feature for each depth level | ||
for i, depth in enumerate(depths): | ||
ax.add_geometries(shp_dict[depth].geometries(), | ||
crs=ccrs.PlateCarree(), | ||
color=colors_depths[i]) | ||
|
||
# Add standard features | ||
ax.add_feature(cfeature.LAND, color='grey') | ||
ax.coastlines(lw=1, resolution='110m') | ||
ax.gridlines(draw_labels=False) | ||
ax.set_position([0.03, 0.05, 0.8, 0.9]) | ||
|
||
# Add custom colorbar | ||
axi = fig.add_axes([0.85, 0.1, 0.025, 0.8]) | ||
ax.add_feature(cfeature.BORDERS, linestyle=':') | ||
matplotlib.colorbar.ColorbarBase(ax=axi, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
cmap=tmp.reversed(), | ||
norm=norm, | ||
boundaries=depths.astype(int)[::-1], | ||
spacing='proportional', | ||
extend='min', | ||
ticks=depths.astype(int), | ||
label='Depth (m)') | ||
|
||
# Convert vector bathymetries to raster (saves a lot of disk space) | ||
# while leaving labels as vectors | ||
ax.set_rasterized(True) |
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
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.
Would it be better to quantize this and use BoundaryNorm() here since you have explicit levels?
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.
Thanks for this comment @greglucas . I have switched it to use BoundaryNorm. This ended up helping better link the data to the colorbar.