Skip to content

Commit

Permalink
[processing] Use layer crs/fields instead of provider crs/fields
Browse files Browse the repository at this point in the history
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 4, 2016
1 parent 00a8fea commit 06c4b07
Show file tree
Hide file tree
Showing 45 changed files with 94 additions and 127 deletions.
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/AddTableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/AutoincrementalField.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Centroids.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def processAlgorithm(self, progress):

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(
layer.pendingFields().toList(),
layer.fields(),
QgsWkbTypes.Point,
layer.crs())

Expand Down
9 changes: 4 additions & 5 deletions python/plugins/processing/algs/qgis/CheckValidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ConcaveHull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ConvexHull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/Dissolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/Eliminate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/EquivalentNumField.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/Explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExportGeometryInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExtractByLocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
7 changes: 3 additions & 4 deletions python/plugins/processing/algs/qgis/FieldPyculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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())
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/FieldsCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/FieldsMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT_LAYER)

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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/Intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/JoinAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/LinesIntersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions python/plugins/processing/algs/qgis/Merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand All @@ -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):
Expand All @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/MergeLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 17 additions & 17 deletions python/plugins/processing/algs/qgis/MultipartToSingleparts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 06c4b07

Please sign in to comment.