1 Preparation

Ensure that local folder structure exists

dataFolder = paste0(ws, "/data/")
if(!dir.exists(dataFolder))
  dir.create(dataFolder)

2 Getting the data from a WFS

wfsUrl <- "https://geodienste.hamburg.de/HH_WFS_EMobility"

Connect to the service

hh_client <- WFSClient$new(wfsUrl, 
                            serviceVersion = "2.0.0")
## Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
## deprecated. It might return a CRS with a non-EPSG compliant axis order.

What are the capabilities of the service?

hh_client$getCapabilities()
## <WFSCapabilities>
##   Inherits from: <OWSCapabilities>
##   Public:
##     attrs: list
##     clone: function (deep = FALSE) 
##     defaults: list
##     encode: function (addNS = TRUE, geometa_validate = TRUE, geometa_inspire = FALSE) 
##     ERROR: function (text) 
##     findFeatureTypeByName: function (expr, exact = TRUE) 
##     getClass: function () 
##     getClassName: function () 
##     getFeatureTypes: function (pretty = FALSE) 
##     getOperationsMetadata: function () 
##     getOWSVersion: function () 
##     getRequest: function () 
##     getService: function () 
##     getServiceIdentification: function () 
##     getServiceProvider: function () 
##     getServiceVersion: function () 
##     getUrl: function () 
##     INFO: function (text) 
##     initialize: function (url, version, logger = NULL) 
##     logger: function (type, text) 
##     loggerType: NULL
##     verbose.debug: FALSE
##     verbose.info: FALSE
##     WARN: function (text) 
##     wrap: FALSE
##   Private:
##     featureTypes: list
##     fetchFeatureTypes: function (xmlObj, version) 
##     operationsMetadata: OWSOperationsMetadata, R6
##     owsVersion: 1.1
##     request: OWSGetCapabilities, OWSRequest, OGCAbstractObject, R6
##     service: WFS
##     serviceIdentification: OWSServiceIdentification, R6
##     serviceProvider: OWSServiceProvider, R6
##     serviceVersion: 2.0.0
##     system_fields: verbose.info verbose.debug loggerType wrap attrs defaults
##     url: https://geodienste.hamburg.de/HH_WFS_EMobility
##     xmlElement: NULL
##     xmlNamespace: NULL
##     xmlNodeToCharacter: function (x, ..., indent = "", tagSeparator = "\n")

Which features are present?

hh_client$getFeatureTypes(pretty = TRUE)
##                      name                   title
## 1 app:stromnetz_emobility app:stromnetz_emobility

Get the features from app:stromnetz_emobility

wfsUrlParsed <- parse_url(wfsUrl)
wfsUrlParsed$query <- list(service = "wfs",
                  version = "2.0.0",
                  request = "GetFeature",
                  typename = "app:stromnetz_emobility",
                  srsname='urn:x-ogc:def:crs:EPSG:25832',
                  bbox = "552378.61, 5921782.142, 582209.533, 5956674.616")
request <- build_url(wfsUrlParsed)

chargingStations <- read_sf(request) 

See what we have got:

names(chargingStations)
##  [1] "gml_id"            "standort"          "anzahl_ladepunkte"
##  [4] "ladesaeule_status" "adresse"           "koordinaten"      
##  [7] "typ"               "ladepunkt"         "stecker"          
## [10] "status"            "authmethod_1"      "authmethod_2"     
## [13] "the_geom"

The CRS did not make it through the request so we need to manually assign the CRS. The meta-data of the service indicate that it is EPSG:25832 (UTM zone 32N).

st_crs(chargingStations) <- 25832
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(chargingStations) + tm_dots(col = "anzahl_ladepunkte", size=0.5, alpha=.5) + tm_basemap(server= "OpenStreetMap.DE")

We could even get information on the current status of the charging station.

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(chargingStations) + tm_dots(col = "ladesaeule_status", size=0.5, alpha=.5) + tm_basemap(server= "OpenStreetMap.DE")

2.1 Save data to geopackage

We will not be using the live data but just the locations in the following. Therefore, it is fine to save the data - which might save us some time later on.

st_write(chargingStations, dsn= paste0(dataFolder, "HH.gpkg"), layer="chargingStations", delete_layer=TRUE)
## Deleting layer `chargingStations' using driver `GPKG'
## Writing layer `chargingStations' to data source `/home/slautenb/Documents/lehre/HD/ws_2020_21/heikaLab/R4UrbanDataLab_2020//data/HH.gpkg' using driver `GPKG'
## Writing 1061 features with 12 fields and geometry type Point.