Search notes:

Using SPARQL and R and to plot the open data coordinates of fountains onto a shape file

This is an attempt to use the R SPARQL package to query the approx. 1200 fountains of Zurich from Wikidata and to plot their coordinates onto the Open data shape file provided by the City of Zurich.
The SPARQ query was inspired by DINAcon: Linked Data Experience.
The coordinates that the query returns are then plotted. identify() is used so that the user can click onto a fountain on the map. The selected fountain will then be shown in the Wikidata resource in the user's default browser.
library(rgdal )
library(SPARQL)
library(raster)

stzh_data_root <- paste0 (Sys.getenv('USERPROFILE'),  '/github/github/data.stadt-zuerich.ch/');
stadtkreise    <- readOGR(paste0(stzh_data_root, 'dataset/stadtkreise'), 'Stadtkreis' );

endPointWikiData <- 'https://query.wikidata.org/sparql';

query <- '
  select
       ?fountain
       ?lat
       ?lon
  where
  {
    ?fountain      p:P528                ?statement     .
    ?statement     pq:P972                wd:Q53629101  .
    ?fountain      p:P625/psv:P625       ?coord         .
    ?coord         wikibase:geoLatitude  ?lat           .
    ?coord         wikibase:geoLongitude ?lon           .
  }
';


fountains <- SPARQL(endPointWikiData, query);

par(mar=rep(0, 4));
plot(stadtkreise);

coordinates(fountains$results) <- ~lon + lat
crs(fountains$results) <- CRS('+proj=longlat +datum=WGS84')
trx <- spTransform(fountains$results, crs(stadtkreise))
points(trx$lon, trx$lat, col='#3355ff', pch=16, cex = 0.4);

fountain_ <- identify(trx$lon, trx$lat, n = 1, plot = FALSE);
browseURL(gsub('<|>', '', fountains$results$fountain[fountain_]));
Github repository data.stadt-zuerich.ch, path: /scripts/plot-fountains.R
After posting on Twitter, @csarasuagar noted that this query can also be rendered on a map with Wikidata Query Service.
@sfkeller added that it's possible to type fountain in Zürich into the Wizard of Overpass Turbo.

Links

1200 Fountains in Zurich: All over the city, top-quality drinking water flows out of fountains both big and small – and free of charge for everyone

Index