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.
This package is adapted from Javascript’s https://github.com/tmcw/geojson-random
Stable randgeo
version from CRAN
install.packages("randgeo")
Or, the development version from Github
devtools::install_github("ropensci/randgeo")
library("randgeo")
rg_position()
#> [[1]]
#> [1] -74.06390 -87.36485
Many positions
rg_position(10)
#> [[1]]
#> [1] 77.17355 -32.30020
#>
#> [[2]]
#> [1] 84.05821 57.93236
#>
#> [[3]]
#> [1] 42.14033 66.52106
#>
#> [[4]]
#> [1] 157.014833 6.449384
#>
#> [[5]]
#> [1] 161.42861 50.54066
#>
#> [[6]]
#> [1] -61.57501 62.07831
#>
#> [[7]]
#> [1] -166.76059 26.83366
#>
#> [[8]]
#> [1] -62.17688 38.43604
#>
#> [[9]]
#> [1] 56.16074 75.77106
#>
#> [[10]]
#> [1] 5.766758 -64.996998
Random position within a bounding box
rg_position(bbox = c(50, 50, 60, 60))
#> [[1]]
#> [1] 52.93500 58.86946
A single point
wkt_point()
#> [1] "POINT (-9.9000676 -68.5268778)"
Many points
wkt_point(count = 10)
#> [1] "POINT (114.2834514 -11.5755934)" "POINT (-125.2285062 -34.1879608)"
#> [3] "POINT (132.2388468 58.4822677)" "POINT (153.0674642 -53.2880208)"
#> [5] "POINT (-176.9335501 -56.1095363)" "POINT (-40.9432452 47.4262187)"
#> [7] "POINT (56.0227752 -47.6665055)" "POINT (-112.3389297 -75.1399540)"
#> [9] "POINT (-90.7793142 -35.3431232)" "POINT (-84.6566495 16.7822561)"
Within a bounding box
wkt_point(bbox = c(50, 50, 60, 60))
#> [1] "POINT (50.0554653 59.2302343)"
The fmt
parameter controls how many decimal points
wkt_point()
#> [1] "POINT (108.0644611 -18.2481140)"
wkt_point(fmt = 10)
#> [1] "POINT (-44.0865632426 -27.2874641838)"
wkt_polygon()
#> [1] "POLYGON ((119.9306491 86.3414919, 118.4973035 81.0835588, 119.1653785 79.9118630, 115.9788006 78.1580547, 121.2360846 78.7913316, 118.1706751 74.8128016, 110.0806750 72.4576639, 106.7217717 75.4919190, 108.6344858 79.0059077, 115.4405399 83.7939850, 119.9306491 86.3414919))"
Adjust number of vertices (Default: 10)
wkt_polygon(num_vertices = 4)
#> [1] "POLYGON ((113.9276454 19.9578275, 112.9714505 16.3541293, 109.9622229 15.4431155, 109.5994788 23.9286316, 113.9276454 19.9578275))"
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 ((-103.1243803 -21.1479401, -101.8352672 -24.6234432, -101.1910575 -26.0143264, -102.8795510 -26.3067287, -103.2415268 -29.5199554, -103.7621622 -30.4258566, -105.2739466 -28.3091590, -106.1991860 -26.2351665, -105.2660700 -23.6535208, -103.2763652 -24.7282687, -103.1243803 -21.1479401))"
Within a bounding box
wkt_polygon(bbox = c(-130, 50, -120, 60))
#> [1] "POLYGON ((-124.0775778 67.1682973, -118.8926645 64.9398761, -119.0513583 60.0691618, -119.5001228 54.7974205, -125.4916129 55.5714253, -126.0146202 51.5173717, -132.1799319 52.9233847, -127.0354244 58.3734682, -126.7424145 59.0712072, -125.8387269 67.6603622, -124.0775778 67.1682973))"
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] 90.02780 -44.10505
#>
#>
#> $features[[1]]$properties
#> 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] 57.91960 50.34914
#>
#>
#> $features[[1]]$properties
#> list()
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] -135.965962 9.452831
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -134.297839 9.345981
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -129.763256 6.372181
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -133.22060 2.34061
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -137.140008 5.845483
#>
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -138.085007 6.414444
#>
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -142.474135 7.755437
#>
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -143.64275 13.21087
#>
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -137.39094 11.35879
#>
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -136.838210 9.180942
#>
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -135.965962 9.452831
#>
#>
#>
#>
#> $features[[1]]$properties
#> 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] 47.93140 20.40781
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] 40.41455 17.81822
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] 38.00157 23.45877
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] 43.30502 25.42211
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] 47.93140 20.40781
#>
#>
#>
#>
#> $features[[1]]$properties
#> 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] -109.52953 84.29132
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -110.40255 82.76538
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -107.5941 79.9520
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -111.9264 78.2094
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -112.10173 80.76133
#>
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -114.58710 79.54456
#>
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -111.58318 82.89267
#>
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -113.71507 85.19098
#>
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -113.56492 86.56351
#>
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -111.55730 87.22447
#>
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -109.52953 84.29132
#>
#>
#>
#>
#> $features[[1]]$properties
#> 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] -123.1596 56.1835
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -122.41820 54.07676
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -125.21531 49.76009
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -126.66604 48.29315
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -129.94013 47.13407
#>
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -130.27722 50.33908
#>
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -136.19700 48.52439
#>
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -136.04867 52.12332
#>
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -130.0454 52.4305
#>
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -129.13771 54.28362
#>
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -123.1596 56.1835
#>
#>
#>
#>
#> $features[[1]]$properties
#> list()