Skip to content
Permalink
Browse files

review Simplify geometries tool

  • Loading branch information
alexbruy committed Oct 4, 2012
1 parent 7362994 commit 414f70c8cf4e9d2741ae016a9aebe1d8ecbfc54e
Showing with 89 additions and 76 deletions.
  1. +89 −76 python/plugins/sextante/ftools/SimplifyGeometries.py
@@ -1,108 +1,121 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector
from sextante.core.SextanteLog import SextanteLog

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.core.SextanteLog import SextanteLog

from sextante.outputs.OutputVector import OutputVector

class SimplifyGeometries(GeoAlgorithm):

INPUT = "INPUT"
TOLERANCE = "TOLERANCE"
USE_SELECTION = "USE_SELECTION"
INPUT = "INPUT"
OUTPUT = "OUTPUT"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/simplify.png")

def processAlgorithm(self, progress):
self.processedFeatures = 0
self.progress = progress
tolerance =self.getParameterValue(SimplifyGeometries.TOLERANCE)
useSelection = self.getParameterValue(SimplifyGeometries.USE_SELECTION)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(SimplifyGeometries.INPUT))
self.generalize(vlayer, useSelection, tolerance)


def defineCharacteristics(self):
self.name = "Simplify geometries"
self.group = "Geometry tools"
self.addParameter(ParameterVector(SimplifyGeometries.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterNumber(SimplifyGeometries.TOLERANCE, "Tolerance", 0, 10000000, 1))
self.addParameter(ParameterBoolean(SimplifyGeometries.USE_SELECTION, "Use only selected features"))
self.addOutput(OutputVector(SimplifyGeometries.OUTPUT, "Simplified layer"))

self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterNumber(self.TOLERANCE, "Tolerance", 0.0, 10000000.0, 1.0))
self.addParameter(ParameterBoolean(self.USE_SELECTION, "Use only selected features", False))

def geomVertexCount(self, geometry):
geomType = geometry.type()
if geomType == 1: # line
points = geometry.asPolyline()
return len( points )
elif geomType == 2: # polygon
polylines = geometry.asPolygon()
points = []
for l in polylines:
points.extend( l )
return len( points )
else:
return None
self.addOutput(OutputVector(self.OUTPUT, "Simplified layer"))

def processAlgorithm(self, progress):
settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()

layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
useSelection = self.getParameterValue(self.USE_SELECTION)
tolerance =self.getParameterValue(self.TOLERANCE)
output = self.getOutputValue(self.OUTPUT)

pointsBefore = 0
pointsAfter = 0

provider = layer.dataProvider()
layer.select(layer.pendingAllAttributesList())

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

def generalize( self, inputLayer, useSelection, tolerance):
self.inputLayer = inputLayer
self.useSelection = useSelection
self.tolerance = tolerance
self.pointsBefore = 0
self.pointsAfter = 0
vProvider = self.inputLayer.dataProvider()
allAttrs = vProvider.attributeIndexes()
vProvider.select( allAttrs )
shapeFields = vProvider.fields()
crs = vProvider.crs()
wkbType = self.inputLayer.wkbType()
if not crs.isValid():
crs = None
shapeFileWriter = self.getOutputFromName(SimplifyGeometries.OUTPUT).getVectorWriter(shapeFields, wkbType, crs )
featureId = 0
if self.useSelection:
selection = self.inputLayer.selectedFeatures()
self.maxRange = len( selection )
current = 0
if useSelection:
selection = layer.selectedFeatures()
total = 100.0 / float(len(selection))
for f in selection:
featGeometry = QgsGeometry( f.geometry() )
featGeometry = QgsGeometry(f.geometry())
attrMap = f.attributeMap()
self.pointsBefore += geomVertexCount( featGeometry )
newGeometry = featGeometry.simplify( self.tolerance )
self.pointsAfter += geomVertexCount( newGeometry )

pointsBefore += self.geomVertexCount(featGeometry)
newGeometry = featGeometry.simplify(tolerance)
pointsAfter += self.geomVertexCount(newGeometry)

feature = QgsFeature()
feature.setGeometry( newGeometry )
feature.setAttributeMap( attrMap )
shapeFileWriter.addFeature( feature )
featureId += 1
self.emit( SIGNAL( "featureProcessed()" ) )
feature.setGeometry(newGeometry)
feature.setAttributeMap(attrMap)
writer.addFeature(feature)
current += 1
progress.setPercentage(int(current * total))
else:
self.maxRange = vProvider.featureCount()
total = 100.0 / float(provider.featureCount())
f = QgsFeature()
while vProvider.nextFeature( f ):
featGeometry = QgsGeometry( f.geometry() )
attrMap = f.attributeMap()
self.pointsBefore += self.geomVertexCount( featGeometry )
newGeometry = featGeometry.simplify( self.tolerance )
self.pointsAfter += self.geomVertexCount( newGeometry )
feature = QgsFeature()
feature.setGeometry( newGeometry )
feature.setAttributeMap( attrMap )
shapeFileWriter.addFeature( feature )
featureId += 1
self.progress.setPercentage(self.processedFeatures/self.maxRange * 100)
while layer.nextFeature(f):
featGeometry = QgsGeometry(f.geometry())
attrMap = f.attributeMap()

pointsBefore += self.geomVertexCount(featGeometry)
newGeometry = featGeometry.simplify(tolerance)
pointsAfter += self.geomVertexCount(newGeometry)

feature = QgsFeature()
feature.setGeometry(newGeometry)
feature.setAttributeMap(attrMap)
writer.addFeature(feature)

if shapeFileWriter != None:
del shapeFileWriter
current += 1
progress.setPercentage(int(current * total))

del writer

SextanteLog.addToLog(SextanteLog.LOG_INFO, "Simplify: Input geometries have been simplified from"
+ str(self.pointsBefore) + " to " + str(self.pointsAfter) + " points.")
+ str(pointsBefore) + " to " + str(pointsAfter) + " points.")

def geomVertexCount(self, geometry):
geomType = geometry.type()

if geomType == QGis.Line:
if geometry.isMultipart():
pointsList = geometry.asMultiPolyline()
points = sum( pointsList, [] )
else:
points = geometry.asPolyline()
return len( points )
elif geomType == QGis.Polygon:
if geometry.isMultipart():
polylinesList = geometry.asMultiPolygon()
polylines = sum( polylinesList, [] )
else:
polylines = geometry.asPolygon()

points = []
for l in polylines:
points.extend( l )

return len( points )
else:
return None

0 comments on commit 414f70c

Please sign in to comment.
You can’t perform that action at this time.