|
35 | 35 | "import xarray_leaflet\n",
|
36 | 36 | "from rasterio.warp import Resampling\n",
|
37 | 37 | "from xarray_leaflet.transform import passthrough, normalize\n",
|
38 |
| - "from ipyleaflet import Map, basemaps\n", |
39 |
| - "from ipywidgets import Output" |
| 38 | + "from ipyleaflet import Map, basemaps" |
40 | 39 | ]
|
41 | 40 | },
|
42 | 41 | {
|
|
159 | 158 | "The third transformation applies to the data contained in each Leaflet tile before reprojection. Reprojection needs your data to fit into memory, so you may want to downsample your data and keep approximately the same number of points as there are in a tile (256 x 256). The default transformation for this stage does just that, but we will change it a little bit so that the aggregation function is not `mean` but `max`. We also downsample our data but it is very specific to this dataset."
|
160 | 159 | ]
|
161 | 160 | },
|
162 |
| - { |
163 |
| - "cell_type": "code", |
164 |
| - "execution_count": null, |
165 |
| - "metadata": {}, |
166 |
| - "outputs": [], |
167 |
| - "source": [ |
168 |
| - "debug_output = Output()" |
169 |
| - ] |
170 |
| - }, |
171 | 161 | {
|
172 | 162 | "cell_type": "code",
|
173 | 163 | "execution_count": null,
|
174 | 164 | "metadata": {},
|
175 | 165 | "outputs": [],
|
176 | 166 | "source": [
|
177 | 167 | "def transform2(array, *args, **kwargs):\n",
|
178 |
| - " with debug_output:\n", |
179 |
| - " tile_width = kwargs['tile_width']\n", |
180 |
| - " tile_height = kwargs['tile_height']\n", |
181 |
| - " ny, nx = array.shape\n", |
182 |
| - " wx = nx // (tile_width // 2)\n", |
183 |
| - " wy = ny // (tile_height // 2)\n", |
184 |
| - " dim = {}\n", |
185 |
| - " if wx > 1:\n", |
186 |
| - " dim['x'] = wx\n", |
187 |
| - " if wy > 1:\n", |
188 |
| - " dim['y'] = wy\n", |
189 |
| - " array = array.coarsen(**dim, boundary='pad')\n", |
190 |
| - " with warnings.catch_warnings():\n", |
191 |
| - " warnings.simplefilter(\"ignore\", category=RuntimeWarning)\n", |
192 |
| - " array = xr.core.rolling.DataArrayCoarsen.max(array)\n", |
193 |
| - " return array" |
| 168 | + " tile_width = kwargs['tile_width']\n", |
| 169 | + " tile_height = kwargs['tile_height']\n", |
| 170 | + " ny, nx = array.shape\n", |
| 171 | + " wx = nx // (tile_width // 2)\n", |
| 172 | + " wy = ny // (tile_height // 2)\n", |
| 173 | + " dim = {}\n", |
| 174 | + " if wx > 1:\n", |
| 175 | + " dim['x'] = wx\n", |
| 176 | + " if wy > 1:\n", |
| 177 | + " dim['y'] = wy\n", |
| 178 | + " array = array.coarsen(**dim, boundary='pad')\n", |
| 179 | + " with warnings.catch_warnings():\n", |
| 180 | + " warnings.simplefilter(\"ignore\", category=RuntimeWarning)\n", |
| 181 | + " array = xr.core.rolling.DataArrayCoarsen.max(array)\n", |
| 182 | + " return array" |
194 | 183 | ]
|
195 | 184 | },
|
196 | 185 | {
|
|
207 | 196 | "outputs": [],
|
208 | 197 | "source": [
|
209 | 198 | "def transform3(array, *args, **kwargs):\n",
|
210 |
| - " with debug_output:\n", |
211 |
| - " radius = 2\n", |
212 |
| - " circle = np.zeros((2*radius+1, 2*radius+1)).astype('uint8')\n", |
213 |
| - " y, x = np.ogrid[-radius:radius+1,-radius:radius+1]\n", |
214 |
| - " index = x**2 + y**2 <= radius**2\n", |
215 |
| - " circle[index] = 1\n", |
216 |
| - " with warnings.catch_warnings():\n", |
217 |
| - " warnings.simplefilter(\"ignore\", category=RuntimeWarning)\n", |
218 |
| - " array = np.sqrt(array)\n", |
219 |
| - " array = scipy.ndimage.maximum_filter(array, footprint=circle)\n", |
220 |
| - " return array" |
| 199 | + " radius = 2\n", |
| 200 | + " circle = np.zeros((2*radius+1, 2*radius+1)).astype('uint8')\n", |
| 201 | + " y, x = np.ogrid[-radius:radius+1,-radius:radius+1]\n", |
| 202 | + " index = x**2 + y**2 <= radius**2\n", |
| 203 | + " circle[index] = 1\n", |
| 204 | + " with warnings.catch_warnings():\n", |
| 205 | + " warnings.simplefilter(\"ignore\", category=RuntimeWarning)\n", |
| 206 | + " array = np.sqrt(array)\n", |
| 207 | + " array = scipy.ndimage.maximum_filter(array, footprint=circle)\n", |
| 208 | + " return array" |
221 | 209 | ]
|
222 | 210 | },
|
223 | 211 | {
|
|
230 | 218 | {
|
231 | 219 | "cell_type": "code",
|
232 | 220 | "execution_count": null,
|
233 |
| - "metadata": { |
234 |
| - "scrolled": false |
235 |
| - }, |
| 221 | + "metadata": {}, |
236 | 222 | "outputs": [],
|
237 | 223 | "source": [
|
238 | 224 | "m = Map(center=[-20, -60], zoom=3, basemap=basemaps.CartoDB.DarkMatter, interpolation='nearest')\n",
|
|
246 | 232 | "To show our data on the map, we call `leaflet.plot()` on our `DataArray`, and pass as parameters the map, the transformation functions, the `dynamic` value, and the resampling method for the reprojection."
|
247 | 233 | ]
|
248 | 234 | },
|
249 |
| - { |
250 |
| - "cell_type": "code", |
251 |
| - "execution_count": null, |
252 |
| - "metadata": {}, |
253 |
| - "outputs": [], |
254 |
| - "source": [ |
255 |
| - "debug_output" |
256 |
| - ] |
257 |
| - }, |
258 | 235 | {
|
259 | 236 | "cell_type": "code",
|
260 | 237 | "execution_count": null,
|
|
270 | 247 | " transform3=transform3,\n",
|
271 | 248 | " colormap=plt.cm.inferno,\n",
|
272 | 249 | " dynamic=dynamic,\n",
|
273 |
| - " resampling=Resampling.max,\n", |
274 |
| - " debug_output=debug_output)\n", |
| 250 | + " resampling=Resampling.max)\n", |
275 | 251 | "l.interact(opacity=(0.0,1.0,0.1))"
|
276 | 252 | ]
|
277 | 253 | },
|
|
285 | 261 | ],
|
286 | 262 | "metadata": {
|
287 | 263 | "kernelspec": {
|
288 |
| - "display_name": "Python 3", |
| 264 | + "display_name": "Python 3 (ipykernel)", |
289 | 265 | "language": "python",
|
290 | 266 | "name": "python3"
|
291 | 267 | },
|
|
299 | 275 | "name": "python",
|
300 | 276 | "nbconvert_exporter": "python",
|
301 | 277 | "pygments_lexer": "ipython3",
|
302 |
| - "version": "3.8.6" |
| 278 | + "version": "3.9.6" |
303 | 279 | }
|
304 | 280 | },
|
305 | 281 | "nbformat": 4,
|
|
0 commit comments