Skip to content

Commit

Permalink
add package prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghah committed Aug 24, 2017
1 parent e042f83 commit 16e144c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 27 deletions.
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Package: esri2sf
Type: Package
Title: What the Package Does (Title Case)
Title: Create Simple Features from ArcGIS Server REST API
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Author: Yongha Hwang
Maintainer: Yongha Hwang <yongha.hwang@gmail.com>
Description: This package enables you to create simple features directrly from ArcGIS servers REST API.
License: MIT
Encoding: UTF-8
LazyData: true
Imports:
dplyr,
jsonlite,
httr,
sf
RoxygenNote: 6.0.1
42 changes: 21 additions & 21 deletions R/esri2sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ library(dplyr)
#' url <- "https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3"
#' outFields <- c("POP2007", "POP2000")
#' where <- "STATE_NAME = 'Michigan'"
#' df <- esri2sf(url, where=where)
#' df <- esri2sf(url, outFields=outFields, where=where)
#' plot(df)
#' @export
esri2sf <- function(url, outFields="*", where="1=1", token='') {
layerInfo <- fromJSON(content(POST(url, query=list(f="json", token=token), encode="form")))
layerInfo <- jsonlite::fromJSON(httr::content(httr::POST(url, query=list(f="json", token=token), encode="form")))
print(layerInfo$type)
geomType <- layerInfo$geometryType
print(geomType)
Expand All @@ -28,28 +28,28 @@ esri2sf <- function(url, outFields="*", where="1=1", token='') {
return(simpleFeatures)
}

getEsriFeatures <- function(url, fields, where, token='') {
ids <- getObjectIds(url, where, token)
getEsriFeatures <- function(queryUrl, fields, where, token='') {
ids <- getObjectIds(queryUrl, where, token)
idSplits <- split(ids, ceiling(seq_along(ids)/500))
results <- lapply(idSplits, getEsriFeaturesByIds, url, fields, token)
results <- lapply(idSplits, getEsriFeaturesByIds, queryUrl, fields, token)
merged <- unlist(results, recursive=FALSE)
return(merged)
}

getObjectIds <- function(url, where, token=''){
getObjectIds <- function(queryUrl, where, token=''){
# create Simple Features from ArcGIS servers json response
query <- list(
where=where,
returnIdsOnly="true",
token=token,
f="json"
)
responseRaw <- content(POST(url, body=query, encode="form"))
response <- fromJSON(responseRaw)
responseRaw <- httr::content(httr::POST(queryUrl, body=query, encode="form"))
response <- jsonlite::fromJSON(responseRaw)
return(response$objectIds)
}

getEsriFeaturesByIds <- function(ids, url, fields, token=''){
getEsriFeaturesByIds <- function(ids, queryUrl, fields, token=''){
# create Simple Features from ArcGIS servers json response
query <- list(
objectIds=paste(ids, collapse=","),
Expand All @@ -58,8 +58,8 @@ getEsriFeaturesByIds <- function(ids, url, fields, token=''){
outSR='4326',
f="json"
)
responseRaw <- content(POST(url, body=query, encode="form"))
response <- fromJSON(responseRaw,
responseRaw <- httr::content(httr::POST(queryUrl, body=query, encode="form"))
response <- jsonlite::fromJSON(responseRaw,
simplifyDataFrame = FALSE,
simplifyVector = FALSE,
digits=NA)
Expand All @@ -80,17 +80,17 @@ esri2sfGeom <- function(jsonFeats, geomType) {
}
# attributes
atts <- lapply(jsonFeats, '[[', 1)
af <- bind_rows(lapply(atts, as.data.frame.list, stringsAsFactors=FALSE))
af <- dplyr::bind_rows(lapply(atts, as.data.frame.list, stringsAsFactors=FALSE))
# geometry + attributes
df <- st_sf(geoms, af, geom=geoms, crs="+init=epsg:4326")
df <- sf::st_sf(geoms, af, geom=geoms, crs="+init=epsg:4326")
return(df)
}

esri2sfPoint <- function(features) {
getPointGeometry <- function(feature) {
return(st_point(unlist(feature$geometry)))
return(sf::st_point(unlist(feature$geometry)))
}
geoms <- st_sfc(lapply(features, getPointGeometry))
geoms <- sf::st_sfc(lapply(features, getPointGeometry))
return(geoms)
}

Expand All @@ -99,12 +99,12 @@ esri2sfPolygon <- function(features) {
return(do.call(rbind, lapply(ring, unlist)))
}
rings2multipoly <- function(rings) {
return(st_multipolygon(list(lapply(rings, ring2matrix))))
return(sf::st_multipolygon(list(lapply(rings, ring2matrix))))
}
getGeometry <- function(feature) {
return(rings2multipoly(feature$geometry$rings))
}
geoms <- st_sfc(lapply(features, getGeometry))
geoms <- sf::st_sfc(lapply(features, getGeometry))
return(geoms)
}

Expand All @@ -113,12 +113,12 @@ esri2sfPolyline <- function(features) {
return(do.call(rbind, lapply(path, unlist)))
}
paths2multiline <- function(paths) {
return(st_multilinestring(lapply(paths, path2matrix)))
return(sf::st_multilinestring(lapply(paths, path2matrix)))
}
getGeometry <- function(feature) {
return(paths2multiline(feature$geometry$paths))
}
geoms <- st_sfc(lapply(features, getGeometry))
geoms <- sf::st_sfc(lapply(features, getGeometry))
return(geoms)
}

Expand All @@ -133,7 +133,7 @@ generateToken <- function(server, uid){
f="json"
)
url <- paste(server, "arcgis/admin/generateToken", sep="/")
r <- POST(url, body=query, encode="form")
token <- fromJSON(content(r, "parsed"))$token
r <- httr::POST(url, body=query, encode="form")
token <- jsonlite::fromJSON(httr::content(r, "parsed"))$token
return(token)
}
1 change: 1 addition & 0 deletions esri2sf.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette
29 changes: 29 additions & 0 deletions man/esri2sf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions vignettes/esri2sf-vignettes.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: "esri2sf-vignettes"
author: "Yongha Hwang"
date: "August 23, 2017"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{esri2sf-vignettes}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## esri2sf

Still many geographic data is delivered through ESRI's ArcGIS Server. It is not easy to utilize the geographic data in the server. This package enables users to use vector data in ArcGIS Server through the server's REST API. It download geographic features from ArcGIS Server and saves it as Simple Features.

## How it works

This program sends a request to ArcGIS Server and gets json responses containing coordinates. Unfortunately, ESRI's json format is not the same as geojson. So it converts the json into WKT strings and creates simple feature geometries from them. Then it combines attributes to the geometries to create sf dataframe. Often ArcGIS servers limits the maximum number of result sets. So this program creates features by 500 in one request and send a request until it gets all features.

## Install

I recommend devtools to install this package.This package has dependency on dplyr, sf, httr, jsonlite

```{r}
devtools::install_github("yonghah/esri2sf")
library("esri2sf")
```

## How to use

The key is to get the endpoint of REST service that you want. Once you get the URL of the REST service, follow this.


### Point data

```{r}
url <- "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0"
outFields <- c('Height', "Leaf_Area")
df <- esri2sf(url, outFields)
plot(df)
```


```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

0 comments on commit 16e144c

Please sign in to comment.