Skip to content

Instantly share code, notes, and snippets.

@FrieseWoudloper
Last active August 9, 2018 15:18
Show Gist options
  • Save FrieseWoudloper/47f4dcf68be39bff9dfaf2cd57610db3 to your computer and use it in GitHub Desktop.
Save FrieseWoudloper/47f4dcf68be39bff9dfaf2cd57610db3 to your computer and use it in GitHub Desktop.
ArcGIS Online hosted feature service in lezen in R
# Living Atlas of the world
# Rijksmonumenten
# https://www.arcgis.com/home/item.html?id=da0ee4c59ec44cff9683016c58a67470
# https://services.arcgis.com/nSZVuSZjHpEZZbRo/arcgis/rest/services/Rijksmonumenten/FeatureServer
library(sf)
library(tmap)
library(httr)
library(dplyr)
library(jsonlite)
library(data.table)
url <- list(
hostname = "services.arcgis.com/nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Rijksmonumenten/FeatureServer",
scheme = "https",
query = list(f = "json")
) %>% setattr("class","url")
# Layers
response <- build_url(url) %>% fromJSON()
layer_id <- response$layers %>% filter(name == 'Rijksmonumenten') %>% select(id) %>% as.numeric()
# Capabilities
url$path <- paste(layer_id)
capabilities <- build_url(url) %>% fromJSON()
max_record_count <- capabilities$maxRecordCount
supports_pagination <- capabilities$advancedQueryCapabilities$supportsPagination
print(max_record_count)
print(supports_pagination)
# Features
url$path <- paste(layer_id, "query", sep = "/")
# How many hits?
url$query<- list(where = "1=1",
outFields = "*",
returnCountOnly = "true",
f = "geojson")
response <- build_url(url) %>% fromJSON()
hits <- response$properties$count
print(hits)
# Request the first page with features
url$query$returnCountOnly = "false"
data <- build_url(url) %>% st_read(stringsAsFactors = FALSE) %>% st_set_crs(28992)
nrow(data) #64020 > This is strange!!! Shouldn't that be 10000, because maxRecordCount is 10000?
# Request all pages and all features using pagination
#### This code doesn't seem to be necessary - but I don't understand why ####
query$resultRecordCount = max_record_count
data_full <- lapply(seq(0, hits, max_record_count), function(x) {
url$query$resultOffset <- x
build_url(url) %>% st_read(stringsAsFactors = F)
}) %>% do.call(rbind, .)
nrow(data_full) # 64020
head(data_full)
qtm(data_full)
# Statistics
url$query$outStatistics = '[{"statisticType":"MIN","onStatisticField":"OBJECTID","outStatisticFieldName":"MIN_OBJECTID"},
{"statisticType":"MAX","onStatisticField":"OBJECTID","outStatisticFieldName":"MAX_OBJECTID"}]'
response <- build_url(url) %>% fromJSON()
minimum <- response$features$properties$MIN_OBJECTID
maximum <- response$features$properties$MAX_OBJECTID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment