46
geojson_write
21
list(latitude=38, longitude=125, state="NY"),
list(latitude=40, longitude=128, state="VT"))
# just color
geojson_style(mylist, var = state,
color=brewer.pal(length(unique(sapply(mylist, [[, state))), "Blues"))
# color and size
geojson_style(mylist, var = state,
color=brewer.pal(length(unique(sapply(mylist, [[, state))), "Blues"),
size=c(small,medium,large,large))
# color, size, and symbol
geojson_style(mylist, var = state,
color=brewer.pal(length(unique(sapply(mylist, [[, state))), "Blues"),
size=c(small,medium,large,large),
symbol="zoo")
# stroke, fill
geojson_style(mylist, var = state,
stroke=brewer.pal(length(unique(sapply(mylist, [[, state))), "Blues"),
fill=brewer.pal(length(unique(sapply(mylist, [[, state))), "Greens"))
# from data.frame - polygon data
smallstates <- states[states$group %in% 1:3, ]
head(smallstates)
geojson_style(smallstates, var = group,
stroke = brewer.pal(length(unique(smallstates$group)), "Blues"),
stroke_width = c(1, 2, 3),
fill = brewer.pal(length(unique(smallstates$group)), "Greens"))
## End(Not run)
geojson_write
Convert many input types with spatial data to a geojson file
Description
Convert many input types with spatial data to a geojson file
Usage
geojson_write(input, lat = NULL, lon = NULL, geometry = "point",
group = NULL, file = "myfile.geojson", overwrite = TRUE,
precision = NULL, ...)
Arguments
input
Input list, data.frame, or spatial class. Inputs can also be dplyr tbl_df class
since it inherits from data.frame.
lat
(character) Latitude name. The default is NULL, and we attempt to guess.
lon
(character) Longitude name. The default is NULL, and we attempt to guess.
geometry
(character) One of point (Default) or polygon.
48
22
geojson_write
group
(character) A grouping variable to perform grouping for polygons - doesn’t ap-
ply for points
file
(character) Apathandfile name (e.g., myfile), with the .geojson file extension.
Default writes to current working directory.
overwrite
(logical) Overwrite the file given in file with input. Default: TRUE. If this
param is FALSE and the file already exists, we stop with error message.
precision
desired number of decimal places for the coordinates in the geojson file. Using
fewer decimal places can decrease file sizes (at the cost of precision).
...
Further args passed on towriteOGR
See Also
geojson_list, geojson_json
Examples
## Not run:
# From a data.frame
## to points
geojson_write(us_cities[1:2,], lat=lat, lon=long)
## to polygons
head(states)
geojson_write(input=states, lat=lat, lon=long, geometry=group, group="group")
## partial states dataset to points (defaults to points)
geojson_write(input=states, lat=lat, lon=long)
## Lists
### list of numeric pairs
poly <- list(c(-114.345703125,39.436192999314095),
c(-114.345703125,43.45291889355468),
c(-106.61132812499999,43.45291889355468),
c(-106.61132812499999,39.436192999314095),
c(-114.345703125,39.436192999314095))
geojson_write(poly, geometry = "polygon")
### named list
mylist <- list(list(latitude=30, longitude=120, marker="red"),
list(latitude=30, longitude=130, marker="blue"))
geojson_write(mylist)
# From a numeric vector of length 2
## Expected order is lon, lat
vec <- c(-99.74, 32.45)
geojson_write(vec)
## polygon from a series of numeric pairs
### this requires numeric class input, so inputting a list will
### dispatch on the list method
poly <- c(c(-114.345703125,39.436192999314095),
46
geojson_write
23
c(-114.345703125,43.45291889355468),
c(-106.61132812499999,43.45291889355468),
c(-106.61132812499999,39.436192999314095),
c(-114.345703125,39.436192999314095))
geojson_write(poly, geometry = "polygon")
# Write output of geojson_list to file
res <- geojson_list(us_cities[1:2,], lat=lat, lon=long)
class(res)
geojson_write(res)
# Write output of geojson_json to file
res <- geojson_json(us_cities[1:2,], lat=lat, lon=long)
class(res)
geojson_write(res)
# From SpatialPolygons class
library(sp)
poly1 <- Polygons(list(Polygon(cbind(c(-100,-90,-85,-100),
c(40,50,45,40)))), "1")
poly2 <- Polygons(list(Polygon(cbind(c(-90,-80,-75,-90),
c(30,40,35,30)))), "2")
sp_poly <- SpatialPolygons(list(poly1, poly2), 1:2)
geojson_write(sp_poly)
# From SpatialPolygonsDataFrame class
sp_polydf <- as(sp_poly, "SpatialPolygonsDataFrame")
geojson_write(input = sp_polydf)
# From SpatialGrid
x <- GridTopology(c(0,0), c(1,1), c(5,5))
y <- SpatialGrid(x)
geojson_write(y)
# From SpatialGridDataFrame
sgdim <- c(3,4)
sg <- SpatialGrid(GridTopology(rep(0,2), rep(10,2), sgdim))
sgdf <- SpatialGridDataFrame(sg, data.frame(val = 1:12))
geojson_write(sgdf)
# From SpatialRings
r1 <- Ring(cbind(x=c(1,1,2,2,1), y=c(1,2,2,1,1)), ID="1")
r2 <- Ring(cbind(x=c(1,1,2,2,1), y=c(1,2,2,1,1)), ID="2")
r1r2 <- SpatialRings(list(r1, r2))
geojson_write(r1r2)
# From SpatialRingsDataFrame
dat <- data.frame(id = c(1,2), value = 3:4)
r1r2df <- SpatialRingsDataFrame(r1r2, data = dat)
geojson_write(r1r2df)
# From SpatialPixels
library("sp")
41
24
lint
pixels <- suppressWarnings(SpatialPixels(SpatialPoints(us_cities[c("long", "lat")])))
summary(pixels)
geojson_write(pixels)
# From SpatialPixelsDataFrame
library("sp")
pixelsdf <- suppressWarnings(
SpatialPixelsDataFrame(points = canada_cities[c("long", "lat")], data = canada_cities)
)
geojson_write(pixelsdf)
# From SpatialCollections
library("sp")
poly1 <- Polygons(list(Polygon(cbind(c(-100,-90,-85,-100), c(40,50,45,40)))), "1")
poly2 <- Polygons(list(Polygon(cbind(c(-90,-80,-75,-90), c(30,40,35,30)))), "2")
poly <- SpatialPolygons(list(poly1, poly2), 1:2)
coordinates(us_cities) <- ~long+lat
dat <- SpatialCollections(points = us_cities, polygons = poly)
geojson_write(dat)
## End(Not run)
lint
Lint geojson
Description
Lint geojson
Usage
lint(x, ...)
Arguments
x
Input, a geojson character string or list
...
Further args passed on to helper functions.
Examples
## Not run:
lint({"type": "FooBar"})
lint({ "type": "FeatureCollection" })
lint({"type":"Point","geometry":{"type":"Point","coordinates":[-80,40]},"properties":{}})
# From a list turned into geo_list
mylist <- list(list(latitude=30, longitude=120, marker="red"),
list(latitude=30, longitude=130, marker="blue"))
x <- geojson_list(mylist)
class(x)
39
map_gist
25
lint(x)
# A file
file <- system.file("examples", "zillow_or.geojson", package = "geojsonio")
lint(as.location(file))
# A URL
url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
lint(as.location(url))
# from json (jsonlite class)
x <- jsonlite::minify({ "type": "FeatureCollection" })
class(x)
lint(x)
# From SpatialPoints class
library("sp")
a <- c(1,2,3,4,5)
b <- c(3,2,5,1,4)
(x <- SpatialPoints(cbind(a,b)))
class(x)
lint(x)
# From a data.frame
## need to specify what columns are lat and long with a data.frame
lint(us_cities[1:2,], lat=lat, lon=long)
# From numeric
vec <- c(32.45,-99.74)
lint(vec)
# From a list
mylist <- list(list(latitude=30, longitude=120, marker="red"),
list(latitude=30, longitude=130, marker="blue"))
lint(mylist)
## End(Not run)
map_gist
Publish an interactive map as a GitHub gist
Description
There are two ways to authorize to work with your GitHub account:
• PAT - Generate a personal access token (PAT) athttps://help.github.com/articles/
creating-an-access-token-for-command-line-useandrecorditintheGITHUB_PAT
envar in your .Renviron file.
• Interactive - Interactively login into your GitHub account and authorise with OAuth.
51
26
map_gist
Using the PAT method is recommended.
Using the gist_auth() function you can authenticate seperately first, or if you’re not authenticated,
this functionwill run internallywith eachfunctionncall. If youhave a PAT, that willbe used, if not,
OAuth will be used.
Usage
map_gist(input, lat = "lat", lon = "long", geometry = "point",
group = NULL, type = "FeatureCollection", file = "myfile.geojson",
description = "", public = TRUE, browse = TRUE, ...)
Arguments
input
Input object
lat
Name of latitude variable
lon
Name of longitude variable
geometry
(character) Are polygons in the object
group
(character) A grouping variable to perform grouping for polygons - doesn’t ap-
ply for points
type
(character) One of FeatureCollection or GeometryCollection
file
File name to use to put up as the gist file
description
Description for the Github gist, or leave to default (=no description)
public
(logical) Want gist to be public or not? Default: TRUE
browse
If TRUE (default) thef map opens in your default browser.
...
Further arguments passed on toPOST
Examples
## Not run:
# From file
file <- "myfile.geojson"
geojson_write(us_cities[1:20, ], lat=lat, lon=long, file = file)
map_gist(file=as.location(file))
# From SpatialPoints class
library("sp")
x <- c(1,2,3,4,5)
y <- c(3,2,5,1,4)
s <- SpatialPoints(cbind(x,y))
map_gist(s)
# from SpatialPointsDataFrame class
x <- c(1,2,3,4,5)
y <- c(3,2,5,1,4)
s <- SpatialPointsDataFrame(cbind(x,y), mtcars[1:5,])
map_gist(s)
47
map_gist
27
# from SpatialPolygons class
poly1 <- Polygons(list(Polygon(cbind(c(-100,-90,-85,-100),
c(40,50,45,40)))), "1")
poly2 <- Polygons(list(Polygon(cbind(c(-90,-80,-75,-90),
c(30,40,35,30)))), "2")
sp_poly <- SpatialPolygons(list(poly1, poly2), 1:2)
map_gist(sp_poly)
# From SpatialPolygonsDataFrame class
sp_polydf <- as(sp_poly, "SpatialPolygonsDataFrame")
map_gist(sp_poly)
# From SpatialLines class
c1 <- cbind(c(1,2,3), c(3,2,2))
c2 <- cbind(c1[,1]+.05,c1[,2]+.05)
c3 <- cbind(c(1,2,3),c(1,1.5,1))
L1 <- Line(c1)
L2 <- Line(c2)
L3 <- Line(c3)
Ls1 <- Lines(list(L1), ID = "a")
Ls2 <- Lines(list(L2, L3), ID = "b")
sl1 <- SpatialLines(list(Ls1))
sl12 <- SpatialLines(list(Ls1, Ls2))
map_gist(sl1)
# From SpatialLinesDataFrame class
dat <- data.frame(X = c("Blue", "Green"),
Y = c("Train", "Plane"),
Z = c("Road", "River"), row.names = c("a", "b"))
sldf <- SpatialLinesDataFrame(sl12, dat)
map_gist(sldf)
# From SpatialGrid
x <- GridTopology(c(0,0), c(1,1), c(5,5))
y <- SpatialGrid(x)
map_gist(y)
# From SpatialGridDataFrame
sgdim <- c(3,4)
sg <- SpatialGrid(GridTopology(rep(0,2), rep(10,2), sgdim))
sgdf <- SpatialGridDataFrame(sg, data.frame(val = 1:12))
map_gist(sgdf)
# from data.frame
## to points
map_gist(us_cities)
## to polygons
head(states)
map_gist(states[1:351, ], lat=lat, lon=long, geometry="polygon", group=group)
## From a list
mylist <- list(list(lat=30, long=120, marker="red"),
43
28
map_gist
list(lat=30, long=130, marker="blue"))
map_gist(mylist, lat="lat", lon="long")
# From a numeric vector
## of length 2 to a point
vec <- c(-99.74,32.45)
map_gist(vec)
## this requires numeric class input, so inputting a list will dispatch on the list method
poly <- c(c(-114.345703125,39.436192999314095),
c(-114.345703125,43.45291889355468),
c(-106.61132812499999,43.45291889355468),
c(-106.61132812499999,39.436192999314095),
c(-114.345703125,39.436192999314095))
map_gist(poly, geometry = "polygon")
# From a json object
(x <- geojson_json(c(-99.74,32.45)))
map_gist(x)
## another example
map_gist(geojson_json(us_cities[1:10,], lat=lat, lon=long))
# From a geo_list object
(res <- geojson_list(us_cities[1:2,], lat=lat, lon=long))
map_gist(res)
# From SpatialPixels
pixels <- suppressWarnings(SpatialPixels(SpatialPoints(us_cities[c("long", "lat")])))
summary(pixels)
map_gist(pixels)
# From SpatialPixelsDataFrame
pixelsdf <- suppressWarnings(
SpatialPixelsDataFrame(points = canada_cities[c("long", "lat")], data = canada_cities)
)
map_gist(pixelsdf)
# From SpatialRings
library("rgeos")
r1 <- Ring(cbind(x=c(1,1,2,2,1), y=c(1,2,2,1,1)), ID="1")
r2 <- Ring(cbind(x=c(1,1,2,2,1), y=c(1,2,2,1,1)), ID="2")
r1r2 <- SpatialRings(list(r1, r2))
map_gist(r1r2)
# From SpatialRingsDataFrame
dat <- data.frame(id = c(1,2), value = 3:4)
r1r2df <- SpatialRingsDataFrame(r1r2, data = dat)
map_gist(r1r2df)
## End(Not run)
43
map_leaf
29
map_leaf
Make an interactive map locally
Description
Make an interactive map locally
Usage
map_leaf(input, lat = NULL, lon = NULL, basemap = "Stamen.Toner", ...)
Arguments
input
Input object
lat
Name of latitude variable
lon
Name of longitude variable
basemap
Basemap to use. SeeaddProviderTiles. Default: Stamen.Toner
...
Further arguments passed on toaddPolygons,addMarkers, addGeoJSON, or
addPolylines
Examples
## Not run:
# Well need leaflet below
library("leaflet")
# From file
file <- "myfile.geojson"
geojson_write(us_cities[1:20, ], lat=lat, lon=long, file = file)
map_leaf(as.location(file))
# From SpatialPoints class
library("sp")
x <- c(1,2,3,4,20)
y <- c(3,2,5,3,4)
s <- SpatialPoints(cbind(x,y))
map_leaf(s)
# from SpatialPointsDataFrame class
x <- c(1,2,3,4,5)
y <- c(3,2,5,1,4)
s <- SpatialPointsDataFrame(cbind(x,y), mtcars[1:5,])
map_leaf(s)
# from SpatialPolygons class
poly1 <- Polygons(list(Polygon(cbind(c(-100,-90,-85,-100),
c(40,50,45,40)))), "1")
poly2 <- Polygons(list(Polygon(cbind(c(-90,-80,-75,-90),
46
30
map_leaf
c(30,40,35,30)))), "2")
sp_poly <- SpatialPolygons(list(poly1, poly2), 1:2)
map_leaf(sp_poly)
# From SpatialPolygonsDataFrame class
sp_polydf <- as(sp_poly, "SpatialPolygonsDataFrame")
map_leaf(sp_poly)
# From SpatialLines class
c1 <- cbind(c(1,2,3), c(3,2,2))
c2 <- cbind(c1[,1]+.05,c1[,2]+.05)
c3 <- cbind(c(1,2,3),c(1,1.5,1))
L1 <- Line(c1)
L2 <- Line(c2)
L3 <- Line(c3)
Ls1 <- Lines(list(L1), ID = "a")
Ls2 <- Lines(list(L2, L3), ID = "b")
sl1 <- SpatialLines(list(Ls1))
sl12 <- SpatialLines(list(Ls1, Ls2))
map_leaf(sl1)
map_leaf(sl12)
# From SpatialLinesDataFrame class
dat <- data.frame(X = c("Blue", "Green"),
Y = c("Train", "Plane"),
Z = c("Road", "River"), row.names = c("a", "b"))
sldf <- SpatialLinesDataFrame(sl12, dat)
map_leaf(sldf)
# From SpatialGrid
x <- GridTopology(c(0,0), c(1,1), c(5,5))
y <- SpatialGrid(x)
map_leaf(y)
# From SpatialGridDataFrame
sgdim <- c(3,4)
sg <- SpatialGrid(GridTopology(rep(0,2), rep(10,2), sgdim))
sgdf <- SpatialGridDataFrame(sg, data.frame(val = 1:12))
map_leaf(sgdf)
# from data.frame
map_leaf(us_cities)
## another example
head(states)
map_leaf(states[1:351, ])
## From a named list
mylist <- list(list(lat=30, long=120, marker="red"),
list(lat=30, long=130, marker="blue"))
map_leaf(mylist, lat="lat", lon="long")
## From an unnamed list
Documents you may be interested
Documents you may be interested