-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathgrdmask.py
More file actions
154 lines (115 loc) · 4.3 KB
/
grdmask.py
File metadata and controls
154 lines (115 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Create grid masks from geospatial shapes
========================================
:func:`pygmt.grdmask`.
:func:`pygmt.grdlandmask` and gallery example https://www.pygmt.org/latest/gallery/images/grdlandmask.html.
"""
# %%
import geopandas
import numpy as np
import pygmt
from shapely.geometry import Point
# %%
# Polygons based on NumPy arrays
# ------------------------------
# Define a study region
region = [125, 135, 25, 36]
# Define two closed polygons, here a quare and a triangle.
# Use nan to separate the polygons
polygon = np.array(
[
[129, 31],
[134, 31],
[134, 35],
[129, 35],
[129, 31],
[np.nan, np.nan],
[126, 26],
[131, 26],
[131, 30],
[126, 26],
],
)
# Download elevation grid
grid = pygmt.datasets.load_earth_relief(region=region, resolution="30s")
# Create a grid mask based on the two polygons defined above
# Set all grid nodes outside the polygons to NaN
mask_out = pygmt.grdmask(region=region, data=polygon, spacing="30s", outside="NaN")
# Set all grid nodes inside the polygons to NaN
# Set the outside parameter to a value larger 0 to keep the nodes outside unchanged
mask_in = pygmt.grdmask(
region=region, data=polygon, spacing="30s", inside="NaN", outside=1
)
# Apply the grid mask to the downloaded elevation grid by multiplying the two grids
grid_mask_out = grid * mask_out
grid_mask_in = grid * mask_in
fig = pygmt.Figure()
pygmt.makecpt(cmap="oleron", series=[-2000, 2000])
# Plot the elevation grid
fig.basemap(region=region, projection="M12c", frame=True)
fig.grdimage(grid=grid, cmap=True)
fig.plot(data=polygon, pen="2p,darkorange")
fig.shift_origin(xshift="+w+2c")
# Plot the masked elevation grid outside
fig.basemap(region=region, projection="M12c", frame=True)
fig.grdimage(grid=grid_mask_out, cmap=True)
fig.plot(data=polygon, pen="2p,darkorange")
fig.shift_origin(xshift="+w+2c")
# Plot the masked elevation grid inside
fig.basemap(region=region, projection="M12c", frame=True)
fig.grdimage(grid=grid_mask_in, cmap=True)
fig.plot(data=polygon, pen="2p,darkorange")
fig.colorbar(frame=True)
fig.show()
# %%
# US state Missouri based on GeoPandas polygon geometry
# -----------------------------------------------------
region = [-126, -66, 25, 49]
provider = "https://naciscdn.org/naturalearth"
states = geopandas.read_file(
f"{provider}/50m/cultural/ne_50m_admin_1_states_provinces.zip"
)
missouri = states[states["name"] == "Missouri"]
grid = pygmt.datasets.load_earth_relief(region=region, resolution="01m")
mask = pygmt.grdmask(region=region, data=missouri, spacing="01m", outside="NaN")
mask_lonlat = mask.rename(new_name_or_name_dict={"x": "lon", "y": "lat"})
grid_mask = grid * mask_lonlat
fig = pygmt.Figure()
pygmt.makecpt(cmap="oleron", series=[-2000, 2000])
# Plot the elevation grid
fig.basemap(projection="L-96/35/33/41/12c", region=region, frame=True)
fig.grdimage(grid=grid, cmap=True)
fig.plot(data=missouri, pen="1p,darkorange")
fig.shift_origin(xshift="+w+1c")
# Plot the masked elevation grid
# fig.basemap(projection="L-96/35/33/41/12c", region=region, frame=True)
fig.basemap(projection="M10c", region=[-96.5, -88.5, 35.8, 41], frame=True)
fig.grdimage(grid=grid_mask, cmap=True)
fig.plot(data=missouri, pen="1p,darkorange")
fig.colorbar(frame=True)
fig.show()
# %%
# Circle based on GeoPandas polygon geometry
# ------------------------------------------
region = [125, 135, 25, 36]
# Create a point and buffer it
point = geopandas.GeoSeries([Point(126.5, 33.5)])
circle = point.buffer(0.6) # 10 is the radius
grid = pygmt.datasets.load_earth_relief(region=region, resolution="30s")
mask = pygmt.grdmask(region=region, data=circle, spacing="30s", outside="NaN")
mask_lonlat = mask.rename(new_name_or_name_dict={"x": "lon", "y": "lat"})
grid_mask = grid * mask_lonlat
fig = pygmt.Figure()
pygmt.makecpt(cmap="oleron", series=[-2000, 2000])
# Plot the elevation grid
fig.basemap(region=region, projection="M12c", frame=True)
fig.grdimage(grid=grid, cmap=True)
fig.plot(data=circle, pen="2p,darkorange")
fig.shift_origin(xshift="+w+2c")
# Plot the masked elevation grid
fig.basemap(region=[125.5, 127.5, 32.5, 34.5], projection="M12c", frame=True)
fig.grdimage(grid=grid_mask, cmap=True)
fig.plot(data=circle, pen="2p,darkorange")
fig.colorbar(frame=True)
fig.show()
# sphinx_gallery_thumbnail_number = 1