Skip to content

Commit 574d7c5

Browse files
committed
gdf to cartopy
1 parent f4a7b01 commit 574d7c5

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

index.Rmd

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,27 +348,41 @@ ax.coastlines()
348348
ax.add_feature(cfeature.BORDERS, linewidth=0.5, edgecolor='blue');
349349
```
350350

351-
## Regional maps
352-
We have seen now how to make worldwide maps, let's now make a smaller map with our own data. We can use the `set_extent` method to define an area of interest. In the following code we define an area of interest in units of the Dutch projection system RDNew (with the EPSG ode 28992). When passing these coordinates, do not forget to tell Cartopy from which reference system they are! Let's also add some gridlines so, another nice way to add some reference to map.
351+
## Smaller maps
352+
We have seen now how to make worldwide maps, let's now make a smaller map with our own data. To do this we use the `set_extent` method to define an area of interest and the `add_geometries` method to add geometries in a GeoDataFrame. Have a look at the code below
353353

354354
```{Python,engine.path='/usr/bin/python3', eval=FALSE}
355+
import geopandas as gpd
355356
356-
xmin, ymin, xmax, ymax = 171206, 440178, 178100, 444527 # The boundingbox of Wageningen
357-
crs = ccrs.epsg(28992) #
357+
gdf = gpd.read_file('https://raw.githubusercontent.com/GeoScripting-WUR/PythonProgramming/master/data/gadm41_NLD_2.json')
358358
359-
fig = plt.figure(figsize=(11, 8.5))
359+
# Using the dutch coodinate reference system RDNew (epsg code 28992)
360+
crs = ccrs.epsg(28992)
361+
362+
# The data is in another projection as our plot, reprojection to RDnew
363+
gdf = gdf.to_crs(28992)
364+
365+
# Initiate the plot, a little bigger then before
366+
fig = plt.figure(figsize=(15, 15))
360367
ax = plt.subplot(1, 1, 1, projection=crs)
361-
ax.set_title('RDNew')
368+
ax.set_title('The municipalities of NL')
369+
370+
# Draw gridlines
362371
gl = ax.gridlines(
363372
draw_labels=True, linewidth=2, color='gray', alpha=0.5, linestyle='--'
364373
)
365-
ax.set_extent([xmin, xmax, ymin, ymax], crs=crs)
366-
ax.coastlines(resolution=res, color='black')
367-
ax.add_feature(cfeature.RIVERS, linewidth=0.3, edgecolor='brown')
368-
ax.add_feature(cfeature.BORDERS, linewidth=0.5, edgecolor='blue');
374+
375+
# Set the extent to the extent of the municipalities
376+
min_x, max_x, min_y, max_y = gdf.total_bounds
377+
ax.set_extent((min_x, max_x, min_y, max_y), crs=crs)
378+
379+
# ax.set_extent(gdf.total_bounds, crs=crs) would do this in one step,
380+
# but the coordinates can be defined seperately as well in this order!
381+
382+
# Add the geometries to the map
383+
ax.add_geometries(gdf["geometry"], crs=crs, edgecolor = 'black', facecolor = '#FFFFFF')
369384
```
370385

371-
So we see that the pre defined features are useful, but when zooming in to Wageningen coastlines will not really help us... A good moment to add our own data! Let's start with a GeoDataFrame
372386

373387

374388
# Python help

0 commit comments

Comments
 (0)