2

I am completely new to QGIS, and to the world of shapefiles and GIS in general.

I have the shapefile of a city, divided into its districts. On top of it I add a layer from a csv, with a certain number of points with precise coordinates. Is it possible to assign to each one of these points the name of the districts they are in? For example, I would like to add a new 'district' column to the attribute table, and each row (i.e. each point) will have the right district value, so that I can export the updated table as csv and use this information in my analysis.

I know that there exists a feature called 'points in polygon' which can assign each area the number of points that falls within it, which basically is the exact opposite of what I would like to do.

  • Welcome to GIS SE. As a new user, please take the Tour, which explains how our "Focused question / Best answer" model operates. In order to prevent GIS SE from becoming a coding service, questions about coding need to contain a good-faith effort to code a solution. – Vince May 18 '18 at 10:38
  • 1
    Spatial Join tool should work. – BERA May 18 '18 at 10:52
1
  • Menu > Vector > Data Management Tools > Join attributes by location
  • See the settings screenshot:

screenshot

Explanation :

  • Target : points layer, Join layer: polygons layer. That way info from your districts goes into your points
  • Geometric predicate : Check ON all the options: intersects, touches etc. I haven't looked further into this yet but just one option had not worked out earlier.
  • Attributes summary : Take attributes of first located feature
  • Joined table : Keep all records ... that will preserve the points that were outside of all the wards too
  • Get rid of everything in the "Statistics" option

This will create and add another layer which has all your points as they were, but now the attributes have extra columns from the polygon layer. So this is kind of like the "left join" in python-pandas. The "keep all records" option ensures that points falling outside of all polygons are not lost.

To export this data back into CSV format, I prefer using the MMQGIS plugin and Export-Geometry-to-CSV option in that.

1

Tested on QGIS 2.18

I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

1. Simply Drag and Drop your CSV-file in QGIS and then proceed with the following query

SELECT *,setsrid(make_point(pointscsv.x, pointscsv.y),4326)
FROM pointscsv

Save this the output layer as a point shapefile through Right click > Save AS ... > ESRI Shapefile

Alternatively, you may use a standard QGIS option via Layer > Add Layer ... > Add delimited text layer.... Then you do not need to save your output.

2. Go with the following Query in the Virtual Layer

SELECT *
FROM points
JOIN districts.name ON ST_WITHIN(points.geometry, districts.geometry)

3. Save the output as CSV-file through Right click > Save AS ... > CSV

P.S. Do not know yet how to combine those statements in one


References:

  • 1
    Instead of adding a virtual layer, you could use DB-manager. And there you can use the with-clause to combine the both queries. eg. with points as (select setsrid ... from csv) select * from points join district on etc – PieterB Apr 17 at 7:13
  • Thank you for a hint! I have to explore this feature more – Taras Apr 17 at 9:14
  • Here is an answer where I use the with-clause: gis.stackexchange.com/a/316107/7849 – PieterB Apr 17 at 9:16
0

For this task I use the SAGA "Add polygon attributes to points" algorithm available in the Processing Toolbox.

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.