Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
nyalldawson committed Mar 12, 2018
1 parent 41d16a8 commit 251e1d5
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 2 deletions.
19 changes: 17 additions & 2 deletions python/plugins/processing/algs/qgis/ExportGeometryInfo.py
Expand Up @@ -26,6 +26,7 @@
__revision__ = '$Format:%H$'

import os
import math

from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
Expand All @@ -35,6 +36,7 @@
QgsField,
QgsFields,
QgsWkbTypes,
QgsPointXY,
QgsFeatureSink,
QgsDistanceArea,
QgsProcessingUtils,
Expand All @@ -58,7 +60,7 @@ def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'export_geometry.png'))

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

def group(self):
return self.tr('Vector geometry')
Expand Down Expand Up @@ -101,6 +103,9 @@ def processAlgorithm(self, parameters, context, feedback):
new_fields.append(QgsField('perimeter', QVariant.Double))
elif QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.LineGeometry:
new_fields.append(QgsField('length', QVariant.Double))
if not QgsWkbTypes.isMultiType(source.wkbType()):
new_fields.append(QgsField('straightdis', QVariant.Double))
new_fields.append(QgsField('sinuosity', QVariant.Double))
else:
new_fields.append(QgsField('xcoord', QVariant.Double))
new_fields.append(QgsField('ycoord', QVariant.Double))
Expand Down Expand Up @@ -181,7 +186,17 @@ def point_attributes(self, geometry):
return attrs

def line_attributes(self, geometry):
return [self.distance_area.measureLength(geometry)]
if geometry.isMultipart():
return [self.distance_area.measureLength(geometry)]
else:
curve = geometry.constGet()
p1 = curve.startPoint()
p2 = curve.endPoint()
straight_distance = self.distance_area.measureLine(QgsPointXY(p1), QgsPointXY(p2))
sinuosity = curve.sinuosity()
if math.isnan(sinuosity):
sinuosity = NULL
return [self.distance_area.measureLength(geometry), straight_distance, sinuosity]

def polygon_attributes(self, geometry):
area = self.distance_area.measureArea(geometry)
Expand Down
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ export_line_info.xsd"
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:export_line_info 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:length>6.82842712474619</ogr:length>
<ogr:straightdis>5.8309518948453</ogr:straightdis>
<ogr:sinuosity>1.1710655906427</ogr:sinuosity>
</ogr:export_line_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_line_info fid="lines.1">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
<ogr:length>2</ogr:length>
<ogr:straightdis>2</ogr:straightdis>
<ogr:sinuosity>1</ogr:sinuosity>
</ogr:export_line_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_line_info 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:length>4</ogr:length>
<ogr:straightdis>3.16227766016838</ogr:straightdis>
<ogr:sinuosity>1.26491106406735</ogr:sinuosity>
</ogr:export_line_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_line_info fid="lines.3">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
<ogr:length>2</ogr:length>
<ogr:straightdis>2</ogr:straightdis>
<ogr:sinuosity>1</ogr:sinuosity>
</ogr:export_line_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_line_info fid="lines.4">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-3 10,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
<ogr:length>3</ogr:length>
<ogr:straightdis>3</ogr:straightdis>
<ogr:sinuosity>1</ogr:sinuosity>
</ogr:export_line_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_line_info fid="lines.5">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6,-3 10,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
<ogr:length>5.65685424949238</ogr:length>
<ogr:straightdis>5.65685424949238</ogr:straightdis>
<ogr:sinuosity>1</ogr:sinuosity>
</ogr:export_line_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_line_info fid="lines.6">
</ogr:export_line_info>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
<xs:complexType name="FeatureCollectionType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureCollectionType">
<xs:attribute name="lockId" type="xs:string" use="optional"/>
<xs:attribute name="scope" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="export_line_info" type="ogr:export_line_info_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="export_line_info_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geometryProperty" type="gml:LineStringPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="length" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="straightdis" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="sinuosity" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ export_multiline_info.xsd"
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>-1</gml:Y></gml:coord>
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:export_multiline_info fid="lines.1">
<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>
<ogr:length>2</ogr:length>
</ogr:export_multiline_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_multiline_info fid="lines.2">
<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>
<ogr:length>3.41498595862145</ogr:length>
</ogr:export_multiline_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_multiline_info fid="lines.3">
</ogr:export_multiline_info>
</gml:featureMember>
<gml:featureMember>
<ogr:export_multiline_info fid="lines.4">
<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>
<ogr:length>9.09717929727474</ogr:length>
</ogr:export_multiline_info>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
<xs:complexType name="FeatureCollectionType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureCollectionType">
<xs:attribute name="lockId" type="xs:string" use="optional"/>
<xs:attribute name="scope" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="export_multiline_info" type="ogr:export_multiline_info_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="export_multiline_info_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geometryProperty" type="gml:MultiLineStringPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="length" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
24 changes: 24 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -1455,6 +1455,30 @@ tests:
name: expected/add_geometry_pointz.gml
type: vector

- algorithm: qgis:exportaddgeometrycolumns
name: Export line info
params:
CALC_METHOD: 0
INPUT:
name: lines.gml
type: vector
results:
OUTPUT:
name: expected/export_line_info.gml
type: vector

- algorithm: qgis:exportaddgeometrycolumns
name: Export multiline info
params:
CALC_METHOD: 0
INPUT:
name: multilines.gml
type: vector
results:
OUTPUT:
name: expected/export_multiline_info.gml
type: vector

- algorithm: qgis:texttofloat
name: Text to float
params:
Expand Down

0 comments on commit 251e1d5

Please sign in to comment.