start refactoring of the Processing mmqgis backend: Delete column and

Delete duplicate geometries
This commit is contained in:
Alexander Bruy 2014-10-02 18:59:12 +03:00
parent 25fa170539
commit 07d584d264
4 changed files with 178 additions and 19 deletions

View File

@ -73,7 +73,8 @@ from ftools.DensifyGeometriesInterval import DensifyGeometriesInterval
from ftools.Eliminate import Eliminate
from ftools.SpatialJoin import SpatialJoin
from mmqgisx.MMQGISXAlgorithms import *
from mmqgisx.DeleteColumn import DeleteColumn
from mmqgisx.DeleteDuplicateGeometries import DeleteDuplicateGeometries
from ConcaveHull import ConcaveHull
from Polygonize import Polygonize
@ -138,17 +139,17 @@ class QGISAlgorithmProvider(AlgorithmProvider):
RandomExtractWithinSubsets(), ExtractByLocation(),
SpatialJoin(),
# ------ mmqgisx ------
mmqgisx_delete_columns_algorithm(),
mmqgisx_delete_duplicate_geometries_algorithm(),
mmqgisx_geometry_convert_algorithm(),
mmqgisx_grid_algorithm(),
mmqgisx_gridify_algorithm(),
mmqgisx_hub_distance_algorithm(),
mmqgisx_hub_lines_algorithm(),
mmqgisx_merge_algorithm(),
mmqgisx_select_algorithm(),
mmqgisx_extract_algorithm(),
mmqgisx_text_to_float_algorithm(),
DeleteColumn(), DeleteDuplicateGeometries(),
#mmqgisx_delete_duplicate_geometries_algorithm(),
#mmqgisx_geometry_convert_algorithm(),
#mmqgisx_grid_algorithm(),
#mmqgisx_gridify_algorithm(),
#mmqgisx_hub_distance_algorithm(),
#mmqgisx_hub_lines_algorithm(),
#mmqgisx_merge_algorithm(),
#mmqgisx_select_algorithm(),
#mmqgisx_extract_algorithm(),
#mmqgisx_text_to_float_algorithm(),
# ------ native algs ------
AddTableField(), FieldsCalculator(),
SaveSelectedFeatures(), JoinAttributes(),

View File

@ -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

View File

@ -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

View File

@ -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