I played with assign()
, ls()
, and mget()
to accomplish something that I believe will improve your workflow. First I use ls()
to get a list of all environment variables that start with "p":
names_poly <- ls(pattern='^p.')
I used combn()
to find all the unique combinations of polygons
combos <- combn(names_poly,2)
I looped over combos
using get()
to fetch the polygons i
and j
named in combos
. I also used assign()
to create a new object whose name is pasted together (e.g. int_p1_p2
)
for(k in seq_along(combos[1,])){
i <- combos[1,k]
j <- combos[2,k]
print(paste("intersecting",i,j))
assign(paste("int",i,j,sep="_"),gIntersection(get(i), get(j), byid = FALSE))
}
You may not need this step below since you have all the new int_pi_pj
SpatialPolygons as separate objects, but if you want them in a list:
int <- mget(ls(pattern='^int_.'))
rm(list = ls(pattern='^int_.') #cleanup
Then you can access the items in your list with $
:
plot(int$int_p1_p2)
EDIT: In the above, when there is no overlap, the variable will be NULL.If you don't want to even see the combinations where there is no overlap, you could add this if
statement to the same for
loop:
for(k in seq_along(combos[1,])){
i <- combos[1,k]
j <- combos[2,k]
print(paste("intersecting",i,j))
if(gIntersects(get(i), get(j), byid = FALSE)){
assign(paste("int",i,j,sep="_"),gIntersection(get(i), get(j), byid = FALSE))
}
}
The SAGA Polygon self-intersection
would have created new ID
field to store the combination of overlapped polygons. Please keep it when you export it as Polygon C
.
Example output of Polygon self-intersection
With a bit of editing, this can be used as id field to put together with Polygon A
(or Polygon B
).
For instance, if we want to use second number from ID
, expression like:
substr( "ID", strpos("ID", '|')+1, 1)
will return below.
![enter image description here](https://i.stack.imgur.com/vhuEj.png)
(Please modify it to fit with your data. If you want to use the first one, it will be substr( "ID", strpos("ID", '|')-1, 1)
. )
After merging them, Collect geometries
(QGIS 3.0) or Singleparts to multipart
using this field (overlap in the above example) as Unique ID will do the job.
Best Answer
First make sure your Buildings have a unique
"id"
(which does not contain a|
character, you will see why below). You can add it for example via field calculator by adding a new field with the expression$id
if they do not have one already.Then run the "Polygon Self-Intersection" from SAGA Processing tools and choose this
"id"
as Identifier:Which will create a new layer, where the overlapping Parts become their own polygons.
This new layer will contain a new
"id"
field, containing the"id"
s of their original buildings split by a|
sign.Now open the field calculator of this new layer and calculate a new field with this expression:
to get the second
"ID"
as"mergeID"
orto get the first
"ID"
as"mergeID"
:This expression looks up in your
"ID"
field, whether it contains a|
character. If so, it chooses theID
before
orafter
the|
character. If it does not contain this character, it will justcopy
over theID
to the"mergeID"
.Then run the "Dissolve" from processing toolbox and choose this
"mergeID"
as dissolve field:Your final result: