Skip to content

Commit

Permalink
Small tweaks to processing split algorithm, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 23, 2016
1 parent 0e2ef06 commit 38a0ea0
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 38 deletions.
24 changes: 12 additions & 12 deletions python/plugins/processing/algs/qgis/SplitWithLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def defineCharacteristics(self):
self.group, self.i18n_group = self.trAlgorithm('Vector overlay tools')
self.addParameter(ParameterVector(self.INPUT_A,
self.tr('Input layer, single geometries only'), [dataobjects.TYPE_VECTOR_POLYGON,
dataobjects.TYPE_VECTOR_LINE]))
dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(ParameterVector(self.INPUT_B,
self.tr('Split layer'), [dataobjects.TYPE_VECTOR_LINE]))

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

engine = QgsGeometry.createGeometryEngine(inGeom.geometry())
engine.prepareGeometry()

for i in lines:
try:
splitGeom = splitGeoms[i]
Expand All @@ -112,9 +115,6 @@ def processAlgorithm(self, progress):
if inFeatA.id() == i:
continue

engine = QgsGeometry.createGeometryEngine(inGeom.geometry())
engine.prepareGeometry()

if engine.intersects(splitGeom.geometry()):
splittingLines.append(splitGeom)

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

split_geom_engine = QgsGeometry.createGeometryEngine(splitGeom.geometry())
split_geom_engine.prepareGeometry()

while len(inGeoms) > 0:
inGeom = inGeoms.pop()
engine = QgsGeometry.createGeometryEngine(inGeom.geometry())
engine.prepareGeometry()
inPoints = vector.extractPoints(inGeom)

if engine.intersects(splitGeom.geometry()):
if split_geom_engine.intersects(inGeom.geometry()):
inPoints = vector.extractPoints(inGeom)
if splitterPList == None:
splitterPList = vector.extractPoints(splitGeom)

Expand All @@ -143,7 +144,6 @@ def processAlgorithm(self, progress):
# splitGeometry: If there are several intersections
# between geometry and splitLine, only the first one is considered.
if result == 0: # split occurred

if inPoints == vector.extractPoints(inGeom):
# bug in splitGeometry: sometimes it returns 0 but
# the geometry is unchanged
Expand All @@ -166,12 +166,12 @@ def processAlgorithm(self, progress):
passed = True

if QgsWkbTypes.geometryType( aGeom.wkbType() ) == QgsWkbTypes.LineGeometry \
and not QgsWkbTypes.isMultiType( aGeom.wkbType() ):
and not QgsWkbTypes.isMultiType(aGeom.wkbType()):
passed = len(aGeom.asPolyline()) > 2

if not passed:
passed = (len(aGeom.asPolyline()) == 2 and
aGeom.asPolyline()[0] != aGeom.asPolyline()[1])
aGeom.asPolyline()[0] != aGeom.asPolyline()[1])
# sometimes splitting results in lines of zero length

if passed:
Expand All @@ -182,6 +182,6 @@ def processAlgorithm(self, progress):

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

del writer
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/TestTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def createTest(text):
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'

params[param.name] = p
if isinstance(param, ParameterTable):
elif isinstance(param, ParameterTable):
schema, filepath = extractSchemaPath(token)
p = {
'type': 'table',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>split_lines_with_lines</Name>
<ElementPath>split_lines_with_lines</ElementPath>
<!--LINESTRING-->
<GeometryType>2</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>11</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>11.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>5.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>-1</gml:X><gml:Y>-3</gml:Y></gml:coord>
<gml:coord><gml:X>11</gml:X><gml:Y>5</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.0">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,2 9,2 9,3 11,5</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.1">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.2">
<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>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.2">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2,0 2.0,1.86331771490359</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.3">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>4.21601489757914,1.0 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.3">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 4.21601489757914,1.0</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.4">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>8.5655671605538,-3.0 10,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.4">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-3 8.5655671605538,-3.0</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.5">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6.91202704418487,-2.08797295581513 10,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.5">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,-3 6.91202704418487,-2.08797295581513</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines fid="lines.6">
</ogr:split_lines_with_lines>
</gml:featureMember>
</ogr:FeatureCollection>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>split_lines_with_lines_same</Name>
<ElementPath>split_lines_with_lines_same</ElementPath>
<!--LINESTRING-->
<GeometryType>2</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>7</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>11.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>5.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>-1</gml:X><gml:Y>-3</gml:Y></gml:coord>
<gml:coord><gml:X>11</gml:X><gml:Y>5</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:split_lines_with_lines_same fid="lines.0">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,2 9,2 9,3 11,5</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines_same>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines_same fid="lines.1">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines_same>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines_same fid="lines.2">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2,0 2,2 3,2 3,3</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines_same>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines_same fid="lines.3">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines_same>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines_same fid="lines.4">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-3 10,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines_same>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines_same fid="lines.5">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,-3 10,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:split_lines_with_lines_same>
</gml:featureMember>
<gml:featureMember>
<ogr:split_lines_with_lines_same fid="lines.6">
</ogr:split_lines_with_lines_same>
</gml:featureMember>
</ogr:FeatureCollection>
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>polys_split_with_lines</Name>
<ElementPath>polys_split_with_lines</ElementPath>
<Name>split_polys_with_lines</Name>
<ElementPath>split_polys_with_lines</ElementPath>
<!--POLYGON-->
<GeometryType>3</GeometryType>
<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>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>7</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>10.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>6.00000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>fid</Name>
<ElementPath>fid</ElementPath>
<Type>String</Type>
<Width>7</Width>
</PropertyDefn>
<PropertyDefn>
<Name>name</Name>
<ElementPath>name</ElementPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,54 @@
</gml:boundedBy>

<gml:featureMember>
<ogr:polys_split_with_lines fid="polys.0">
<ogr:split_polys_with_lines fid="polys.0">
<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>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:polys_split_with_lines>
</ogr:split_polys_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_split_with_lines fid="polys.1">
<ogr:split_polys_with_lines fid="polys.1">
<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>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0</ogr:floatval>
</ogr:polys_split_with_lines>
</ogr:split_polys_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_split_with_lines fid="polys.2">
<ogr:split_polys_with_lines fid="polys.2">
<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>
<ogr:name>bbaaa</ogr:name>
<ogr:floatval>0.123</ogr:floatval>
</ogr:polys_split_with_lines>
</ogr:split_polys_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_split_with_lines fid="polys.3">
<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>
<ogr:split_polys_with_lines fid="polys.3">
<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>
<ogr:name>ASDF</ogr:name>
<ogr:intval>0</ogr:intval>
</ogr:polys_split_with_lines>
</ogr:split_polys_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_split_with_lines fid="polys.3">
<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>
<ogr:split_polys_with_lines fid="polys.3">
<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>
<ogr:name>ASDF</ogr:name>
<ogr:intval>0</ogr:intval>
</ogr:polys_split_with_lines>
</ogr:split_polys_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_split_with_lines fid="polys.4">
<ogr:split_polys_with_lines fid="polys.4">
<ogr:intval>120</ogr:intval>
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:polys_split_with_lines>
</ogr:split_polys_with_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_split_with_lines fid="polys.5">
<ogr:split_polys_with_lines fid="polys.5">
<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>
<ogr:name>elim</ogr:name>
<ogr:intval>2</ogr:intval>
<ogr:floatval>3.33</ogr:floatval>
</ogr:polys_split_with_lines>
</ogr:split_polys_with_lines>
</gml:featureMember>
</ogr:FeatureCollection>
42 changes: 42 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1595,3 +1595,45 @@ tests:
OUTPUT:
name: expected/create_points.gml
type: vector

- algorithm: qgis:splitwithlines
name: Split lines with lines (new alg)
params:
INPUT_A:
name: lines.gml
type: vector
INPUT_B:
name: custom/lines2.gml
type: vector
results:
OUTPUT:
name: expected/split_lines_with_lines.gml
type: vector

- algorithm: qgis:splitwithlines
name: Split poly with lines
params:
INPUT_A:
name: polys.gml
type: vector
INPUT_B:
name: lines.gml
type: vector
results:
OUTPUT:
name: expected/split_polys_with_lines.gml
type: vector

- algorithm: qgis:splitwithlines
name: Split lines with same lines
params:
INPUT_A:
name: lines.gml
type: vector
INPUT_B:
name: lines.gml
type: vector
results:
OUTPUT:
name: expected/split_lines_with_lines_same.gml
type: vector

0 comments on commit 38a0ea0

Please sign in to comment.