Skip to content

Commit

Permalink
completes rev ch8 jn
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowosad committed Oct 20, 2023
1 parent 2b31be4 commit 83ccdf1
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions 08-mapping.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,15 @@ plt.savefig('output/plot_rasterio2.svg', dpi=300)

## Interactive maps {#sec-interactive-maps}

### Minimal example
<!-- jn: an intro paragraph is missing -->

### Minimal example of interactive map

An interactive map of a `GeoSeries` or `GeoDataFrame` can be created with `.explore` (@sec-vector-layers).
Here is a minimal example:

```{python}
#| label: fig-explore
#| fig-cap: Minimal example of an interactive vector layer plot with `.explore`
nz.explore()
```

Expand All @@ -558,7 +558,6 @@ For example, here is how we can set green fill color and 30% opaque black outlin
```{python}
#| label: fig-explore-styling-polygons
#| fig-cap: Styling of polygons in `.explore`
nz.explore(color='green', style_kwds={'color':'black', 'opacity':0.3})
```

Expand All @@ -573,50 +572,48 @@ Additionally, for points, we can set the `marker_type`, to one of:
- `'circle'`---A vector circle with radius specified in $m$
- `'circle_marker'`---A vector circle with radius specified in pixels (the default)

For example, the following expression draws `'circe_marker`' points with 20 pixel radius, green fill, and black outline (@fig-explore-styling-points):
For example, the following expression draws `'circe_marker`' points with 20 pixel radius, green fill, and black outline (@fig-explore-styling-points).

```{python}
#| label: fig-explore-styling-points
#| fig-cap: Styling of points in `.explore` (using `circle_marker`)
nz_height.explore(
color='green',
style_kwds={'color':'black', 'opacity':0.5, 'fillOpacity':0.1},
marker_kwds={'color':'black', 'radius':20}
)
```

The following expression demonstrates the `'marker'` option (@fig-explore-styling-points2).
Note that the above-mentioned styling properties (other then `opacity`) are not applicable when using `marker_type='marker'`, because the markers are fixed PNG images:
@fig-explore-styling-points2 demonstrates the `'marker_type'` option.
Note that the above-mentioned styling properties (other then `opacity`) are not applicable when using `marker_type='marker'`, because the markers are fixed PNG images.

```{python}
#| label: fig-explore-styling-points2
#| fig-cap: Styling of points in `.explore` (using `marker`)
nz_height.explore(marker_type='marker')
```

<!-- jn: can we use our own png images as well? -->

### Layers

To display multiple layers, one on top of another, with `.explore`, we use the `m` argument, which stands for the previous map (@fig-explore-layers):
To display multiple layers, one on top of another, with `.explore`, we use the `m` argument, which stands for the previous map (@fig-explore-layers).

```{python}
#| label: fig-explore-layers
#| fig-cap: Displaying multiple layers in an interactive map with `.explore`
m = nz.explore()
nz_height.explore(m=m, color='red')
map1 = nz.explore()
nz_height.explore(m=map1, color='red')
```

One of the advantages of interactive maps is the ability to turn layers "on" and "off".
This capability is implemented in [`folium.LayerControl`](https://python-visualization.github.io/folium/latest/user_guide/ui_elements/layer_control.html#LayerControl) from package **folium**, which the **geopandas** `.explore` method is a wrapper of.
For example, this is how we can add a layer control for the `nz` and `nz_height` layers (@fig-explore-layers-controls).
Note the `name` properties, used to specify layer names in the control, and the `collapsed` property, used to specify whether the control is fully visible at all times (`False`), or on mouse hover (`True`, the default):
Note the `name` properties, used to specify layer names in the control, and the `collapsed` property, used to specify whether the control is fully visible at all times (`False`), or on mouse hover (`True`, the default).

```{python}
#| label: fig-explore-layers-controls
#| fig-cap: Displaying multiple layers in an interactive map with `.explore`
m = nz.explore(name='Polygons (adm. areas)')
nz_height.explore(m=m, color='red', name='Points (elevation)')
folium.LayerControl(collapsed=False).add_to(m)
Expand All @@ -626,23 +623,21 @@ m
### Symbology {#sec-explore-symbology}

Symbology can be specified in `.explore` using similar arguments as in `.plot` (@sec-plot-symbology).
For example, here is an interactive version of @fig-plot-symbology-colors (a).
For example, @fig-explore-symbology is an interactive version of @fig-plot-symbology-colors (a).

```{python}
#| label: fig-explore-symbology
#| fig-cap: 'Symbology in an interactive map of a vector layer, created with `.explore`'
nz.explore(column='Median_income', legend=True, cmap='Reds')
```

Fixed styling (@sec-explore-symbology) can be combined with symbology settings.
For example, polygon outline colors in @fig-explore-symbology are styled according to `'Median_income'`, however, this layer has overlapping outlines and the color is arbitrarily set according to the order of features (top-most features), which may be misleading and confusing.
For example, polygon outline colors in @fig-explore-symbology are styled according to `'Median_income'`, however, this layer has overlapping outlines and their color is arbitrarily set according to the order of features (top-most features), which may be misleading and confusing.
To specify fixed outline colors (e.g., black), we can use the `color` and `weight` properties of `style_kwds` (@fig-explore-symbology2):

```{python}
#| label: fig-explore-symbology2
#| fig-cap: 'Symbology combined with fixed styling in `.explore`'
nz.explore(column='Median_income', legend=True, cmap='Reds', style_kwds={'color':'black', 'weight': 0.5})
```

Expand All @@ -658,24 +653,24 @@ Several popular built-in basemaps can be specified using a string:
- `'CartoDB positron'`
- `'CartoDB dark_matter'`

Other basemaps are available through the **xyzservices** package, which needs to be installed (see `xyzservices.providers` for a list), or using a custom tile server URL.
<!-- jn: please recheck the above list... there was some changes with Stamen maps recently, and I am not sure if the list is still correct... -->

For example, the following expression displays the `'CartoDB positron'` tiles in an `.explore` map (@fig-explore-basemaps):
Other basemaps are available through the **xyzservices** package, which needs to be installed (see `xyzservices.providers` for a list), or using a custom tile server URL.
For example, the following expression displays the `'CartoDB positron'` tiles in an `.explore` map (@fig-explore-basemaps).

```{python}
#| label: fig-explore-basemaps
#| fig-cap: Specifying the basemap in `.explore`
nz.explore(tiles='CartoDB positron')
```

### Exporting interactive maps

An interactive map can be exported to an HTML file using the `.save` method of the `map` object.
The HTML file can then be shared with other people, or published on a server and shared through a URL.
A good free option for publishing a web map is [GitHub Pages](https://pages.github.com/).
A good free option for publishing a web map is through [GitHub Pages](https://pages.github.com/).

For example, here is how we can export the map shown in @fig-explore-layers-controls, to a file named `map.html`:
For example, here is how we can export the map shown in @fig-explore-layers-controls, to a file named `map.html`.

```{python}
#| output: false
Expand Down

0 comments on commit 83ccdf1

Please sign in to comment.