Skip to content

Commit 1ebdb69

Browse files
committedJul 10, 2013
Fix #8069
1 parent 89b3e06 commit 1ebdb69

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed
 

‎python/plugins/fTools/tools/doRandPoints.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ def accept(self):
116116
QMessageBox.information(self, self.tr("Random Points"), self.tr("Unknown layer type..."))
117117
minimum = 0.00
118118
self.progressBar.setValue(10)
119-
self.randomize(inLayer, outPath, minimum, design, value)
120-
self.progressBar.setValue(100)
121-
self.outShape.clear()
122-
addToTOC = QMessageBox.question(self, self.tr("Random Points"),
123-
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)
124-
if addToTOC == QMessageBox.Yes:
125-
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
126-
QgsMapLayerRegistry.instance().addMapLayers([self.vlayer])
127-
self.populateLayers()
119+
if self.randomize(inLayer, outPath, minimum, design, value):
120+
self.progressBar.setValue(100)
121+
self.outShape.clear()
122+
addToTOC = QMessageBox.question(self, self.tr("Random Points"),
123+
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)
124+
if addToTOC == QMessageBox.Yes:
125+
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
126+
QgsMapLayerRegistry.instance().addMapLayers([self.vlayer])
127+
self.populateLayers()
128128
self.progressBar.setValue(0)
129129
self.buttonOk.setEnabled( True )
130130

@@ -204,28 +204,32 @@ def randomize(self, inLayer, outPath, minimum, design, value):
204204
else:
205205
points = self.vectorRandom(int(value), inLayer,
206206
ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
207-
else: points = self.loopThruPolygons(inLayer, value, design)
208-
crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
209-
if not crs.isValid(): crs = None
210-
fields = QgsFields()
211-
fields.append( QgsField("ID", QVariant.Int) )
212-
outFeat.setFields(fields)
213-
check = QFile(self.shapefileName)
214-
if check.exists():
215-
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
216-
return
217-
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPoint, crs)
218-
idVar = 0
219-
count = 70.00
220-
add = ( 100.00 - 70.00 ) / len(points)
221-
for i in points:
222-
outFeat.setGeometry(i)
223-
outFeat.setAttribute(0, idVar)
224-
writer.addFeature(outFeat)
225-
idVar = idVar + 1
226-
count = count + add
227-
self.progressBar.setValue(count)
228-
del writer
207+
else:
208+
points = self.loopThruPolygons(inLayer, value, design)
209+
if len(points):
210+
crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
211+
if not crs.isValid(): crs = None
212+
fields = QgsFields()
213+
fields.append( QgsField("ID", QVariant.Int) )
214+
outFeat.setFields(fields)
215+
check = QFile(self.shapefileName)
216+
if check.exists():
217+
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
218+
return
219+
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fields, QGis.WKBPoint, crs)
220+
idVar = 0
221+
count = 70.00
222+
add = ( 100.00 - 70.00 ) / len(points)
223+
for i in points:
224+
outFeat.setGeometry(i)
225+
outFeat.setAttribute(0, idVar)
226+
writer.addFeature(outFeat)
227+
idVar = idVar + 1
228+
count = count + add
229+
self.progressBar.setValue(count)
230+
del writer
231+
return True
232+
return False
229233

230234
#
231235
def loopThruPolygons(self, inLayer, numRand, design):
@@ -234,12 +238,7 @@ def loopThruPolygons(self, inLayer, numRand, design):
234238
sGeom = QgsGeometry()
235239
sPoints = []
236240
if design == self.tr("field"):
237-
i = 0
238-
for attr in sProvider.fields():
239-
if (unicode(numRand) == attr.name()):
240-
index = i #get input field index
241-
break
242-
i += 1
241+
index = sProvider.fieldNameIndex(numRand)
243242
count = 10.00
244243
add = 60.00 / sProvider.featureCount()
245244
sFit = sProvider.getFeatures()
@@ -250,7 +249,12 @@ def loopThruPolygons(self, inLayer, numRand, design):
250249
value = int(round(numRand * sDistArea.measure(sGeom)))
251250
elif design == self.tr("field"):
252251
sAtMap = sFeat.attributes()
253-
value = sAtMap[index]
252+
try:
253+
value = int(sAtMap[index])
254+
except (ValueError,TypeError):
255+
warn_msg = self.tr("The selected field has NULL values.\nTry to select other field.")
256+
QMessageBox.warning(self, self.tr("Warning"), warn_msg)
257+
return list()
254258
else:
255259
value = numRand
256260
sExt = sGeom.boundingBox()

0 commit comments

Comments
 (0)
Please sign in to comment.