Skip to content

Commit 38a0ea0

Browse files
committedNov 23, 2016
Small tweaks to processing split algorithm, add tests
1 parent 0e2ef06 commit 38a0ea0

File tree

9 files changed

+222
-38
lines changed

9 files changed

+222
-38
lines changed
 

‎python/plugins/processing/algs/qgis/SplitWithLines.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def defineCharacteristics(self):
4747
self.group, self.i18n_group = self.trAlgorithm('Vector overlay tools')
4848
self.addParameter(ParameterVector(self.INPUT_A,
4949
self.tr('Input layer, single geometries only'), [dataobjects.TYPE_VECTOR_POLYGON,
50-
dataobjects.TYPE_VECTOR_LINE]))
50+
dataobjects.TYPE_VECTOR_LINE]))
5151
self.addParameter(ParameterVector(self.INPUT_B,
5252
self.tr('Split layer'), [dataobjects.TYPE_VECTOR_LINE]))
5353

@@ -101,6 +101,9 @@ def processAlgorithm(self, progress):
101101
if len(lines) > 0: # has intersection of bounding boxes
102102
splittingLines = []
103103

104+
engine = QgsGeometry.createGeometryEngine(inGeom.geometry())
105+
engine.prepareGeometry()
106+
104107
for i in lines:
105108
try:
106109
splitGeom = splitGeoms[i]
@@ -112,9 +115,6 @@ def processAlgorithm(self, progress):
112115
if inFeatA.id() == i:
113116
continue
114117

115-
engine = QgsGeometry.createGeometryEngine(inGeom.geometry())
116-
engine.prepareGeometry()
117-
118118
if engine.intersects(splitGeom.geometry()):
119119
splittingLines.append(splitGeom)
120120

@@ -123,13 +123,14 @@ def processAlgorithm(self, progress):
123123
splitterPList = None
124124
outGeoms = []
125125

126+
split_geom_engine = QgsGeometry.createGeometryEngine(splitGeom.geometry())
127+
split_geom_engine.prepareGeometry()
128+
126129
while len(inGeoms) > 0:
127130
inGeom = inGeoms.pop()
128-
engine = QgsGeometry.createGeometryEngine(inGeom.geometry())
129-
engine.prepareGeometry()
130-
inPoints = vector.extractPoints(inGeom)
131131

132-
if engine.intersects(splitGeom.geometry()):
132+
if split_geom_engine.intersects(inGeom.geometry()):
133+
inPoints = vector.extractPoints(inGeom)
133134
if splitterPList == None:
134135
splitterPList = vector.extractPoints(splitGeom)
135136

@@ -143,7 +144,6 @@ def processAlgorithm(self, progress):
143144
# splitGeometry: If there are several intersections
144145
# between geometry and splitLine, only the first one is considered.
145146
if result == 0: # split occurred
146-
147147
if inPoints == vector.extractPoints(inGeom):
148148
# bug in splitGeometry: sometimes it returns 0 but
149149
# the geometry is unchanged
@@ -166,12 +166,12 @@ def processAlgorithm(self, progress):
166166
passed = True
167167

168168
if QgsWkbTypes.geometryType( aGeom.wkbType() ) == QgsWkbTypes.LineGeometry \
169-
and not QgsWkbTypes.isMultiType( aGeom.wkbType() ):
169+
and not QgsWkbTypes.isMultiType(aGeom.wkbType()):
170170
passed = len(aGeom.asPolyline()) > 2
171171

172172
if not passed:
173173
passed = (len(aGeom.asPolyline()) == 2 and
174-
aGeom.asPolyline()[0] != aGeom.asPolyline()[1])
174+
aGeom.asPolyline()[0] != aGeom.asPolyline()[1])
175175
# sometimes splitting results in lines of zero length
176176

177177
if passed:
@@ -182,6 +182,6 @@ def processAlgorithm(self, progress):
182182

183183
if multiGeoms > 0:
184184
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
185-
self.tr('Feature geometry error: %s input features ignored due to multi-geometry.') % str(multiGeoms))
185+
self.tr('Feature geometry error: %s input features ignored due to multi-geometry.') % str(multiGeoms))
186186

187187
del writer

‎python/plugins/processing/gui/TestTools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def createTest(text):
174174
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
175175

176176
params[param.name] = p
177-
if isinstance(param, ParameterTable):
177+
elif isinstance(param, ParameterTable):
178178
schema, filepath = extractSchemaPath(token)
179179
p = {
180180
'type': 'table',
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>split_lines_with_lines</Name>
4+
<ElementPath>split_lines_with_lines</ElementPath>
5+
<!--LINESTRING-->
6+
<GeometryType>2</GeometryType>
7+
<SRSName>EPSG:4326</SRSName>
8+
<DatasetSpecificInfo>
9+
<FeatureCount>11</FeatureCount>
10+
<ExtentXMin>-1.00000</ExtentXMin>
11+
<ExtentXMax>11.00000</ExtentXMax>
12+
<ExtentYMin>-3.00000</ExtentYMin>
13+
<ExtentYMax>5.00000</ExtentYMax>
14+
</DatasetSpecificInfo>
15+
</GMLFeatureClass>
16+
</GMLFeatureClassList>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>-1</gml:X><gml:Y>-3</gml:Y></gml:coord>
10+
<gml:coord><gml:X>11</gml:X><gml:Y>5</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:split_lines_with_lines fid="lines.0">
16+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,2 9,2 9,3 11,5</gml:coordinates></gml:LineString></ogr:geometryProperty>
17+
</ogr:split_lines_with_lines>
18+
</gml:featureMember>
19+
<gml:featureMember>
20+
<ogr:split_lines_with_lines fid="lines.1">
21+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
22+
</ogr:split_lines_with_lines>
23+
</gml:featureMember>
24+
<gml:featureMember>
25+
<ogr:split_lines_with_lines fid="lines.2">
26+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2.0,1.86331771490359 2,2 3,2 3,3</gml:coordinates></gml:LineString></ogr:geometryProperty>
27+
</ogr:split_lines_with_lines>
28+
</gml:featureMember>
29+
<gml:featureMember>
30+
<ogr:split_lines_with_lines fid="lines.2">
31+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2,0 2.0,1.86331771490359</gml:coordinates></gml:LineString></ogr:geometryProperty>
32+
</ogr:split_lines_with_lines>
33+
</gml:featureMember>
34+
<gml:featureMember>
35+
<ogr:split_lines_with_lines fid="lines.3">
36+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>4.21601489757914,1.0 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
37+
</ogr:split_lines_with_lines>
38+
</gml:featureMember>
39+
<gml:featureMember>
40+
<ogr:split_lines_with_lines fid="lines.3">
41+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 4.21601489757914,1.0</gml:coordinates></gml:LineString></ogr:geometryProperty>
42+
</ogr:split_lines_with_lines>
43+
</gml:featureMember>
44+
<gml:featureMember>
45+
<ogr:split_lines_with_lines fid="lines.4">
46+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>8.5655671605538,-3.0 10,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
47+
</ogr:split_lines_with_lines>
48+
</gml:featureMember>
49+
<gml:featureMember>
50+
<ogr:split_lines_with_lines fid="lines.4">
51+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-3 8.5655671605538,-3.0</gml:coordinates></gml:LineString></ogr:geometryProperty>
52+
</ogr:split_lines_with_lines>
53+
</gml:featureMember>
54+
<gml:featureMember>
55+
<ogr:split_lines_with_lines fid="lines.5">
56+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6.91202704418487,-2.08797295581513 10,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
57+
</ogr:split_lines_with_lines>
58+
</gml:featureMember>
59+
<gml:featureMember>
60+
<ogr:split_lines_with_lines fid="lines.5">
61+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,-3 6.91202704418487,-2.08797295581513</gml:coordinates></gml:LineString></ogr:geometryProperty>
62+
</ogr:split_lines_with_lines>
63+
</gml:featureMember>
64+
<gml:featureMember>
65+
<ogr:split_lines_with_lines fid="lines.6">
66+
</ogr:split_lines_with_lines>
67+
</gml:featureMember>
68+
</ogr:FeatureCollection>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>split_lines_with_lines_same</Name>
4+
<ElementPath>split_lines_with_lines_same</ElementPath>
5+
<!--LINESTRING-->
6+
<GeometryType>2</GeometryType>
7+
<SRSName>EPSG:4326</SRSName>
8+
<DatasetSpecificInfo>
9+
<FeatureCount>7</FeatureCount>
10+
<ExtentXMin>-1.00000</ExtentXMin>
11+
<ExtentXMax>11.00000</ExtentXMax>
12+
<ExtentYMin>-3.00000</ExtentYMin>
13+
<ExtentYMax>5.00000</ExtentYMax>
14+
</DatasetSpecificInfo>
15+
</GMLFeatureClass>
16+
</GMLFeatureClassList>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>-1</gml:X><gml:Y>-3</gml:Y></gml:coord>
10+
<gml:coord><gml:X>11</gml:X><gml:Y>5</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:split_lines_with_lines_same fid="lines.0">
16+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,2 9,2 9,3 11,5</gml:coordinates></gml:LineString></ogr:geometryProperty>
17+
</ogr:split_lines_with_lines_same>
18+
</gml:featureMember>
19+
<gml:featureMember>
20+
<ogr:split_lines_with_lines_same fid="lines.1">
21+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
22+
</ogr:split_lines_with_lines_same>
23+
</gml:featureMember>
24+
<gml:featureMember>
25+
<ogr:split_lines_with_lines_same fid="lines.2">
26+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2,0 2,2 3,2 3,3</gml:coordinates></gml:LineString></ogr:geometryProperty>
27+
</ogr:split_lines_with_lines_same>
28+
</gml:featureMember>
29+
<gml:featureMember>
30+
<ogr:split_lines_with_lines_same fid="lines.3">
31+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
32+
</ogr:split_lines_with_lines_same>
33+
</gml:featureMember>
34+
<gml:featureMember>
35+
<ogr:split_lines_with_lines_same fid="lines.4">
36+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-3 10,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
37+
</ogr:split_lines_with_lines_same>
38+
</gml:featureMember>
39+
<gml:featureMember>
40+
<ogr:split_lines_with_lines_same fid="lines.5">
41+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,-3 10,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
42+
</ogr:split_lines_with_lines_same>
43+
</gml:featureMember>
44+
<gml:featureMember>
45+
<ogr:split_lines_with_lines_same fid="lines.6">
46+
</ogr:split_lines_with_lines_same>
47+
</gml:featureMember>
48+
</ogr:FeatureCollection>

‎python/plugins/processing/tests/testdata/expected/polys_split_with_lines.gfs renamed to ‎python/plugins/processing/tests/testdata/expected/split_polys_with_lines.gfs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
<GMLFeatureClassList>
22
<GMLFeatureClass>
3-
<Name>polys_split_with_lines</Name>
4-
<ElementPath>polys_split_with_lines</ElementPath>
3+
<Name>split_polys_with_lines</Name>
4+
<ElementPath>split_polys_with_lines</ElementPath>
55
<!--POLYGON-->
66
<GeometryType>3</GeometryType>
7-
<SRSName>GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]</SRSName>
7+
<SRSName>EPSG:4326</SRSName>
88
<DatasetSpecificInfo>
99
<FeatureCount>7</FeatureCount>
1010
<ExtentXMin>-1.00000</ExtentXMin>
1111
<ExtentXMax>10.00000</ExtentXMax>
1212
<ExtentYMin>-3.00000</ExtentYMin>
1313
<ExtentYMax>6.00000</ExtentYMax>
1414
</DatasetSpecificInfo>
15-
<PropertyDefn>
16-
<Name>fid</Name>
17-
<ElementPath>fid</ElementPath>
18-
<Type>String</Type>
19-
<Width>7</Width>
20-
</PropertyDefn>
2115
<PropertyDefn>
2216
<Name>name</Name>
2317
<ElementPath>name</ElementPath>

‎python/plugins/processing/tests/testdata/expected/polys_split_with_lines.gml renamed to ‎python/plugins/processing/tests/testdata/expected/split_polys_with_lines.gml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,54 @@
1212
</gml:boundedBy>
1313

1414
<gml:featureMember>
15-
<ogr:polys_split_with_lines fid="polys.0">
15+
<ogr:split_polys_with_lines fid="polys.0">
1616
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 -1,3 3,3 3,2 2,2 2,-1 -1,-1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
1717
<ogr:name>aaaaa</ogr:name>
1818
<ogr:intval>33</ogr:intval>
1919
<ogr:floatval>44.123456</ogr:floatval>
20-
</ogr:polys_split_with_lines>
20+
</ogr:split_polys_with_lines>
2121
</gml:featureMember>
2222
<gml:featureMember>
23-
<ogr:polys_split_with_lines fid="polys.1">
23+
<ogr:split_polys_with_lines fid="polys.1">
2424
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 6,4 4,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
2525
<ogr:name>Aaaaa</ogr:name>
2626
<ogr:intval>-33</ogr:intval>
2727
<ogr:floatval>0</ogr:floatval>
28-
</ogr:polys_split_with_lines>
28+
</ogr:split_polys_with_lines>
2929
</gml:featureMember>
3030
<gml:featureMember>
31-
<ogr:polys_split_with_lines fid="polys.2">
31+
<ogr:split_polys_with_lines fid="polys.2">
3232
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,5 2,6 3,6 3,5 2,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
3333
<ogr:name>bbaaa</ogr:name>
3434
<ogr:floatval>0.123</ogr:floatval>
35-
</ogr:polys_split_with_lines>
35+
</ogr:split_polys_with_lines>
3636
</gml:featureMember>
3737
<gml:featureMember>
38-
<ogr:polys_split_with_lines fid="polys.3">
39-
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,-3 7,-2 9,-2 9,0 10,1 10,-3 6,-3</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
38+
<ogr:split_polys_with_lines fid="polys.3">
39+
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>7,-2 6,-3 6,1 10,1 9,0 7,0 7,-2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
4040
<ogr:name>ASDF</ogr:name>
4141
<ogr:intval>0</ogr:intval>
42-
</ogr:polys_split_with_lines>
42+
</ogr:split_polys_with_lines>
4343
</gml:featureMember>
4444
<gml:featureMember>
45-
<ogr:polys_split_with_lines fid="polys.3">
46-
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>7,-2 6,-3 6,1 10,1 9,0 7,0 7,-2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
45+
<ogr:split_polys_with_lines fid="polys.3">
46+
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,-3 7,-2 9,-2 9,0 10,1 10,-3 6,-3</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
4747
<ogr:name>ASDF</ogr:name>
4848
<ogr:intval>0</ogr:intval>
49-
</ogr:polys_split_with_lines>
49+
</ogr:split_polys_with_lines>
5050
</gml:featureMember>
5151
<gml:featureMember>
52-
<ogr:polys_split_with_lines fid="polys.4">
52+
<ogr:split_polys_with_lines fid="polys.4">
5353
<ogr:intval>120</ogr:intval>
5454
<ogr:floatval>-100291.43213</ogr:floatval>
55-
</ogr:polys_split_with_lines>
55+
</ogr:split_polys_with_lines>
5656
</gml:featureMember>
5757
<gml:featureMember>
58-
<ogr:polys_split_with_lines fid="polys.5">
58+
<ogr:split_polys_with_lines fid="polys.5">
5959
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>3,2 6,1 6,-3 2,-1 2,2 3,2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
6060
<ogr:name>elim</ogr:name>
6161
<ogr:intval>2</ogr:intval>
6262
<ogr:floatval>3.33</ogr:floatval>
63-
</ogr:polys_split_with_lines>
63+
</ogr:split_polys_with_lines>
6464
</gml:featureMember>
6565
</ogr:FeatureCollection>

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,3 +1595,45 @@ tests:
15951595
OUTPUT:
15961596
name: expected/create_points.gml
15971597
type: vector
1598+
1599+
- algorithm: qgis:splitwithlines
1600+
name: Split lines with lines (new alg)
1601+
params:
1602+
INPUT_A:
1603+
name: lines.gml
1604+
type: vector
1605+
INPUT_B:
1606+
name: custom/lines2.gml
1607+
type: vector
1608+
results:
1609+
OUTPUT:
1610+
name: expected/split_lines_with_lines.gml
1611+
type: vector
1612+
1613+
- algorithm: qgis:splitwithlines
1614+
name: Split poly with lines
1615+
params:
1616+
INPUT_A:
1617+
name: polys.gml
1618+
type: vector
1619+
INPUT_B:
1620+
name: lines.gml
1621+
type: vector
1622+
results:
1623+
OUTPUT:
1624+
name: expected/split_polys_with_lines.gml
1625+
type: vector
1626+
1627+
- algorithm: qgis:splitwithlines
1628+
name: Split lines with same lines
1629+
params:
1630+
INPUT_A:
1631+
name: lines.gml
1632+
type: vector
1633+
INPUT_B:
1634+
name: lines.gml
1635+
type: vector
1636+
results:
1637+
OUTPUT:
1638+
name: expected/split_lines_with_lines_same.gml
1639+
type: vector

0 commit comments

Comments
 (0)
Please sign in to comment.