# Select Polygons in layer A with a specific geolocation criteria in relation to each other and layer B

I am trying to select Polygons from Layer A with the following rules:

1. Polygon in Layer A is smaller than 1 ha and further than 100 m from any other polygon in either Layer A or B that is greater than 2 hectares.

2. Polygon in Layer A measures between 1 ha and 2 ha and is further than 100 m from any other polygon in either Layer A or B greater than two hectares.

3. Polygon in Layer A measures between 2 ha and 3 ha and further than 100 m from any other polygon in either Layer A or B greater than two hectares.

4. Remove narrow corridors (less than < 50 m in width (see Fig 1)) from polygons in Layer A if the polygon is further than 30m away from any other polygon in either Layer A or B.

Narrow corridors are maximum 50m wide and can have any lenght. Not sure if it would work, but what about:

1. Create vertices along the polygon using 'extract vertices';
2. If necessary, create lat/lon of all points (if not skip to step 3)
3. Create lines between all vertices (x1, x2, x3) (use make_line? command) and measure line_length (use \$length command) (Fig 2)
4. SELECT all lines <=50m
5. Create a new polygon around the selection and call “narrow_corridors”
6. Subtract narrow corridors from the original layer

Happy to have these run separate, as Layer B has hundreds of thousands of polygons and I can't think of a way around this.

Was thinking of using 'select by expression', but don't really know anything about coding yet.

• Can you further clarify how you define 'narrow corridor' more formally ? Jan 31 at 12:56
• @Karper I've edited the post to clarify Feb 3 at 5:50
• I think one additional rule to add should be to exclude the lines which are going from point to point along the polygon contour. Otherwise everything or lines cutting in concave turns will be selected. It would have to be X<50 and distance along polygon_contour > a greater criterion. Feb 3 at 7:57
• You should probably split this overall question in 2 different ones by separating the 'narrow corridor' which is a topic on it's own that can be handled separately from your 3 first items that have an answer below. Feb 3 at 9:46

With Virtual Layers, you can easily handle the first 3 items, the 4th item should be manageable with the same approach but the geometric requirement needs to be further defined.

Add a virtual layer with the SQL query below. Your Layers need to be in a CRS with units in meters and for which distance/area computations are correct (units in the query are in meters or square meters)

``````with min_distance_table as
(select temp.geometry, temp.area, min(distance_a) min_a ,min(distance_b) min_b from
(select a.*, st_area(a.geometry) as area ,st_distance(a.geometry,aa.geometry) as distance_a, st_distance(a.geometry,b.geometry) as distance_b from LayerA a, LayerA aa, LayerB b where st_area(aa.geometry)> 20000 and st_area(b.geometry)> 20000  and a.geometry != aa.geometry) temp

where temp.area < 30000
group by temp.geometry,temp.area)

select *,
case
when area < 10000 then '1ha'
when area <20000 and area >=10000 then '2ha'
when area < 30000 and area >= 2000 then '3ha'
end area_category

from min_distance_table where min_a >100 and min_b > 100
`````` A & B layers in green/pink: Selected A layer geometries in red: Selected layer attributes (categorizing the 1/2/3 Ha bins) Someone even probably has a more efficient/elegant query to achieve the same result

For your 4th item, you should clarify how you would geometrically define a 'narrow corridor'

• Hi @Kasper, thanks for that. However, I can't get it to work... I used the same code you gave me (with 1 edit: when area < 30000 and area >= 2000 then '3ha') using the actual names of the layers (LayerA = VHC_inReserves_Spark and LayerB = SCC_VHC_-EnvOpsEstate). Query execution error on CREATE TEMP VIEW _tview AS with min_distance_table as (select temp.geometry,......... and min_b > 100: 1 - near "-": syntax error. Layers are in GDA2020MGA56. Feb 3 at 5:32
• The error comes from the '-' in your second layer name. Rename it to avoid this character Feb 3 at 7:50