Skip to content
Permalink
Browse files

Large commit with several related bug fixes:

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@13037 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer
cfarmer committed Mar 10, 2010
1 parent 63c3671 commit 24ba0309ff649edd949d8e546b6621faba7d248f
@@ -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))
@@ -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)
@@ -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()))
@@ -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):
@@ -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')
@@ -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))
@@ -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()
@@ -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)
@@ -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.
@@ -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:
@@ -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)
@@ -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:
@@ -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():
@@ -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()
@@ -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):
@@ -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)
@@ -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()
@@ -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 24ba030

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