@@ -116,15 +116,15 @@ def accept(self):
116
116
QMessageBox .information (self , self .tr ("Random Points" ), self .tr ("Unknown layer type..." ))
117
117
minimum = 0.00
118
118
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 \n Would 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 \n Would 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 ()
128
128
self .progressBar .setValue (0 )
129
129
self .buttonOk .setEnabled ( True )
130
130
@@ -204,28 +204,32 @@ def randomize(self, inLayer, outPath, minimum, design, value):
204
204
else :
205
205
points = self .vectorRandom (int (value ), inLayer ,
206
206
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
229
233
230
234
#
231
235
def loopThruPolygons (self , inLayer , numRand , design ):
@@ -234,12 +238,7 @@ def loopThruPolygons(self, inLayer, numRand, design):
234
238
sGeom = QgsGeometry ()
235
239
sPoints = []
236
240
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 )
243
242
count = 10.00
244
243
add = 60.00 / sProvider .featureCount ()
245
244
sFit = sProvider .getFeatures ()
@@ -250,7 +249,12 @@ def loopThruPolygons(self, inLayer, numRand, design):
250
249
value = int (round (numRand * sDistArea .measure (sGeom )))
251
250
elif design == self .tr ("field" ):
252
251
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.\n Try to select other field." )
256
+ QMessageBox .warning (self , self .tr ("Warning" ), warn_msg )
257
+ return list ()
254
258
else :
255
259
value = numRand
256
260
sExt = sGeom .boundingBox ()
0 commit comments