[R-sig-Geo] Shapefile clipping operation help request

Micha Silver t@v|b@r @end|ng |rom gm@||@com
Mon Sep 5 21:21:13 CEST 2022


On 05/09/2022 20:10, Bhaskar Mitra wrote:
> Hello All,
>
>
>
>   I am new to geospatial analysis and would really appreciate some help in
> shapefile clipping operation in R. I want to use a clipping function (or
> something equivalent), whereby, if the majority of  shapefile 2 (e.x:
> greater than 50 % area of shapefile 2) is within shapefile 1, I can
> incorporate
>
> shapefile 2 within shapefile 1 and generate shapefile 3.
>
>
> However, if less than 50% area of shapefile 2 is within shapefile 1, then
> it will be discarded.
>

>From your question, I assume that the two shapefiles contain polygon 
features. ("Shapefile" can also be points or lines). What you want is 
the intersection between two polygon shapefiles if that intersection is 
larger in area than half the area of the second shapefile.

Shapefiles are read into R with the sf package. So this might help:


library(sf)


PartialOverlap <- function(p1, p2, overlap_portion = 0.5) {

     area2 <- st_area(p2)

     intersection <- st_intersection(p1, p2)

     area_overlap = st_area(intersection)

     if (area_overlap/area2 >= overlap_portion) {

         return(intersection)

     }

     else {

         return(NULL)

     }

}


p1 <- st_read("your_first.shp")

p2 <- st_read("your_second.shp")

p3 <- PartialOverlap(p1, p2)


However, I would point out that polygon shapefiles can contain several 
polygon features, and also multi-polygons. You probably want to consider 
how you want to handle these cases. For example, your shapefile2 might 
contain many polygons outside of shapefile1, and only a few that 
overlap. Those individual overlaps might each be larger than half of the 
individual polygon areas but smaller than the total area of all polygons 
in shapefile2. How do you want to treat this case?

  Refer to Chapter 2 of: 
https://geocompr.robinlovelace.net/spatial-class.html  or any other 
basic introduction to GIS vector features for a better explanation.



>
> I would really appreciate some help in this regard.
>
>
>
> Sincerely,
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918



More information about the R-sig-Geo mailing list