<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/intersecting-points-and-overlapping-polygons/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aghaynes.wordpress.com/2018/05/27/intersecting-points-and-overlapping-polygons/"> R – Insights of a PhD</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’ve been doing some spatial stuff of late and the next little step will involve intersecting points with possibly many overlapping polygons. The sp package has a function called over which returns the polygons that points intersects with. The catch though, is that it only returns the last (highest numerical value) polygon a point overlaps with. So it’s not so useful if you have many overlapping polygons. A little playing, and I’ve overcome that problem…</p>
<p>Here’s a toy example.</p>
<p>Create a couple of polygons and put them into a SpatialPolygons object.</p>
<pre>library(sp)

p1 &lt;- matrix(c(1,1,
 2,1,
 4,2,
 3,2), ncol = 2, byrow = TRUE)
p2 &lt;- matrix(c(2.2,1,
 3,1,
 3,2,
 3,3,
 2.8,3), ncol = 2, byrow = TRUE)
p1s &lt;- Polygons(list(Polygon(p1)), 3)
p2s &lt;- Polygons(list(Polygon(p2)), 4)
sps &lt;- SpatialPolygons(list(p1s, p2s))</pre>
<p>Define a few points and put them in a SpatialPoints object</p>
<pre>point &lt;- matrix(c(2.5, 1.5,
 3.2, 1.75,
 2,3,
 1.5, 1.25, 
 2.75, 2.5), ncol = 2, byrow = TRUE)
points &lt;- SpatialPoints(point)</pre>
<p>We can plot them…(not shown)</p>
<pre>plot(sps, border = c(&quot;black&quot;, &quot;blue&quot;))
plot(points, add = TRUE)</pre>
<p>As here we have the issue:</p>
<pre>over(points, sps)</pre>
<pre><span> 1 2 3 4 5 2 1 NA 1 2 </span></pre>
<p>only returning a single “hit” per point (point 1 overlaps with both polygons 1 and 2).</p>
<p>To get around this, we can rip the individual polygons out of the SpatialPolygons object and put them in a list, converting the individual polygons into SpatialPolygons along the way:</p>
<pre>sps2 &lt;- lapply(sps@polygons, function(x) SpatialPolygons(list(x)))</pre>
<p>Then lapply-ing over sps2 we can see which polygons each point intersects…</p>
<pre>lapply(sps2, function(x) over(points, x))</pre>
<pre><span>[[1]]
1 2 3 4 5 1 1 NA 1 NA [[2]]
1 2 3 4 5 1 NA NA NA 1</span></pre>
<p>And now we see that point one overlaps with both polygons.</p>
<p>Code <a rel="nofollow" href="https://gist.github.com/aghaynes/d3131233506e9ef1623222232d6d5502" target="_blank">>>>here</a></p>
<p> </p>
<p> </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="https://aghaynes.wordpress.com/2018/05/27/intersecting-points-and-overlapping-polygons/"> R – Insights of a PhD</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/66YwoCL3LLU" height="1"></a></div>