rrgdalweb-feature-service

List aviable WFS layers and read into data frame with rgdal


I have the following problem according to different sources it should be able to read WFS layer in R using rgdal.

dsn<-"WFS:http://geomap.reteunitaria.piemonte.it/ws/gsareprot/rp-01/areeprotwfs/wfs_gsareprot_1?service=WFS&request=getCapabilities"

ogrListLayers(dsn)
readOGR(dsn,"SIC")

The result of that code should be 1) to list the available WFS layer and 2) to read a specific Layer (SIC) into R as a Spatial(Points)DataFrame.

I tried several other WFS server but it does not work. I always get the warning:

Cannot open data source

Checking for the WFS driver i get the following result:

> "WFS" %in% ogrDrivers()$name
[1] FALSE

Well it looks like the WFS driver is not implemented in rgdal (anymore?) Or why are there so many examples "claiming" the opposite?

I also tried the gdalUtils package and well it works but It gives out the whole console message of ogrinfo.exe and not only the available layers.(I guess it "just" calls the ogrinfo.exe and sends the result back to R like using the r shell or system command).

Well does anyone know what I´m making wrong, or if something like that is even possible with rgdal or any similar package?


Solution

  • You can combine the two packages to accomplish your task.

    First, convert the layer you need into a local shapefile using gdalUtils. Then, use rgdal as normal. NOTE: you'll see a warning message after the ogr2ogr call but it performed the conversion fine for me. Also, ogr2ogr won't overwrite local files without the overwrite parameter being TRUE (there are other parameters that may be of use as well).

    library(gdalUtils)
    library(rgdal)
    
    dsn <- "WFS:http://geomap.reteunitaria.piemonte.it/ws/gsareprot/rp-01/areeprotwfs/wfs_gsareprot_1?service=WFS&request=getCapabilities"
    
    ogrinfo(dsn, so=TRUE)
    ## [1] "Had to open data source read only."
    ## [2] "INFO: Open of `WFS:http://geomap.reteunitaria.piemonte.it/ws/gsareprot/rp-01/areeprotwfs/wfs_gsareprot_1?service=WFS&request=getCapabilities'"
    ## [3] "      using driver `WFS' successful."
    ## [4] "1: AreeProtette"
    ## [5] "2: ZPS"  
    ## [6] "3: SIC"
    
    ogr2ogr(dsn, "sic.shp", "SIC")
    
    sic <- readOGR("sic.shp", "sic", stringsAsFactors=FALSE)
    ## OGR data source with driver: ESRI Shapefile 
    ## Source: "sic.shp", layer: "sic"
    ## with 128 features
    ## It has 23 fields
    
    plot(sic)
    

    enter image description here

    str(sic@data)
    
    ## 'data.frame': 128 obs. of  23 variables:
    ##  $ gml_id    : chr  "SIC.510" "SIC.472" "SIC.470" "SIC.508" ...
    ##  $ objectid  : chr  "510" "472" "470" "508" ...
    ##  $ inspire_id: chr  NA NA NA NA ...
    ##  $ codice    : chr  "IT1160026" "IT1160017" "IT1160018" "IT1160020" ...
    ##  $ nome      : chr  "Faggete di Pamparato, Tana del Forno, Grotta delle Turbiglie e Grotte di Bossea" "Stazione di Linum narbonense" "Sorgenti del T.te Maira, Bosco di Saretto, Rocca Provenzale" "Bosco di Bagnasco" ...
    ##  $ cod_tipo  : chr  "B" "B" "B" "B" ...
    ##  $ tipo      : chr  "SIC" "SIC" "SIC" "SIC" ...
    ##  $ cod_reg_bi: chr  "1" "1" "1" "1" ...
    ##  $ des_reg_bi: chr  "Alpina" "Alpina" "Alpina" "Alpina" ...
    ##  $ mese_istit: chr  "11" "11" "11" "11" ...
    ##  $ anno_istit: chr  "1996" "1996" "1996" "1996" ...
    ##  $ mese_ultmo: chr  "2" NA NA NA ...
    ##  $ anno_ultmo: chr  "2002" NA NA NA ...
    ##  $ sup_sito  : chr  "29396102.9972" "82819.1127" "7272687.002" "3797600.3563" ...
    ##  $ perim_sito: chr  "29261.8758" "1227.8846" "17650.289" "9081.4963" ...
    ##  $ url1      : chr  "http://gis.csi.it/parchi/schede/IT1160026.pdf" "http://gis.csi.it/parchi/schede/IT1160017.pdf" "http://gis.csi.it/parchi/schede/IT1160018.pdf" "http://gis.csi.it/parchi/schede/IT1160020.pdf" ...
    ##  $ url2      : chr  "http://gis.csi.it/parchi/carte/IT1160026.djvu" "http://gis.csi.it/parchi/carte/IT1160017.djvu" "http://gis.csi.it/parchi/carte/IT1160018.djvu" "http://gis.csi.it/parchi/carte/IT1160020.djvu" ...
    ##  $ fk_ente   : chr  NA NA NA NA ...
    ##  $ nome_ente : chr  NA NA NA NA ...
    ##  $ url3      : chr  NA NA NA NA ...
    ##  $ url4      : chr  NA NA NA NA ...
    ##  $ tipo_geome: chr  "poligono" "poligono" "poligono" "poligono" ...
    ##  $ schema    : chr  "Natura2000" "Natura2000" "Natura2000" "Natura2000" ...