randgeo is a no dependency R package for generating random lat/long positions, or random WKT or GeoJSON points or polygons.

The benefit of no dependencies is that it can easily be used in other packages without any pain. e.g., you may want to show examples in your package but you don’t want a heavy dependency just for examples.

This package is adapted from Javascript’s https://github.com/tmcw/geojson-random but with modifications.

Install

Stable randgeo version from CRAN

install.packages("randgeo")

Or, the development version from Github

devtools::install_github("ropensci/randgeo")
library("randgeo")

random position

rg_position()
#> [[1]]
#> [1] -40.70262  52.02680

Many positions

rg_position(10)
#> [[1]]
#> [1] 71.30631 -1.48772
#> 
#> [[2]]
#> [1] 77.2828672 -0.9230931
#> 
#> [[3]]
#> [1] -61.23915  46.18009
#> 
#> [[4]]
#> [1] 56.88035 52.09329
#> 
#> [[5]]
#> [1] -9.154793 21.452871
#> 
#> [[6]]
#> [1] 155.43424  21.16008
#> 
#> [[7]]
#> [1] -145.1187  -13.1029
#> 
#> [[8]]
#> [1] 42.81181 63.70341
#> 
#> [[9]]
#> [1] -92.39288 -19.47951
#> 
#> [[10]]
#> [1] -124.92816   57.03624

Random position within a bounding box

rg_position(bbox = c(50, 50, 60, 60))
#> [[1]]
#> [1] 50.93872 55.73010

Well-known text

random points

A single point

wkt_point()
#> [1] "POINT (-172.0973864 -18.9789647)"

Many points

wkt_point(count = 10)
#>  [1] "POINT (68.3575330 -52.0463678)"   "POINT (-101.4904908 35.5327984)" 
#>  [3] "POINT (-128.1938096 19.4778910)"  "POINT (166.8660912 19.1888260)"  
#>  [5] "POINT (-92.6154178 -63.6690229)"  "POINT (-154.1171215 -14.7369225)"
#>  [7] "POINT (119.2705964 61.3824964)"   "POINT (46.4577135 -9.6446657)"   
#>  [9] "POINT (-132.7021326 69.8287100)"  "POINT (-176.4408439 -44.2192469)"

Within a bounding box

wkt_point(bbox = c(50, 50, 60, 60))
#> [1] "POINT (50.9353474 50.1131234)"

The fmt parameter controls how many decimal points

wkt_point()
#> [1] "POINT (-30.5666724 84.9101614)"
wkt_point(fmt = 10)
#> [1] "POINT (61.7023302801 -46.7607209102)"

random polygons

wkt_polygon()
#> [1] "POLYGON ((-173.2646466 53.5807431, -172.2477860 52.5737091, -170.4350013 51.1767737, -158.7469390 48.2591723, -170.0327059 45.9919673, -169.8757636 41.9184954, -172.0241583 44.6205635, -178.8582923 48.0325235, -176.7858367 49.9441700, -174.2324566 50.5311970, -173.2646466 53.5807431))"

Adjust number of vertices (Default: 10)

wkt_polygon(num_vertices = 4)
#> [1] "POLYGON ((-120.2021274 -75.5859346, -123.9802663 -74.4319645, -120.6919555 -74.6574658, -122.2599103 -73.2977774, -120.2021274 -75.5859346))"

Adjust maximum number of decimal degrees latitude or longitude that a vertex can reach out of the center of the Polygon (Default: 10)

wkt_polygon(max_radial_length = 5)
#> [1] "POLYGON ((34.9829227 -13.4961814, 33.7758086 -14.9575394, 36.8873192 -15.4329617, 33.2624577 -16.9449096, 32.9200256 -17.9856370, 32.4713769 -18.6240574, 31.6452227 -18.5082652, 28.4057347 -16.0461613, 32.3549206 -15.1883193, 29.7207089 -14.7091355, 34.9829227 -13.4961814))"

Within a bounding box

wkt_polygon(bbox = c(-130, 50, -120, 60))
#> [1] "POLYGON ((-124.4153993 66.2653073, -127.4301376 61.4991157, -129.0315565 58.6905524, -113.7631123 60.2905106, -118.5106773 59.0787522, -123.7043677 57.8005336, -122.9734939 56.6502253, -130.4990860 57.7387297, -140.4546794 52.6279614, -138.0231695 55.0068130, -124.4153993 66.2653073))"

GeoJSON

random points

A single point

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] -178.99096   67.24202
#> 
#> 
#> $features[[1]]$properties
#> NULL
#> 
#> 
#> 
#> attr(,"class")
#> [1] "geo_list"

Many points

geo_point(count = 10)
#> $type
#> [1] "FeatureCollection"
#> 
#> $features
#> $features[[1]]
#> $features[[1]]$type
#> [1] "Feature"
#> 
#> $features[[1]]$geometry
#> $features[[1]]$geometry$type
...

Within a bounding box

geo_point(bbox = c(50, 50, 60, 60))
#> $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] 56.29726 54.79440
#> 
#> 
#> $features[[1]]$properties
#> NULL
#> 
#> 
#> 
#> attr(,"class")
#> [1] "geo_list"

random polygons

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] 25.08105 12.69329
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] 27.945448  5.973037
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] 25.285523  5.109598
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] 22.1086870  0.9204744
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] 21.19194 -3.30745
#> 
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] 20.071585  2.969379
#> 
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] 16.4737867  0.6687876
#> 
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] 13.58250  4.29731
#> 
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] 14.860300  5.127263
#> 
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] 20.311110  8.063901
#> 
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] 25.08105 12.69329
#> 
#> 
#> 
#> 
#> $features[[1]]$properties
#> NULL
#> 
#> 
#> 
#> attr(,"class")
#> [1] "geo_list"

Adjust number of vertices (Default: 10)

geo_polygon(num_vertices = 4)
#> $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] -90.15366  27.88571
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -92.28119  19.23274
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -94.07616  18.66629
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -99.83160  17.35976
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -90.15366  27.88571
#> 
#> 
#> 
#> 
#> $features[[1]]$properties
#> NULL
#> 
#> 
#> 
#> attr(,"class")
#> [1] "geo_list"

Adjust maximum number of decimal degrees latitude or longitude that a vertex can reach out of the center of the Polygon (Default: 10)

geo_polygon(max_radial_length = 5)
#> $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] 29.01177 66.35855
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] 27.23866 64.62797
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] 26.01197 64.55600
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] 24.84331 64.50155
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] 26.02203 61.24502
#> 
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] 22.70766 61.95952
#> 
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] 19.89515 61.01474
#> 
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] 16.71801 61.77957
#> 
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] 15.75019 63.79897
#> 
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] 23.66560 66.34743
#> 
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] 29.01177 66.35855
#> 
#> 
#> 
#> 
#> $features[[1]]$properties
#> NULL
#> 
#> 
#> 
#> attr(,"class")
#> [1] "geo_list"

Within a bounding box

geo_polygon(bbox = c(-130, 50, -120, 60))
#> $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] -114.82966   54.01116
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -121.59762   51.97227
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -112.3845   51.4866
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -133.60109   44.64348
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -129.93111   48.32581
#> 
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -134.02385   47.74202
#> 
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -136.78001   47.73452
#> 
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -137.12550   49.62802
#> 
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -142.88829   49.29128
#> 
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -131.61421   55.82815
#> 
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -114.82966   54.01116
#> 
#> 
#> 
#> 
#> $features[[1]]$properties
#> NULL
#> 
#> 
#> 
#> attr(,"class")
#> [1] "geo_list"