Skip to content
Permalink
Browse files
adapted mmqgis and sextante algs to changes in management of selected…
… features
  • Loading branch information
volaya committed Dec 23, 2012
1 parent 8761cf2 commit 6880a7600f0d3e952eb38478d5d99c2f512513a5
@@ -32,7 +32,6 @@
from sextante.parameters.ParameterString import ParameterString
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.core.QGisLayers import QGisLayers
import os
from PyQt4 import QtGui


@@ -68,13 +67,13 @@ def processAlgorithm(self, progress):
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, self.TYPES[fieldtype])
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature(inFeat):
features = QGisLayers.features(vlayer)
for inFeat in features:
progress.setPercentage(int((100 * nElement)/nFeat))
nElement += 1
inGeom = inFeat.geometry()
@@ -29,7 +29,6 @@
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector
import os
from PyQt4 import QtGui
from sextante.parameters.ParameterTableField import ParameterTableField

@@ -61,17 +60,19 @@ def processAlgorithm(self, progress):
nFeat = vprovider.featureCount()
nElement = 0
classes = {}
while vprovider.nextFeature(inFeat):
features = QGisLayers.features(layer)
for feature in features:
progress.setPercentage(int((100 * nElement)/nFeat))
nElement += 1
atMap = inFeat.attributeMap()
atMap = feature.attributeMap()
clazz = atMap[field_index].toString()
if clazz not in classes:
classes[clazz] = len(classes.keys())
while vprovider.nextFeature(inFeat):
features = QGisLayers.features(layer)
for feature in features:
progress.setPercentage(int((100 * nElement)/nFeat))
nElement += 1
inGeom = inFeat.geometry()
inGeom = features.geometry()
outFeat.setGeometry( inGeom )
atMap = inFeat.attributeMap()
clazz = atMap[field_index].toString()
@@ -24,7 +24,6 @@
__revision__ = '$Format:%H$'

from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
@@ -56,11 +55,12 @@ def processAlgorithm(self, progress):
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature( inFeat ):
features = QGisLayers.features(layer)
for feature in features:
nElement += 1
progress.setPercentage((nElement*100)/nFeat)
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
inGeom = feature.geometry()
atMap = feature.attributeMap()
segments = self.extractAsSingleSegments( inGeom )
outFeat.setAttributeMap( atMap )
for segment in segments:
@@ -56,8 +56,7 @@ class FieldsPyculator(GeoAlgorithm):
def defineCharacteristics(self):
self.name = "Advanced Python field calculator"
self.group = "Vector table tools"
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterBoolean(self.USE_SELECTED, "Use only selected features (all if noone selected)", False))
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.FIELD_NAME, "Result field name", "NewField"))
self.addParameter(ParameterString(self.GLOBAL, "Global expression", multiline = True))
self.addParameter(ParameterString(self.FORMULA, "Formula", "value = ", multiline = True))
@@ -67,8 +66,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
fieldname = self.getParameterValue(self.FIELD_NAME)
code = self.getParameterValue(self.FORMULA)
globalExpression = self.getParameterValue(self.GLOBAL)
useSelected = self.getParameterValue(self.USE_SELECTED)
globalExpression = self.getParameterValue(self.GLOBAL)
output = self.getOutputFromName(self.OUTPUT_LAYER)
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = layer.dataProvider()
@@ -113,113 +111,67 @@ def processAlgorithm(self, progress):
"Field code block can't be executed! %s \n %s"
(unicode(sys.exc_info()[0].__name__), unicode(sys.exc_info()[1])))

#run
nElement = 1
features = layer.selectedFeatures()
#run
if need_attrs:
attr_ind = vprovider.attributeIndexes()
else:
attr_ind = []
vprovider.select(attr_ind, QgsRectangle(), True)

features = QGisLayers.features(layer)
nFeatures = len(features)
if not useSelected or nFeatures == 0:
nFeatures = vprovider.featureCount()
feat = QgsFeature()
nElement = 1
for feat in features:
progress.setPercentage(int((100 * nElement)/nFeatures))
attrMap = feat.attributeMap()
feat_id = feat.id()

#add needed vars
if need_id:
new_ns['__id'] = feat_id

if need_geom:
geom = feat.geometry()
new_ns['__geom'] = geom

if need_attrs:
attr_ind = vprovider.attributeIndexes()
else:
attr_ind = []
vprovider.select(attr_ind, QgsRectangle(), True)

while vprovider.nextFeature(feat):
progress.setPercentage(int((100 * nElement)/nFeatures))
attrMap = feat.attributeMap()
feat_id = feat.id()

#add needed vars
if need_id:
new_ns['__id'] = feat_id

if need_geom:
geom = feat.geometry()
new_ns['__geom'] = geom

if need_attrs:
attr = []
for num,a in attrMap.iteritems():
attr.append(self.Qvar2py(a))
new_ns['__attr'] = attr

#clear old result
if new_ns.has_key(self.RESULT_VAR_NAME):
del new_ns[self.RESULT_VAR_NAME]


#exec
#try:
exec bytecode in new_ns
#except:
# raise e
#===============================================================
# GeoAlgorithmExecutionException("FieldPyculator code execute error\n"+
# "Field code block can't be executed for feature %s\n%s\n%s" %
# (unicode(sys.exc_info()[0].__name__),
# unicode(sys.exc_info()[1]),
# unicode(feat_id)))
#===============================================================

#check result
if not new_ns.has_key(self.RESULT_VAR_NAME):
raise GeoAlgorithmExecutionException("FieldPyculator code execute error\n" +
"Field code block does not return '%s1' variable! Please declare this variable in your code!" %
self.RESULT_VAR_NAME)


#write feature
nElement += 1
outFeat.setGeometry( feat.geometry() )
outFeat.setAttributeMap( attrMap )
outFeat.addAttribute(len(vprovider.fields()), QVariant(new_ns[self.RESULT_VAR_NAME]))
writer.addFeature(outFeat)

else:
features = layer.selectedFeatures()
nFeatures = len(features)
for feat in features:
progress.setPercentage(int((100 * nElement)/nFeatures))
attrMap = feat.attributeMap()
feat_id = feat.id()

#add needed vars
if need_id:
new_ns['__id'] = feat_id

if need_geom:
geom = feat.geometry()
new_ns['__geom'] = geom

if need_attrs:
attrMap = feat.attributeMap()
attr = []
for num,a in attrMap.iteritems():
attr.append(self.Qvar2py(a))
new_ns['__attr'] = attr

#clear old result
if new_ns.has_key(self.RESULT_VAR_NAME):
del new_ns[self.RESULT_VAR_NAME]

#exec
exec bytecode in new_ns

#check result
if not new_ns.has_key(self.RESULT_VAR_NAME):
raise GeoAlgorithmExecutionException("FieldPyculator code execute error\n" +
"Field code block does not return '%s1' variable! Please declare this variable in your code!" %
self.RESULT_VAR_NAME)

#write feature
nElement += 1
outFeat.setGeometry( feat.geometry() )
outFeat.setAttributeMap( attrMap )
outFeat.addAttribute(len(vprovider.fields()), QVariant(new_ns[self.RESULT_VAR_NAME]))
writer.addFeature(outFeat)

attr = []
for num,a in attrMap.iteritems():
attr.append(self.Qvar2py(a))
new_ns['__attr'] = attr

#clear old result
if new_ns.has_key(self.RESULT_VAR_NAME):
del new_ns[self.RESULT_VAR_NAME]


#exec
#try:
exec bytecode in new_ns
#except:
# raise e
#===============================================================
# GeoAlgorithmExecutionException("FieldPyculator code execute error\n"+
# "Field code block can't be executed for feature %s\n%s\n%s" %
# (unicode(sys.exc_info()[0].__name__),
# unicode(sys.exc_info()[1]),
# unicode(feat_id)))
#===============================================================

#check result
if not new_ns.has_key(self.RESULT_VAR_NAME):
raise GeoAlgorithmExecutionException("FieldPyculator code execute error\n" +
"Field code block does not return '%s1' variable! Please declare this variable in your code!" %
self.RESULT_VAR_NAME)


#write feature
nElement += 1
outFeat.setGeometry( feat.geometry() )
outFeat.setAttributeMap( attrMap )
outFeat.addAttribute(len(vprovider.fields()), QVariant(new_ns[self.RESULT_VAR_NAME]))
writer.addFeature(outFeat)

del writer


@@ -31,7 +31,6 @@
from PyQt4.QtGui import *
from sextante.parameters.ParameterString import ParameterString
from sextante.core.QGisLayers import QGisLayers
import os
from PyQt4 import QtGui
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

@@ -66,13 +65,13 @@ def processAlgorithm(self, progress):
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, QVariant.Double)
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature(inFeat):
features = QGisLayers.features(vlayer)
for inFeat in features:
progress.setPercentage(int((100 * nElement)/nFeat))
attrs = inFeat.attributeMap()
expression = formula
@@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from sextante.algs.JoinAttributes import JoinAttributes

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -59,16 +60,14 @@
from sextante.algs.ftools.SelectByLocation import SelectByLocation
from sextante.algs.ftools.Union import Union
from sextante.algs.ftools.DensifyGeometriesInterval import DensifyGeometriesInterval
from sextante.algs.mmqgisx.MMQGISXAlgorithms import mmqgisx_attribute_export_algorithm,\
mmqgisx_attribute_join_algorithm, mmqgisx_delete_columns_algorithm,\
mmqgisx_delete_duplicate_geometries_algorithm,\
mmqgisx_geocode_google_algorithm, mmqgisx_geometry_convert_algorithm,\
mmqgisx_geometry_export_algorithm, mmqgisx_geometry_import_algorithm,\
mmqgisx_grid_algorithm, mmqgisx_gridify_algorithm,\
mmqgisx_hub_distance_algorithm, mmqgisx_hub_lines_algorithm,\
mmqgisx_label_point_algorithm, mmqgisx_merge_algorithm,\
mmqgisx_select_algorithm, mmqgisx_sort_algorithm,\
mmqgisx_text_to_float_algorithm, mmqgisx_voronoi_algorithm
from sextante.algs.mmqgisx.MMQGISXAlgorithms import (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_label_point_algorithm, mmqgisx_merge_algorithm,
mmqgisx_select_algorithm, mmqgisx_sort_algorithm,
mmqgisx_text_to_float_algorithm)

from sextante.algs.EquivalentNumField import EquivalentNumField
from sextante.core.AlgorithmProvider import AlgorithmProvider
@@ -85,7 +84,7 @@ class QGISAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [AddTableField(), FieldsCalculator(), SaveSelectedFeatures(),
self.alglist = [AddTableField(), FieldsCalculator(), SaveSelectedFeatures(), JoinAttributes(),
AutoincrementalField(), Explode(), FieldsPyculator(), EquivalentNumField(),
#FTOOLS
SumLines(), PointsInPolygon(), PointsInPolygonWeighted(), PointsInPolygonUnique(),
@@ -105,13 +104,13 @@ def __init__(self):
ExtentFromLayer(), RandomSelection(), RandomSelectionWithinSubsets(),
SelectByLocation(),
#MMQGISX
mmqgisx_attribute_export_algorithm(),
mmqgisx_attribute_join_algorithm(),
#mmqgisx_attribute_export_algorithm(),
#mmqgisx_attribute_join_algorithm(),
mmqgisx_delete_columns_algorithm(),
mmqgisx_delete_duplicate_geometries_algorithm(),
mmqgisx_geocode_google_algorithm(),
#mmqgisx_geocode_google_algorithm(),
mmqgisx_geometry_convert_algorithm(),
mmqgisx_geometry_export_algorithm(),
#mmqgisx_geometry_export_algorithm(),
#mmqgisx_geometry_import_algorithm(),
mmqgisx_grid_algorithm(),
mmqgisx_gridify_algorithm(),
@@ -18,7 +18,6 @@
from PyQt4.QtGui import *
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.QGisLayers import QGisLayers
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterString import ParameterString
@@ -307,9 +306,9 @@ def defineCharacteristics(self):
self.name = "Geometry export"
self.group = "Vector general tools"

self.addParameter(ParameterVector(self.LAYERNAME, "Input Layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(self.LAYERNAME, "Input layer", ParameterVector.VECTOR_TYPE_ANY))

self.addOutput(OutputTable(self.NODEFILENAME, "Node CSV Output File Name"))
self.addOutput(OutputTable(self.NODEFILENAME, "Node output file"))
self.addOutput(OutputTable(self.ATTRIBUTEFILENAME, "Attribute CSV Output File Name (non-points only)"))

self.delimiters = ["Comma", "Bar", "Space"]
Loading

0 comments on commit 6880a76

Please sign in to comment.