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] 69.83303 -7.17743

Many positions

rg_position(10)
#> [[1]]
#> [1] -39.27414  49.81889
#> 
#> [[2]]
#> [1] 109.5555  31.3705
#> 
#> [[3]]
#> [1] 111.28564  27.02205
#> 
#> [[4]]
#> [1] 111.037211  -0.164524
#> 
#> [[5]]
#> [1] -164.97511  -80.50037
#> 
#> [[6]]
#> [1] -50.10316  53.90830
#> 
#> [[7]]
#> [1] 164.59462 -22.30489
#> 
#> [[8]]
#> [1] -61.22736 -48.03626
#> 
#> [[9]]
#> [1] -138.56446  -29.92118
#> 
#> [[10]]
#> [1]  67.41320 -47.21744

Random position within a bounding box

rg_position(bbox = c(50, 50, 60, 60))
#> [[1]]
#> [1] 54.83129 58.83257

Well-known text

random points

A single point

wkt_point()
#> [1] "POINT (167.9243990 -28.4317717)"

Many points

wkt_point(count = 10)
#>  [1] "POINT (128.2529857 -21.8233791)"  "POINT (-91.8453129 40.4102317)"  
#>  [3] "POINT (-178.0342005 4.6794248)"   "POINT (-157.0651483 31.6231509)" 
#>  [5] "POINT (-56.0522317 64.9699008)"   "POINT (7.0732058 -12.4455125)"   
#>  [7] "POINT (-85.3241323 -44.0961132)"  "POINT (-157.6575750 -37.0987830)"
#>  [9] "POINT (-122.7231469 22.3803640)"  "POINT (119.9307564 46.5370053)"

Within a bounding box

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

The fmt parameter controls how many decimal points

wkt_point()
#> [1] "POINT (-49.9782371 -61.5490198)"
wkt_point(fmt = 10)
#> [1] "POINT (-53.6068043020 -82.7953180285)"

random polygons

wkt_polygon()
#> [1] "POLYGON ((41.8334717 8.3837178, 37.5344958 4.0845876, 39.1649227 0.2496668, 34.1492506 -2.1066298, 36.2351888 2.6254269, 36.3914547 2.9687380, 29.9128763 -0.3563479, 36.4717574 4.6701928, 35.8994675 10.2931558, 36.8802331 6.4771268, 41.8334717 8.3837178))"

Adjust number of vertices (Default: 10)

wkt_polygon(num_vertices = 4)
#> [1] "POLYGON ((166.2624787 -39.5691237, 166.5979661 -43.1435874, 173.4184148 -48.6970981, 152.6013733 -45.4186823, 166.2624787 -39.5691237))"

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 ((108.6909788 10.7106637, 111.2203199 8.9608450, 111.3178471 7.2737954, 110.1756388 3.3992956, 107.1236632 4.0751006, 107.6347087 5.2253757, 106.4377740 3.8116082, 105.8762172 7.0627184, 108.2144629 7.1010360, 108.3804659 10.8916493, 108.6909788 10.7106637))"

Within a bounding box

wkt_polygon(bbox = c(-130, 50, -120, 60))
#> [1] "POLYGON ((-114.6485009 48.9547909, -116.2771189 48.9252603, -114.8584246 46.3404153, -117.6175254 46.0832031, -121.6647403 41.4715168, -131.5868211 50.1168350, -132.3644103 50.3783427, -126.0377228 53.5820215, -122.9928800 51.5979424, -122.3550949 51.2407582, -114.6485009 48.9547909))"

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] 69.91385 44.46649
#> 
#> 
#> $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] 52.77944 53.53346
#> 
#> 
#> $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] 26.77023 27.04441
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] 33.28784 30.03010
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] 30.54568 21.14169
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] 32.36306 18.85237
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] 27.89786 22.08887
#> 
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] 21.52363 16.82681
#> 
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] 19.64342 18.47453
#> 
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] 16.70661 20.50020
#> 
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] 15.82406 25.82438
#> 
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] 25.73102 26.54356
#> 
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] 26.77023 27.04441
#> 
#> 
#> 
#> 
#> $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] -96.01308 -64.77333
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -110.27487  -63.64629
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -109.51818  -61.40999
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -107.20966  -57.89684
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -96.01308 -64.77333
#> 
#> 
#> 
#> 
#> $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] 81.44271 36.62889
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] 82.17415 35.52575
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] 81.20805 34.55776
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] 81.72939 33.40979
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] 82.32494 32.66834
#> 
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] 83.06729 32.20607
#> 
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] 80.91382 31.03555
#> 
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] 77.83647 31.87513
#> 
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] 76.44146 33.47605
#> 
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] 78.01137 33.96739
#> 
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] 81.44271 36.62889
#> 
#> 
#> 
#> 
#> $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] -120.50873   67.92302
#> 
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -118.32356   59.44919
#> 
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -112.70950   56.29794
#> 
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -120.12160   48.30491
#> 
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -122.09120   53.04732
#> 
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -125.82407   48.86781
#> 
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -132.45633   52.25394
#> 
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -127.8069   56.0352
#> 
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -128.03246   57.73999
#> 
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -121.9308   58.9749
#> 
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -120.50873   67.92302
#> 
#> 
#> 
#> 
#> $features[[1]]$properties
#> NULL
#> 
#> 
#> 
#> attr(,"class")
#> [1] "geo_list"