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.
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] 16.26309 -20.49169
Many positions
rg_position(10)
#> [[1]]
#> [1] -64.20071 29.56893
#>
#> [[2]]
#> [1] 119.21557 75.74335
#>
#> [[3]]
#> [1] -134.02831 -44.87294
#>
#> [[4]]
#> [1] 31.581536 -6.733494
#>
#> [[5]]
#> [1] -147.73676 28.70597
#>
#> [[6]]
#> [1] 149.09694 -36.09327
#>
#> [[7]]
#> [1] 47.61316 -29.87324
#>
#> [[8]]
#> [1] 163.80079 -2.00392
#>
#> [[9]]
#> [1] -93.72101 -23.86253
#>
#> [[10]]
#> [1] 44.843323 -4.570225
Random position within a bounding box
rg_position(bbox = c(50, 50, 60, 60))
#> [[1]]
#> [1] 58.63560 57.12427
A single point
wkt_point()
#> [1] "POINT (11.3218117 -53.8703366)"
Many points
wkt_point(count = 10)
#> [1] "POINT (-165.7128765 5.0229956)" "POINT (-63.3769480 28.9736430)"
#> [3] "POINT (125.7658089 -44.9764785)" "POINT (-35.7280351 21.2939754)"
#> [5] "POINT (-36.1726092 17.9106945)" "POINT (55.6272414 -44.5082799)"
#> [7] "POINT (-7.6419210 27.6591188)" "POINT (144.5196636 56.3800052)"
#> [9] "POINT (-160.8216522 -25.6168422)" "POINT (93.8309566 -16.1211283)"
Within a bounding box
wkt_point(bbox = c(50, 50, 60, 60))
#> [1] "POINT (54.8443825 50.3958323)"
The fmt
parameter controls how many decimal points
wkt_point()
#> [1] "POINT (102.3587774 -31.9259790)"
wkt_point(fmt = 10)
#> [1] "POINT (103.5536607262 17.4024823365)"
wkt_polygon()
#> [1] "POLYGON ((63.2823490 -25.7054741, 63.4523567 -25.9684390, 62.9670919 -30.9163767, 63.2149274 -34.2831224, 64.4845864 -37.7595139, 63.0758891 -40.2550889, 60.5546734 -40.4072621, 56.9718570 -35.9447850, 58.7914705 -30.6887794, 59.4597379 -30.0789652, 63.2823490 -25.7054741))"
Adjust number of vertices (Default: 10)
wkt_polygon(num_vertices = 4)
#> [1] "POLYGON ((-174.6711900 14.5249710, -176.1393651 14.0267011, -175.1461312 7.7867574, 176.2172323 15.0829967, -174.6711900 14.5249710))"
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 ((42.7927245 -6.0258349, 44.2893579 -6.7569194, 45.2393970 -10.1062241, 43.4535923 -10.9996621, 41.8065480 -13.0507695, 40.6697369 -11.2285134, 38.4966280 -10.7027852, 40.4465983 -7.8102983, 40.0443685 -5.2048567, 41.0392213 -7.0177526, 42.7927245 -6.0258349))"
Within a bounding box
wkt_polygon(bbox = c(-130, 50, -120, 60))
#> [1] "POLYGON ((-118.4996387 64.3059280, -122.7692701 61.3746591, -127.9127762 59.5346132, -124.9864635 54.0431376, -132.2414212 50.5377687, -134.7031653 56.6733491, -132.0285028 58.0821148, -133.1705754 59.6575353, -131.6322056 67.6619979, -129.0756604 63.7996862, -118.4996387 64.3059280))"
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] 145.92325 -44.52934
#>
#>
#> $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] 58.54885 55.97950
#>
#>
#> $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] -81.21896 31.27434
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -82.24629 27.19063
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -79.93488 28.16909
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -78.13081 25.67931
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -78.68357 22.56137
#>
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -85.63125 19.04148
#>
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -84.03943 26.23307
#>
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -87.04261 28.28492
#>
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -90.44392 30.94729
#>
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -88.96495 31.69639
#>
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -81.21896 31.27434
#>
#>
#>
#>
#> $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] 55.86574 -36.49930
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] 61.81770 -35.90057
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] 42.34878 -37.74771
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] 41.36271 -36.50848
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] 55.86574 -36.49930
#>
#>
#>
#>
#> $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] -78.445668 4.008648
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -79.963716 2.755261
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -81.290842 1.174407
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -79.378058 -2.275203
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -80.628321 -2.218369
#>
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -82.0410023 0.3730364
#>
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -81.760879 1.650902
#>
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -84.440044 3.673493
#>
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -83.911668 4.838071
#>
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -82.628292 3.999905
#>
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -78.445668 4.008648
#>
#>
#>
#>
#> $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] -115.54969 64.21415
#>
#> $features[[1]]$geometry$coordinates[[1]][[2]]
#> [1] -119.6129 60.5224
#>
#> $features[[1]]$geometry$coordinates[[1]][[3]]
#> [1] -121.92215 59.23754
#>
#> $features[[1]]$geometry$coordinates[[1]][[4]]
#> [1] -122.23710 58.92262
#>
#> $features[[1]]$geometry$coordinates[[1]][[5]]
#> [1] -127.26100 53.11282
#>
#> $features[[1]]$geometry$coordinates[[1]][[6]]
#> [1] -128.5130 51.7688
#>
#> $features[[1]]$geometry$coordinates[[1]][[7]]
#> [1] -131.19075 55.08492
#>
#> $features[[1]]$geometry$coordinates[[1]][[8]]
#> [1] -134.98627 58.65348
#>
#> $features[[1]]$geometry$coordinates[[1]][[9]]
#> [1] -132.83032 59.29787
#>
#> $features[[1]]$geometry$coordinates[[1]][[10]]
#> [1] -130.36971 59.39261
#>
#> $features[[1]]$geometry$coordinates[[1]][[11]]
#> [1] -115.54969 64.21415
#>
#>
#>
#>
#> $features[[1]]$properties
#> list()