Skip to content

Commit

Permalink
[processing] Fix very broken delete columns algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 23, 2016
1 parent d712b49 commit 3c51a93
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 14 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -115,7 +115,7 @@ qgis:delaunaytriangulation: >
This algorithm creates a polygon layer with the delaunay triangulation corresponding to a points layer.

qgis:deletecolumn: >
This algorithm takes a vector layer and generates a new one that has the exact same content but without one of its columns.
This algorithm takes a vector layer and generates a new one that has the exact same content but without the selected columns.

qgis:deleteduplicategeometries: >
This algorithm finds duplicated geometries and removes them. Attributes are not checked, so in case two feature have identical geometries but different attributes, only one of them will be added to the result layer.
Expand Down
30 changes: 18 additions & 12 deletions python/plugins/processing/algs/qgis/DeleteColumn.py
Expand Up @@ -42,6 +42,7 @@ class DeleteColumn(GeoAlgorithm):
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Delete column')
self.group, self.i18n_group = self.trAlgorithm('Vector table tools')
self.tags = self.tr('drop,delete,remove,fields,columns,attributes')

self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer')))
Expand All @@ -52,28 +53,33 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))

toDelete = self.getParameterValue(self.COLUMNS)
fields_to_delete = self.getParameterValue(self.COLUMNS).split(';')
fields = layer.fields()
idxs = []
for f in toDelete:
idx = layer.fieldNameIndex()
fields.remove(idx)
idxs.append[idx]
field_indices = []
# loop through twice - first we need to build up a list of original attribute indices
for f in fields_to_delete:
index = fields.lookupField(f)
field_indices.append(index)

# important - make sure we remove from the end so we aren't changing used indices as we go
field_indices.sort(reverse=True)

# this second time we make a cleaned version of the fields
for index in field_indices:
fields.remove(index)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
layer.wkbType(), layer.crs())

features = vector.features(layer)
total = 100.0 / len(features)

feat = QgsFeature()
for current, f in enumerate(features):
feat.setGeometry(f.geometry())
attributes = f.attributes()
for idx in idxs:
del attributes[idx]
feat.setAttributes(attributes)
writer.addFeature(feat)
for index in field_indices:
del attributes[index]
f.setAttributes(attributes)
writer.addFeature(f)

progress.setPercentage(int(current * total))

Expand Down
@@ -0,0 +1,27 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>delete_column</Name>
<ElementPath>delete_column</ElementPath>
<!--POLYGON-->
<GeometryType>3</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>6</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>10.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>6.00000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>name</Name>
<ElementPath>name</ElementPath>
<Type>String</Type>
<Width>5</Width>
</PropertyDefn>
<PropertyDefn>
<Name>floatval</Name>
<ElementPath>floatval</ElementPath>
<Type>Real</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,53 @@
<?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>10</gml:X><gml:Y>6</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:delete_column 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:floatval>44.123456</ogr:floatval>
</ogr:delete_column>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_column 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:floatval>0</ogr:floatval>
</ogr:delete_column>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_column 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:delete_column>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_column fid="polys.3">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,1 10,1 10,-3 6,-3 6,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>7,0 7,-2 9,-2 9,0 7,0</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>ASDF</ogr:name>
</ogr:delete_column>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_column fid="polys.4">
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:delete_column>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_column 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:floatval>3.33</ogr:floatval>
</ogr:delete_column>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,21 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>delete_columns</Name>
<ElementPath>delete_columns</ElementPath>
<!--POLYGON-->
<GeometryType>3</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>6</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>10.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>6.00000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>intval</Name>
<ElementPath>intval</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -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>10</gml:X><gml:Y>6</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:delete_columns 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:intval>33</ogr:intval>
</ogr:delete_columns>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_columns 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:intval>-33</ogr:intval>
</ogr:delete_columns>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_columns 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:delete_columns>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_columns fid="polys.3">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,1 10,1 10,-3 6,-3 6,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>7,0 7,-2 9,-2 9,0 7,0</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:intval>0</ogr:intval>
</ogr:delete_columns>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_columns fid="polys.4">
<ogr:intval>120</ogr:intval>
</ogr:delete_columns>
</gml:featureMember>
<gml:featureMember>
<ogr:delete_columns 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:intval>2</ogr:intval>
</ogr:delete_columns>
</gml:featureMember>
</ogr:FeatureCollection>
Expand Up @@ -1551,4 +1551,28 @@ tests:
INPUT:
name: lines.gml
type: vector
results: {}
results: {}

- algorithm: qgis:deletecolumn
name: Delete columns (multiple)
params:
COLUMN: floatval;name
INPUT:
name: polys.gml
type: vector
results:
OUTPUT:
name: expected/delete_columns.gml
type: vector

- algorithm: qgis:deletecolumn
name: Delete columns (single)
params:
COLUMN: intval
INPUT:
name: polys.gml
type: vector
results:
OUTPUT:
name: expected/delete_column.gml
type: vector

0 comments on commit 3c51a93

Please sign in to comment.