@@ -194,42 +194,59 @@ def vectorRandom(self, n, layer, xmin, xmax, ymin, ymax):
194
194
return points
195
195
196
196
def randomize (self , inLayer , outPath , minimum , design , value ):
197
- outFeat = QgsFeature ()
198
- outFeat .initAttributes (1 )
199
- if design == self .tr ("unstratified" ):
200
- ext = inLayer .extent ()
201
- if inLayer .type () == inLayer .RasterLayer :
202
- points = self .simpleRandom (int (value ), ext , ext .xMinimum (),
203
- ext .xMaximum (), ext .yMinimum (), ext .yMaximum ())
204
- else :
205
- points = self .vectorRandom (int (value ), inLayer ,
206
- ext .xMinimum (), ext .xMaximum (), ext .yMinimum (), ext .yMaximum ())
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
197
+ outFeat = QgsFeature ()
198
+ outFeat .initAttributes (1 )
199
+ if design == self .tr ("unstratified" ):
200
+ ext = inLayer .extent ()
201
+ if inLayer .type () == inLayer .RasterLayer :
202
+ points = self .simpleRandom (int (value ), ext , ext .xMinimum (),
203
+ ext .xMaximum (), ext .yMinimum (), ext .yMaximum ())
204
+ else :
205
+ points = self .vectorRandom (int (value ), inLayer ,
206
+ ext .xMinimum (), ext .xMaximum (), ext .yMinimum (), ext .yMaximum ())
207
+ else :
208
+ points , featErrors = self .loopThruPolygons (inLayer , value , design )
209
+ if featErrors :
210
+ if len (featErrors ) >= 10 :
211
+ err_msg = "Too many features couldn't be calculated due to conversion error. "
212
+ err_msg += "Please check out message log for more info."
213
+ msgLogInstance = QgsMessageLog .instance ( )
214
+ msgLogInstance .logMessage ( "WARNING - fTools: " + self .tr ( "Random Points" ) )
215
+ msgLogInstance .logMessage ( "The following feature ids should be checked." )
216
+ for feat in featErrors :
217
+ msgLogInstance .logMessage ( "Feature id: %d" % feat .id ( ) )
218
+ msgLogInstance .logMessage ( "End of features to be checked." )
219
+ else :
220
+ features_ids = []
221
+ for feat in featErrors :
222
+ features_ids .append ( str ( feat .id ( ) ) )
223
+ erroneous_ids = ', ' .join (features_ids )
224
+ err_msg = "The following features IDs couldn't be calculated due to conversion error: %s" % erroneous_ids
225
+ self .iface .messageBar ().pushMessage ("Errors" , err_msg )
226
+ if len (points ):
227
+ crs = self .iface .mapCanvas ().mapRenderer ().destinationCrs ()
228
+ if not crs .isValid (): crs = None
229
+ fields = QgsFields ()
230
+ fields .append ( QgsField ("ID" , QVariant .Int ) )
231
+ outFeat .setFields (fields )
232
+ check = QFile (self .shapefileName )
233
+ if check .exists ():
234
+ if not QgsVectorFileWriter .deleteShapeFile (self .shapefileName ):
235
+ return
236
+ writer = QgsVectorFileWriter (self .shapefileName , self .encoding , fields , QGis .WKBPoint , crs )
237
+ idVar = 0
238
+ count = 70.00
239
+ add = ( 100.00 - 70.00 ) / len (points )
240
+ for i in points :
241
+ outFeat .setGeometry (i )
242
+ outFeat .setAttribute (0 , idVar )
243
+ writer .addFeature (outFeat )
244
+ idVar = idVar + 1
245
+ count = count + add
246
+ self .progressBar .setValue (count )
247
+ del writer
248
+ return True
249
+ return False
233
250
234
251
#
235
252
def loopThruPolygons (self , inLayer , numRand , design ):
@@ -242,6 +259,7 @@ def loopThruPolygons(self, inLayer, numRand, design):
242
259
count = 10.00
243
260
add = 60.00 / sProvider .featureCount ()
244
261
sFit = sProvider .getFeatures ()
262
+ featureErrors = []
245
263
while sFit .nextFeature (sFeat ):
246
264
sGeom = sFeat .geometry ()
247
265
if design == self .tr ("density" ):
@@ -252,13 +270,12 @@ def loopThruPolygons(self, inLayer, numRand, design):
252
270
try :
253
271
value = int (sAtMap [index ])
254
272
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 ()
273
+ featureErrors .append (sFeat )
274
+ continue
258
275
else :
259
276
value = numRand
260
277
sExt = sGeom .boundingBox ()
261
278
sPoints .extend (self .simpleRandom (value , sGeom , sExt .xMinimum (), sExt .xMaximum (), sExt .yMinimum (), sExt .yMaximum ()))
262
279
count = count + add
263
280
self .progressBar .setValue (count )
264
- return sPoints
281
+ return sPoints , featureErrors
0 commit comments