V tomto příspěvku ukážeme, jak můžete vy - jako Spatial Data Scientist - použít R<\/A> k přístupu k ArcGIS REST službě a odpovědět na následující otázku: "Existují v Floridě nějaké opuštěné železnice?<\/EM>".<\/P><\/P>ArcGIS Living Atlas of the World<\/A> nabízí pěkný datový soubor s železnicemi ve Spojených státech: USA Railroads<\/A>. Následováním URL služby se dostaneme na dotazovací stránku této Feature Layer<\/A>.<\/P><\/P>Následující R skript ukazuje, jak rychle vytvořit interaktivní mapu pro zodpovězení naší otázky. Prosím, posuňte se dolů pro malé vysvětlení...<\/P>library<SPAN class="punctuation token">(<\/SPAN>httr<SPAN class="punctuation token">)<\/SPAN> library<SPAN class="punctuation token">(<\/SPAN>sf<SPAN class="punctuation token">)<\/SPAN> library<SPAN class="punctuation token">(<\/SPAN>tmap<SPAN class="punctuation token">)<\/SPAN> url <SPAN class="operator token"><<\/SPAN><SPAN class="operator token">-<\/SPAN> parse_url<SPAN class="punctuation token">(<\/SPAN><SPAN class="string token">"https:\/\/services.arcgis.com\/P3ePLMYs2RVChkJx\/arcgis\/rest\/services"<SPAN class="punctuation token">)<\/SPAN> url$path <SPAN class="operator token"><<\/SPAN><SPAN class="operator token">-<\/SPAN> paste<SPAN class="punctuation token">(<\/SPAN>url$path<SPAN class="punctuation token">,<\/SPAN> <SPAN class="string token">"USA_Railroads_1\/FeatureServer\/0\/query"<SPAN class="punctuation token">,<\/SPAN> sep <SPAN class="operator token">=<\/SPAN> <SPAN class="string token">"\/<SPAN class=\"punctuation token\">"<SPAN class=\"punctuation token\">)<SPAN> url$query <SPAM class=\"operator token\"><<SPAM><SPAM class=\"operator token\">-</SPAM> list<SPAM class=\"punctuation token\">(<SPAM>where <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"STATE = 'FL'"</SPAM>, outFields <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"*"</SPAM>, returnGeometry <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"true"</SPAM>, f <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"geojson"</SPAM>) request <SPAM class=\"operator token\"><<SPAM><SPAM class=\"operator token\">-</SPAM> build_url<SPAM class=\"punctuation token\">(<SPAM>url<SPAM class=\"punctuation token\">)</SPAM> Florida_Railroads <SPAM class=\"operator token\"><<SPAM><SPAM class=\"operator token\">-</SPAM> st_read<SPAM class=\"punctuation token\">(<SPAM>request<SPAM class=\"punctuation token\">)</SPAM> tmap_mode<SPAM class=\"punctuation token\">(<SPAM>mode <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"view"</SPAM>) tm_shape<SPAM class=\"punctuation token\">(<SPAM>Florida_Railroads<SPAM class=\"punctuation token\">)+tm_lines(<SMALL col=<SMALL "NET_DESC"><SMALL palette = "Set1", lwd = 5)</SMALL>V RStudio<\/A> začínáme načtením následujících 3 balíčků R:httr - pomáhá nám sestavit URL dotazusf - pro čtení prostorových dattmap - pro vytvoření interaktivní mapy založené na Leaflet JavaScript knihovně
<\/P>
Následující R skript ukazuje, jak rychle vytvořit interaktivní mapu pro zodpovězení naší otázky. Prosím, posuňte se dolů pro malé vysvětlení...<\/P>
library<SPAN class="punctuation token">(<\/SPAN>httr<SPAN class="punctuation token">)<\/SPAN> library<SPAN class="punctuation token">(<\/SPAN>sf<SPAN class="punctuation token">)<\/SPAN> library<SPAN class="punctuation token">(<\/SPAN>tmap<SPAN class="punctuation token">)<\/SPAN> url <SPAN class="operator token"><<\/SPAN><SPAN class="operator token">-<\/SPAN> parse_url<SPAN class="punctuation token">(<\/SPAN><SPAN class="string token">"https:\/\/services.arcgis.com\/P3ePLMYs2RVChkJx\/arcgis\/rest\/services"<SPAN class="punctuation token">)<\/SPAN> url$path <SPAN class="operator token"><<\/SPAN><SPAN class="operator token">-<\/SPAN> paste<SPAN class="punctuation token">(<\/SPAN>url$path<SPAN class="punctuation token">,<\/SPAN> <SPAN class="string token">"USA_Railroads_1\/FeatureServer\/0\/query"<SPAN class="punctuation token">,<\/SPAN> sep <SPAN class="operator token">=<\/SPAN> <SPAN class="string token">"\/<SPAN class=\"punctuation token\">"<SPAN class=\"punctuation token\">)<SPAN> url$query <SPAM class=\"operator token\"><<SPAM><SPAM class=\"operator token\">-</SPAM> list<SPAM class=\"punctuation token\">(<SPAM>where <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"STATE = 'FL'"</SPAM>, outFields <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"*"</SPAM>, returnGeometry <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"true"</SPAM>, f <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"geojson"</SPAM>) request <SPAM class=\"operator token\"><<SPAM><SPAM class=\"operator token\">-</SPAM> build_url<SPAM class=\"punctuation token\">(<SPAM>url<SPAM class=\"punctuation token\">)</SPAM> Florida_Railroads <SPAM class=\"operator token\"><<SPAM><SPAM class=\"operator token\">-</SPAM> st_read<SPAM class=\"punctuation token\">(<SPAM>request<SPAM class=\"punctuation token\">)</SPAM> tmap_mode<SPAM class=\"punctuation token\">(<SPAM>mode <SPAM class=\"operator token\">=<SPAM> <SPAM class=\"string token\">"view"</SPAM>) tm_shape<SPAM class=\"punctuation token\">(<SPAM>Florida_Railroads<SPAM class=\"punctuation token\">)+tm_lines(<SMALL col=<SMALL "NET_DESC"><SMALL palette = "Set1", lwd = 5)</SMALL>
V
Zpracujeme URL tak, že vytvoříme seznam, do kterého přidáme všechny parametry našeho dotazu - where, outFields, returnGeometry a f - s jejich příslušnými hodnotami. Jakmile je seznam naplněn, použijeme funkci build_url(), abychom vytvořili správně zakódovaný požadavek. Tento požadavek je pak předán funkci st_read(), která naplní objekt Florida_Railroads. Tento objekt se stává jak objektem simple features tak datovým rámcem, tj. obsahuje jak geometrii tak atributové hodnoty železnic.
where
outFields
returnGeometry
f
build_url()
st_read()
Florida_Railroads
Z níže uvedeného výpisu obrazovky vidíme, že celkový počet vrácených záznamů je 3305, což výrazně překračuje limit maxRecordCount (1000) pro tuto Feature Layer. Knihovna GDAL používaná funkcí st_read() v balíčku sf, poskytuje automatické stránkování. Pěkné, že?
maxRecordCount
sf
S posledními dvěma řádky skriptu vytvoříme interaktivní mapu ve Vieweru v RStudio.
A tedy odpověď na naši otázku je: "Ano, ve Floridě je docela dost opuštěných železnic." Tyto opuštěné tratě jsou na mapě ve výpisu obrazovky označeny červeně.
Hi @Egge-Jan_Pollé,
Thank you for the example. I am not familiar with the URL construction. What if I need to query multiple values of a field. In your example, would that be possible to set where = STATE = 'FL' and 'WA' and 'OR'?
The expression where = STATE = c('FL', 'WA','OR') doesn't work either.
Could you help? Thanks!
Yuan Grund
Hi @Egge-Jan_Pollé
Following the question already asked by @YuanGrund , I have the same doubt, but in outFields, c("Var1", "Var2", "Var3") does not work.
Thank you.
EDITED & SOLVED:
Just use: outFields = "Var1, Var2, Var3"
What if we want to access a private survey of our organization, I tried inputing my credentials ins url$username and url$password, but it didn't worked out at all. Here's my code and my output, censoring my sensible information with "~~~"
library(httr)library(sf)library(tmap)
url <- parse_url("https://services6.arcgis.com/~~~~~~~/arcgis/rest/services")url$path <- paste(url$path,"~~~~~~~~/FeatureServer/0/query",sep = "/")url$username <- '~~~USERNAME~~~'url$password <- '~~~PASSWORD~~~'url$query <- list(where = 'objectid = "*"',outFields = "*",returnGeometry = "true",f = "geojson")request <- build_url(url)test <- st_read(request)
https://services6.arcgis.com/~~~~~~~/arcgis/rest/services/~~~~~~~~/FeatureServer/0/query?where=objectid = "*"&outFields=*&returnGeometry=true&f=geojson";Check connection parameters.
Should I try another library rather than httr?
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Nový zde? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.