Skip to content

Commit 251e1d5

Browse files
committedMar 12, 2018
[FEATURE][processing] Add line sinuosity to "Export Geometry Info"
Adds line sinuosity and straight distance to the stats calculated by "Export Geometry Info" Fixes #12376
1 parent 41d16a8 commit 251e1d5

File tree

6 files changed

+213
-2
lines changed

6 files changed

+213
-2
lines changed
 

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
__revision__ = '$Format:%H$'
2727

2828
import os
29+
import math
2930

3031
from qgis.PyQt.QtGui import QIcon
3132
from qgis.PyQt.QtCore import QVariant
@@ -35,6 +36,7 @@
3536
QgsField,
3637
QgsFields,
3738
QgsWkbTypes,
39+
QgsPointXY,
3840
QgsFeatureSink,
3941
QgsDistanceArea,
4042
QgsProcessingUtils,
@@ -58,7 +60,7 @@ def icon(self):
5860
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'export_geometry.png'))
5961

6062
def tags(self):
61-
return self.tr('export,add,information,measurements,areas,lengths,perimeters,latitudes,longitudes,x,y,z,extract,points,lines,polygons').split(',')
63+
return self.tr('export,add,information,measurements,areas,lengths,perimeters,latitudes,longitudes,x,y,z,extract,points,lines,polygons,sinuosity').split(',')
6264

6365
def group(self):
6466
return self.tr('Vector geometry')
@@ -101,6 +103,9 @@ def processAlgorithm(self, parameters, context, feedback):
101103
new_fields.append(QgsField('perimeter', QVariant.Double))
102104
elif QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.LineGeometry:
103105
new_fields.append(QgsField('length', QVariant.Double))
106+
if not QgsWkbTypes.isMultiType(source.wkbType()):
107+
new_fields.append(QgsField('straightdis', QVariant.Double))
108+
new_fields.append(QgsField('sinuosity', QVariant.Double))
104109
else:
105110
new_fields.append(QgsField('xcoord', QVariant.Double))
106111
new_fields.append(QgsField('ycoord', QVariant.Double))
@@ -181,7 +186,17 @@ def point_attributes(self, geometry):
181186
return attrs
182187

183188
def line_attributes(self, geometry):
184-
return [self.distance_area.measureLength(geometry)]
189+
if geometry.isMultipart():
190+
return [self.distance_area.measureLength(geometry)]
191+
else:
192+
curve = geometry.constGet()
193+
p1 = curve.startPoint()
194+
p2 = curve.endPoint()
195+
straight_distance = self.distance_area.measureLine(QgsPointXY(p1), QgsPointXY(p2))
196+
sinuosity = curve.sinuosity()
197+
if math.isnan(sinuosity):
198+
sinuosity = NULL
199+
return [self.distance_area.measureLength(geometry), straight_distance, sinuosity]
185200

186201
def polygon_attributes(self, geometry):
187202
area = self.distance_area.measureArea(geometry)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/ export_line_info.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>-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:export_line_info 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:length>6.82842712474619</ogr:length>
18+
<ogr:straightdis>5.8309518948453</ogr:straightdis>
19+
<ogr:sinuosity>1.1710655906427</ogr:sinuosity>
20+
</ogr:export_line_info>
21+
</gml:featureMember>
22+
<gml:featureMember>
23+
<ogr:export_line_info fid="lines.1">
24+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
25+
<ogr:length>2</ogr:length>
26+
<ogr:straightdis>2</ogr:straightdis>
27+
<ogr:sinuosity>1</ogr:sinuosity>
28+
</ogr:export_line_info>
29+
</gml:featureMember>
30+
<gml:featureMember>
31+
<ogr:export_line_info fid="lines.2">
32+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2,0 2,2 3,2 3,3</gml:coordinates></gml:LineString></ogr:geometryProperty>
33+
<ogr:length>4</ogr:length>
34+
<ogr:straightdis>3.16227766016838</ogr:straightdis>
35+
<ogr:sinuosity>1.26491106406735</ogr:sinuosity>
36+
</ogr:export_line_info>
37+
</gml:featureMember>
38+
<gml:featureMember>
39+
<ogr:export_line_info fid="lines.3">
40+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
41+
<ogr:length>2</ogr:length>
42+
<ogr:straightdis>2</ogr:straightdis>
43+
<ogr:sinuosity>1</ogr:sinuosity>
44+
</ogr:export_line_info>
45+
</gml:featureMember>
46+
<gml:featureMember>
47+
<ogr:export_line_info fid="lines.4">
48+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-3 10,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
49+
<ogr:length>3</ogr:length>
50+
<ogr:straightdis>3</ogr:straightdis>
51+
<ogr:sinuosity>1</ogr:sinuosity>
52+
</ogr:export_line_info>
53+
</gml:featureMember>
54+
<gml:featureMember>
55+
<ogr:export_line_info fid="lines.5">
56+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,-3 10,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
57+
<ogr:length>5.65685424949238</ogr:length>
58+
<ogr:straightdis>5.65685424949238</ogr:straightdis>
59+
<ogr:sinuosity>1</ogr:sinuosity>
60+
</ogr:export_line_info>
61+
</gml:featureMember>
62+
<gml:featureMember>
63+
<ogr:export_line_info fid="lines.6">
64+
</ogr:export_line_info>
65+
</gml:featureMember>
66+
</ogr:FeatureCollection>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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="export_line_info" type="ogr:export_line_info_Type" substitutionGroup="gml:_Feature"/>
14+
<xs:complexType name="export_line_info_Type">
15+
<xs:complexContent>
16+
<xs:extension base="gml:AbstractFeatureType">
17+
<xs:sequence>
18+
<xs:element name="geometryProperty" type="gml:LineStringPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
19+
<xs:element name="length" nillable="true" minOccurs="0" maxOccurs="1">
20+
<xs:simpleType>
21+
<xs:restriction base="xs:decimal">
22+
</xs:restriction>
23+
</xs:simpleType>
24+
</xs:element>
25+
<xs:element name="straightdis" nillable="true" minOccurs="0" maxOccurs="1">
26+
<xs:simpleType>
27+
<xs:restriction base="xs:decimal">
28+
</xs:restriction>
29+
</xs:simpleType>
30+
</xs:element>
31+
<xs:element name="sinuosity" nillable="true" minOccurs="0" maxOccurs="1">
32+
<xs:simpleType>
33+
<xs:restriction base="xs:decimal">
34+
</xs:restriction>
35+
</xs:simpleType>
36+
</xs:element>
37+
</xs:sequence>
38+
</xs:extension>
39+
</xs:complexContent>
40+
</xs:complexType>
41+
</xs:schema>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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/ export_multiline_info.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>-1</gml:Y></gml:coord>
10+
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:export_multiline_info fid="lines.1">
16+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
17+
<ogr:length>2</ogr:length>
18+
</ogr:export_multiline_info>
19+
</gml:featureMember>
20+
<gml:featureMember>
21+
<ogr:export_multiline_info fid="lines.2">
22+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>5.02418426103647,2.4147792706334 5,1</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
23+
<ogr:length>3.41498595862145</ogr:length>
24+
</ogr:export_multiline_info>
25+
</gml:featureMember>
26+
<gml:featureMember>
27+
<ogr:export_multiline_info fid="lines.3">
28+
</ogr:export_multiline_info>
29+
</gml:featureMember>
30+
<gml:featureMember>
31+
<ogr:export_multiline_info fid="lines.4">
32+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>2,0 2,2 3,2 3,3</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>2.94433781190019,4.04721689059501 5.4595009596929,4.11976967370441</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>3,3 5.58042226487524,2.9468330134357</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
33+
<ogr:length>9.09717929727474</ogr:length>
34+
</ogr:export_multiline_info>
35+
</gml:featureMember>
36+
</ogr:FeatureCollection>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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="export_multiline_info" type="ogr:export_multiline_info_Type" substitutionGroup="gml:_Feature"/>
14+
<xs:complexType name="export_multiline_info_Type">
15+
<xs:complexContent>
16+
<xs:extension base="gml:AbstractFeatureType">
17+
<xs:sequence>
18+
<xs:element name="geometryProperty" type="gml:MultiLineStringPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
19+
<xs:element name="length" nillable="true" minOccurs="0" maxOccurs="1">
20+
<xs:simpleType>
21+
<xs:restriction base="xs:decimal">
22+
</xs:restriction>
23+
</xs:simpleType>
24+
</xs:element>
25+
</xs:sequence>
26+
</xs:extension>
27+
</xs:complexContent>
28+
</xs:complexType>
29+
</xs:schema>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,30 @@ tests:
14551455
name: expected/add_geometry_pointz.gml
14561456
type: vector
14571457

1458+
- algorithm: qgis:exportaddgeometrycolumns
1459+
name: Export line info
1460+
params:
1461+
CALC_METHOD: 0
1462+
INPUT:
1463+
name: lines.gml
1464+
type: vector
1465+
results:
1466+
OUTPUT:
1467+
name: expected/export_line_info.gml
1468+
type: vector
1469+
1470+
- algorithm: qgis:exportaddgeometrycolumns
1471+
name: Export multiline info
1472+
params:
1473+
CALC_METHOD: 0
1474+
INPUT:
1475+
name: multilines.gml
1476+
type: vector
1477+
results:
1478+
OUTPUT:
1479+
name: expected/export_multiline_info.gml
1480+
type: vector
1481+
14581482
- algorithm: qgis:texttofloat
14591483
name: Text to float
14601484
params:

0 commit comments

Comments
 (0)
Please sign in to comment.