# noqa: D100
import json
from owslib.wfs import WebFeatureService
from birdy.dependencies import ipyleaflet as ipyl
from birdy.dependencies import ipywidgets as ipyw
ipyl_not_installed = "Ipyleaflet is not supported. Please install *ipyleaflet*."
ipyw_not_installed = "Ipywidgets is not supported. Please install *ipywidgets*."
# # # # # # # # # # #
# Utility functions #
# # # # # # # # # # #
[docs]
def _map_extent_to_bbox_filter(source_map):
"""Return formatted coordinates, from ipylealet format to owslib.wfs format.
This function takes the result of ipyleaflet's Map.bounds() function and formats it
so it can be used as a bbox filter in an owslib WFS request.
Parameters
----------
source_map: Map instance
The map instance from which the extent will calculated
Returns
-------
Tuple
Coordinates formatted to WebFeatureService bounding box filter.
"""
coords = source_map.bounds
lon1 = coords[0][1]
lat1 = coords[0][0]
lon2 = coords[1][1]
lat2 = coords[1][0]
formatted_coordinates = (lon1, lat1, lon2, lat2)
return formatted_coordinates
[docs]
class IpyleafletWFS:
"""Create a connection to a WFS service capable of geojson output.
This class is a small wrapper for ipylealet to facilitate the use of
a WFS service, as well as provide some automation.
Request to the WFS service is done through the owslib module and requires
a geojson output capable WFS. The geojson data is filtered for the map extent
and loaded as an ipyleaflet GeoJSON layer.
The automation done through build_layer() supports only a single
layer per instance is supported.
For multiple layers, used different instances of IpylealetWFS and Ipyleaflet.Map()
or use the create_wfsgeojson_layer() function to build your own custom map and widgets
with ipyleaflet.
Parameters
----------
url: str
The url of the WFS service
wfs_version: str
The version of the WFS service to use. Defaults to 2.0.0.
Returns
-------
IpyleafletWFS
Instance from which the WFS layers can be created.
"""
def __init__(self, url, wfs_version="2.0.0"):
self._geojson = None
self._layer = None
self._layer_typename = ""
self._layerstyle = {}
self._property = None
self._property_widgets = None
self._refresh_widget = None
self._source_map = None
self._wfs = WebFeatureService(url, version=wfs_version)
# Check if dependency is installed
if ipyl is None:
print(ipyl_not_installed)
# _property_widgets structure is as follows
# { 'widget_name': {
# 'widget': widget instance,
# 'property_key': property_string,
# 'position': position_string,
# },
# }
# # # # # # # # # # # # # # #
# Layer creation function #
# # # # # # # # # # # # # # #
[docs]
def build_layer(
self, layer_typename, source_map, layer_style=None, feature_property=None
):
"""Return an ipyleaflet GeoJSON layer from a geojson wfs request.
Requires the WFS service to be capable of geojson output.
Running this function multiple times will overwrite the previous layer and widgets.
Parameters
----------
layer_typename: string
Typename of the layer to display. Listed as Layer_ID by get_layer_list().
Must include namespace and layer name, separated by a colon.
ex: public:canada_forest_layer
source_map: Map instance
The map instance on which the layer is to be added.
layer_style: dictionnary
ipyleaflet GeoJSON style format, for example
`{ 'color': 'white', 'opacity': 1, 'dashArray': '9', 'fillOpacity': 0.1, 'weight': 1 }`.
See ipyleaflet documentation for more information.
feature_property: string
The property key to be used by the widget. Use the property_list() function
to get a list of the available properties.
"""
# Check if dependency is installed
if ipyl is None:
print(ipyl_not_installed)
return
# Check if layer already exists
if self._layer:
self._source_map.remove_layer(self._layer)
# Filter for None values
if feature_property is not None:
self._property = feature_property
if layer_style is not None:
self._layerstyle = layer_style
# Set parameters
self._layer_typename = layer_typename
self._source_map = source_map
# Calculate extent filter
bbox_filter_coords = _map_extent_to_bbox_filter(self._source_map)
# Fetch and prepare data
data = self._wfs.getfeature(
typename=self._layer_typename, bbox=bbox_filter_coords, outputFormat="JSON"
)
self._geojson = json.loads(data.getvalue().decode())
# Create layer and add to the map
self._layer = ipyl.GeoJSON(
data=self._geojson,
style=self._layerstyle,
hover_style={
"color": "yellow",
"dashArray": "0",
"fillOpacity": 0.5,
"fillColor": "yellow",
},
)
self._source_map.add_layer(self._layer)
# Create default property widget
if self._property_widgets is None:
self._property_widgets = {}
self.create_feature_property_widget("main_widget", self._property)
# Create refresh button
self._create_refresh_widget()
[docs]
def create_wfsgeojson_layer(self, layer_typename, source_map, layer_style=None):
"""Create a static ipyleaflett GeoJSON layer from a WFS service.
Simple wrapper for a WFS => GeoJSON layer, using owslib.
Will create a GeoJSON layer, filtered by the extent of the source_map parameter.
If no source map is given, it will not filter by extent, which can cause problems
with large layers.
WFS service need to have geojson output.
Parameters
----------
layer_typename: string
Typename of the layer to display. Listed as Layer_ID by get_layer_list().
Must include namespace and layer name, separated by a colon.
ex: public:canada_forest_layer
source_map: Map instance
The map instance from which the extent will be used to filter the request.
layer_style: dictionnary
ipyleaflet GeoJSON style format, for example
`{ 'color': 'white', 'opacity': 1, 'dashArray': '9', 'fillOpacity': 0.1, 'weight': 1 }`.
See ipyleaflet documentation for more information.
Returns
-------
GeoJSON layer: an instance of an ipyleaflet GeoJSON layer.
"""
# Check if dependency is installed
if ipyl is None:
print(ipyl_not_installed)
return
style = layer_style
if layer_style is None:
style = {}
# Calculate extent filter
bbox_filter_coords = _map_extent_to_bbox_filter(source_map)
# Fetch and prepare data
data = self._wfs.getfeature(
typename=layer_typename, bbox=bbox_filter_coords, outputFormat="JSON"
)
self._geojson = json.loads(data.getvalue().decode())
# Create layer, default widget and add to the map
layer = ipyl.GeoJSON(data=self._geojson, style=style)
return layer
[docs]
def _refresh_layer(self, placeholder=None):
"""Refresh the wfs layer for the current map extent.
Also updates the existing widgets.
Parameters
----------
placeholder: string
Parameter is only there so that button.on_click() will work properly.
"""
if self._layer:
self.build_layer(
self._layer_typename, self._source_map, self._layerstyle, self._property
)
for widget in self._property_widgets:
self.create_feature_property_widget(
widget_name=widget,
feature_property=self._property_widgets[widget]["property_key"],
widget_position=self._property_widgets[widget]["position"],
)
else:
print("There is no layer to refresh")
[docs]
def remove_layer(self):
"""Remove layer instance and it's widgets from map."""
if self._layer:
# Remove maps elements
self.clear_property_widgets()
self._source_map.remove_control(self._refresh_widget)
self._source_map.remove_layer(self._layer)
# Reset instance
self._layer = None
self._layer_typename = ""
self._layerstyle = {}
self._property = None
self._geojson = None
self._refresh_widget = None
else:
print("There is no layer to remove")
# # # # # # # # # # # # # # # #
# Layer information functions #
# # # # # # # # # # # # # # # #
[docs]
def feature_properties_by_id(self, feature_id):
"""Return the properties of a feature.
The id field is usually the first field. Since the name is
always different, this is the only assumption that can be
made to automate this process. Hence, this will not work if
the layer in question does not follow this formatting.
Parameters
----------
feature_id: int
The feature id.
Returns
-------
Dict
A dictionary of the layer's properties
"""
for feature in self._geojson["features"]:
# The id field is usually the first field. Since the name is
# always different, this is the only assumption I could make
# to automate this process.
first_key = list(feature["properties"].keys())[0]
current_feature_id = feature["properties"][first_key]
if current_feature_id == feature_id:
return feature["properties"]
@property
def geojson(self):
"""Return the imported geojson data in a python object format."""
return self._geojson
@property
def layer_list(self):
"""Return a simple layer list available to the WFS service.
Returns
-------
List
A List of the WFS layers available
"""
return sorted(self._wfs.contents.keys())
@property
def property_list(self):
"""Return a list containing the properties of the first feature.
Retrieves the available properties for use subsequent use
by the feature property widget.
Returns
-------
Dict
A dictionary of the layer properties.
"""
return self._geojson["features"][0]["properties"]
@property
def layer(self): # noqa: D102
return self._layer
# # # # # # # # # #
# Widget creation #
# # # # # # # # # #