Skip to content
Permalink
Browse files

Merge pull request #711 from ddanielvaz/bugfixes

Bugfixes for fTools.

Fix #8218
Fix #8069
Fix #7073
  • Loading branch information
NathanW2 committed Jul 11, 2013
2 parents 9ed1803 + 62b30fc commit 56210eb0b5c7914d246b1c82ad48e676b00b6557
Showing with 67 additions and 32 deletions.
  1. +18 −4 python/plugins/fTools/tools/doIntersectLines.py
  2. +49 −28 python/plugins/fTools/tools/doRandPoints.py
@@ -139,11 +139,16 @@ def compute(self, line1, line2, field1, field2, outPath, progressBar):
inFeat = QgsFeature()
inFeatB = QgsFeature()
outFeat = QgsFeature()
outFields = QgsFields()
outFields.append(field1)
outFields.append(field2)
outFeat.setFields(outFields)
start = 15.00
add = 85.00 / layer1.featureCount()

index = ftools_utils.createIndex( provider2 )

singlelayer_tempList = []
fit1 = provider1.getFeatures( QgsFeatureRequest().setSubsetOfAttributes([index1]) )
while fit1.nextFeature(inFeat):
inGeom = inFeat.geometry()
@@ -165,10 +170,19 @@ def compute(self, line1, line2, field1, field2, outPath, progressBar):
tempList.append(tempGeom.asPoint())

for j in tempList:
outFeat.setGeometry(tempGeom.fromPoint(j))
outFeat.setAttribute(0, v1)
outFeat.setAttribute(1, v2)
writer.addFeature(outFeat)
# if same layer, avoid insert duplicated points
if line1 == line2:
if not j in singlelayer_tempList:
singlelayer_tempList.append(j)
outFeat.setGeometry(tempGeom.fromPoint(j))
outFeat.setAttribute(0, v1)
outFeat.setAttribute(1, v2)
writer.addFeature(outFeat)
else:
outFeat.setGeometry(tempGeom.fromPoint(j))
outFeat.setAttribute(0, v1)
outFeat.setAttribute(1, v2)
writer.addFeature(outFeat)

start = start + add
progressBar.setValue(start)
@@ -116,15 +116,15 @@ def accept(self):
QMessageBox.information(self, self.tr("Random Points"), self.tr("Unknown layer type..."))
minimum = 0.00
self.progressBar.setValue(10)
self.randomize(inLayer, outPath, minimum, design, value)
self.progressBar.setValue(100)
self.outShape.clear()
addToTOC = QMessageBox.question(self, self.tr("Random Points"),
self.tr("Created output point shapefile:\n%s\n\nWould you like to add the new layer to the TOC?") % (outPath), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
if addToTOC == QMessageBox.Yes:
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayers([self.vlayer])
self.populateLayers()
if self.randomize(inLayer, outPath, minimum, design, value):
self.progressBar.setValue(100)
self.outShape.clear()
addToTOC = QMessageBox.question(self, self.tr("Random Points"),
self.tr("Created output point shapefile:\n%s\n\nWould you like to add the new layer to the TOC?") % (outPath), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
if addToTOC == QMessageBox.Yes:
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayers([self.vlayer])
self.populateLayers()
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

@@ -194,17 +194,36 @@ def vectorRandom(self, n, layer, xmin, xmax, ymin, ymax):
return points

def randomize(self, inLayer, outPath, minimum, design, value):
outFeat = QgsFeature()
outFeat.initAttributes(1)
if design == self.tr("unstratified"):
ext = inLayer.extent()
if inLayer.type() == inLayer.RasterLayer:
points = self.simpleRandom(int(value), ext, ext.xMinimum(),
ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
else:
points = self.vectorRandom(int(value), inLayer,
ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
else: points = self.loopThruPolygons(inLayer, value, design)
outFeat = QgsFeature()
outFeat.initAttributes(1)
if design == self.tr("unstratified"):
ext = inLayer.extent()
if inLayer.type() == QgsMapLayer.RasterLayer:
points = self.simpleRandom(int(value), ext, ext.xMinimum(),
ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
else:
points = self.vectorRandom(int(value), inLayer,
ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
else:
points, featErrors = self.loopThruPolygons(inLayer, value, design)
if featErrors:
if len(featErrors) >= 10:
err_msg = "Too many features couldn't be calculated due to conversion error. "
err_msg += "Please check out message log for more info."
msgLogInstance = QgsMessageLog.instance( )
msgLogInstance.logMessage( "WARNING - fTools: " + self.tr( "Random Points" ) )
msgLogInstance.logMessage( "The following feature ids should be checked." )
for feat in featErrors:
msgLogInstance.logMessage( "Feature id: %d" % feat.id( ) )
msgLogInstance.logMessage( "End of features to be checked." )
else:
features_ids = []
for feat in featErrors:
features_ids.append( str( feat.id( ) ) )
erroneous_ids = ', '.join(features_ids)
err_msg = "The following features IDs couldn't be calculated due to conversion error: %s" % erroneous_ids
self.iface.messageBar().pushMessage("Errors", err_msg)
if len(points):
crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
if not crs.isValid(): crs = None
fields = QgsFields()
@@ -226,6 +245,8 @@ def randomize(self, inLayer, outPath, minimum, design, value):
count = count + add
self.progressBar.setValue(count)
del writer
return True
return False

#
def loopThruPolygons(self, inLayer, numRand, design):
@@ -234,27 +255,27 @@ def loopThruPolygons(self, inLayer, numRand, design):
sGeom = QgsGeometry()
sPoints = []
if design == self.tr("field"):
i = 0
for attr in sProvider.fields():
if (unicode(numRand) == attr.name()):
index = i #get input field index
break
i += 1
index = sProvider.fieldNameIndex(numRand)
count = 10.00
add = 60.00 / sProvider.featureCount()
sFit = sProvider.getFeatures()
featureErrors = []
while sFit.nextFeature(sFeat):
sGeom = sFeat.geometry()
if design == self.tr("density"):
sDistArea = QgsDistanceArea()
value = int(round(numRand * sDistArea.measure(sGeom)))
elif design == self.tr("field"):
sAtMap = sFeat.attributes()
value = sAtMap[index]
try:
value = int(sAtMap[index])
except (ValueError,TypeError):
featureErrors.append(sFeat)
continue
else:
value = numRand
sExt = sGeom.boundingBox()
sPoints.extend(self.simpleRandom(value, sGeom, sExt.xMinimum(), sExt.xMaximum(), sExt.yMinimum(), sExt.yMaximum()))
count = count + add
self.progressBar.setValue(count)
return sPoints
return sPoints, featureErrors

0 comments on commit 56210eb

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