Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix #8069
  • Loading branch information
ddanielvaz committed Jul 10, 2013
1 parent 89b3e06 commit 1ebdb69
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions python/plugins/fTools/tools/doRandPoints.py
Expand Up @@ -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 )

Expand Down Expand Up @@ -204,28 +204,32 @@ def randomize(self, inLayer, outPath, minimum, design, value):
else:
points = self.vectorRandom(int(value), inLayer,
ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
else: points = self.loopThruPolygons(inLayer, value, design)
crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
if not crs.isValid(): crs = None
fields = QgsFields()
fields.append( QgsField("ID", QVariant.Int) )
outFeat.setFields(fields)
check = QFile(self.shapefileName)
if check.exists():
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
return
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPoint, crs)
idVar = 0
count = 70.00
add = ( 100.00 - 70.00 ) / len(points)
for i in points:
outFeat.setGeometry(i)
outFeat.setAttribute(0, idVar)
writer.addFeature(outFeat)
idVar = idVar + 1
count = count + add
self.progressBar.setValue(count)
del writer
else:
points = self.loopThruPolygons(inLayer, value, design)
if len(points):
crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
if not crs.isValid(): crs = None
fields = QgsFields()
fields.append( QgsField("ID", QVariant.Int) )
outFeat.setFields(fields)
check = QFile(self.shapefileName)
if check.exists():
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
return
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPoint, crs)
idVar = 0
count = 70.00
add = ( 100.00 - 70.00 ) / len(points)
for i in points:
outFeat.setGeometry(i)
outFeat.setAttribute(0, idVar)
writer.addFeature(outFeat)
idVar = idVar + 1
count = count + add
self.progressBar.setValue(count)
del writer
return True
return False

#
def loopThruPolygons(self, inLayer, numRand, design):
Expand All @@ -234,12 +238,7 @@ 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()
Expand All @@ -250,7 +249,12 @@ def loopThruPolygons(self, inLayer, numRand, design):
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):
warn_msg = self.tr("The selected field has NULL values.\nTry to select other field.")
QMessageBox.warning(self, self.tr("Warning"), warn_msg)
return list()
else:
value = numRand
sExt = sGeom.boundingBox()
Expand Down

0 comments on commit 1ebdb69

Please sign in to comment.