geo2topo: GeoJSON to TopoJSON and back

Description Usage Arguments Value See Also Examples

View source: R/geo_topo.R

Description

GeoJSON to TopoJSON and back

Usage

1
2
3
geo2topo(x, object_name = "foo", quantization = 0, ...)

topo2geo(x, ...)

Arguments

x

GeoJSON or TopoJSON as a character string, json, a file path, or url

object_name

(character) name to give to the TopoJSON object created. Default: "foo"

quantization

(numeric) quantization parameter, use this to quantize geometry prior to computing topology. Typical values are powers of ten (1e4, 1e5, ...), default is 0 to not perform quantization. For more information about quantization, see this by Mike Bostock https://stackoverflow.com/questions/18900022/topojson-quantization-vs-simplification/18921214#18921214

...

for geo2topo args passed on to jsonlite::fromJSON(), and for topo2geo args passed on to sf::st_read()

Value

An object of class json, of either GeoJSON or TopoJSON

See Also

topojson_write(), topojson_read()

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
# geojson to topojson
x <- '{"type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}'
z <- geo2topo(x)
jsonlite::prettify(z)
## Not run: 
library(leaflet)
leaflet() %>%
  addProviderTiles(provider = "Stamen.Terrain") %>%
  addTopoJSON(z)

## End(Not run)

# geojson to topojson as a list
x <- list(
 '{"type": "LineString", "coordinates": [ [100, 0], [101, 1] ]}',
 '{"type": "LineString", "coordinates": [ [110, 0], [110, 1] ]}',
 '{"type": "LineString", "coordinates": [ [120, 0], [121, 1] ]}'
)
geo2topo(x)

# change the object name created
x <- '{"type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}'
geo2topo(x, object_name = "HelloWorld")
geo2topo(x, object_name = "4")

x <- list(
 '{"type": "LineString", "coordinates": [ [100, 0], [101, 1] ]}',
 '{"type": "LineString", "coordinates": [ [110, 0], [110, 1] ]}',
 '{"type": "LineString", "coordinates": [ [120, 0], [121, 1] ]}'
)
geo2topo(x, "HelloWorld")
geo2topo(x, c("A", "B", "C"))


# topojson to geojson
w <- topo2geo(z)
jsonlite::prettify(w)

## larger examples
file <- system.file("examples", "us_states.topojson", package = "geojsonio")
topo2geo(file)

geojsonio documentation built on Jan. 15, 2021, 3:33 p.m.