LocalidataContent unavailable! (broken link)https://es.dbpedia.org/wiki/images/out.png is a Spanish company that provides an APIContent unavailable! (broken link)https://es.dbpedia.org/wiki/images/out.png to access their semantic data. We can request information about business premises, district geometries and many more.
In order to use this API you have to use a api_key that you get when you register.
With this example in R you can get from Localidata the contour segments of the Justicia district (in Madrid) and, following the first example, display the locations known by esDBpedia.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
library(httr)
library(jsonlite)
library(ggmap)
library(SPARQL)
#Example without pagination
#Info about the Justicia district (code 280796014)
res <- GET("http://datos.localidata.com",
path = "recurso/territorio/Barrio/280796014.geojson",
query = list(
api_key = "YOU_API_KEY",
`_view` = "coordenadas" #You have to escape variables starting with _
)
)
json <- fromJSON(rawToChar(res$content)) #The json sent by the server
#You have the polyline with the district frontiers in
#json$features$geometry$coordinates[[1]][[1]][[1]][[N]]
#N coordinates. There are 359 segments.
v_lat <- numeric(length(json$features$geometry$coordinates[[1]][[1]][[1]]))
v_lon <- numeric(length(json$features$geometry$coordinates[[1]][[1]][[1]]))
for (i in 1:length(v_lat)){
v_lon[i] <- json$features$geometry$coordinates[[1]][[1]][[1]][[i]][1]
v_lat[i] <- json$features$geometry$coordinates[[1]][[1]][[1]][[i]][2]
}
dfpoli <- data.frame(lat=v_lat, lon=v_lon) #long should NOT change its sign
#Google Map with the district borders
map.center <- geocode("Chueca, Madrid, Spain")
map <- get_map(c(lon=map.center$lon, lat=map.center$lat),
source="google", zoom=15)
plot <- ggmap(map) +
geom_polygon(aes(lon, lat), data = dfpoli, colour = NA, fill = "red", alpha = .3)
#Add esDBpedia geographical data to the map
endpoint <- "http://es.dbpedia.org/sparql"
query <-
"SELECT * WHERE {
?uri geo:lat ?lat .
?uri geo:long ?lon .
?uri rdf:type ?thetype .
FILTER ( (?lat> 40.418 && ?lat < 40.430) &&
(?lon> -3.704 && ?lon <-3.690)
&& regex(?thetype,'^http://schema.org')
)
}
"
reslist <- SPARQL(endpoint,query)
df <- reslist$results
df$thetype <- factor(df$thetype)
plot + geom_point(data=df,
aes(x=lon, y=lat, colour=thetype, position="dodge"),
size=6, alpha=0.8
)
This is the result
Content unavailable! (broken link)https://es.dbpedia.org/wiki/attach/Example%20of%20integration%20between%20esDBpedia%20and%20Localidata%20API/justicia.png |