Skip to content
Permalink
Browse files

[processing] Use layer crs/fields instead of provider crs/fields

Since the layer has more complete knowledge of the crs (ie, when
provider could not determine crs and user has selected it from
the list), and also better knowledge of layer fields (virtual
fields, joined fields) we should use these rather than the
provider methods.

(cherry-picked from 06c4b07)
  • Loading branch information
nyalldawson committed Aug 9, 2016
1 parent 43274b1 commit 60175d71372626e635891446829a7907efc264ef
Showing with 75 additions and 107 deletions.
  1. +2 −3 python/plugins/processing/algs/qgis/AddTableField.py
  2. +2 −3 python/plugins/processing/algs/qgis/AutoincrementalField.py
  3. +1 −1 python/plugins/processing/algs/qgis/Centroids.py
  4. +4 −5 python/plugins/processing/algs/qgis/CheckValidity.py
  5. +1 −1 python/plugins/processing/algs/qgis/ConcaveHull.py
  6. +1 −1 python/plugins/processing/algs/qgis/ConvexHull.py
  7. +2 −2 python/plugins/processing/algs/qgis/Difference.py
  8. +2 −3 python/plugins/processing/algs/qgis/Dissolve.py
  9. +2 −3 python/plugins/processing/algs/qgis/Eliminate.py
  10. +2 −3 python/plugins/processing/algs/qgis/EquivalentNumField.py
  11. +1 −2 python/plugins/processing/algs/qgis/Explode.py
  12. +1 −1 python/plugins/processing/algs/qgis/ExportGeometryInfo.py
  13. +1 −1 python/plugins/processing/algs/qgis/ExtractByLocation.py
  14. +3 −4 python/plugins/processing/algs/qgis/FieldPyculator.py
  15. +2 −3 python/plugins/processing/algs/qgis/FieldsCalculator.py
  16. +1 −2 python/plugins/processing/algs/qgis/FieldsMapper.py
  17. +2 −3 python/plugins/processing/algs/qgis/Intersection.py
  18. +1 −2 python/plugins/processing/algs/qgis/JoinAttributes.py
  19. +5 −5 python/plugins/processing/algs/qgis/Merge.py
  20. +1 −1 python/plugins/processing/algs/qgis/MultipartToSingleparts.py
  21. +2 −3 python/plugins/processing/algs/qgis/OrientedMinimumBoundingBox.py
  22. +2 −3 python/plugins/processing/algs/qgis/PointsDisplacement.py
  23. +3 −4 python/plugins/processing/algs/qgis/PointsInPolygon.py
  24. +2 −3 python/plugins/processing/algs/qgis/PointsInPolygonUnique.py
  25. +3 −4 python/plugins/processing/algs/qgis/PointsInPolygonWeighted.py
  26. +1 −2 python/plugins/processing/algs/qgis/PointsLayerFromTable.py
  27. +1 −1 python/plugins/processing/algs/qgis/PointsToPaths.py
  28. +1 −2 python/plugins/processing/algs/qgis/Polygonize.py
  29. +1 −1 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
  30. +1 −1 python/plugins/processing/algs/qgis/RandomPointsLayer.py
  31. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsFixed.py
  32. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsVariable.py
  33. +1 −2 python/plugins/processing/algs/qgis/ReverseLineDirection.py
  34. +2 −3 python/plugins/processing/algs/qgis/SaveSelectedFeatures.py
  35. +1 −1 python/plugins/processing/algs/qgis/SinglePartsToMultiparts.py
  36. +1 −2 python/plugins/processing/algs/qgis/Smooth.py
  37. +3 −6 python/plugins/processing/algs/qgis/SpatialJoin.py
  38. +2 −4 python/plugins/processing/algs/qgis/SumLines.py
  39. +5 −8 python/plugins/processing/algs/qgis/SymmetricalDifference.py
  40. +2 −4 python/plugins/processing/algs/qgis/Union.py
  41. +1 −1 python/plugins/processing/algs/qgis/VectorSplit.py
  42. +1 −1 python/plugins/processing/algs/qgis/ZonalStatistics.py
@@ -78,11 +78,10 @@ def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))

provider = layer.dataProvider()
fields = provider.fields()
fields = layer.fields()
fields.append(QgsField(fieldName, self.TYPES[fieldType], '',
fieldLength, fieldPrecision))
writer = output.getVectorWriter(fields, provider.geometryType(),
writer = output.getVectorWriter(fields, layer.wkbType(),
layer.crs())
outFeat = QgsFeature()
features = vector.features(layer)
@@ -49,10 +49,9 @@ def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
vlayer = \
dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
fields = vlayer.fields()
fields.append(QgsField('AUTO', QVariant.Int))
writer = output.getVectorWriter(fields, vprovider.geometryType(),
writer = output.getVectorWriter(fields, vlayer.wkbType(),
vlayer.crs())
outFeat = QgsFeature()
features = vector.features(vlayer)
@@ -63,7 +63,7 @@ def processAlgorithm(self, progress):

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(
layer.pendingFields().toList(),
layer.fields(),
QGis.WKBPoint,
layer.crs())

@@ -97,27 +97,26 @@ def processAlgorithm(self, progress):
def doCheck(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))
provider = layer.dataProvider()

settings = QSettings()
method = int(settings.value(settings_method_key, 1))

valid_ouput = self.getOutputFromName(self.VALID_OUTPUT)
valid_fields = layer.pendingFields().toList()
valid_fields = layer.fields()
valid_writer = valid_ouput.getVectorWriter(
valid_fields,
provider.geometryType(),
layer.wkbType(),
layer.crs())
valid_count = 0

invalid_ouput = self.getOutputFromName(self.INVALID_OUTPUT)
invalid_fields = layer.pendingFields().toList() + [
invalid_fields = layer.fields().toList() + [
QgsField(name='_errors',
type=QVariant.String,
len=255)]
invalid_writer = invalid_ouput.getVectorWriter(
invalid_fields,
provider.geometryType(),
layer.wkbType(),
layer.crs())
invalid_count = 0

@@ -111,7 +111,7 @@ def processAlgorithm(self, progress):
feat = QgsFeature()
dissolved_layer.getFeatures(QgsFeatureRequest().setFilterFid(0)).nextFeature(feat)
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
layer.pendingFields().toList(), QGis.WKBPolygon, layer.crs())
layer.fields().toList(), QGis.WKBPolygon, layer.crs())
geom = feat.geometry()
if no_multigeom and geom.isMultipart():
# Only singlepart geometries are allowed
@@ -97,7 +97,7 @@ def processAlgorithm(self, progress):
]

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields, QGis.WKBPolygon, layer.dataProvider().crs())
fields, QGis.WKBPolygon, layer.crs())

outFeat = QgsFeature()
inGeom = QgsGeometry()
@@ -68,11 +68,11 @@ def processAlgorithm(self, progress):
self.getParameterValue(Difference.OVERLAY))
ignoreInvalid = self.getParameterValue(Difference.IGNORE_INVALID)

geomType = layerA.dataProvider().geometryType()
geomType = layerA.wkbType()
writer = self.getOutputFromName(
Difference.OUTPUT).getVectorWriter(layerA.pendingFields(),
geomType,
layerA.dataProvider().crs())
layerA.crs())

outFeat = QgsFeature()
index = vector.spatialindex(layerB)
@@ -70,12 +70,11 @@ def processAlgorithm(self, progress):
fieldname = self.getParameterValue(Dissolve.FIELD)
vlayerA = dataobjects.getObjectFromUri(
self.getParameterValue(Dissolve.INPUT))
vproviderA = vlayerA.dataProvider()
fields = vlayerA.fields()
writer = self.getOutputFromName(
Dissolve.OUTPUT).getVectorWriter(fields,
vproviderA.geometryType(),
vproviderA.crs())
vlayerA.wkbType(),
vlayerA.crs())
outFeat = QgsFeature()
features = vector.features(vlayerA)
total = 100.0 / len(features)
@@ -315,10 +315,9 @@ def processAlgorithm(self, progress):
# End while

# Create output
provider = processLayer.dataProvider()
output = self.getOutputFromName(self.OUTPUT)
writer = output.getVectorWriter(provider.fields(),
provider.geometryType(), processLayer.crs())
writer = output.getVectorWriter(processLayer.fields(),
processLayer.wkbType(), processLayer.crs())

# Write all features that are left over to output layer
iterator = processLayer.getFeatures()
@@ -54,11 +54,10 @@ def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
vlayer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
fieldindex = vlayer.fieldNameIndex(fieldname)
fields = vprovider.fields()
fields = vlayer.fields()
fields.append(QgsField('NUM_FIELD', QVariant.Int))
writer = output.getVectorWriter(fields, vprovider.geometryType(),
writer = output.getVectorWriter(fields, vlayer.wkbType(),
vlayer.crs())
outFeat = QgsFeature()
classes = {}
@@ -48,8 +48,7 @@ def processAlgorithm(self, progress):
vlayer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
output = self.getOutputFromName(self.OUTPUT)
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
fields = vlayer.fields()
writer = output.getVectorWriter(fields, QGis.WKBLineString,
vlayer.crs())
outFeat = QgsFeature()
@@ -89,7 +89,7 @@ def processAlgorithm(self, progress):
fields.append(QgsField(yName, QVariant.Double))

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

ellips = None
crs = None
@@ -71,7 +71,7 @@ def processAlgorithm(self, progress):

output = self.getOutputFromName(self.OUTPUT)
writer = output.getVectorWriter(layer.pendingFields(),
layer.dataProvider().geometryType(), layer.crs())
layer.wkbType(), layer.crs())

if 'disjoint' in predicates:
disjoinSet = []
@@ -88,11 +88,10 @@ def processAlgorithm(self, progress):

layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))
provider = layer.dataProvider()
fields = provider.fields()
fields = layer.fields()
fields.append(QgsField(fieldName, self.TYPES[fieldType], '',
fieldLength, fieldPrecision))
writer = output.getVectorWriter(fields, provider.geometryType(),
writer = output.getVectorWriter(fields, layer.wkbType(),
layer.crs())
outFeat = QgsFeature()
new_ns = {}
@@ -107,7 +106,7 @@ def processAlgorithm(self, progress):
self.tr("FieldPyculator code execute error.Global code block can't be executed!\n%s\n%s") % (unicode(sys.exc_info()[0].__name__), unicode(sys.exc_info()[1])))

# Replace all fields tags
fields = provider.fields()
fields = layer.fields()
num = 0
for field in fields:
field_name = unicode(field.name())
@@ -94,12 +94,11 @@ def processAlgorithm(self, progress):
output.value = system.getTempFilenameInTempFolder(
output.name + '.' + ext)

provider = layer.dataProvider()
fields = layer.pendingFields()
fields = layer.fields()
if newField:
fields.append(QgsField(fieldName, fieldType, '', width, precision))

writer = output.getVectorWriter(fields, provider.geometryType(),
writer = output.getVectorWriter(fields, layer.wkbType(),
layer.crs())

exp = QgsExpression(formula)
@@ -68,7 +68,6 @@ def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT_LAYER)

layer = dataobjects.getObjectFromUri(layer)
provider = layer.dataProvider()
fields = []
expressions = []

@@ -109,7 +108,7 @@ def processAlgorithm(self, progress):
expressions.append(expression)

writer = output.getVectorWriter(fields,
provider.geometryType(),
layer.wkbType(),
layer.crs())

# Create output vector layer with new attributes
@@ -73,12 +73,11 @@ def processAlgorithm(self, progress):
self.getParameterValue(self.INPUT))
vlayerB = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT2))
vproviderA = vlayerA.dataProvider()

geomType = vproviderA.geometryType()
geomType = vlayerA.wkbType()
fields = vector.combineVectorFields(vlayerA, vlayerB)
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
geomType, vproviderA.crs())
geomType, vlayerA.crs())
outFeat = QgsFeature()
index = vector.spatialindex(vlayerB)
selectionA = vector.features(vlayerA)
@@ -69,14 +69,13 @@ def processAlgorithm(self, progress):
field2 = self.getParameterValue(self.TABLE_FIELD_2)

layer = dataobjects.getObjectFromUri(input)
provider = layer.dataProvider()
joinField1Index = layer.fieldNameIndex(field)

layer2 = dataobjects.getObjectFromUri(input2)
joinField2Index = layer2.fieldNameIndex(field2)

outFields = vector.combineVectorFields(layer, layer2)
writer = output.getVectorWriter(outFields, provider.geometryType(),
writer = output.getVectorWriter(outFields, layer.wkbType(),
layer.crs())

# Cache attributes of Layer 2
@@ -66,14 +66,14 @@ def processAlgorithm(self, progress):
layer = QgsVectorLayer(paths[x], unicode(x), 'ogr')

if (len(layers) > 0):
if (layer.dataProvider().geometryType() != layers[0].dataProvider().geometryType()):
if (layer.wkbType() != layers[0].wkbType()):
raise GeoAlgorithmExecutionException(
self.tr('All layers must have same geometry type!'))

layers.append(layer)
totalFeatureCount += layer.featureCount()

for sindex, sfield in enumerate(layer.dataProvider().fields()):
for sindex, sfield in enumerate(layer.fields()):
found = None
for dfield in fields:
if (dfield.name().upper() == sfield.name().upper()):
@@ -88,12 +88,12 @@ def processAlgorithm(self, progress):

total = 100.0 / totalFeatureCount
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields.toList(), layers[0].dataProvider().geometryType(),
fields.toList(), layers[0].wkbType(),
layers[0].crs())

featureCount = 0
for layer in layers:
for feature in layer.dataProvider().getFeatures():
for feature in layer.getFeatures():
sattributes = feature.attributes()
dattributes = []
for dindex, dfield in enumerate(fields):
@@ -104,7 +104,7 @@ def processAlgorithm(self, progress):
else:
dattribute = ''

for sindex, sfield in enumerate(layer.dataProvider().fields()):
for sindex, sfield in enumerate(layer.fields()):
if (sfield.name().upper() == dfield.name().upper()):
if (sfield.type() != dfield.type()):
raise GeoAlgorithmExecutionException(
@@ -58,7 +58,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))

geomType = self.multiToSingleGeom(layer.dataProvider().geometryType())
geomType = self.multiToSingleGeom(layer.wkbType())

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
layer.pendingFields().toList(), geomType, layer.crs())
@@ -82,11 +82,10 @@ def processAlgorithm(self, progress):

def layerOmmb(self, layer, writer, progress):
current = 0
vprovider = layer.dataProvider()

fit = vprovider.getFeatures()
fit = layer.getFeatures()
inFeat = QgsFeature()
total = 100.0 / vprovider.featureCount()
total = 100.0 / layer.featureCount()
newgeometry = QgsGeometry()
first = True
while fit.nextFeature(inFeat):
@@ -62,9 +62,8 @@ def processAlgorithm(self, progress):

layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))

provider = layer.dataProvider()
writer = output.getVectorWriter(provider.fields(),
provider.geometryType(), provider.crs())
writer = output.getVectorWriter(layer.fields(),
layer.wkbType(), layer.crs())

features = vector.features(layer)

@@ -68,15 +68,14 @@ def processAlgorithm(self, progress):
pointLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.POINTS))
fieldName = self.getParameterValue(self.FIELD)

polyProvider = polyLayer.dataProvider()
fields = polyProvider.fields()
fields = polyLayer.fields()
fields.append(QgsField(fieldName, QVariant.Int))

(idxCount, fieldList) = vector.findOrCreateField(polyLayer,
polyLayer.pendingFields(), fieldName)
polyLayer.fields(), fieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields.toList(), polyProvider.geometryType(), polyProvider.crs())
fields.toList(), polyLayer.wkbType(), polyLayer.crs())

spatialIndex = vector.spatialindex(pointLayer)

@@ -62,16 +62,15 @@ def processAlgorithm(self, progress):
fieldName = self.getParameterValue(self.FIELD)
classFieldName = self.getParameterValue(self.CLASSFIELD)

polyProvider = polyLayer.dataProvider()
fields = polyProvider.fields()
fields = polyLayer.fields()
fields.append(QgsField(fieldName, QVariant.Int))

classFieldIndex = pointLayer.fieldNameIndex(classFieldName)
(idxCount, fieldList) = vector.findOrCreateField(polyLayer,
polyLayer.pendingFields(), fieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields.toList(), polyProvider.geometryType(), polyProvider.crs())
fields.toList(), polyLayer.wkbType(), polyLayer.crs())

spatialIndex = vector.spatialindex(pointLayer)

@@ -69,15 +69,14 @@ def processAlgorithm(self, progress):
fieldName = self.getParameterValue(self.FIELD)
fieldIdx = pointLayer.fieldNameIndex(self.getParameterValue(self.WEIGHT))

polyProvider = polyLayer.dataProvider()
fields = polyProvider.fields()
fields = polyLayer.fields()
fields.append(QgsField(fieldName, QVariant.Int))

(idxCount, fieldList) = vector.findOrCreateField(polyLayer,
polyLayer.pendingFields(), fieldName)
polyLayer.fields(), fieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields.toList(), polyProvider.geometryType(), polyProvider.crs())
fields.toList(), polyLayer.wkbType(), polyLayer.crs())

spatialIndex = vector.spatialindex(pointLayer)

@@ -63,8 +63,7 @@ def processAlgorithm(self, progress):
source = self.getParameterValue(self.INPUT)
vlayer = dataobjects.getObjectFromUri(source)
output = self.getOutputFromName(self.OUTPUT)
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
fields = vlayer.fields()
writer = output.getVectorWriter(fields, QGis.WKBPoint, self.crs)
xfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.XFIELD))
yfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.YFIELD))

0 comments on commit 60175d7

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