Skip to content

Commit 4f829fd

Browse files
committed
Port Intersection algorithm to C++, cleanups and tests
Finally starting a suite of unit tests for overlay algorithms: - overlay1 - layers that cover various basic overlay situations - overlay2 - layers where one input has self-intersecting polygons - overlay3 - layers where intersections return different geometry types
1 parent 3400199 commit 4f829fd

15 files changed

+547
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "FeatureCollection",
3+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
4+
"features": [
5+
{ "type": "Feature", "properties": { "id_a": "A1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 1.0, 3.0 ], [ 2.0, 3.0 ], [ 2.0, 10.0 ], [ 8.0, 10.0 ], [ 8.0, 11.0 ], [ 1.0, 11.0 ], [ 1.0, 3.0 ] ] ] } },
6+
{ "type": "Feature", "properties": { "id_a": "A4" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 3.0, 3.0 ], [ 3.0, 4.0 ], [ 4.0, 4.0 ], [ 4.0, 3.0 ], [ 3.0, 3.0 ] ] ] } },
7+
{ "type": "Feature", "properties": { "id_a": "A2" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 3.0, 5.0 ], [ 6.0, 5.0 ], [ 6.0, 6.0 ], [ 3.0, 6.0 ], [ 3.0, 5.0 ] ] ] } },
8+
{ "type": "Feature", "properties": { "id_a": "A3" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 5.0, 7.0 ], [ 9.0, 7.0 ], [ 9.0, 8.0 ], [ 5.0, 8.0 ], [ 5.0, 7.0 ] ] ] } }
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "FeatureCollection",
3+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
4+
"features": [
5+
{ "type": "Feature", "properties": { "id_b": "B1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 1.0, 1.0 ], [ 8.0, 1.0 ], [ 8.0, 9.0 ], [ 7.0, 9.0 ], [ 7.0, 2.0 ], [ 1.0, 2.0 ], [ 1.0, 1.0 ] ] ] } },
6+
{ "type": "Feature", "properties": { "id_b": "B4" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 5.0, 3.0 ], [ 6.0, 3.0 ], [ 6.0, 4.0 ], [ 5.0, 4.0 ], [ 5.0, 3.0 ] ] ] } },
7+
{ "type": "Feature", "properties": { "id_b": "B2" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.0, 5.0 ], [ 4.0, 5.0 ], [ 4.0, 6.0 ], [ 0.0, 6.0 ], [ 0.0, 5.0 ] ] ] } },
8+
{ "type": "Feature", "properties": { "id_b": "B3" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 3.0, 7.0 ], [ 6.0, 7.0 ], [ 6.0, 8.0 ], [ 3.0, 8.0 ], [ 3.0, 7.0 ] ] ] } }
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "FeatureCollection",
3+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
4+
"features": [
5+
{ "type": "Feature", "properties": { "id_a": "A1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 3.0, 3.0 ], [ 3.0, 7.0 ], [ 6.0, 7.0 ], [ 6.0, 3.0 ], [ 3.0, 3.0 ] ] ] } }
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "FeatureCollection",
3+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
4+
"features": [
5+
{ "type": "Feature", "properties": { "id_b": "B1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2.0, 4.0 ], [ 5.0, 4.0 ], [ 5.0, 6.0 ], [ 2.0, 6.0 ], [ 2.0, 4.0 ] ] ] } },
6+
{ "type": "Feature", "properties": { "id_b": "B2" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.0, 4.0 ], [ 4.0, 4.0 ], [ 4.0, 6.0 ], [ 7.0, 6.0 ], [ 7.0, 4.0 ] ] ] } }
7+
]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "FeatureCollection",
3+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
4+
"features": [
5+
{ "type": "Feature", "properties": { "id_a": "A1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2.0, 2.0 ], [ 7.0, 2.0 ], [ 7.0, 3.0 ], [ 2.0, 3.0 ], [ 2.0, 2.0 ] ] ] } }
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "FeatureCollection",
3+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
4+
"features": [
5+
{ "type": "Feature", "properties": { "id_b": "B1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2.0, 1.0 ], [ 4.0, 1.0 ], [ 4.0, 2.0 ], [ 3.0, 2.0 ], [ 3.0, 3.0 ], [ 2.0, 3.0 ], [ 2.0, 1.0 ] ] ] } },
6+
{ "type": "Feature", "properties": { "id_b": "B2" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 1.0, 3.0 ], [ 2.0, 3.0 ], [ 2.0, 4.0 ], [ 1.0, 4.0 ], [ 1.0, 3.0 ] ] ] } },
7+
{ "type": "Feature", "properties": { "id_b": "B3" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.0, 1.0 ], [ 8.0, 1.0 ], [ 8.0, 4.0 ], [ 7.0, 4.0 ], [ 7.0, 1.0 ] ] ] } },
8+
{ "type": "Feature", "properties": { "id_b": "B4" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 5.0, 2.0 ], [ 6.0, 2.0 ], [ 6.0, 5.0 ], [ 4.0, 5.0 ], [ 4.0, 3.0 ], [ 5.0, 4.0 ], [ 5.0, 2.0 ] ] ] } }
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ogr.maptools.org/ intersection1.xsd"
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>5</gml:Y></gml:coord>
10+
<gml:coord><gml:X>8</gml:X><gml:Y>8</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:intersection1 fid="intersection1.0">
16+
<ogr:geometryProperty><gml:MultiPolygon srsName="EPSG:3857"><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,6 2,5 1,5 1,6 2,6</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon></ogr:geometryProperty>
17+
<ogr:id_a>A1</ogr:id_a>
18+
<ogr:id_b>B2</ogr:id_b>
19+
</ogr:intersection1>
20+
</gml:featureMember>
21+
<gml:featureMember>
22+
<ogr:intersection1 fid="intersection1.1">
23+
<ogr:geometryProperty><gml:MultiPolygon srsName="EPSG:3857"><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>4,5 3,5 3,6 4,6 4,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon></ogr:geometryProperty>
24+
<ogr:id_a>A2</ogr:id_a>
25+
<ogr:id_b>B2</ogr:id_b>
26+
</ogr:intersection1>
27+
</gml:featureMember>
28+
<gml:featureMember>
29+
<ogr:intersection1 fid="intersection1.2">
30+
<ogr:geometryProperty><gml:MultiPolygon srsName="EPSG:3857"><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>8,7 7,7 7,8 8,8 8,7</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon></ogr:geometryProperty>
31+
<ogr:id_a>A3</ogr:id_a>
32+
<ogr:id_b>B1</ogr:id_b>
33+
</ogr:intersection1>
34+
</gml:featureMember>
35+
<gml:featureMember>
36+
<ogr:intersection1 fid="intersection1.3">
37+
<ogr:geometryProperty><gml:MultiPolygon srsName="EPSG:3857"><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,7 5,7 5,8 6,8 6,7</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon></ogr:geometryProperty>
38+
<ogr:id_a>A3</ogr:id_a>
39+
<ogr:id_b>B3</ogr:id_b>
40+
</ogr:intersection1>
41+
</gml:featureMember>
42+
</ogr:FeatureCollection>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
3+
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
4+
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
5+
<xs:complexType name="FeatureCollectionType">
6+
<xs:complexContent>
7+
<xs:extension base="gml:AbstractFeatureCollectionType">
8+
<xs:attribute name="lockId" type="xs:string" use="optional"/>
9+
<xs:attribute name="scope" type="xs:string" use="optional"/>
10+
</xs:extension>
11+
</xs:complexContent>
12+
</xs:complexType>
13+
<xs:element name="intersection1" type="ogr:intersection1_Type" substitutionGroup="gml:_Feature"/>
14+
<xs:complexType name="intersection1_Type">
15+
<xs:complexContent>
16+
<xs:extension base="gml:AbstractFeatureType">
17+
<xs:sequence>
18+
<xs:element name="geometryProperty" type="gml:MultiPolygonPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
19+
<xs:element name="id_a" nillable="true" minOccurs="0" maxOccurs="1">
20+
<xs:simpleType>
21+
<xs:restriction base="xs:string">
22+
<xs:maxLength value="255"/>
23+
</xs:restriction>
24+
</xs:simpleType>
25+
</xs:element>
26+
<xs:element name="id_b" nillable="true" minOccurs="0" maxOccurs="1">
27+
<xs:simpleType>
28+
<xs:restriction base="xs:string">
29+
<xs:maxLength value="255"/>
30+
</xs:restriction>
31+
</xs:simpleType>
32+
</xs:element>
33+
</xs:sequence>
34+
</xs:extension>
35+
</xs:complexContent>
36+
</xs:complexType>
37+
</xs:schema>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ogr.maptools.org/ intersection3.xsd"
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>2</gml:X><gml:Y>2</gml:Y></gml:coord>
10+
<gml:coord><gml:X>6</gml:X><gml:Y>3</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:intersection3 fid="intersection3.0">
16+
<ogr:geometryProperty><gml:MultiPolygon srsName="EPSG:3857"><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>3,2 2,2 2,3 3,3 3,2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon></ogr:geometryProperty>
17+
<ogr:id_a>A1</ogr:id_a>
18+
<ogr:id_b>B1</ogr:id_b>
19+
</ogr:intersection3>
20+
</gml:featureMember>
21+
<gml:featureMember>
22+
<ogr:intersection3 fid="intersection3.1">
23+
<ogr:geometryProperty><gml:MultiPolygon srsName="EPSG:3857"><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,2 5,2 5,3 6,3 6,2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon></ogr:geometryProperty>
24+
<ogr:id_a>A1</ogr:id_a>
25+
<ogr:id_b>B4</ogr:id_b>
26+
</ogr:intersection3>
27+
</gml:featureMember>
28+
</ogr:FeatureCollection>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
3+
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
4+
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
5+
<xs:complexType name="FeatureCollectionType">
6+
<xs:complexContent>
7+
<xs:extension base="gml:AbstractFeatureCollectionType">
8+
<xs:attribute name="lockId" type="xs:string" use="optional"/>
9+
<xs:attribute name="scope" type="xs:string" use="optional"/>
10+
</xs:extension>
11+
</xs:complexContent>
12+
</xs:complexType>
13+
<xs:element name="intersection3" type="ogr:intersection3_Type" substitutionGroup="gml:_Feature"/>
14+
<xs:complexType name="intersection3_Type">
15+
<xs:complexContent>
16+
<xs:extension base="gml:AbstractFeatureType">
17+
<xs:sequence>
18+
<xs:element name="geometryProperty" type="gml:MultiPolygonPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
19+
<xs:element name="id_a" nillable="true" minOccurs="0" maxOccurs="1">
20+
<xs:simpleType>
21+
<xs:restriction base="xs:string">
22+
<xs:maxLength value="255"/>
23+
</xs:restriction>
24+
</xs:simpleType>
25+
</xs:element>
26+
<xs:element name="id_b" nillable="true" minOccurs="0" maxOccurs="1">
27+
<xs:simpleType>
28+
<xs:restriction base="xs:string">
29+
<xs:maxLength value="255"/>
30+
</xs:restriction>
31+
</xs:simpleType>
32+
</xs:element>
33+
</xs:sequence>
34+
</xs:extension>
35+
</xs:complexContent>
36+
</xs:complexType>
37+
</xs:schema>

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

+36
Original file line numberDiff line numberDiff line change
@@ -5191,4 +5191,40 @@ tests:
51915191
name: expected/filter_points_big.gml
51925192
type: vector
51935193

5194+
- algorithm: native:intersection
5195+
name: Test Intersection (basic)
5196+
params:
5197+
INPUT:
5198+
name: custom/overlay1_a.geojson
5199+
type: vector
5200+
OVERLAY:
5201+
name: custom/overlay1_b.geojson
5202+
type: vector
5203+
results:
5204+
OUTPUT:
5205+
name: expected/intersection1.gml
5206+
type: vector
5207+
pk: [id_a, id_b]
5208+
compare:
5209+
fields:
5210+
fid: skip
5211+
5212+
- algorithm: native:intersection
5213+
name: Test Intersection (geom types)
5214+
params:
5215+
INPUT:
5216+
name: custom/overlay3_a.geojson
5217+
type: vector
5218+
OVERLAY:
5219+
name: custom/overlay3_b.geojson
5220+
type: vector
5221+
results:
5222+
OUTPUT:
5223+
name: expected/intersection3.gml
5224+
type: vector
5225+
pk: [id_a, id_b]
5226+
compare:
5227+
fields:
5228+
fid: skip
5229+
51945230
# See ../README.md for a description of the file format

src/analysis/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ SET(QGIS_ANALYSIS_SRCS
4040
processing/qgsalgorithmfiledownloader.cpp
4141
processing/qgsalgorithmfilter.cpp
4242
processing/qgsalgorithmfixgeometries.cpp
43+
processing/qgsalgorithmintersection.cpp
4344
processing/qgsalgorithmjoinbyattribute.cpp
4445
processing/qgsalgorithmjoinwithlines.cpp
4546
processing/qgsalgorithmimportphotos.cpp

0 commit comments

Comments
 (0)