<p><strong>randgeo</strong> generates random points and shapes in GeoJSON and WKT formats for
use in examples, teaching, or statistical applications.</p>
<p>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.</p>
<p><strong>randgeo</strong> was adapted from <a href="https://github.com/tmcw/geojson-random">https://github.com/tmcw/geojson-random</a> to have
a pure R implementation without any dependencies as well as appropriate
geometry. Data generated by <strong>randgeo</strong> may be processed or displayed of with
packages such as <a href="https://cran.r-project.org/package=sf"><strong>sf</strong></a>,
<a href="https://cran.r-project.org/package=wicket"><strong>wicket</strong></a>,
<a href="https://cran.r-project.org/package=geojson"><strong>geojson</strong></a>,
<a href="https://cran.r-project.org/package=wellknown"><strong>wellknown</strong></a>,
<a href="https://cran.r-project.org/package=geojsonio"><strong>geojsonio</strong></a>, or
<a href="https://cran.r-project.org/package=lawn"><strong>lawn</strong></a>.</p>
<p>Package API:</p>
<ul><li><code>rg_position</code> - random position (lon, lat)</li>
<li><code>geo_point</code> - random GeoJSON point</li>
<li><code>geo_polygon</code> - random GeoJSON polygon</li>
<li><code>wkt_point</code> - random WKT point</li>
<li><code>wkt_polygon</code> - random WKT polygon</li>
</ul><h2>setup</h2>
<p>Install <code>randgeo</code> - and we'll need a few other packages for examples below.</p>
<div><pre><code><span></span>install.packages<span>(</span><span>&quot;randgeo&quot;</span><span>)</span>
install.packages<span>(</span><span>c</span><span>(</span><span>'leaflet'</span><span>,</span> <span>'lawn'</span><span>))</span>
</code></pre></div><div><pre><code><span></span><span>library</span><span>(</span>randgeo<span>)</span>
</code></pre></div>
<h2>GeoJSON</h2>
<p>Functions that start with <code>geo</code> are for creating GeoJSON data in JSON format.
If you want to create an R list or data.frame, you can use <code>jsonlite::fromJSON</code>.</p>
<h3>Random point</h3>
<p>Evenly distributed across the sphere. The <code>bbox</code> option allows
you to limit points to within long/lat bounds.</p>
<div><pre><code><span></span>geo_point<span>()</span>
<span>#> $type</span>
<span>#> [1] &quot;FeatureCollection&quot;</span>
<span>#></span>
<span>#> $features</span>
<span>#> $features[[1]]</span>
<span>#> $features[[1]]$type</span>
<span>#> [1] &quot;Feature&quot;</span>
<span>#></span>
<span>#> $features[[1]]$geometry</span>
<span>#> $features[[1]]$geometry$type</span>
<span>#> [1] &quot;Point&quot;</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates</span>
<span>#> [1] 105.95999 -46.58477</span>
<span>#></span>
<span>#></span>
<span>#> $features[[1]]$properties</span>
<span>#> NULL</span>
<span>#></span>
<span>#></span>
<span>#></span>
<span>#> attr(,&quot;class&quot;)</span>
<span>#> [1] &quot;geo_list&quot;</span>
</code></pre></div>
<h3>Random polygon</h3>
<p>Centered on a random point, with default maximum size</p>
<div><pre><code><span></span>geo_polygon<span>()</span>
<span>#> $type</span>
<span>#> [1] &quot;FeatureCollection&quot;</span>
<span>#></span>
<span>#> $features</span>
<span>#> $features[[1]]</span>
<span>#> $features[[1]]$type</span>
<span>#> [1] &quot;Feature&quot;</span>
<span>#></span>
<span>#> $features[[1]]$geometry</span>
<span>#> $features[[1]]$geometry$type</span>
<span>#> [1] &quot;Polygon&quot;</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates</span>
<span>#> $features[[1]]$geometry$coordinates[[1]]</span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[1]]</span>
<span>#> [1] -138.49434 -25.11895</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[2]]</span>
<span>#> [1] -145.95566 -28.17623</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[3]]</span>
<span>#> [1] -145.87817 -28.74364</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[4]]</span>
<span>#> [1] -146.61325 -28.59748</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[5]]</span>
<span>#> [1] -139.18167 -31.07703</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[6]]</span>
<span>#> [1] -140.88748 -31.24708</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[7]]</span>
<span>#> [1] -143.50402 -33.93551</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[8]]</span>
<span>#> [1] -146.48114 -30.43185</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[9]]</span>
<span>#> [1] -144.68315 -35.45465</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[10]]</span>
<span>#> [1] -157.58084 -24.52897</span>
<span>#></span>
<span>#> $features[[1]]$geometry$coordinates[[1]][[11]]</span>
<span>#> [1] -138.49434 -25.11895</span>
<span>#></span>
<span>#></span>
<span>#></span>
<span>#></span>
<span>#> $features[[1]]$properties</span>
<span>#> NULL</span>
<span>#></span>
<span>#></span>
<span>#></span>
<span>#> attr(,&quot;class&quot;)</span>
<span>#> [1] &quot;geo_list&quot;</span>
</code></pre></div>
<p>Visualize your shapes with <strong>lawn</strong>.</p>
<div><pre><code><span></span>lawn<span>::</span>view<span>(</span>jsonlite<span>::</span>toJSON<span>(</span><span>unclass</span><span>(</span>geo_polygon<span>(</span>count <span>=</span> <span>4</span><span>)),</span> auto_unbox <span>=</span> <span>TRUE</span><span>))</span>
</code></pre></div>
<p></p>
<h2>WKT</h2>
<p>Functions prefixed with <code>wkt</code> create random Well-Known Text (WKT) data. These functions
wrap the GeoJSON versions, but then convert the data to WKT.</p>
<h3>Random point</h3>
<div><pre><code><span></span>wkt_point<span>()</span>
<span>#> [1] &quot;POINT (179.8795330 -29.1106238)&quot;</span>
</code></pre></div>
<h3>Random polygon</h3>
<div><pre><code><span></span>wkt_polygon<span>()</span>
<span>#> [1] &quot;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))&quot;</span>
</code></pre></div>
<h2>Use case</h2>
<p>Example of geospatial data manipulation, using <code>randgeo</code>, <code>leaflet</code> and
<code>lawn</code>.</p>
<p>Steps:</p>
<ul><li>Generate random overlapping polygons</li>
<li>Calculate a single polygon from overlapping polygons</li>
<li>Map polygon</li>
<li>Generate random locaitons (points)</li>
<li>Clip locations to the polygon</li>
<li>Overlay locations (more random points) on the polygon</li>
</ul><div><pre><code><span></span><span>library</span><span>(</span>randgeo<span>)</span>
<span>library</span><span>(</span>lawn<span>)</span>
<span>library</span><span>(</span>leaflet<span>)</span>
</code></pre></div>
<h3>generate random data</h3>
<div><pre><code><span></span><span>set.seed</span><span>(</span><span>5</span><span>)</span>
polys <span>&lt;-</span> randgeo<span>::</span>geo_polygon<span>(</span>count <span>=</span> <span>2</span><span>,</span> num_vertices <span>=</span> <span>4</span><span>,</span> bbox <span>=</span> <span>c</span><span>(</span><span>-120</span><span>,</span> <span>40</span><span>,</span> <span>-100</span><span>,</span> <span>50</span><span>))</span>
</code></pre></div>
<h3>Get intersection of polygons</h3>
<div><pre><code><span></span>polysinter <span>&lt;-</span> lawn<span>::</span>lawn_intersect<span>(</span>polys<span>$</span>features<span>[[</span><span>1</span><span>]],</span> polys<span>$</span>features<span>[[</span><span>2</span><span>]])</span>
</code></pre></div>
<h3>map polygons</h3>
<div><pre><code><span></span>polysinter <span>%>%</span> lawn<span>::</span>view<span>()</span>
</code></pre></div>
<p></p>
<h3>generate random points - clip points to polygon</h3>
<div><pre><code><span></span>pts <span>&lt;-</span> randgeo<span>::</span>geo_point<span>(</span>count <span>=</span> <span>500</span><span>,</span> bbox <span>=</span> <span>c</span><span>(</span><span>-120</span><span>,</span> <span>40</span><span>,</span> <span>-100</span><span>,</span> <span>50</span><span>))</span>
pts <span>&lt;-</span> lawn<span>::</span>lawn_within<span>(</span>
  points <span>=</span> lawn_featurecollection<span>(</span>pts<span>),</span>
  polygons <span>=</span> lawn_featurecollection<span>(</span>polysinter<span>)</span>
<span>)</span>
</code></pre></div>
<h3>Draw polygon + points on map</h3>
<div><pre><code><span></span>polysinter <span>%>%</span>
  view<span>()</span> <span>%>%</span>
  addGeoJSON<span>(</span>geojson <span>=</span> jsonlite<span>::</span>toJSON<span>(</span><span>unclass</span><span>(</span>pts<span>)))</span>
</code></pre></div>
<p></p>
<h2>Feedback</h2>
<p>Let us know what you think! <code>randgeo</code> doesn't have any revdep's on CRAN yet, but
is being <a href="https://github.com/search?utf8=%E2%9C%93&q=%22randgeo%22+language%3AR+-user%3Acran+-user%3Aropensci&type=Code">used in one package on GitHub</a>.</p>