vl_select_by_spatial_relationship_qgis_2x.py
1 |
#!/usr/bin/env python
|
---|---|
2 |
# -*- coding: utf-8 -*-
|
3 |
|
4 |
# define interface
|
5 |
##Virtual Layers (QGIS 2.x)=group
|
6 |
##Select by spatial relationship=name
|
7 |
##Layer_to_select_from=vector
|
8 |
##Spatial_relationship=selection Intersects;Contains;Within;Touches;Overlaps;Equals
|
9 |
##Intersection_layer=vector
|
10 |
##True_or_false=selection True;False
|
11 |
##Snapping_tolerance=string 1e-5
|
12 |
##Output_layer=string vl_
|
13 |
##Output_unique_identifier_column=string gid
|
14 |
##Output_geometry_column=string geom
|
15 |
|
16 |
from qgis.core import QgsVectorLayer, QgsMapLayerRegistry |
17 |
|
18 |
# get input parameters from GUI
|
19 |
inlayer_select = processing.getObject(Layer_to_select_from) |
20 |
|
21 |
spatial_rs = Spatial_relationship |
22 |
if spatial_rs == 0: |
23 |
spatial_rs = 'ST_Intersects'
|
24 |
elif spatial_rs == 1: |
25 |
spatial_rs = 'ST_Contains'
|
26 |
elif spatial_rs == 2: |
27 |
spatial_rs = 'ST_Within'
|
28 |
elif spatial_rs == 3: |
29 |
spatial_rs = 'ST_Touches'
|
30 |
elif spatial_rs == 4: |
31 |
spatial_rs = 'ST_Overlaps'
|
32 |
elif spatial_rs == 5: |
33 |
spatial_rs = 'ST_Equals'
|
34 |
|
35 |
inlayer_intersection = processing.getObject(Intersection_layer) |
36 |
|
37 |
t_or_f = True_or_false |
38 |
snaptolerance = Snapping_tolerance |
39 |
|
40 |
outlayer = Output_layer |
41 |
outuic = Output_unique_identifier_column |
42 |
outgeom = Output_geometry_column |
43 |
|
44 |
# get geometry type and coordinate reference system from input layer ('Layer to select from')
|
45 |
ingeomtype = inlayer_select.geometryType() |
46 |
if ingeomtype == 0: |
47 |
ingeomtype = 'point'
|
48 |
elif ingeomtype == 1: |
49 |
ingeomtype = 'linestring'
|
50 |
elif ingeomtype == 2: |
51 |
ingeomtype = 'polygon'
|
52 |
|
53 |
incrs = inlayer_select.crs().authid() |
54 |
incrs = incrs[5:]
|
55 |
|
56 |
# create query
|
57 |
query_t = """SELECT
|
58 |
DISTINCT('{0}'.rowid) AS '{4}',
|
59 |
'{0}'.geometry AS '{5}'/*:{6}:{7}*/
|
60 |
FROM '{0}', '{2}'
|
61 |
WHERE {1}(ST_Snap('{0}'.geometry, '{2}'.geometry, {3}), ST_Snap('{2}'.geometry, '{0}'.geometry, {3}));
|
62 |
""".format( inlayer_select.name(), spatial_rs, inlayer_intersection.name(), snaptolerance, outuic, outgeom, ingeomtype, incrs )
|
63 |
|
64 |
query_f = """SELECT
|
65 |
'{0}'.rowid AS '{4}',
|
66 |
'{0}'.geometry AS '{5}'/*:{6}:{7}*/
|
67 |
FROM '{0}' LEFT JOIN '{2}'
|
68 |
ON {1}(ST_Snap('{0}'.geometry, '{2}'.geometry, {3}), ST_Snap('{2}'.geometry, '{0}'.geometry, {3}))
|
69 |
WHERE '{2}'.rowid IS NULL;
|
70 |
""".format( inlayer_select.name(), spatial_rs, inlayer_intersection.name(), snaptolerance, outuic, outgeom, ingeomtype, incrs )
|
71 |
|
72 |
if t_or_f == 0: |
73 |
query = query_t |
74 |
elif t_or_f == 1: |
75 |
query = query_f |
76 |
|
77 |
# run query and add layer to map
|
78 |
vlayer = QgsVectorLayer( "?query={}".format(query), outlayer, "virtual" ) |
79 |
QgsMapLayerRegistry.instance().addMapLayer(vlayer) |