<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/random-geojson-and-wkt-with-randgeo/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/technotes/2017/04/20/randgeo"> rOpenSci Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>randgeo</strong> generates random points and shapes in GeoJSON and WKT formats for<br>
use in examples, teaching, or statistical applications.</p>
<p>Points and shapes are generated in the long/lat coordinate system and with<br>
appropriate spherical geometry; random points are distributed evenly across<br>
the globe, and random shapes are sized according to a maximum great-circle<br>
distance from the center of the shape.</p>
<p><strong>randgeo</strong> was adapted from <a target="_blank" rel="nofollow" href="https://github.com/tmcw/geojson-random">https://github.com/tmcw/geojson-random</a> to have<br>
a pure R implementation without any dependencies as well as appropriate<br>
geometry. Data generated by <strong>randgeo</strong> may be processed or displayed of with<br>
packages such as <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sf"><strong>sf</strong></a>,<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=wicket"><strong>wicket</strong></a>,<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=geojson"><strong>geojson</strong></a>,<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=wellknown"><strong>wellknown</strong></a>,<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=geojsonio"><strong>geojsonio</strong></a>, or<br>
<a target="_blank" rel="nofollow" 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 data-lang="r"><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 data-lang="r"><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.<br>
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<br>
you to limit points to within long/lat bounds.</p>
<div>
<pre><code data-lang="r"><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 data-lang="r"><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 data-lang="r"><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><img alt="map" src="https://i0.wp.com/ropensci.org/assets/blog-images/2017-04-20-randgeo/plot1.png?w=456" data-recalc-dims="1"></p>
<h2>WKT</h2>
<p>Functions prefixed with <code>wkt</code> create random Well-Known Text (WKT) data. These functions<br>
wrap the GeoJSON versions, but then convert the data to WKT.</p>
<h3>Random point</h3>
<div>
<pre><code data-lang="r"><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 data-lang="r"><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<br>
<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 data-lang="r"><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 data-lang="r"><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 data-lang="r"><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 data-lang="r"><span></span>polysinter <span>%>%</span> lawn<span>::</span>view<span>()</span>
</code></pre>
</div>
<p><img alt="map" src="https://i2.wp.com/ropensci.org/assets/blog-images/2017-04-20-randgeo/plot2.png?w=456" data-recalc-dims="1"></p>
<h3>generate random points – clip points to polygon</h3>
<div>
<pre><code data-lang="r"><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 data-lang="r"><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><img alt="map" src="https://i0.wp.com/ropensci.org/assets/blog-images/2017-04-20-randgeo/plot3.png?w=456" data-recalc-dims="1"></p>
<h2>Feedback</h2>
<p>Let us know what you think! <code>randgeo</code> doesn't have any revdep's on CRAN yet, but<br>
is being <a target="_blank" rel="nofollow" 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>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ropensci.org/blog/technotes/2017/04/20/randgeo"> rOpenSci Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/2Cbnqjzcvvc"></a></div>