3

How can I query polygon features that intersect other polygon features in QGIS? I have ZIP code areas and city areas, and I would like to list all the ZIP codes that intersect the city areas. Example: San Antonio= 78209,78258,78259,78250, etc.

enter image description here

I've been looking for a method for a while that can do this in QGIS, I tried "list unique values and union" commands in the vector tools and no luck. I know you can do a Union in ArcMap Pro but I don't have that software here. I was also able to do this in GeoMedia Pro years ago. I am unable to figure out in QGIS how to do some polygon vs polygon analysis.


I am still having problems with this, I was able to create an intersection layer but I can't figure out how to associate the results back to the city area feature. I would like to add a field to City_Area which would list the ZIP Code names separated by comma. (City_A=78205,78204,78203,78201)

  • Check "select by location". Based on your description, you need "intersect" tools not "union". – underdark Oct 4 '16 at 19:47
8

One solution is to create a Virtual Layer and write some SQL.

enter image description here

Type the following query into the Query section of the dialog:

SELECT c.geometry, c.CITY_NAME, group_concat(z.code) AS ZIP_ids
FROM ZIPCODE z, CITY_AREA c 
WHERE ST_Intersects(z.geometry, c.geometry)
GROUP BY c.CITY_NAME;

Adjust field names in the query. For example, I assumed ZIP codes are stored in the code field of your ZIPCODE layer. Test the query.

Choose proper values for other options such as Geometry column, type, and CRS. Finally, click OK.

You should get a new layer with concatenated ZIP ids for each CITY_AREA.

  • 1
    Thank you and howdy from Texas....it worked perfectly....cheers....and thanks again... – Rusty Dickson Oct 11 '16 at 21:06
0

For automization purposes one could also get a combination of city names and ZIP codes by using the Python console (QGIS2.18):

from qgis.utils import *
import csv

CITY_name_list=[]
ZIP_id_list=[]
cities_list=[]
layer = iface.activeLayer()
for field in layer.pendingFields():
    if field.name() == 'CITY_NAME':
        idx_CITY_NAME = layer.fieldNameIndex(field.name())
    if field.name() == 'ZIP_id':
        idx_ZIP_id = layer.fieldNameIndex(field.name())
for feature in layer.getFeatures():
    attrs = feature.attributes()
    name=(attrs[idx_CITY_NAME])
    cities_list.append(name)
    ZIP=(attrs[idx_ZIP_id])
cities_set=set(cities_list)
for cities in cities_set:
    CITY_name_list.append(cities)
    ZIP_list=[]
    for feature in layer.getFeatures():
        attrs = feature.attributes()
        if cities==attrs[idx_CITY_NAME]:
            ZIP=attrs[idx_ZIP_id]
            ZIP_list.append(ZIP)
    ZIP_string=', '.join(ZIP_list)
    ZIP_id_list.append(ZIP_string)

rows=zip(CITY_name_list,ZIP_id_list)
header_list =["CITY_NAME", "ZIP_id"]
with open(r'C:\Users\XXX\Desktop\cities.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=';',quotechar=';')
    writer.writerow(header_list)
    for row in rows:
        writer.writerow(row)

Simply load the intersected layer into QGIS and change the path where the csv with the city - ZIP combinations will be saved.

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.