Skip to content

Commit

Permalink
Large commit with several related bug fixes:
Browse files Browse the repository at this point in the history
1) All fTools functions should now add all layers to dropdowns (including non-visible layers), 
2) All fTools functions should now be non-modal
3) Several fTools functions have been spead up slightly
4) Where possible, internal (cpp) functions have be used to replace older Python functions
5) Defining projections now also considers .qpj files
Fixes bugs #2502 and #2506, and possibly others?


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13037 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Mar 10, 2010
1 parent 781a4d1 commit f1af8c4
Show file tree
Hide file tree
Showing 16 changed files with 671 additions and 971 deletions.
12 changes: 10 additions & 2 deletions python/plugins/fTools/tools/doDefineProj.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def __init__(self, iface):
self.cmbLayer.addItems(layers)

def updateProj1(self, layerName):
self.inRef.clear()
tempLayer = ftools_utils.getVectorLayerByName(layerName)
crs = tempLayer.dataProvider().crs().toProj4()
self.inRef.insert(unicode(crs))

def updateProj2(self, layerName):
self.outRef.clear()
tempLayer = ftools_utils.getVectorLayerByName(layerName)
crs = tempLayer.dataProvider().crs().toProj4()
self.outRef.insert(unicode(crs))
Expand Down Expand Up @@ -82,11 +84,17 @@ def accept(self):
outputFile = QFile( inPath + ".prj" )
outputFile.open( QIODevice.WriteOnly | QIODevice.Text )
outputPrj = QTextStream( outputFile )
self.progressBar.setValue(70)
outputPrj << outputWkt
self.progressBar.setValue(75)
outputPrj.flush()
outputFile.close()
self.progressBar.setValue(70)
checkFile = QFile( inPath + ".qpj" )
if checkFile.exists():
checkFile.open( QIODevice.WriteOnly | QIODevice.Text )
outputPrj = QTextStream( checkFile )
outputPrj << outputWkt
outputPrj.flush()
checkFile.close()
self.progressBar.setValue(95)
vLayer.setCrs(srsDefine)
self.progressBar.setValue(100)
Expand Down
74 changes: 15 additions & 59 deletions python/plugins/fTools/tools/doJoinAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,22 @@ def __init__(self, iface):
# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
for i in range(mapCanvas.layerCount()):
layer = mapCanvas.layer(i)
if layer.type() == layer.VectorLayer:
self.inShape.addItem(layer.name())
self.joinShape.addItem(layer.name())
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
self.inShape.addItems(layers)
self.joinShape.addItems(layers)

def iupdate(self, inputLayer):
changedLayer = self.getVectorLayerByName(unicode(inputLayer))
changedField = self.getFieldList(changedLayer)
changedLayer = ftools_utils.getVectorLayerByName(unicode(inputLayer))
changedField = ftools_utils.getFieldList(changedLayer)
self.inField.clear()
for i in changedField:
self.inField.addItem(unicode(changedField[i].name()))

def jupdate(self):
inputLayer = self.joinShape.currentText()
if inputLayer != "":
changedLayer = self.getVectorLayerByName(unicode(inputLayer))
changedField = self.getFieldList(changedLayer)
changedLayer = ftools_utils.getVectorLayerByName(unicode(inputLayer))
changedField = ftools_utils.getFieldList(changedLayer)
self.joinField.clear()
for i in changedField:
self.joinField.addItem(unicode(changedField[i].name()))
Expand Down Expand Up @@ -103,18 +101,12 @@ def accept(self):
useTable = True
joinField = self.joinField.currentText()
outPath = self.outShape.text()
if outPath.contains("\\"):
outName = outPath.right((outPath.length() - outPath.lastIndexOf("\\")) - 1)
else:
outName = outPath.right((outPath.length() - outPath.lastIndexOf("/")) - 1)
if outName.endsWith(".shp"):
outName = outName.left(outName.length() - 4)
self.compute(inName, inField, joinName, joinField, outPath, keep, useTable, self.progressBar)
self.outShape.clear()
addToTOC = QMessageBox.question(self, self.tr("Join Attributes"), self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?").arg( unicode(self.shapefileName) ), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
if addToTOC == QMessageBox.Yes:
vlayer = QgsVectorLayer(self.shapefileName, outName, "ogr")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( outPath ) ))
self.progressBar.setValue(0)

def outFile(self):
Expand Down Expand Up @@ -152,11 +144,11 @@ def updateTableFields(self):
table = None

def compute(self, inName, inField, joinName, joinField, outName, keep, useTable, progressBar):
layer1 = self.getVectorLayerByName(inName)
layer1 = ftools_utils.getVectorLayerByName(inName)
provider1 = layer1.dataProvider()
allAttrs = provider1.attributeIndexes()
provider1.select(allAttrs)
fieldList1 = self.getFieldList(layer1).values()
fieldList1 = ftools_utils.getFieldList(layer1).values()
index1 = provider1.fieldNameIndex(inField)
if useTable:
f = open(unicode(joinName), 'rb')
Expand All @@ -168,11 +160,11 @@ def compute(self, inName, inField, joinName, joinField, outName, keep, useTable,
table = map(lambda f: map(func, f), table)

else:
layer2 = self.getVectorLayerByName(joinName)
layer2 = ftools_utils.getVectorLayerByName(joinName)
provider2 = layer2.dataProvider()
allAttrs = provider2.attributeIndexes()
provider2.select(allAttrs)
fieldList2 = self.getFieldList(layer2)
fieldList2 = ftools_utils.getFieldList(layer2)
index2 = provider2.fieldNameIndex(joinField)
fieldList2 = self.testForUniqueness(fieldList1, fieldList2.values())
seq = range(0, len(fieldList1) + len(fieldList2))
Expand All @@ -185,7 +177,6 @@ def compute(self, inName, inField, joinName, joinField, outName, keep, useTable,
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
return
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)

inFeat = QgsFeature()
outFeat = QgsFeature()
joinFeat = QgsFeature()
Expand All @@ -196,7 +187,7 @@ def compute(self, inName, inField, joinName, joinField, outName, keep, useTable,
count = 0
provider1.rewind()
while provider1.nextFeature(inFeat):
inGeom = inFeat.geometry()
inGeom = QgsGeometry(inFeat.geometry())
atMap1 = inFeat.attributeMap()
outFeat.setAttributeMap(atMap1)
outFeat.setGeometry(inGeom)
Expand Down Expand Up @@ -265,45 +256,10 @@ def testForUniqueness(self, fieldList1, fieldList2):
for i in fieldList1:
for j in fieldList2:
if j.name() == i.name():
j = self.createUniqueFieldName(j)
j = ftools_utils.createUniqueFieldName(j)
changed = True
return fieldList2

def createUniqueFieldName(self, field):
check = field.name().right(2)
if check.startsWith("_"):
(val,test) = check.right(1).toInt()
if test:
if val < 2:
val = 2
else:
val = val + 1
field.setName(field.name().left(len(field.name())-1) + unicode(val))
else:
field.setName(field.name() + "_2")
else:
field.setName(field.name() + "_2")
return field

def getVectorLayerByName(self, myName):
mc = self.iface.mapCanvas()
nLayers = mc.layerCount()
for l in range(nLayers):
layer = mc.layer(l)
if layer.name() == unicode(myName):
vlayer = QgsVectorLayer(unicode(layer.source()), unicode(myName), unicode(layer.dataProvider().name()))
if vlayer.isValid():
return vlayer
else:
QMessageBox.information(self, self.tr("Join Attributes"), self.tr("Vector layer is not valid"))

def getFieldList(self, vlayer):
fProvider = vlayer.dataProvider()
feat = QgsFeature()
allAttrs = fProvider.attributeIndexes()
fProvider.select(allAttrs)
myFields = fProvider.fields()
return myFields
def dbfreader(self, f):
"""Returns an iterator over records in a Xbase DBF file.
Expand Down
6 changes: 2 additions & 4 deletions python/plugins/fTools/tools/doMeanCoords.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def __init__(self, iface, function):
# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
for i in range(mapCanvas.layerCount()):
layer = mapCanvas.layer(i)
if layer.type() == layer.VectorLayer:
self.inShape.addItem(layer.name())
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
self.inShape.addItems(layers)

def updateUi(self):
if self.function == 1:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/fTools/tools/doPointsInPolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def compute(self, inPoly, inPts, inField, outPath, progressBar):
index = polyProvider.fieldNameIndex(unicode(inField))
if index == -1:
index = polyProvider.fieldCount()
field = QgsField(unicode(inField), QVariant.Int, "real", 24, 15, self.tr("point count field"))
field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15, self.tr("point count field"))
fieldList[index] = field
sRs = polyProvider.crs()
check = QFile(self.shapefileName)
Expand Down
46 changes: 6 additions & 40 deletions python/plugins/fTools/tools/doRandPoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ def __init__(self, iface):
self.progressBar.setValue(0)
self.setWindowTitle(self.tr("Random Points"))
self.mapCanvas = self.iface.mapCanvas()
for i in range(self.mapCanvas.layerCount()):
layer = self.mapCanvas.layer(i)
if (layer.type() == layer.VectorLayer and layer.geometryType() == QGis.Polygon) or layer.type() == layer.RasterLayer:
self.inShape.addItem(layer.name())
layers = ftools_utils.getLayerNames([QGis.Polygon, "Raster"])
self.inShape.addItems(layers)

# If input layer is changed, update field list
def update(self, inputLayer):
self.cmbField.clear()
changedLayer = self.getMapLayerByName(inputLayer)
changedLayer = ftools_utils.getMapLayerByName(unicode(inputLayer))
if changedLayer.type() == changedLayer.VectorLayer:
self.rdoStratified.setEnabled(True)
self.rdoDensity.setEnabled(True)
self.rdoField.setEnabled(True)
self.label_4.setEnabled(True)
changedLayer = self.getVectorLayerByName(inputLayer)
changedFields = self.getFieldList(changedLayer)
changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
changedFields = ftools_utils.getFieldList(changedLayer)
for i in changedFields:
self.cmbField.addItem(unicode(changedFields[i].name()))
else:
Expand Down Expand Up @@ -95,7 +93,7 @@ def accept(self):
if outName.endsWith(".shp"):
outName = outName.left(outName.length() - 4)
self.progressBar.setValue(5)
mLayer = self.getMapLayerByName(unicode(inName))
mLayer = ftools_utils.getMapLayerByName(unicode(inName))
if mLayer.type() == mLayer.VectorLayer:
inLayer = QgsVectorLayer(unicode(mLayer.source()), unicode(mLayer.name()), unicode(mLayer.dataProvider().name()))
if self.rdoUnstratified.isChecked():
Expand Down Expand Up @@ -174,38 +172,6 @@ def simpleRandom(self, n, bound, xmin, xmax, ymin, ymax):
count = count + add
self.progressBar.setValue(count)
return points

# Get vector layer by name from TOC
def getVectorLayerByName(self, myName):
mc = self.mapCanvas
nLayers = mc.layerCount()
for l in range(nLayers):
layer = mc.layer(l)
if layer.name() == unicode(myName):
vlayer = QgsVectorLayer(unicode(layer.source()), unicode(myName), unicode(layer.dataProvider().name()))
if vlayer.isValid():
return vlayer
else:
QMessageBox.information(self, self.tr("Random Points"), self.tr("Vector layer is not valid"))

# Get map layer by name from TOC
def getMapLayerByName(self, myName):
mc = self.mapCanvas
nLayers = mc.layerCount()
for l in range(nLayers):
layer = mc.layer(l)
if layer.name() == unicode(myName):
if layer.isValid():
return layer
# Retrieve the field map of a vector Layer
def getFieldList(self, vlayer):
fProvider = vlayer.dataProvider()
feat = QgsFeature()
allAttrs = fProvider.attributeIndexes()
fProvider.select(allAttrs)
myFields = fProvider.fields()
return myFields


def randomize(self, inLayer, outPath, minimum, design, value):
outFeat = QgsFeature()
Expand Down
24 changes: 6 additions & 18 deletions python/plugins/fTools/tools/doRandom.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *

import ftools_utils
from ui_frmRandom import Ui_Dialog
import random
class Dialog(QDialog, Ui_Dialog):
Expand All @@ -16,13 +17,11 @@ def __init__(self, iface):
# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
for i in range(mapCanvas.layerCount()):
layer = mapCanvas.layer(i)
if layer.type() == layer.VectorLayer:
self.inShape.addItem(layer.name())
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
self.inShape.addItems(layers)

def changed(self, inputLayer):
changedLayer = self.getVectorLayerByName(inputLayer)
changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
changedProvider = changedLayer.dataProvider()
upperVal = changedProvider.featureCount()
self.spnNumber.setMaximum(upperVal)
Expand All @@ -34,7 +33,7 @@ def accept(self):
self.progressBar.setValue(10)
inName = self.inShape.currentText()
self.progressBar.setValue(20)
layer = self.getVectorLayerByName(inName)
layer = ftools_utils.getVectorLayerByName(inName)
self.progressBar.setValue(30)
if self.rdoNumber.isChecked():
value = self.spnNumber.value()
Expand All @@ -51,14 +50,3 @@ def accept(self):
self.progressBar.setValue(100)
layer.setSelectedFeatures(selran)
self.progressBar.setValue(0)

#Gets vector layer by layername in canvas
#Return: QgsVectorLayer
def getVectorLayerByName(self, myName):
mc = self.iface.mapCanvas()
nLayers = mc.layerCount()
for l in range(nLayers):
layer = mc.layer(l)
if layer.name() == unicode(myName):
if layer.isValid():
return layer

0 comments on commit f1af8c4

Please sign in to comment.