1

I have one layer of polygons that stretch across the U.S., and another layer of all counties in the U.S. In the counties layer is a column with population density for each county.

What I am trying to do is to filter the data by polygons that intersect with a county that has a specific population density. For example, I want to highlight all polygons that intersect a county that has a pop density of <50. Ideally, it would be nice to have it create a new layer based on these filtered polygons so that I could color them differently.

Is this possible to do in QGIS using an expression of some kind?

1

An option is to create a new virtual layer (Layer menu / add layer / add virtual layer) and to do both intersection and filtering there.

Using generic names, the query would be:

select bigpoly.* from bigpoly, counties
where st_intersects(bigpoly.geometry,counties.geometry)
and counties.density>50

If you don't want the entire polygon but just the portion intersecting the high density counties, you can construct the new geometry

select bigpoly.attribute1, 
       ST_Intersection(a.geometry,p.geometry) as geometry 
from bigpoly, counties
where st_intersects(bigpoly.geometry,counties.geometry)
and counties.density>50

Should you only want to select the polygons, you could go the Spatial Query way:

Install the Spatial Query Plugin. Select your county polygons based on your density criteria. Go to vector/spatial query. Choose to select from your "big polygon" layer, features intersecting the county layer. Check the box "use selected feature" below the county layer (reference layer)

0

Here's a solution that doesn't require any python coding.

Make sure that the polygon layer has a unique field (ie a field that the counties layer doesn't have) with no null values. I'll refer to this field as polygonfield. Vector menu > Geoprocessing Tools > Union > combine counties + polygons layers as a temporary layer

enter image description here

Use "Select by expression" on Union layer to select only the polygons with population density less than 50.

"pop density" < 50 AND "polygonfield" IS NOT NULL

Right click on Union layer, Save as...

Check the box for "Save only selected features"

Now you have a layer with only the portions of polygons that overlap counties with population density less than 50. If you want the full polygons, merge the new layer with the original polygon layer.

-1

Unfortunately, it is currently not possible to create cross-layer queries using field calculator or expressions.

But if you have your layers in a geodatabase (like postgis), you can make one view that shows polygons that intersect each other.

Example:

CREATE OR REPLACE VIEW public.exampleview AS 
SELECT t01.geom 
FROM t01_polygons t01 
inner join t02_us on st_intersects(t02_us.geom1,t01.geom2)

Next in QGIS you can:

  • Double click layer.
  • Select rule-based style.
  • On filter field use this expresion density>50
  • It is possible via virtual layers. Your query is selecting all polygons covering the US (counties cover the entire country). Returning just the geom won't allow any further rule base styling. You could embed the density criteria within the view. – JGH Sep 12 '17 at 14:59

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.