2010-03-10 01:11:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-03-07 23:29:15 +00:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
#
|
|
|
|
# fTools
|
|
|
|
# Copyright (C) 2008-2011 Carson Farmer
|
|
|
|
# EMAIL: carson.farmer (at) gmail.com
|
|
|
|
# WEB : http://www.ftools.ca/fTools.html
|
|
|
|
#
|
|
|
|
# A collection of data management and analysis tools for vector data
|
|
|
|
#
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#
|
|
|
|
# licensed under the terms of GNU GPL 2
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#---------------------------------------------------------------------
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from PyQt4.QtCore import QObject, SIGNAL
|
|
|
|
from PyQt4.QtGui import QDialog, QDialogButtonBox, QMessageBox
|
|
|
|
from qgis.core import QGis
|
2009-02-01 15:44:02 +00:00
|
|
|
|
2010-03-10 01:11:11 +00:00
|
|
|
import ftools_utils
|
2009-08-16 22:40:48 +00:00
|
|
|
from ui_frmRandom import Ui_Dialog
|
2009-02-01 15:44:02 +00:00
|
|
|
import random
|
|
|
|
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
|
|
|
|
# Set up the user interface from Designer.
|
|
|
|
self.setupUi(self)
|
2009-01-20 22:54:27 +00:00
|
|
|
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.changed)
|
2009-12-31 09:57:28 +00:00
|
|
|
self.setWindowTitle(self.tr("Random selection"))
|
2009-02-01 15:44:02 +00:00
|
|
|
# populate layer list
|
|
|
|
self.progressBar.setValue(0)
|
2010-05-13 22:55:59 +00:00
|
|
|
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
|
2010-03-10 01:11:11 +00:00
|
|
|
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
|
|
|
|
self.inShape.addItems(layers)
|
2010-06-30 22:49:20 +00:00
|
|
|
|
2009-02-01 15:44:02 +00:00
|
|
|
def changed(self, inputLayer):
|
2010-05-13 22:55:59 +00:00
|
|
|
changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
|
|
|
|
changedProvider = changedLayer.dataProvider()
|
|
|
|
upperVal = changedProvider.featureCount()
|
|
|
|
self.spnNumber.setMaximum(upperVal)
|
2009-02-01 15:44:02 +00:00
|
|
|
|
|
|
|
def accept(self):
|
2010-05-13 22:55:59 +00:00
|
|
|
self.buttonOk.setEnabled( False )
|
|
|
|
if self.inShape.currentText() == "":
|
|
|
|
QMessageBox.information(self, self.tr("Random Selection Tool"), self.tr("No input shapefile specified"))
|
2013-12-01 17:26:32 +01:00
|
|
|
return
|
2010-05-13 22:55:59 +00:00
|
|
|
else:
|
2009-02-01 15:44:02 +00:00
|
|
|
self.progressBar.setValue(10)
|
|
|
|
inName = self.inShape.currentText()
|
2010-05-13 22:55:59 +00:00
|
|
|
self.progressBar.setValue(20)
|
|
|
|
layer = ftools_utils.getVectorLayerByName(inName)
|
|
|
|
self.progressBar.setValue(30)
|
|
|
|
if self.rdoNumber.isChecked():
|
|
|
|
value = self.spnNumber.value()
|
|
|
|
self.progressBar.setValue(60)
|
|
|
|
else:
|
|
|
|
value = self.spnPercent.value()
|
|
|
|
self.progressBar.setValue(50)
|
|
|
|
value = int(round((value / 100.0000), 4) * layer.featureCount())
|
|
|
|
self.progressBar.setValue(60)
|
2010-06-30 22:49:20 +00:00
|
|
|
selran = random.sample(xrange(0, layer.featureCount()), value)
|
|
|
|
self.progressBar.setValue(70)
|
|
|
|
self.progressBar.setValue(80)
|
|
|
|
self.progressBar.setValue(90)
|
|
|
|
self.progressBar.setValue(100)
|
|
|
|
layer.setSelectedFeatures(selran)
|
2010-05-13 22:55:59 +00:00
|
|
|
self.progressBar.setValue(0)
|
|
|
|
self.buttonOk.setEnabled( True )
|