# Random GeoJSON and WKT with randgeo

**rOpenSci Blog**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**randgeo** generates random points and shapes in GeoJSON and WKT formats for
use in examples, teaching, or statistical applications.

Points and shapes are generated in the long/lat coordinate system and with appropriate spherical geometry; random points are distributed evenly across the globe, and random shapes are sized according to a maximum great-circle distance from the center of the shape.

**randgeo** was adapted from https://github.com/tmcw/geojson-random to have
a pure R implementation without any dependencies as well as appropriate
geometry. Data generated by **randgeo** may be processed or displayed of with
packages such as **sf**,
**wicket**,
**geojson**,
**wellknown**,
**geojsonio**, or
**lawn**.

Package API:

`rg_position`

– random position (lon, lat)`geo_point`

– random GeoJSON point`geo_polygon`

– random GeoJSON polygon`wkt_point`

– random WKT point`wkt_polygon`

– random WKT polygon

## setup

Install `randgeo`

– and we'll need a few other packages for examples below.

install.packages("randgeo") install.packages(c('leaflet', 'lawn')) library(randgeo)

## GeoJSON

Functions that start with `geo`

are for creating GeoJSON data in JSON format.
If you want to create an R list or data.frame, you can use `jsonlite::fromJSON`

.

### Random point

Evenly distributed across the sphere. The `bbox`

option allows
you to limit points to within long/lat bounds.

geo_point() #> $type #> [1] "FeatureCollection" #> #> $features #> $features[[1]] #> $features[[1]]$type #> [1] "Feature" #> #> $features[[1]]$geometry #> $features[[1]]$geometry$type #> [1] "Point" #> #> $features[[1]]$geometry$coordinates #> [1] 105.95999 -46.58477 #> #> #> $features[[1]]$properties #> NULL #> #> #> #> attr(,"class") #> [1] "geo_list"

### Random polygon

Centered on a random point, with default maximum size

geo_polygon() #> $type #> [1] "FeatureCollection" #> #> $features #> $features[[1]] #> $features[[1]]$type #> [1] "Feature" #> #> $features[[1]]$geometry #> $features[[1]]$geometry$type #> [1] "Polygon" #> #> $features[[1]]$geometry$coordinates #> $features[[1]]$geometry$coordinates[[1]] #> $features[[1]]$geometry$coordinates[[1]][[1]] #> [1] -138.49434 -25.11895 #> #> $features[[1]]$geometry$coordinates[[1]][[2]] #> [1] -145.95566 -28.17623 #> #> $features[[1]]$geometry$coordinates[[1]][[3]] #> [1] -145.87817 -28.74364 #> #> $features[[1]]$geometry$coordinates[[1]][[4]] #> [1] -146.61325 -28.59748 #> #> $features[[1]]$geometry$coordinates[[1]][[5]] #> [1] -139.18167 -31.07703 #> #> $features[[1]]$geometry$coordinates[[1]][[6]] #> [1] -140.88748 -31.24708 #> #> $features[[1]]$geometry$coordinates[[1]][[7]] #> [1] -143.50402 -33.93551 #> #> $features[[1]]$geometry$coordinates[[1]][[8]] #> [1] -146.48114 -30.43185 #> #> $features[[1]]$geometry$coordinates[[1]][[9]] #> [1] -144.68315 -35.45465 #> #> $features[[1]]$geometry$coordinates[[1]][[10]] #> [1] -157.58084 -24.52897 #> #> $features[[1]]$geometry$coordinates[[1]][[11]] #> [1] -138.49434 -25.11895 #> #> #> #> #> $features[[1]]$properties #> NULL #> #> #> #> attr(,"class") #> [1] "geo_list"

Visualize your shapes with **lawn**.

lawn::view(jsonlite::toJSON(unclass(geo_polygon(count = 4)), auto_unbox = TRUE))

## WKT

Functions prefixed with `wkt`

create random Well-Known Text (WKT) data. These functions
wrap the GeoJSON versions, but then convert the data to WKT.

### Random point

wkt_point() #> [1] "POINT (179.8795330 -29.1106238)"

### Random polygon

wkt_polygon() #> [1] "POLYGON ((-60.0870329 -12.9315478, -61.5073816 -25.3204334, -62.6987366 -24.5766272, -64.1853669 -24.0497260, -67.7152546 -27.4752321, -68.4190340 -26.9510818, -67.6018452 -21.5489551, -64.3083560 -21.6772242, -63.1471630 -21.9415438, -64.1137279 -14.2398013, -60.0870329 -12.9315478))"

## Use case

Example of geospatial data manipulation, using `randgeo`

, `leaflet`

and
`lawn`

.

Steps:

- Generate random overlapping polygons
- Calculate a single polygon from overlapping polygons
- Map polygon
- Generate random locaitons (points)
- Clip locations to the polygon
- Overlay locations (more random points) on the polygon

library(randgeo) library(lawn) library(leaflet)

### generate random data

set.seed(5) polys <- randgeo::geo_polygon(count = 2, num_vertices = 4, bbox = c(-120, 40, -100, 50))

### Get intersection of polygons

polysinter <- lawn::lawn_intersect(polys$features[[1]], polys$features[[2]])

### map polygons

polysinter %>% lawn::view()

### generate random points - clip points to polygon

pts <- randgeo::geo_point(count = 500, bbox = c(-120, 40, -100, 50)) pts <- lawn::lawn_within( points = lawn_featurecollection(pts), polygons = lawn_featurecollection(polysinter) )

### Draw polygon + points on map

polysinter %>% view() %>% addGeoJSON(geojson = jsonlite::toJSON(unclass(pts)))

## Feedback

Let us know what you think! `randgeo`

doesn't have any revdep's on CRAN yet, but
is being used in one package on GitHub.

**leave a comment**for the author, please follow the link and comment on their blog:

**rOpenSci Blog**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.