Skip to content

Commit

Permalink
[processing] Add geometry info should add z/m values (fix #14659)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 10, 2016
1 parent 49459c8 commit 90aebd1
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 8 deletions.
17 changes: 17 additions & 0 deletions python/plugins/processing/algs/qgis/ExportGeometryInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def processAlgorithm(self, progress):
geometryType = layer.geometryType()
fields = layer.fields()

export_z = False
export_m = False
if geometryType == QgsWkbTypes.PolygonGeometry:
areaName = vector.createUniqueFieldName('area', fields)
fields.append(QgsField(areaName, QVariant.Double))
Expand All @@ -87,6 +89,14 @@ def processAlgorithm(self, progress):
fields.append(QgsField(xName, QVariant.Double))
yName = vector.createUniqueFieldName('ycoord', fields)
fields.append(QgsField(yName, QVariant.Double))
if QgsWkbTypes.hasZ(layer.wkbType()):
export_z = True
zName = vector.createUniqueFieldName('zcoord', fields)
fields.append(QgsField(zName, QVariant.Double))
if QgsWkbTypes.hasM(layer.wkbType()):
export_m = True
zName = vector.createUniqueFieldName('mvalue', fields)
fields.append(QgsField(zName, QVariant.Double))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields.toList(), layer.wkbType(), layer.crs())
Expand Down Expand Up @@ -130,6 +140,13 @@ def processAlgorithm(self, progress):
attrs.append(attr1)
if attr2 is not None:
attrs.append(attr2)

# add point z/m
if export_z:
attrs.append(inGeom.geometry().z())
if export_m:
attrs.append(inGeom.geometry().m())

outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>add_geometry_pointz</Name>
<ElementPath>add_geometry_pointz</ElementPath>
<GeometryType>-2147483647</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>9</FeatureCount>
<ExtentXMin>0.00000</ExtentXMin>
<ExtentXMax>8.00000</ExtentXMax>
<ExtentYMin>-5.00000</ExtentYMin>
<ExtentYMax>3.00000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>xcoord</Name>
<ElementPath>xcoord</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>ycoord</Name>
<ElementPath>ycoord</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>zcoord</Name>
<ElementPath>zcoord</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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>0</gml:X><gml:Y>-5</gml:Y><gml:Z>0</gml:Z></gml:coord>
<gml:coord><gml:X>8</gml:X><gml:Y>3</gml:Y><gml:Z>7</gml:Z></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:add_geometry_pointz fid="points.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1,3</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>1</ogr:xcoord>
<ogr:ycoord>1</ogr:ycoord>
<ogr:zcoord>3</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3,0</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>3</ogr:xcoord>
<ogr:ycoord>3</ogr:ycoord>
<ogr:zcoord>0</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.2">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>2</ogr:xcoord>
<ogr:ycoord>2</ogr:ycoord>
<ogr:zcoord>2</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.3">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2,0</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>5</ogr:xcoord>
<ogr:ycoord>2</ogr:ycoord>
<ogr:zcoord>0</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.4">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1,0</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>4</ogr:xcoord>
<ogr:ycoord>1</ogr:ycoord>
<ogr:zcoord>0</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-5,5</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>0</ogr:xcoord>
<ogr:ycoord>-5</ogr:ycoord>
<ogr:zcoord>5</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.6">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1,7</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>8</ogr:xcoord>
<ogr:ycoord>-1</ogr:ycoord>
<ogr:zcoord>7</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.7">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>7,-1,6</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>7</ogr:xcoord>
<ogr:ycoord>-1</ogr:ycoord>
<ogr:zcoord>6</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
<gml:featureMember>
<ogr:add_geometry_pointz fid="points.8">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-1,4</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:xcoord>0</ogr:xcoord>
<ogr:ycoord>-1</ogr:ycoord>
<ogr:zcoord>4</ogr:zcoord>
</ogr:add_geometry_pointz>
</gml:featureMember>
</ogr:FeatureCollection>
15 changes: 15 additions & 0 deletions python/plugins/processing/tests/testdata/pointsz.gfs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>pointsz</Name>
<ElementPath>pointsz</ElementPath>
<GeometryType>-2147483647</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>9</FeatureCount>
<ExtentXMin>0.00000</ExtentXMin>
<ExtentXMax>8.00000</ExtentXMax>
<ExtentYMin>-5.00000</ExtentYMin>
<ExtentYMax>3.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>
59 changes: 59 additions & 0 deletions python/plugins/processing/tests/testdata/pointsz.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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>0</gml:X><gml:Y>-5</gml:Y><gml:Z>0</gml:Z></gml:coord>
<gml:coord><gml:X>8</gml:X><gml:Y>3</gml:Y><gml:Z>7</gml:Z></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:pointsz fid="points.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1,3</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3,0</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.2">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.3">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2,0</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.4">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1,0</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-5,5</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.6">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1,7</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.7">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>7,-1,6</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
<gml:featureMember>
<ogr:pointsz fid="points.8">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-1,4</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:pointsz>
</gml:featureMember>
</ogr:FeatureCollection>
12 changes: 12 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,15 @@ tests:
OUTPUT_LAYER:
name: expected/smoothed_lines_max_angle.gml
type: vector

- algorithm: qgis:exportaddgeometrycolumns
name: Add Geometry PointZ
params:
CALC_METHOD: '0'
INPUT:
name: pointsz.gml
type: vector
results:
OUTPUT:
name: expected/add_geometry_pointz.gml
type: vector
17 changes: 9 additions & 8 deletions python/plugins/processing/tools/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,15 @@ def simpleMeasure(geom, method=0, ellips=None, crs=None):
# 1 - project CRS
# 2 - ellipsoidal

if geom.wkbType() in [QgsWkbTypes.Point, QgsWkbTypes.Point25D]:
pt = geom.asPoint()
attr1 = pt.x()
attr2 = pt.y()
elif geom.wkbType() in [QgsWkbTypes.MultiPoint, QgsWkbTypes.MultiPoint25D]:
pt = geom.asMultiPoint()
attr1 = pt[0].x()
attr2 = pt[0].y()
if geom.type() == QgsWkbTypes.PointGeometry:
if not geom.isMultipart():
pt = geom.geometry()
attr1 = pt.x()
attr2 = pt.y()
else:
pt = geom.asMultiPoint()
attr1 = pt[0].x()
attr2 = pt[0].y()
else:
measure = QgsDistanceArea()

Expand Down

0 comments on commit 90aebd1

Please sign in to comment.