Skip to content

Instantly share code, notes, and snippets.

@SwampThingPaul
Created June 14, 2022 16:41
Show Gist options
  • Save SwampThingPaul/419f641a0f5dd8d3155f8a7a315d0ae9 to your computer and use it in GitHub Desktop.
Save SwampThingPaul/419f641a0f5dd8d3155f8a7a315d0ae9 to your computer and use it in GitHub Desktop.
Example to Query FDEP ArcGIS REST Server for blue-green algae
## Objective: To query FDEP's ArcGIS REST server via R
## You must have server link and know a little about the dataset.
##
## Due to the query limit and area of interest, query is broken into two regions
### Libraries
library(httr)
library(jsonlite)
path <- 'https://services1.arcgis.com/nRHtyn3uE1kyzoYc/arcgis/rest/services/VIEW_FL_Algal_Bloom_Site_Visits_1/FeatureServer/0/query?'
request <- GET(
url = path,
query= list(
where = "County='Lee' OR County='Collier' OR County='Hendry' OR County='Glades' OR County='Martin'",
outFields = '*',
f = 'pjson'
)
)
response <- content(request, as = "text", encoding = "UTF-8")
results <- jsonlite::fromJSON(response,flatten=T)
bgdat=results$features;# saves results from last query
request <- GET(
url = path,
query= list(
where = "County='Okeechobee' OR County='PalmBeach' OR County='Broward' OR County='MiamiDade' OR County='Monroe'",
outFields = '*',
f = 'pjson'
)
)
response <- content(request, as = "text", encoding = "UTF-8")
results <- jsonlite::fromJSON(response,flatten=T)
bgdat=rbind(bgdat, results$features);# combines all results
## Some data cleaning and reformatting
vars=c("attributes.objectid", "attributes.globalid", "attributes.SiteVisitDate",
"attributes.Location", "attributes.County", "attributes.Visitor",
"attributes.AlgaeObserved", "attributes.SampleTaken", "attributes.DepthDesc",
"attributes.SampleDepth", "attributes.AnalyzedBy", "attributes.Otherlab",
"attributes.Comments", "attributes.Latitude", "attributes.Longitude",
"attributes.AlgalID", "attributes.Microcystin", "attributes.OtherToxin",
"attributes.EditDate", "attributes.PicURL", "attributes.ToxinPresent",
"attributes.CyanobacteriaDominant", "geometry.x", "geometry.y"
)
vars=strsplit(vars,"\\.")
vars=sapply(vars,"[",2)
colnames(bgdat)<-tolower(vars)
bgdat$sitevisitdate=as.numeric(gsub('000$', '',format(bgdat$sitevisitdate,scientific=F)))
bgdat$datetime=as.POSIXct(bgdat$sitevisitdate,origin = c('1970-01-01'), tz = 'UTC')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment