Spark Online Training by Edureka

geojson_json: Convert many input types with spatial data to geojson...

Description Usage Arguments Details Value Examples

Description

Convert many input types with spatial data to geojson specified as a json string

Usage

1
2
3
geojson_json(input, lat = NULL, lon = NULL, group = NULL,
  geometry = "point", type = "FeatureCollection", convert_wgs84 = FALSE,
  crs = NULL, ...)

Arguments

input

Input list, data.frame, spatial class, or sf 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.

group

(character) A grouping variable to perform grouping for polygons - doesn't apply for points

geometry

(character) One of point (Default) or polygon.

type

(character)The type of collection. One of FeatureCollection (default) or GeometryCollection.

convert_wgs84

Should the input be converted to the standard coordinate referece system defined for GeoJSON (geographic coordinate reference system, using the WGS84 datum, with longitude and latitude unitsof decimal degrees; EPSG: 4326). Default is FALSE though this may change in a future package version. This will only work for sf or Spatial objects with a CRS already defined. If one is not defined but you know what it is, you may define it in the crs argument below.

crs

The CRS of the input if it is not already defined. This can be an epsg code as a four or five digit integer or a valid proj4 string. This argument will be ignored if convert_wgs84 is FALSE or the object already has a CRS.

...

Further args passed on to toJSON

Details

This function creates a geojson structure as a json character string; it does not write a file using rgdal - see geojson_write for that.

Note that all sp class objects will output as FeatureCollection objects, while other classes (numeric, list, data.frame) can be output as FeatureCollection or GeometryCollection objects. We're working on allowing GeometryCollection option for sp class objects.

Also note that with sp classes we do make a round-trip, using 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:

  • sfg: the approprite geometry Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection

  • sfc: GeometryCollection, unless the sfc is length 1, then the geometry as above

  • sf: FeatureCollection

Value

An object of class geo_json (and json)

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
## Not run: 
# From a numeric vector of length 2, making a point type
geojson_json(c(-99.74,32.45), pretty=TRUE)
geojson_json(c(-99.74,32.45), type = "GeometryCollection", pretty=TRUE)

## polygon type
### 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))
geojson_json(poly, geometry = "polygon", pretty=TRUE)

# Lists
## From a list of numeric vectors to a polygon
vecs <- list(c(100.0,0.0), c(101.0,0.0), c(101.0,1.0), c(100.0,1.0), c(100.0,0.0))
geojson_json(vecs, geometry="polygon", pretty=TRUE)

## from a named list
mylist <- list(list(latitude=30, longitude=120, marker="red"),
               list(latitude=30, longitude=130, marker="blue"))
geojson_json(mylist, lat='latitude', lon='longitude')

# From a data.frame to points
geojson_json(us_cities[1:2,], lat='lat', lon='long', pretty=TRUE)
geojson_json(us_cities[1:2,], lat='lat', lon='long',
   type="GeometryCollection", pretty=TRUE)

# from data.frame to polygons
head(states)
## make list for input to e.g., rMaps
geojson_json(states[1:351, ], lat='lat', lon='long', geometry="polygon", group='group')

# from a geo_list
a <- geojson_list(us_cities[1:2,], lat='lat', lon='long')
geojson_json(a)

# sp classes

## 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_json(sp_poly)
geojson_json(sp_poly, pretty=TRUE)

## Another SpatialPolygons
library("sp")
library("rgeos")
pt <- SpatialPoints(coordinates(list(x = 0, y = 0)), CRS("+proj=longlat +datum=WGS84"))
## transfrom to web mercator becuase geos needs project coords
crs <- gsub("\n", "", paste0("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0
   +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs", collapse = ""))
pt <- spTransform(pt, CRS(crs))
## buffer
pt <- gBuffer(pt, width = 100)
pt <- spTransform(pt, CRS("+proj=longlat +datum=WGS84"))
geojson_json(pt)

## data.frame to geojson
geojson_write(us_cities[1:2,], lat='lat', lon='long') %>% as.json

# From SpatialPoints class
x <- c(1,2,3,4,5)
y <- c(3,2,5,1,4)
s <- SpatialPoints(cbind(x,y))
geojson_json(s)

## From SpatialPointsDataFrame class
s <- SpatialPointsDataFrame(cbind(x,y), mtcars[1:5,])
geojson_json(s)

## From SpatialLines class
library("sp")
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))
geojson_json(sl1)
geojson_json(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)
geojson_json(sldf)
geojson_json(sldf, pretty=TRUE)

## From SpatialGrid
x <- GridTopology(c(0,0), c(1,1), c(5,5))
y <- SpatialGrid(x)
geojson_json(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_json(sgdf)

# 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))
geojson_json(r1r2)

# From SpatialRingsDataFrame
dat <- data.frame(id = c(1,2), value = 3:4)
r1r2df <- SpatialRingsDataFrame(r1r2, data = dat)
geojson_json(r1r2df)

# From SpatialPixels
library("sp")
pixels <- suppressWarnings(SpatialPixels(SpatialPoints(us_cities[c("long", "lat")])))
summary(pixels)
geojson_json(pixels)

# From SpatialPixelsDataFrame
library("sp")
pixelsdf <- suppressWarnings(
 SpatialPixelsDataFrame(points = canada_cities[c("long", "lat")], data = canada_cities)
)
geojson_json(pixelsdf)

# From SpatialCollections
library("sp")
library("rgeos")
pts <- SpatialPoints(cbind(c(1,2,3,4,5), c(3,2,5,1,4)))
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)
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

## End(Not run)


Search within the geojsonio package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.