Friday, 22 February 2019

qgis - Spatial join of a single point to multiple polygons



I have a point layer which I want to spatially join to a polygon layer (bring in the attributes).


This works fine unless there are overlapping polygons, if so the QGIS spatial join tool will only bring back the features from the first one found (or some average). An average is not suitable for text attributes.


What I would prefer to happen is that if there is multiple polygons it can be joined to, it will, and result in a duplication of the point. Essentially the ArcGIS: join_operation = JOIN_ONE_TO_MANY


I am working in QGIS 2.8+.


Happy with a script/PyQGIS solution, as I don't think there are any ready made tools for it. But would prefer not to have a SpatiaLite solution as I want to keep it simple for the end user.


Ultimately if there is a solution to this problem that someone has already implemented I would love to see it. If not I will try to come up with the solution myself.



Answer



You are correct (at least as far as I know) that QGIS' spatial join does not offer the relationship parameter available in ArcGIS' spatial join.


In your case, since you describe a many-to-one rather than one-to-many relationship (polygons to points), there is a workaround. If you had many points you wanted to join to each polygon it wouldn't work of course, because you'd have more than one value to fill the same available slot (that would require a relate, or duplicating the polygons). But you've got multiple slots to match and only a few points, so a quick workaround is to duplicate the points - each one time for each of the polygons it touches. The Intersect tool should do this.


Once you've intersected the two layers, you should have a new set of points with at least one point for each intersecting polygon, and the points will have the ID attribute of the polygons they intersect. Points that don't intersect will be dropped of course. You should then be able to do a regular attribute/table join between the two based on the polygon ID field.



Note that if you do have multiple point matches for a single polygon, you'll have issues with this join and be back where you started - the polygon will take the first point found and none of the others. We do have some other questions for that situation.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...