Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods for sf objects #95

Merged
merged 46 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
148975a
Initial method for sf class
ateucher Nov 15, 2016
31a82ff
Methods for sfc and sfg; ignore crs for now
ateucher Nov 16, 2016
613b948
Start tests
ateucher Nov 16, 2016
7e89ced
Export list and json methods for sf, sfc, sfg
ateucher Nov 16, 2016
4f2110b
Always return GeometryCollection for sfc
ateucher Nov 16, 2016
dc55938
sfc - return GeometryCollection if length > 1, else just the geometry
ateucher Nov 16, 2016
e6ededf
Work on dealing with crs
ateucher Nov 18, 2016
8edd894
For now only warn on non WGS84 CRS
ateucher Nov 20, 2016
21a1d5c
Merge branch 'master' into sf
ateucher Nov 20, 2016
a27bd31
Add geo_list class to final object only (not nested lists)
ateucher Nov 20, 2016
4f71f0b
unclass geo_list before converting to json
ateucher Nov 20, 2016
55a97a3
Don't need to unclass, as.json does it for you
ateucher Nov 20, 2016
b2e8321
Don't need to get geometry type for sf - always FeatureCollection
ateucher Nov 20, 2016
9d54e49
Get epsg code for conversion message
ateucher Nov 20, 2016
d5ed3b3
Obey warn argument, return logical value
ateucher Nov 20, 2016
429bfed
Add some tests
ateucher Nov 20, 2016
93478bc
Add objects for creating multipoly test
ateucher Nov 20, 2016
4857f0c
Access new location of crs attribute
ateucher Nov 25, 2016
9c8be00
Travis
ateucher Nov 26, 2016
bf925e7
Add missing libraries
ateucher Nov 26, 2016
9f1e7ba
Trying again. colorspace for munsell for scales
ateucher Nov 26, 2016
880bacb
Trying travis again
ateucher Nov 27, 2016
b4dda6d
Try trusty dist
ateucher Nov 27, 2016
ee70bf7
Try libgeos-dev instead of libgeos++-dev
ateucher Nov 27, 2016
f17952b
back to last travis config
ateucher Nov 27, 2016
3c1f9db
Travis: libgeos-dev instead of libgoes++-dev
ateucher Nov 28, 2016
0fbd2c9
More tests
ateucher Nov 28, 2016
c402ad7
test linestring and multilinestring
ateucher Dec 16, 2016
2e49c0e
Start dealing with m, failing tests
ateucher Dec 16, 2016
44e0d46
Simplify geojson_list.sf
ateucher Jan 8, 2017
becfe97
Fix attribute name in tests
ateucher Jan 8, 2017
9f2654a
Remove M dimension for points
ateucher Jan 8, 2017
6eb66d5
Merge branch 'master' into sf
ateucher Jan 8, 2017
b6ec450
Add libudunites2-dev to travis
ateucher Jan 8, 2017
e9c6363
Generalize M dimension removal
ateucher Jan 8, 2017
2244ef9
Reorganize a bit, comment out unused code
ateucher Jan 8, 2017
afc5bdb
Try travis config based on @mdsumner's
ateucher Jan 8, 2017
7cf2501
Add cache: packages to travis
ateucher Jan 8, 2017
3f6d3be
rm cache: packages
ateucher Jan 8, 2017
cc4e29b
Saner attribute extraction
ateucher Jan 9, 2017
4da3ddc
Add examples and document
ateucher Jan 10, 2017
ea64f92
Examples and documentation for geojson_json
ateucher Jan 11, 2017
919ef48
Update documentation for geojson_list
ateucher Jan 11, 2017
86cda45
geojson_write methods for sf
ateucher Jan 11, 2017
3bd5c75
Comment in test
ateucher Jan 11, 2017
13f32a7
Document
ateucher Jan 11, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
language: c
sudo: required
language: r
dist: trusty
script: ./travis-tool.sh run_tests
sudo: required

before_script:
apt_packages:
- libudunits2-dev

before_install:
- sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable --yes
- sudo apt-get --yes --force-yes update -qq
- sudo apt-get install -y gdal-bin libgdal-dev libgdal1-dev netcdf-bin libproj-dev libv8-dev
- curl -OL http://raw.github.com/craigcitro/r-travis/master/scripts/travis-tool.sh
- chmod 755 ./travis-tool.sh
- ./travis-tool.sh bootstrap
- ./travis-tool.sh install_deps
- sudo apt-get install -y libudunits2-dev libproj-dev libgeos-dev libgdal-dev netcdf-bin libv8-dev

install_github:
- ropensci/gistr
compiler:
- clang

after_failure:
- ./travis-tool.sh dump_logs
warnings_are_errors: false

r_packages:
- colorspace

r_github_packages:
- ropensci/gistr

env:
global:
Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Suggests:
gistr,
testthat,
knitr,
leaflet
leaflet,
sf
Enhances: RColorBrewer
RoxygenNote: 5.0.1
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ S3method(geojson_json,data.frame)
S3method(geojson_json,geo_list)
S3method(geojson_json,list)
S3method(geojson_json,numeric)
S3method(geojson_json,sf)
S3method(geojson_json,sfc)
S3method(geojson_json,sfg)
S3method(geojson_list,SpatialCollections)
S3method(geojson_list,SpatialGrid)
S3method(geojson_list,SpatialGridDataFrame)
Expand All @@ -46,6 +49,9 @@ S3method(geojson_list,geo_list)
S3method(geojson_list,json)
S3method(geojson_list,list)
S3method(geojson_list,numeric)
S3method(geojson_list,sf)
S3method(geojson_list,sfc)
S3method(geojson_list,sfg)
S3method(geojson_read,character)
S3method(geojson_read,location)
S3method(geojson_sp,geo_json)
Expand All @@ -71,6 +77,9 @@ S3method(geojson_write,geo_list)
S3method(geojson_write,json)
S3method(geojson_write,list)
S3method(geojson_write,numeric)
S3method(geojson_write,sf)
S3method(geojson_write,sfc)
S3method(geojson_write,sfg)
S3method(lint,SpatialGrid)
S3method(lint,SpatialGridDataFrame)
S3method(lint,SpatialLines)
Expand Down
61 changes: 56 additions & 5 deletions R/geojson_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @export
#'
#' @param input Input list, data.frame, or spatial class. Inputs can also be dplyr \code{tbl_df}
#' @param input Input list, data.frame, spatial class, or sf class. Inputs can also be dplyr \code{tbl_df}
#' class since it inherits from \code{data.frame}.
#' @param lat (character) Latitude name. The default is \code{NULL}, and we attempt to guess.
#' @param lon (character) Longitude name. The default is \code{NULL}, and we attempt to guess.
Expand All @@ -25,6 +25,15 @@
#' Also note that with sp classes we do make a round-trip, using \code{\link[rgdal]{writeOGR}}
#' to write GeoJSON to disk, then read it back in. This is fast and we don't have to think
#' about it too much, but this disk round-trip is not ideal.
#'
#' For sf classes (sf, sfc, sfg), the following conversions are made:
#'
#' \itemize{
#' \item sfg: the approprite geometry \code{Point, LineString, Polygon, MultiPoint,
#' MultiLineString, MultiPolygon, GeometryCollection}
#' \item sfc: \code{GeometryCollection}, unless the sfc is length 1, then the geometry as above
#' \item sf: \code{FeatureCollection}
#' }
#'
#' @examples \dontrun{
#' # From a numeric vector of length 2, making a point type
Expand Down Expand Up @@ -170,7 +179,29 @@
#' poly <- SpatialPolygons(list(poly1, poly2), 1:2)
#' dat <- SpatialCollections(pts, polygons = poly)
#' geojson_json(dat)
#'
#'
#' # From sf classes:
#' if (require(sf)) {
#' ## sfg (a single simple features geometry)
#' p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
#' poly <- rbind(c(1,1), c(1,2), c(2,2), c(1,1))
#' poly_sfg <-st_polygon(list(p1))
#' geojson_json(poly_sfg)
#'
#' ## sfc (a collection of geometries)
#' p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
#' p2 <- rbind(c(5,5), c(5,6), c(4,5), c(5,5))
#' poly_sfc <- st_sfc(st_polygon(list(p1)), st_polygon(list(p2)))
#' geojson_json(poly_sfc)
#'
#' ## sf (collection of geometries with attributes)
#' p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
#' p2 <- rbind(c(5,5), c(5,6), c(4,5), c(5,5))
#' poly_sfc <- st_sfc(st_polygon(list(p1)), st_polygon(list(p2)))
#' poly_sf <- st_sf(foo = c("a", "b"), bar = 1:2, poly_sfc)
#' geojson_json(poly_sf)
#' }
#'
#' ## Pretty print a json string
#' geojson_json(c(-99.74,32.45))
#' geojson_json(c(-99.74,32.45)) %>% pretty
Expand Down Expand Up @@ -242,10 +273,30 @@ geojson_json.SpatialPixelsDataFrame <- function(input, lat = NULL, lon = NULL, g
class_json(geojson_rw(input, target = "char"), ...)
}

# sf classes ---------------------------------

#' @export
geojson_json.sf <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
as.json(geojson_list(input))
}

#' @export
geojson_json.sfc <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
as.json(geojson_list(input))
}

#' @export
geojson_json.sfg <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
as.json(geojson_list(input))
}

# spatial classes from rgeos --------------------------
#' @export
geojson_json.SpatialRings <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
geometry = "point", type='FeatureCollection', ...) {
class_json(geojson_rw(input, target = "char"), ...)
}

Expand All @@ -257,7 +308,7 @@ geojson_json.SpatialRingsDataFrame <- function(input, lat = NULL, lon = NULL, gr

#' @export
geojson_json.SpatialCollections <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type='FeatureCollection', ...) {
geometry = "point", type='FeatureCollection', ...) {
lapply(geojson_rw(input, target = "char", ...), class_json)
}

Expand Down Expand Up @@ -292,6 +343,6 @@ geojson_json.list <- function(input, lat = NULL, lon = NULL, group = NULL,
#' @export
geojson_json.geo_list <- function(input, lat = NULL, lon = NULL, group = NULL,
geometry = "point", type = "FeatureCollection", ...) {

to_json(unclass(input), ...)
}
Loading