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.
  • Loading branch information
nyalldawson committed Aug 3, 2016
1 parent 00a8fea commit 06c4b07222f9410e1669387a6497897c61748a16
Showing with 94 additions and 127 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. +1 −2 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. +1 −1 python/plugins/processing/algs/qgis/LinesIntersection.py
  20. +5 −5 python/plugins/processing/algs/qgis/Merge.py
  21. +1 −2 python/plugins/processing/algs/qgis/MergeLines.py
  22. +17 −17 python/plugins/processing/algs/qgis/MultipartToSingleparts.py
  23. +2 −3 python/plugins/processing/algs/qgis/OrientedMinimumBoundingBox.py
  24. +2 −3 python/plugins/processing/algs/qgis/PointsDisplacement.py
  25. +3 −4 python/plugins/processing/algs/qgis/PointsInPolygon.py
  26. +2 −3 python/plugins/processing/algs/qgis/PointsInPolygonUnique.py
  27. +3 −4 python/plugins/processing/algs/qgis/PointsInPolygonWeighted.py
  28. +1 −2 python/plugins/processing/algs/qgis/PointsLayerFromTable.py
  29. +1 −1 python/plugins/processing/algs/qgis/PointsToPaths.py
  30. +1 −2 python/plugins/processing/algs/qgis/Polygonize.py
  31. +1 −1 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
  32. +1 −1 python/plugins/processing/algs/qgis/RandomPointsLayer.py
  33. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsFixed.py
  34. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygonsVariable.py
  35. +1 −2 python/plugins/processing/algs/qgis/ReverseLineDirection.py
  36. +2 −3 python/plugins/processing/algs/qgis/SaveSelectedFeatures.py
  37. +1 −1 python/plugins/processing/algs/qgis/SinglePartsToMultiparts.py
  38. +1 −2 python/plugins/processing/algs/qgis/Smooth.py
  39. +3 −6 python/plugins/processing/algs/qgis/SpatialJoin.py
  40. +1 −1 python/plugins/processing/algs/qgis/SplitLinesWithLines.py
  41. +2 −4 python/plugins/processing/algs/qgis/SumLines.py
  42. +5 −8 python/plugins/processing/algs/qgis/SymmetricalDifference.py
  43. +3 −5 python/plugins/processing/algs/qgis/Union.py
  44. +1 −1 python/plugins/processing/algs/qgis/VectorSplit.py
  45. +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.wkbType(),
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.wkbType(),
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(),
QgsWkbTypes.Point,
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.wkbType(),
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.wkbType(),
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(), QgsWkbTypes.Polygon, layer.crs())
layer.fields().toList(), QgsWkbTypes.Polygon, 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, QgsWkbTypes.Polygon, layer.dataProvider().crs())
fields, QgsWkbTypes.Polygon, 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().wkbType()
geomType = layerA.wkbType()
writer = self.getOutputFromName(
Difference.OUTPUT).getVectorWriter(layerA.pendingFields(),
geomType,
layerA.dataProvider().crs())
layerA.crs())

outFeat = QgsFeature()
index = vector.spatialindex(layerB)
@@ -71,12 +71,11 @@ def processAlgorithm(self, progress):
field_names = self.getParameterValue(Dissolve.FIELD)
vlayerA = dataobjects.getObjectFromUri(
self.getParameterValue(Dissolve.INPUT))
vproviderA = vlayerA.dataProvider()

writer = self.getOutputFromName(
Dissolve.OUTPUT).getVectorWriter(
vlayerA.fields().toList(),
vproviderA.wkbType(),
vlayerA.wkbType(),
vlayerA.crs())

outFeat = QgsFeature()
@@ -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.wkbType(), 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.wkbType(),
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, QgsWkbTypes.LineString,
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().wkbType(), 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().wkbType(), 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.wkbType(),
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.wkbType(),
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.wkbType(),
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.wkbType()
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.wkbType(),
writer = output.getVectorWriter(outFields, layer.wkbType(),
layer.crs())

# Cache attributes of Layer 2
@@ -86,7 +86,7 @@ def processAlgorithm(self, progress):
layerB.pendingFields()[idxB]]

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
QgsWkbTypes.Point, layerA.dataProvider().crs())
QgsWkbTypes.Point, layerA.crs())

spatialIndex = vector.spatialindex(layerB)

@@ -66,14 +66,14 @@ def processAlgorithm(self, progress):
layer = QgsVectorLayer(paths[x], unicode(x), 'ogr')

if (len(layers) > 0):
if (layer.dataProvider().wkbType() != layers[0].dataProvider().wkbType()):
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().wkbType(),
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(
@@ -59,12 +59,11 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))
provider = layer.dataProvider()

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(
layer.fields().toList(),
provider.wkbType(),
layer.wkbType(),
layer.crs())

features = vector.features(layer)
@@ -58,7 +58,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))

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

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

def multiToSingleGeom(self, wkbType):
try:
if wkbType in (QgsWkbTypes.Point, QgsWkbTypes.MultiPoint,
QgsWkbTypes.Point25D, QgsWkbTypes.MultiPoint25D):
return QgsWkbTypes.Point
elif wkbType in (QgsWkbTypes.LineString, QgsWkbTypes.MultiLineString,
QgsWkbTypes.MultiLineString25D,
QgsWkbTypes.LineString25D):

return QgsWkbTypes.LineString
elif wkbType in (QgsWkbTypes.Polygon, QgsWkbTypes.MultiPolygon,
QgsWkbTypes.MultiPolygon25D, QgsWkbTypes.Polygon25D):

return QgsWkbTypes.Polygon
if wkbType in (Qgis.WKBPoint, Qgis.WKBMultiPoint,
Qgis.WKBPoint25D, Qgis.WKBMultiPoint25D):
return Qgis.WKBPoint
elif wkbType in (Qgis.WKBLineString, Qgis.WKBMultiLineString,
Qgis.WKBMultiLineString25D,
Qgis.WKBLineString25D):

return Qgis.WKBLineString
elif wkbType in (Qgis.WKBPolygon, Qgis.WKBMultiPolygon,
Qgis.WKBMultiPolygon25D, Qgis.WKBPolygon25D):

return Qgis.WKBPolygon
else:
return QgsWkbTypes.Unknown
return Qgis.WKBUnknown
except Exception as err:
raise GeoAlgorithmExecutionException(unicode(err))

def extractAsSingle(self, geom):
multiGeom = QgsGeometry()
geometries = []
if geom.type() == QgsWkbTypes.PointGeometry:
if geom.type() == Qgis.Point:
if geom.isMultipart():
multiGeom = geom.asMultiPoint()
for i in multiGeom:
geometries.append(QgsGeometry().fromPoint(i))
else:
geometries.append(geom)
elif geom.type() == QgsWkbTypes.LineGeometry:
elif geom.type() == Qgis.Line:
if geom.isMultipart():
multiGeom = geom.asMultiPolyline()
for i in multiGeom:
geometries.append(QgsGeometry().fromPolyline(i))
else:
geometries.append(geom)
elif geom.type() == QgsWkbTypes.PolygonGeometry:
elif geom.type() == Qgis.Polygon:
if geom.isMultipart():
multiGeom = geom.asMultiPolygon()
for i in multiGeom:
@@ -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):

0 comments on commit 06c4b07

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