0

I have a shapefile 1 and a shapefile 2. Shapefile 1 contains ~2k Polygons while shapefile 2 contains ~4k polygons. I want to know, which Polygons from shapefile 1 intersect polygons in shapefile 2. Further want to write the information from the intersecting polygons into the attributetable from shapefile 2, into the corresponding rows of the polygon. I want to preserve the shape/outline of shapefile 2! One polygon from shapefile 2 can be intersected by many polygons from shapefile 1. That means my resulting shapefile will have way more than ~4k Polygons. Whatever I try though (Intersect/Union) returns me the information I need BUT with the outline of shapefile 1. I tried a Spatial Join but that did not help neither. It did not write multiple information into one polygon in shapefile 1. The resulting shapefile from the spatial join had as many rows as shapefile 2.

Is there a way to do this in QGIS/GRASS/GDAL?

8
  • 1
    Have you tried intersecting both polygons and calculating relative area after intersection for each polygon? With that, you can select by ID the intersection with bigger relative area in each object and usesearch in Excel or lookup(qdap) in R to merge fields to shapefile 2.
    – aldo_tapia
    Nov 11, 2016 at 13:06
  • @aldo_tapia maybe you misunderstood me: I want to write the information from one shapefile into another, where there is an overlap/intersection. That has nothing to do with calculated area or some sort...
    – four-eyes
    Nov 11, 2016 at 13:07
  • There are two ways to solve this, and Union is one of them, so you probably don't want to refuse a correct answer.
    – Vince
    Nov 11, 2016 at 13:10
  • @Vince I reasked becasue importing something into Excel etc. sounds like a strange workaround. What would the other solution be? Further, Union only works if the geometries overlay exactly.
    – four-eyes
    Nov 11, 2016 at 13:33
  • 1
    You seem to be assuming that one-to-many relationships will return the source multiple times, as in a SQL query, but that is not likely to be the case with shapefiles. If you provide details on what you have tried in the question, then maybe a QGIS person can tell you where it went wrong.
    – Vince
    Nov 11, 2016 at 15:11

1 Answer 1

1

You intersect shapefile 1 and shapefile 2 (that would be v.overlay with AND operator in GRASS) and produce shapefile 3.

Then you add a text column to your shapefile 2 where you can upload a e.g. comma separated list of polygon IDs in text form (you can use an array if you DB-backend supports that data type).

Next is to aggregate attributes from shapefile 3:

CREATE TABLE intersection AS SELECT ID_shapefile_2, group_concat(ID_shapefile_2) FROM shapefile_3 GROUP BY ID_shapefile_2; ("group_concat" would be an SQLite function). In GRASS you can run this in db.execute

Finally, you join attributes from "intersection" table to shapefile 2.

If prefer a QGIS solution, have a look at expressions and aggregates (but I am not that familiar with those...

BTW: In PostGIS you could do that in one fairly simple query:

CREATE TABLE result AS SELECT b.*, array_agg(DISTINCT a."ID") AS shape1_ids FROM shapefile_1 As a, shapefile_2 As b WHERE ST_Intersects(a.geom, b.geom) GROUP BY b.*;

3
  • Wouldnt the PostGIS solution be CREATE TABLE table3 AS SELECT t1.a, t1.geom t2.c FROM table1 t1 JOIN table2 t2 ON ST_Intersects(t1.geom, t1.geom);?
    – four-eyes
    Nov 17, 2016 at 12:36
  • Not if you want the result to equal shapefile_2 with only on additional column for ids from shapefile_1. The solution you suggest would lead to duplicate rows from both t1 and t2... But i forgot the GROUP BY clause so I fixed that in my answer...
    – Stefan B.
    Nov 17, 2016 at 13:27
  • I posted another question here: gis.stackexchange.com/questions/218135/… There is a sketch which might make it clearer. Performing my query does not leave me with duplicated rows.
    – four-eyes
    Nov 17, 2016 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.