I have the following shapefiles:

  • county.shp: polygons referring to country land subdivisions
  • lake.shp: polygons referring to lakes, ponds, etc.

Layers attributes are as follow:

  • county.shp

    • id INTEGER
    • name TEXT
    • code INTEGER
  • lake.shp

    • id INTEGER
    • code INTEGER

The codeattribute is not filled for lake.shplayer. I would like to update this field by intersecting both layers and bring back codevalues from county.shp to lake.shp.

I know I can do this with the Join Attributes by Location tool, but this produces a new shapefile with columns put together. Is it possible to do a simply update lake.shp layer based on a spatial intersection?

2 Answers 2


Apologies for being vague, but this is a similar workflow i've used in ArcGIS and I'm sure it can translate to QGIS:

  1. Create a uniqueid for your Lakes
  2. Select by location
  3. Calculate code for Lakes
  4. Join uniqueid, code in new shapefile with Lakes uniqueid layer
  5. Export new Lakes Layer, delete uniqueid column

Or create a python script/tool that will:

  1. Iterate through each row in Lakes or County (which ever is smallest and makes the most sense), get the value of the row in code. For this example lets use County.
  2. Select the row and compute an intersection with Lakes (or the other layer you didn't use in 1). If they intersect or are contained, then:
  3. Populate code in Lakes

This may be a good start http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/vector.html

  • Thanks @GISkid! I'll try to script something, then... And be back here if I need any further help. The workflow you suggest for ArcGIS is a bit as much work as using the Join attribute by location tool, I think.
    – wiltomap
    Commented Jul 13, 2016 at 6:54
  • @wiltomap is it a timing issue? Like it takes a long time to process? If so - I think your best bet would definitely be scripting. I had a script for Arc that did #2, but I don't know how much help it would be for you to use in Q
    – GISHuman
    Commented Jul 14, 2016 at 13:19

What you could do 'select by location', selecting only those lakes that fall within a county. Then update 'code' with the county code. I'm sure python could make this a lot faster.

  • Thanks @Knightshound! I mentionned Join Layers by Location tool in my post. The other solution is much more time-consuming I'm afraid... I don't use Python enough to be able to script on my own, but it surely will do the job!
    – wiltomap
    Commented Jul 12, 2016 at 15:01
  • Fair enough. I thought you were referring to another tool. Unfortunately without having any associated data between the two layers it's going to be very difficult to join them. Commented Jul 12, 2016 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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