2010-03-08 02:20:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-01-20 22:54:27 +00:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
#
|
2011-03-07 23:29:15 +00:00
|
|
|
# fTools
|
|
|
|
# Copyright (C) 2008-2011 Carson Farmer
|
2009-01-20 22:54:27 +00:00
|
|
|
# EMAIL: carson.farmer (at) gmail.com
|
2011-03-07 23:29:15 +00:00
|
|
|
# WEB : http://www.ftools.ca/fTools.html
|
|
|
|
#
|
|
|
|
# A collection of data management and analysis tools for vector data
|
2009-01-20 22:54:27 +00:00
|
|
|
#
|
|
|
|
#-----------------------------------------------------------
|
2011-03-07 23:29:15 +00:00
|
|
|
#
|
2009-01-20 22:54:27 +00:00
|
|
|
# licensed under the terms of GNU GPL 2
|
2011-03-07 23:29:15 +00:00
|
|
|
#
|
2009-01-20 22:54:27 +00:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2011-03-07 23:29:15 +00:00
|
|
|
#
|
2009-01-20 22:54:27 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-03-07 23:29:15 +00:00
|
|
|
#
|
2009-01-20 22:54:27 +00:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-03-07 23:29:15 +00:00
|
|
|
#
|
2009-01-20 22:54:27 +00:00
|
|
|
#---------------------------------------------------------------------
|
2009-02-01 15:44:02 +00:00
|
|
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
|
|
|
|
from qgis.core import *
|
|
|
|
from random import *
|
|
|
|
import math, ftools_utils
|
2009-08-16 22:40:48 +00:00
|
|
|
from ui_frmRandPoints import Ui_Dialog
|
2009-02-01 15:44:02 +00:00
|
|
|
|
|
|
|
class Dialog(QDialog, Ui_Dialog):
|
|
|
|
def __init__(self, iface):
|
2012-01-26 00:54:56 +01:00
|
|
|
QDialog.__init__(self, iface.mainWindow())
|
2009-02-01 15:44:02 +00:00
|
|
|
self.iface = iface
|
|
|
|
self.setupUi(self)
|
|
|
|
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
|
|
|
|
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)
|
2009-01-20 22:54:27 +00:00
|
|
|
self.progressBar.setValue(0)
|
2009-12-31 09:57:28 +00:00
|
|
|
self.setWindowTitle(self.tr("Random Points"))
|
2010-05-13 22:55:59 +00:00
|
|
|
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
|
2011-11-12 13:05:27 +02:00
|
|
|
self.populateLayers()
|
|
|
|
|
|
|
|
def populateLayers( self ):
|
2010-03-10 01:11:11 +00:00
|
|
|
layers = ftools_utils.getLayerNames([QGis.Polygon, "Raster"])
|
2012-05-22 16:10:43 +03:00
|
|
|
self.inShape.blockSignals(True)
|
2011-11-12 13:05:27 +02:00
|
|
|
self.inShape.clear()
|
2012-05-22 16:10:43 +03:00
|
|
|
self.inShape.blockSignals(False)
|
2010-03-10 01:11:11 +00:00
|
|
|
self.inShape.addItems(layers)
|
2009-01-20 22:54:27 +00:00
|
|
|
|
2012-05-22 16:10:43 +03:00
|
|
|
# If input layer is changed, update field list
|
2009-02-01 15:44:02 +00:00
|
|
|
def update(self, inputLayer):
|
2009-01-20 22:54:27 +00:00
|
|
|
self.cmbField.clear()
|
2010-03-10 01:11:11 +00:00
|
|
|
changedLayer = ftools_utils.getMapLayerByName(unicode(inputLayer))
|
2009-01-20 22:54:27 +00:00
|
|
|
if changedLayer.type() == changedLayer.VectorLayer:
|
2010-03-08 02:20:27 +00:00
|
|
|
self.rdoStratified.setEnabled(True)
|
|
|
|
self.rdoDensity.setEnabled(True)
|
|
|
|
self.rdoField.setEnabled(True)
|
|
|
|
self.label_4.setEnabled(True)
|
2010-03-10 01:11:11 +00:00
|
|
|
changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
|
|
|
|
changedFields = ftools_utils.getFieldList(changedLayer)
|
2013-02-07 00:31:24 +01:00
|
|
|
for f in changedFields:
|
|
|
|
if f.typeName() == "Integer":
|
|
|
|
self.cmbField.addItem(unicode(f.name()))
|
2009-01-20 22:54:27 +00:00
|
|
|
else:
|
2010-03-08 02:20:27 +00:00
|
|
|
self.rdoUnstratified.setChecked(True)
|
|
|
|
self.rdoStratified.setEnabled(False)
|
|
|
|
self.rdoDensity.setEnabled(False)
|
|
|
|
self.rdoField.setEnabled(False)
|
|
|
|
self.spnStratified.setEnabled(False)
|
|
|
|
self.spnDensity.setEnabled(False)
|
|
|
|
self.cmbField.setEnabled(False)
|
|
|
|
self.label_4.setEnabled(False)
|
2009-01-20 22:54:27 +00:00
|
|
|
|
2012-05-22 16:26:12 +03:00
|
|
|
# when 'OK' button is pressed, gather required inputs, and initiate random points generation
|
2009-02-01 15:44:02 +00:00
|
|
|
def accept(self):
|
2010-05-13 22:55:59 +00:00
|
|
|
self.buttonOk.setEnabled( False )
|
2010-03-08 02:20:27 +00:00
|
|
|
if self.inShape.currentText() == "":
|
|
|
|
QMessageBox.information(self, self.tr("Random Points"), self.tr("No input layer specified"))
|
|
|
|
elif self.outShape.text() == "":
|
|
|
|
QMessageBox.information(self, self.tr("Random Points"), self.tr("Please specify output shapefile"))
|
|
|
|
else:
|
|
|
|
inName = self.inShape.currentText()
|
|
|
|
self.progressBar.setValue(1)
|
|
|
|
outPath = self.outShape.text()
|
|
|
|
self.progressBar.setValue(2.5)
|
2013-06-14 19:51:48 +12:00
|
|
|
outName = ftools_utils.getShapefileName( outPath )
|
2010-03-08 02:20:27 +00:00
|
|
|
self.progressBar.setValue(5)
|
2010-03-10 01:11:11 +00:00
|
|
|
mLayer = ftools_utils.getMapLayerByName(unicode(inName))
|
2010-03-08 02:20:27 +00:00
|
|
|
if mLayer.type() == mLayer.VectorLayer:
|
2012-05-22 16:26:12 +03:00
|
|
|
inLayer = ftools_utils.getVectorLayerByName(unicode(inName))
|
2010-03-08 02:20:27 +00:00
|
|
|
if self.rdoUnstratified.isChecked():
|
|
|
|
design = self.tr("unstratified")
|
|
|
|
value = self.spnUnstratified.value()
|
|
|
|
elif self.rdoStratified.isChecked():
|
|
|
|
design = self.tr("stratified")
|
|
|
|
value = self.spnStratified.value()
|
|
|
|
elif self.rdoDensity.isChecked():
|
|
|
|
design = self.tr("density")
|
|
|
|
value = self.spnDensity.value()
|
|
|
|
else:
|
|
|
|
design = self.tr("field")
|
|
|
|
value = unicode(self.cmbField.currentText())
|
|
|
|
elif mLayer.type() == mLayer.RasterLayer:
|
2012-05-22 16:26:12 +03:00
|
|
|
inLayer = ftools_utils.getRasterLayerByName(unicode(inName))
|
2010-03-08 02:20:27 +00:00
|
|
|
design = self.tr("unstratified")
|
|
|
|
value = self.spnUnstratified.value()
|
|
|
|
else:
|
|
|
|
QMessageBox.information(self, self.tr("Random Points"), self.tr("Unknown layer type..."))
|
2010-11-21 22:33:42 +00:00
|
|
|
minimum = 0.00
|
2010-03-08 02:20:27 +00:00
|
|
|
self.progressBar.setValue(10)
|
2013-07-09 20:57:53 -03:00
|
|
|
if self.randomize(inLayer, outPath, minimum, design, value):
|
|
|
|
self.progressBar.setValue(100)
|
|
|
|
self.outShape.clear()
|
2013-09-12 19:21:48 +03:00
|
|
|
if self.addToCanvasCheck.isChecked():
|
|
|
|
addCanvasCheck = ftools_utils.addShapeToCanvas(unicode(outPath))
|
|
|
|
if not addCanvasCheck:
|
|
|
|
QMessageBox.warning( self, self.tr("Random Points"), self.tr( "Error loading output shapefile:\n%s" ) % ( unicode( outPath ) ))
|
|
|
|
self.populateLayers()
|
|
|
|
else:
|
|
|
|
QMessageBox.information(self, self.tr("Random Points"),self.tr("Created output shapefile:\n%s" ) % ( unicode( outPath )))
|
2010-05-13 22:55:59 +00:00
|
|
|
self.progressBar.setValue(0)
|
|
|
|
self.buttonOk.setEnabled( True )
|
2009-02-01 15:44:02 +00:00
|
|
|
|
2009-01-20 22:54:27 +00:00
|
|
|
def outFile(self):
|
2010-03-08 02:20:27 +00:00
|
|
|
self.outShape.clear()
|
|
|
|
( self.shapefileName, self.encoding ) = ftools_utils.saveDialog( self )
|
|
|
|
if self.shapefileName is None or self.encoding is None:
|
|
|
|
return
|
2013-06-03 16:05:08 +04:00
|
|
|
self.outShape.setText( self.shapefileName )
|
2011-07-29 19:57:22 +03:00
|
|
|
|
2010-05-13 22:55:59 +00:00
|
|
|
# combine all polygons in layer to create single polygon (slow for complex polygons)
|
2010-03-08 02:20:27 +00:00
|
|
|
def createSinglePolygon(self, vlayer):
|
|
|
|
provider = vlayer.dataProvider()
|
|
|
|
feat = QgsFeature()
|
|
|
|
geom = QgsGeometry()
|
2013-02-01 01:03:19 +01:00
|
|
|
fit = provider.getFeatures()
|
|
|
|
fit.nextFeature(feat)
|
2010-06-30 22:50:17 +00:00
|
|
|
geom = QgsGeometry(feat.geometry())
|
2010-03-08 02:20:27 +00:00
|
|
|
count = 10.00
|
|
|
|
add = ( 40.00 - 10.00 ) / provider.featureCount()
|
2013-02-01 01:03:19 +01:00
|
|
|
while fit.nextFeature(feat):
|
2010-03-08 02:20:27 +00:00
|
|
|
geom = geom.combine(QgsGeometry( feat.geometry() ))
|
|
|
|
count = count + add
|
|
|
|
self.progressBar.setValue(count)
|
|
|
|
return geom
|
2011-07-29 19:57:22 +03:00
|
|
|
|
2010-05-13 22:55:59 +00:00
|
|
|
# Generate list of random points
|
2009-01-20 22:54:27 +00:00
|
|
|
def simpleRandom(self, n, bound, xmin, xmax, ymin, ymax):
|
2010-03-08 02:20:27 +00:00
|
|
|
seed()
|
|
|
|
points = []
|
|
|
|
i = 1
|
|
|
|
count = 40.00
|
2011-07-29 19:57:22 +03:00
|
|
|
if n == 0:
|
|
|
|
return []
|
2010-03-08 02:20:27 +00:00
|
|
|
add = ( 70.00 - 40.00 ) / n
|
|
|
|
while i <= n:
|
|
|
|
pGeom = QgsGeometry().fromPoint(QgsPoint(xmin + (xmax-xmin) * random(), ymin + (ymax-ymin) * random()))
|
|
|
|
if pGeom.intersects(bound):
|
|
|
|
points.append(pGeom)
|
|
|
|
i = i + 1
|
|
|
|
count = count + add
|
|
|
|
self.progressBar.setValue(count)
|
|
|
|
return points
|
2009-01-20 22:54:27 +00:00
|
|
|
|
2010-06-30 22:50:17 +00:00
|
|
|
def vectorRandom(self, n, layer, xmin, xmax, ymin, ymax):
|
|
|
|
provider = layer.dataProvider()
|
|
|
|
index = ftools_utils.createIndex(provider)
|
|
|
|
seed()
|
|
|
|
points = []
|
|
|
|
feat = QgsFeature()
|
|
|
|
i = 1
|
|
|
|
count = 40.00
|
|
|
|
add = ( 70.00 - 40.00 ) / n
|
|
|
|
while i <= n:
|
|
|
|
point = QgsPoint(xmin + (xmax-xmin) * random(), ymin + (ymax-ymin) * random())
|
|
|
|
pGeom = QgsGeometry().fromPoint(point)
|
|
|
|
ids = index.intersects(pGeom.buffer(5,5).boundingBox())
|
|
|
|
for id in ids:
|
2013-02-01 01:03:19 +01:00
|
|
|
provider.getFeatures( QgsFeatureRequest().setFilterFid( int(id) ) ).nextFeature( feat )
|
2010-06-30 22:50:17 +00:00
|
|
|
tGeom = QgsGeometry(feat.geometry())
|
|
|
|
if pGeom.intersects(tGeom):
|
|
|
|
points.append(pGeom)
|
|
|
|
i = i + 1
|
|
|
|
count = count + add
|
|
|
|
self.progressBar.setValue(count)
|
|
|
|
break
|
|
|
|
return points
|
|
|
|
|
2010-03-08 02:20:27 +00:00
|
|
|
def randomize(self, inLayer, outPath, minimum, design, value):
|
2013-07-10 21:33:19 -03:00
|
|
|
outFeat = QgsFeature()
|
|
|
|
outFeat.initAttributes(1)
|
|
|
|
if design == self.tr("unstratified"):
|
|
|
|
ext = inLayer.extent()
|
2013-07-10 22:02:29 -03:00
|
|
|
if inLayer.type() == QgsMapLayer.RasterLayer:
|
2013-07-10 21:33:19 -03:00
|
|
|
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()
|
|
|
|
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
|
2011-07-29 19:57:22 +03:00
|
|
|
|
|
|
|
#
|
2010-03-08 02:20:27 +00:00
|
|
|
def loopThruPolygons(self, inLayer, numRand, design):
|
|
|
|
sProvider = inLayer.dataProvider()
|
|
|
|
sFeat = QgsFeature()
|
|
|
|
sGeom = QgsGeometry()
|
|
|
|
sPoints = []
|
2010-06-30 22:54:16 +00:00
|
|
|
if design == self.tr("field"):
|
2013-07-09 20:57:53 -03:00
|
|
|
index = sProvider.fieldNameIndex(numRand)
|
2010-03-08 02:20:27 +00:00
|
|
|
count = 10.00
|
|
|
|
add = 60.00 / sProvider.featureCount()
|
2013-02-01 01:03:19 +01:00
|
|
|
sFit = sProvider.getFeatures()
|
2013-07-10 21:33:19 -03:00
|
|
|
featureErrors = []
|
2013-02-01 01:03:19 +01:00
|
|
|
while sFit.nextFeature(sFeat):
|
2010-03-08 02:20:27 +00:00
|
|
|
sGeom = sFeat.geometry()
|
|
|
|
if design == self.tr("density"):
|
|
|
|
sDistArea = QgsDistanceArea()
|
|
|
|
value = int(round(numRand * sDistArea.measure(sGeom)))
|
|
|
|
elif design == self.tr("field"):
|
2013-02-01 01:03:19 +01:00
|
|
|
sAtMap = sFeat.attributes()
|
2013-07-09 20:57:53 -03:00
|
|
|
try:
|
|
|
|
value = int(sAtMap[index])
|
|
|
|
except (ValueError,TypeError):
|
2013-07-10 21:33:19 -03:00
|
|
|
featureErrors.append(sFeat)
|
|
|
|
continue
|
2010-03-08 02:20:27 +00:00
|
|
|
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)
|
2013-07-10 21:33:19 -03:00
|
|
|
return sPoints, featureErrors
|