Skip to content

Commit

Permalink
Update python code
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 1, 2016
1 parent b6779f6 commit 4a7a8ff
Show file tree
Hide file tree
Showing 33 changed files with 95 additions and 62 deletions.
16 changes: 15 additions & 1 deletion python/core/qgsfields.sip
Expand Up @@ -157,9 +157,10 @@ class QgsFields
%End

/**
* Look up field's index from the field name.
* Get the field index from the field name.
* This method takes is case sensitive and only matches the data source
* name of the field.
* Alias for indexOf
*
* @param fieldName The name of the field.
*
Expand All @@ -168,6 +169,19 @@ class QgsFields
*/
int indexFromName( const QString& fieldName ) const;

/**
* Get the field index from the field name.
* This method takes is case sensitive and only matches the data source
* name of the field.
*
* @param fieldName The name of the field.
*
* @return The field index if found or -1 in case it cannot be found.
* @see lookupField For a more tolerant alternative.
* @note Added in QGIS 3.0
*/
int indexOf( const QString& fieldName ) const;

/**
* Look up field's index from the field name.
* This method matches in the following order:
Expand Down
Expand Up @@ -86,7 +86,7 @@ def processAlgorithm(self, progress):

outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)

index = layer.fieldNameIndex(fieldName)
index = layer.fields().lookupField(fieldName)

sumValue = 0
minValue = 0
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Buffer.py
Expand Up @@ -35,7 +35,7 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve,
segments, endCapStyle=1, joinStyle=1, mitreLimit=2):

if useField:
field = layer.fieldNameIndex(field)
field = layer.fields().lookupField(field)

outFeat = QgsFeature()

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ConvexHull.py
Expand Up @@ -78,7 +78,7 @@ def processAlgorithm(self, progress):

f = QgsField('value', QVariant.String, '', 255)
if useField:
index = layer.fieldNameIndex(fieldName)
index = layer.fields().lookupField(fieldName)
fType = layer.fields()[index].type()
if fType in [QVariant.Int, QVariant.UInt, QVariant.LongLong, QVariant.ULongLong]:
f.setType(fType)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/DeleteColumn.py
Expand Up @@ -52,7 +52,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
idx = layer.fieldNameIndex(self.getParameterValue(self.COLUMN))
idx = layer.fields().lookupField(self.getParameterValue(self.COLUMN))

fields = layer.fields()
fields.remove(idx)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Dissolve.py
Expand Up @@ -127,7 +127,7 @@ def processAlgorithm(self, progress):
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
else:
field_indexes = [vlayerA.fieldNameIndex(f) for f in field_names.split(';')]
field_indexes = [vlayerA.fields().lookupField(f) for f in field_names.split(';')]

attribute_dict = {}
geometry_dict = defaultdict(lambda: [])
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/EquivalentNumField.py
Expand Up @@ -54,7 +54,7 @@ def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
vlayer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
fieldindex = vlayer.fieldNameIndex(fieldname)
fieldindex = vlayer.fields().lookupField(fieldname)
fields = vlayer.fields()
fields.append(QgsField('NUM_FIELD', QVariant.Int))
writer = output.getVectorWriter(fields, vlayer.wkbType(),
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExtractByAttribute.py
Expand Up @@ -87,7 +87,7 @@ def processAlgorithm(self, progress):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
layer.wkbType(), layer.crs())

idx = layer.fieldNameIndex(fieldName)
idx = layer.fields().lookupField(fieldName)
fieldType = fields[idx].type()

if fieldType != QVariant.String and operator in self.OPERATORS[-2:]:
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/JoinAttributes.py
Expand Up @@ -70,10 +70,10 @@ def processAlgorithm(self, progress):
field2 = self.getParameterValue(self.TABLE_FIELD_2)

layer = dataobjects.getObjectFromUri(input)
joinField1Index = layer.fieldNameIndex(field)
joinField1Index = layer.fields().lookupField(field)

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

outFields = vector.combineVectorFields(layer, layer2)
writer = output.getVectorWriter(outFields, layer.wkbType(),
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/LinesIntersection.py
Expand Up @@ -79,8 +79,8 @@ def processAlgorithm(self, progress):
fieldA = self.getParameterValue(self.FIELD_A)
fieldB = self.getParameterValue(self.FIELD_B)

idxA = layerA.fieldNameIndex(fieldA)
idxB = layerB.fieldNameIndex(fieldB)
idxA = layerA.fields().lookupField(fieldA)
idxB = layerB.fields().lookupField(fieldB)

fieldList = [layerA.fields()[idxA],
layerB.fields()[idxB]]
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/MeanCoords.py
Expand Up @@ -79,12 +79,12 @@ def processAlgorithm(self, progress):
if weightField is None:
weightIndex = -1
else:
weightIndex = layer.fieldNameIndex(weightField)
weightIndex = layer.fields().lookupField(weightField)

if uniqueField is None:
uniqueIndex = -1
else:
uniqueIndex = layer.fieldNameIndex(uniqueField)
uniqueIndex = layer.fields().lookupField(uniqueField)

fieldList = [QgsField('MEAN_X', QVariant.Double, '', 24, 15),
QgsField('MEAN_Y', QVariant.Double, '', 24, 15),
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/PointDistance.py
Expand Up @@ -122,8 +122,8 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField,

index = vector.spatialindex(targetLayer)

inIdx = inLayer.fieldNameIndex(inField)
outIdx = targetLayer.fieldNameIndex(targetField)
inIdx = inLayer.fields().lookupField(inField)
outIdx = targetLayer.fields().lookupField(targetField)

distArea = QgsDistanceArea()

Expand Down Expand Up @@ -162,7 +162,7 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField,
nPoints, progress):
index = vector.spatialindex(targetLayer)

inIdx = inLayer.fieldNameIndex(inField)
inIdx = inLayer.fields().lookupField(inField)

distArea = QgsDistanceArea()

Expand Down
Expand Up @@ -65,7 +65,7 @@ def processAlgorithm(self, progress):
fields = polyLayer.fields()
fields.append(QgsField(fieldName, QVariant.Int))

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

Expand Down
Expand Up @@ -68,7 +68,7 @@ def processAlgorithm(self, progress):
polyLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.POLYGONS))
pointLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.POINTS))
fieldName = self.getParameterValue(self.FIELD)
fieldIdx = pointLayer.fieldNameIndex(self.getParameterValue(self.WEIGHT))
fieldIdx = pointLayer.fields().lookupField(self.getParameterValue(self.WEIGHT))

fields = polyLayer.fields()
fields.append(QgsField(fieldName, QVariant.Int))
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/PointsLayerFromTable.py
Expand Up @@ -65,8 +65,8 @@ def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
fields = vlayer.fields()
writer = output.getVectorWriter(fields, QgsWkbTypes.Point, self.crs)
xfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.XFIELD))
yfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.YFIELD))
xfieldindex = vlayer.fields().lookupField(self.getParameterValue(self.XFIELD))
yfieldindex = vlayer.fields().lookupField(self.getParameterValue(self.YFIELD))

crsId = self.getParameterValue(self.TARGET_CRS)
targetCrs = QgsCoordinateReferenceSystem()
Expand Down
Expand Up @@ -71,7 +71,7 @@ def processAlgorithm(self, progress):
field = self.getParameterValue(self.FIELD)
method = self.getParameterValue(self.METHOD)

index = layer.fieldNameIndex(field)
index = layer.fields().lookupField(field)

features = vector.features(layer)
featureCount = len(features)
Expand Down
Expand Up @@ -83,7 +83,7 @@ def processAlgorithm(self, progress):
method = self.getParameterValue(self.METHOD)

layer.removeSelection()
index = layer.fieldNameIndex(field)
index = layer.fields().lookupField(field)

unique = vector.getUniqueValues(layer, index)
featureCount = layer.featureCount()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SelectByAttribute.py
Expand Up @@ -86,7 +86,7 @@ def processAlgorithm(self, progress):

fields = layer.fields()

idx = layer.fieldNameIndex(fieldName)
idx = layer.fields().lookupField(fieldName)
fieldType = fields[idx].type()

if fieldType != QVariant.String and operator in self.OPERATORS[-2:]:
Expand Down
Expand Up @@ -75,7 +75,7 @@ def processAlgorithm(self, progress):
inGeom = QgsGeometry()
outGeom = QgsGeometry()

index = layer.fieldNameIndex(fieldName)
index = layer.fields().lookupField(fieldName)
unique = vector.getUniqueValues(layer, index)

current = 0
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/StatisticsByCategories.py
Expand Up @@ -62,8 +62,8 @@ def processAlgorithm(self, progress):
categoriesFieldName = self.getParameterValue(self.CATEGORIES_FIELD_NAME)

output = self.getOutputFromName(self.OUTPUT)
valuesField = layer.fieldNameIndex(valuesFieldName)
categoriesField = layer.fieldNameIndex(categoriesFieldName)
valuesField = layer.fields().lookupField(valuesFieldName)
categoriesField = layer.fields().lookupField(categoriesFieldName)

features = vector.features(layer)
total = 100.0 / len(features)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/TextToFloat.py
Expand Up @@ -53,7 +53,7 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
fieldName = self.getParameterValue(self.FIELD)
idx = layer.fieldNameIndex(fieldName)
idx = layer.fields().lookupField(fieldName)

fields = layer.fields()
fields[idx] = QgsField(fieldName, QVariant.Double, '', 24, 15)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/UniqueValues.py
Expand Up @@ -69,7 +69,7 @@ def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
fieldName = self.getParameterValue(self.FIELD_NAME)
outputFile = self.getOutputValue(self.OUTPUT)
values = vector.getUniqueValues(layer, layer.fieldNameIndex(fieldName))
values = vector.getUniqueValues(layer, layer.fields().lookupField(fieldName))
self.createHTML(outputFile, values)
self.setOutputValue(self.TOTAL_VALUES, len(values))
self.setOutputValue(self.UNIQUE_VALUES, ';'.join([str(v) for v in
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/VectorSplit.py
Expand Up @@ -67,7 +67,7 @@ def processAlgorithm(self, progress):

mkdir(directory)

fieldIndex = layer.fieldNameIndex(fieldName)
fieldIndex = layer.fields().lookupField(fieldName)
uniqueValues = vector.uniqueValues(layer, fieldIndex)
baseName = os.path.join(directory, '{0}_{1}'.format(layer.name(), fieldName))

Expand Down
Expand Up @@ -14,8 +14,8 @@
writer = VectorWriter(N_unique_values, None, fields, layer.wkbType(),
layer.crs())

class_field_index = layer.fieldNameIndex(class_field)
value_field_index = layer.fieldNameIndex(value_field)
class_field_index = layer.fields().lookupField(class_field)
value_field_index = layer.fields().lookupField(value_field)

outFeat = QgsFeature()
classes = {}
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -156,7 +156,7 @@ def resolveFieldIndex(layer, attr):
if isinstance(attr, int):
return attr
else:
index = layer.fieldNameIndex(str(attr))
index = layer.fields().lookupField(attr)
if index == -1:
raise ValueError('Wrong field name')
return index
Expand Down Expand Up @@ -236,7 +236,7 @@ def found(name):


def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
idx = layer.fieldNameIndex(fieldName)
idx = layer.fields().lookupField(fieldName)
if idx == -1:
fn = createUniqueFieldName(fieldName, fieldList)
field = QgsField(fn, QVariant.Double, '', fieldLen, fieldPrec)
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsfields.cpp
Expand Up @@ -173,7 +173,12 @@ int QgsFields::fieldOriginIndex( int fieldIdx ) const
return d->fields[fieldIdx].originIndex;
}

int QgsFields::indexFromName( const QString &fieldName ) const
int QgsFields::indexFromName( const QString& fieldName ) const
{
return d->nameToIndex.value( fieldName, -1 );
}

int QgsFields::indexOf( const QString& fieldName ) const
{
return d->nameToIndex.value( fieldName, -1 );
}
Expand Down
16 changes: 15 additions & 1 deletion src/core/qgsfields.h
Expand Up @@ -121,9 +121,10 @@ class CORE_EXPORT QgsFields
int fieldOriginIndex( int fieldIdx ) const;

/**
* Look up field's index from the field name.
* Get the field index from the field name.
* This method takes is case sensitive and only matches the data source
* name of the field.
* Alias for indexOf
*
* @param fieldName The name of the field.
*
Expand All @@ -132,6 +133,19 @@ class CORE_EXPORT QgsFields
*/
int indexFromName( const QString& fieldName ) const;

/**
* Get the field index from the field name.
* This method takes is case sensitive and only matches the data source
* name of the field.
*
* @param fieldName The name of the field.
*
* @return The field index if found or -1 in case it cannot be found.
* @see lookupField For a more tolerant alternative.
* @note Added in QGIS 3.0
*/
int indexOf( const QString& fieldName ) const;

/**
* Look up field's index from the field name.
* This method matches in the following order:
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/offlineditingtestbase.py
Expand Up @@ -152,7 +152,7 @@ def test_updateFeatures(self):
# Edit feature 2
feat2 = self._getFeatureByAttribute(offline_layer, 'name', "'name 2'")
self.assertTrue(offline_layer.startEditing())
self.assertTrue(offline_layer.changeAttributeValue(feat2.id(), offline_layer.fieldNameIndex('name'), 'name 2 edited'))
self.assertTrue(offline_layer.changeAttributeValue(feat2.id(), offline_layer.fields().lookupField('name'), 'name 2 edited'))
self.assertTrue(offline_layer.changeGeometry(feat2.id(), QgsGeometry.fromPoint(QgsPoint(33.0, 60.0))))
self.assertTrue(offline_layer.commitChanges())
feat2 = self._getFeatureByAttribute(offline_layer, 'name', "'name 2 edited'")
Expand Down Expand Up @@ -182,11 +182,11 @@ def test_updateFeatures(self):
# Edit feature 2
feat2 = self._getFeatureByAttribute(offline_layer, 'name', "'name 2 edited'")
self.assertTrue(offline_layer.startEditing())
self.assertTrue(offline_layer.changeAttributeValue(feat2.id(), offline_layer.fieldNameIndex('name'), 'name 2'))
self.assertTrue(offline_layer.changeAttributeValue(feat2.id(), offline_layer.fields().lookupField('name'), 'name 2'))
self.assertTrue(offline_layer.changeGeometry(feat2.id(), QgsGeometry.fromPoint(TEST_FEATURES[1][2])))
# Edit feat 4
feat4 = self._getFeatureByAttribute(offline_layer, 'name', "'name 4'")
self.assertTrue(offline_layer.changeAttributeValue(feat4.id(), offline_layer.fieldNameIndex('name'), 'name 4 edited'))
self.assertTrue(offline_layer.changeAttributeValue(feat4.id(), offline_layer.fields().lookupField('name'), 'name 4 edited'))
self.assertTrue(offline_layer.commitChanges())
# Sync
ol.synchronize()
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_provider_mssql.py
Expand Up @@ -72,13 +72,13 @@ def testDateTimeTypes(self):

f = next(vl.getFeatures(QgsFeatureRequest()))

date_idx = vl.fieldNameIndex('date_field')
date_idx = vl.fields().lookupField('date_field')
assert isinstance(f.attributes()[date_idx], QDate)
self.assertEqual(f.attributes()[date_idx], QDate(2004, 3, 4))
time_idx = vl.fieldNameIndex('time_field')
time_idx = vl.fields().lookupField('time_field')
assert isinstance(f.attributes()[time_idx], QTime)
self.assertEqual(f.attributes()[time_idx], QTime(13, 41, 52))
datetime_idx = vl.fieldNameIndex('datetime_field')
datetime_idx = vl.fields().lookupField('datetime_field')
assert isinstance(f.attributes()[datetime_idx], QDateTime)
self.assertEqual(f.attributes()[datetime_idx], QDateTime(
QDate(2004, 3, 4), QTime(13, 41, 52)))
Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_provider_oracle.py
Expand Up @@ -91,10 +91,10 @@ def testDateTimeTypes(self):

f = next(vl.getFeatures(QgsFeatureRequest()))

date_idx = vl.fieldNameIndex('date_field')
date_idx = vl.fields().lookupField('date_field')
self.assertTrue(isinstance(f.attributes()[date_idx], QDate))
self.assertEqual(f.attributes()[date_idx], QDate(2004, 3, 4))
datetime_idx = vl.fieldNameIndex('datetime_field')
datetime_idx = vl.fields().lookupField('datetime_field')
self.assertTrue(isinstance(f.attributes()[datetime_idx], QDateTime))
self.assertEqual(f.attributes()[datetime_idx], QDateTime(
QDate(2004, 3, 4), QTime(13, 41, 52)))
Expand Down

0 comments on commit 4a7a8ff

Please sign in to comment.