Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start refactoring of the Processing mmqgis backend: Delete column and
Delete duplicate geometries
- Loading branch information
Showing
4 changed files
with
178 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
python/plugins/processing/algs/qgis/mmqgisx/DeleteColumn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
DeleteColumn.py | ||
--------------------- | ||
Date : May 2010 | ||
Copyright : (C) 2010 by Michael Minn | ||
Email : pyqgis at michaelminn dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
__author__ = 'Michael Minn' | ||
__date__ = 'May 2010' | ||
__copyright__ = '(C) 2010, Michael Minn' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
from PyQt4.QtCore import * | ||
from qgis.core import * | ||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.core.parameters import ParameterVector | ||
from processing.core.parameters import ParameterTableField | ||
from processing.core.outputs import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
|
||
class DeleteColumn(GeoAlgorithm): | ||
INPUT = 'INPUT' | ||
COLUMN = 'COLUMN' | ||
OUTPUT = 'OUTPUT' | ||
|
||
def defineCharacteristics(self): | ||
self.name = 'Delete column' | ||
self.group = 'Vector table tools' | ||
|
||
self.addParameter(ParameterVector( | ||
self.INPUT, 'Input layer', [ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addParameter(ParameterTableField( | ||
self.COLUMN, 'Field to delete', self.INPUT)) | ||
self.addOutput(OutputVector(self.OUTPUT, 'Output')) | ||
|
||
def processAlgorithm(self, progress): | ||
layer = dataobjects.getObjectFromUri( | ||
self.getParameterValue(self.INPUT)) | ||
idx = layer.fieldNameIndex(self.getParameterValue(self.COLUMN)) | ||
|
||
fields = layer.pendingFields() | ||
fields.remove(idx) | ||
|
||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, | ||
layer.wkbType(), layer.crs()) | ||
|
||
features = vector.features(layer) | ||
count = len(features) | ||
total = 100.0 / float(count) | ||
|
||
feat = QgsFeature() | ||
for count, f in enumerate(features): | ||
feat.setGeometry(f.geometry()) | ||
attributes = f.attributes() | ||
del attributes[idx] | ||
feat.setAttributes(attributes) | ||
writer.addFeature(feat) | ||
|
||
progress.setPercentage(int(count * total)) | ||
|
||
del writer | ||
|
82 changes: 82 additions & 0 deletions
82
python/plugins/processing/algs/qgis/mmqgisx/DeleteDuplicateGeometries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
DeleteColumn.py | ||
--------------------- | ||
Date : May 2010 | ||
Copyright : (C) 2010 by Michael Minn | ||
Email : pyqgis at michaelminn dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
__author__ = 'Michael Minn' | ||
__date__ = 'May 2010' | ||
__copyright__ = '(C) 2010, Michael Minn' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
from PyQt4.QtCore import * | ||
from qgis.core import * | ||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.core.parameters import ParameterVector | ||
from processing.core.outputs import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
|
||
class DeleteDuplicateGeometries(GeoAlgorithm): | ||
INPUT = 'INPUT' | ||
OUTPUT = 'OUTPUT' | ||
|
||
def defineCharacteristics(self): | ||
self.name = 'Delete duplicate geometries' | ||
self.group = 'Vector general tools' | ||
|
||
self.addParameter(ParameterVector( | ||
self.INPUT, 'Input layer', [ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addOutput(OutputVector(self.OUTPUT, 'Output')) | ||
|
||
def processAlgorithm(self, progress): | ||
layer = dataobjects.getObjectFromUri( | ||
self.getParameterValue(self.INPUT)) | ||
|
||
fields = layer.pendingFields() | ||
|
||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, | ||
layer.wkbType(), layer.crs()) | ||
|
||
features = vector.features(layer) | ||
|
||
count = len(features) | ||
total = 100.0 / float(count) | ||
geoms = dict() | ||
for count, f in enumerate(features): | ||
geoms[f.id()] = QgsGeometry(f.geometry()) | ||
progress.setPercentage(int(count * total)) | ||
|
||
cleaned = dict(geoms) | ||
|
||
for i, g in geoms.iteritems(): | ||
for j in cleaned.keys(): | ||
if i == j or i not in cleaned: | ||
continue | ||
if g.isGeosEqual(cleaned[j]): | ||
del cleaned[j] | ||
|
||
count = len(cleaned) | ||
total = 100.0 / float(count) | ||
request = QgsFeatureRequest().setFilterFids(cleaned.keys()) | ||
for count, f in enumerate(layer.getFeatures(request)): | ||
writer.addFeature(f) | ||
progress.setPercentage(int(count * total)) | ||
|
||
del writer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
Copyright notice | ||
------------------- | ||
MMQGIS Wrapper | ||
|
||
---------------- | ||
MMQGIS (c) 2012 by Michael Minn | ||
|
||
http://michaelminn.com | ||
|
||
MMQGIS is free software and is offered without guarantee | ||
or warranty. You can redistribute it and/or modify it | ||
under the terms of version 2 of the GNU General Public | ||
License (GPL v2) as published by the Free Software | ||
or warranty. You can redistribute it and/or modify it | ||
under the terms of version 2 of the GNU General Public | ||
License (GPL v2) as published by the Free Software | ||
Foundation (www.gnu.org). | ||
|
||
QGIS processing framework (c) 2012 by Victor Olaya | ||
QGIS Processing framework (c) 2012 by Victor Olaya |