0

I am trying to load some WFS data into a leaflet using R. The problem is that there doesn't seem to be a straightforward way to collect it, unlike with WMS, which has the addWMSTiles() function in the leaflet package for R.

How can I proceed?

Out of all attempts so far, the one with "greatest" success (no success really, just fetched the XML data) was done using the following code:

library(c(XML,plyr))

fileurl <- "http://ovc.catastro.meh.es/INSPIRE/wfsCP.aspx?service=WFS&request=GetFeature&Typenames=cp.cadastralparcel&SRSname=EPSG::25830&bbox=233673,4015968,233761,4016008&version=2.0.0&"
doc <- xmlParse(fileurl,useInternalNodes = TRUE) ### xmlParse()- is to parse the xml content, the parsed content is stored into doc
xL <- xmlToList(doc) ###is to convert xml doc into List

data <- ldply(xL, data.frame)
head(data)

This yields an XML with polygon points in one of the columns, but how can I include this in a Leaflet? It seems too much of a workaround to do this in this way. Does anyone know how to do this in a more straightforward manner?

Link to the inspire XML document on how to request data

UPDATE 1: I have found a way to do this using the sf R package. With

st_read("http://ovc.catastro.meh.es/INSPIRE/wfsCP.aspx?service=WFS&request=GetFeature&Typenames=cp.cadastralparcel&SRSname=EPSG::25830&bbox=233673,4015968,233761,4016008&version=2.0.0&")

Unfortunately it was not as straightforward as it seems. I had to install OSGeo4W64 in my machine because the rgdal package was not working properly. I also had to make the evironment variables point toward the OSGeo4W64, and delete the ones that pointed to sf. I followed this guide.

However I am still wondering how to integrate this in R just as I did with WMS. As you may notice from the url in st_read, it includes a Bounding Box, which I want leaflet to determine just like it did with WMS (which also required one)

2 Answers 2

1

If you want to load WFS data in leaflet, you can follow these steps:

  1. Download WFS data in R (with sf)
  2. Transform data to WGS84 datum (if needed)
  3. Visualise in Leaflet

    # load libraries
    library(sf)
    library(leaflet)
    
    # 1. read WFS data
    data_sf <- st_read("http://ovc.catastro.meh.es/INSPIRE/wfsCP.aspx?service=WFS&request=GetFeature&Typenames=cp.cadastralparcel&SRSname=EPSG::25830&bbox=233673,4015968,233761,4016008&version=2.0.0&")
    
    # 2. transform to WGS84
    data_sf_wgs84 <- st_transform(data_sf,4326)
    
    # 3. load data in leaflet
    leaflet() %>%
      addTiles() %>%
      addPolygons(data = data_sf_wgs84)
    

You can also customize the appearance, fill color,...
See https://rstudio.github.io/leaflet/ for more info and examples

    # with some additional options:
    leaflet() %>%
      addTiles() %>%
      addPolygons(data = data_sf_wgs84,
          color = 'black',
          fillOpacity = 1.0,
          fillColor = ~colorQuantile("viridis", areaValue)(areaValue),
          popup = ~nationalCadastralReference)
4
  • This does work, but it does not resolve my question, because I'd like to use the WFS as a dynamic service; so that when I move around the map, new data gets queried, just like when I use a WMS...
    – Miguel M.
    Commented Sep 26, 2018 at 10:35
  • I think you can not do this at the moment, because WFS plugins (e.g. Leaflet-WFST ) or WMS GetFeatureInfo requests -if this is what you're looking after- are not (yet) implemented in the leaflet.extras package.
    – Lennert
    Commented Sep 28, 2018 at 9:09
  • Is it still not possible? Do you know if maybe someone is working on this issue?
    – JdP
    Commented Dec 2, 2019 at 15:43
  • I did a quick search, but I'm not aware of someone implementing this in r leaflet.
    – Lennert
    Commented Feb 3, 2020 at 21:29
0

You probably won't be able to do it directly, because most clients can't handle the output of complex feature WFS such as this one.

We can see the output is a complex feature WFS by doing a DescribeFeatureType operation like:

http://ovc.catastro.meh.es/INSPIRE/wfsCP.aspx?service=WFS&request=DescribeFeatureType&Typenames=cp.cadastralparcel&version=2.0.0&

And looking at the result:

http://inspire.ec.europa.eu/schemas/cp/4.0/CadastralParcels.xsd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.