Created
August 7, 2018 07:24
-
-
Save FrieseWoudloper/9fb2b1b6ded34ba359c8f7e97ffc1c4a to your computer and use it in GitHub Desktop.
Adapted the script by Yonhah: https://github.com/yonghah/esri2sf/blob/master/R/esri2sf.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
esri2sf <- function(url, outFields=c("*"), where="1=1", token='') { | |
library(httr) | |
library(jsonlite) | |
library(sf) | |
library(dplyr) | |
layerInfo <- jsonlite::fromJSON( | |
httr::content( | |
httr::POST( | |
url, | |
query=list(f="json", token=token), | |
encode="form", | |
config = httr::config(ssl_verifypeer = FALSE) | |
), | |
as="text" | |
) | |
) | |
print(layerInfo$type) | |
geomType <- layerInfo$geometryType | |
print(geomType) | |
queryUrl <- paste(url, "query", sep="/") | |
esriFeatures <- getEsriFeatures(queryUrl, outFields, where, token) | |
simpleFeatures <- st_read(esriFeatures) | |
return(simpleFeatures) | |
} | |
getEsriFeatures <- function(queryUrl, fields, where, token='') { | |
ids <- getObjectIds(queryUrl, where, token) | |
if(is.null(ids)){ | |
warning("No records match the search critera") | |
return() | |
} | |
idSplits <- split(ids, ceiling(seq_along(ids)/500)) | |
results <- lapply(idSplits, getEsriFeaturesByIds, queryUrl, fields, token) | |
merged <- unlist(results, recursive=FALSE) | |
return(merged) | |
} | |
getObjectIds <- function(queryUrl, where, token=''){ | |
# create Simple Features from ArcGIS servers json response | |
query <- list( | |
where=where, | |
returnIdsOnly="true", | |
token=token, | |
f="json" | |
) | |
responseRaw <- httr::content( | |
httr::POST( | |
queryUrl, | |
body=query, | |
encode="form", | |
config = httr::config(ssl_verifypeer = FALSE)), | |
as="text" | |
) | |
response <- jsonlite::fromJSON(responseRaw) | |
return(response$objectIds) | |
} | |
getEsriFeaturesByIds <- function(ids, queryUrl, fields, token=''){ | |
# create Simple Features from ArcGIS servers json response | |
query <- list( | |
objectIds = paste(ids, collapse=","), | |
outFields = paste(fields, collapse=","), | |
token = token, | |
outSR = '4326', | |
f = "geojson" | |
) | |
responseRaw <- httr::content( | |
httr::POST( | |
queryUrl, | |
body=query, | |
encode="form", | |
config = httr::config(ssl_verifypeer = FALSE) | |
)) | |
return(responseRaw) | |
} | |
generateToken <- function(server, uid, pwd='', expiration=5000){ | |
# generate auth token from GIS server | |
if (pwd=='') { | |
pwd <- rstudioapi::askForPassword("pwd") | |
} | |
query <- list( | |
username=uid, | |
password=pwd, | |
expiration=expiration, | |
client="requestip", | |
f="json" | |
) | |
url <- paste(server, "arcgis/admin/generateToken", sep="/") | |
r <- httr::POST(url, body=query, encode="form") | |
token <- jsonlite::fromJSON(httr::content(r, "parsed"))$token | |
return(token) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment