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 *
import ftools_utils
from qgis . core import *
from random import *
from math import *
2009-08-16 22:40:48 +00:00
from ui_frmRegPoints import Ui_Dialog
2009-02-01 15:44:02 +00:00
2009-01-20 22:54:27 +00:00
class Dialog ( QDialog , Ui_Dialog ) :
2010-03-08 02:20:27 +00:00
def __init__ ( self , iface ) :
2012-01-26 00:54:56 +01:00
QDialog . __init__ ( self , iface . mainWindow ( ) )
2010-03-08 02:20:27 +00:00
self . iface = iface
self . setupUi ( self )
2010-04-15 00:24:02 +00:00
self . xMin . setValidator ( QDoubleValidator ( self . xMin ) )
self . xMax . setValidator ( QDoubleValidator ( self . xMax ) )
self . yMin . setValidator ( QDoubleValidator ( self . yMin ) )
self . yMax . setValidator ( QDoubleValidator ( self . yMax ) )
2010-03-08 02:20:27 +00:00
QObject . connect ( self . toolOut , SIGNAL ( " clicked() " ) , self . outFile )
self . setWindowTitle ( self . tr ( " Regular points " ) )
2010-05-13 22:55:59 +00:00
self . buttonOk = self . buttonBox_2 . button ( QDialogButtonBox . Ok )
2010-03-08 02:20:27 +00:00
self . progressBar . setValue ( 0 )
self . mapCanvas = self . iface . mapCanvas ( )
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 ( " all " )
2011-11-12 13:05:27 +02:00
self . inShape . clear ( )
2010-03-10 01:11:11 +00:00
self . inShape . addItems ( layers )
2009-01-20 22:54:27 +00:00
2010-03-08 02:20:27 +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 not self . rdoCoordinates . isChecked ( ) and self . inShape . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Generate Regular Points " ) , self . tr ( " Please specify input layer " ) )
elif self . rdoCoordinates . isChecked ( ) and ( self . xMin . text ( ) == " " or self . xMax . text ( ) == " " or self . yMin . text ( ) == " " or self . yMax . text ( ) == " " ) :
QMessageBox . information ( self , self . tr ( " Generate Regular Points " ) , self . tr ( " Please properly specify extent coordinates " ) )
elif self . outShape . text ( ) == " " :
QMessageBox . information ( self , self . tr ( " Generate Regular Points " ) , self . tr ( " Please specify output shapefile " ) )
else :
inName = self . inShape . currentText ( )
outPath = self . outShape . text ( )
self . outShape . clear ( )
if outPath . contains ( " \\ " ) :
outName = outPath . right ( ( outPath . length ( ) - outPath . lastIndexOf ( " \\ " ) ) - 1 )
else :
outName = outPath . right ( ( outPath . length ( ) - outPath . lastIndexOf ( " / " ) ) - 1 )
if outName . endsWith ( " .shp " ) :
outName = outName . left ( outName . length ( ) - 4 )
if self . rdoSpacing . isChecked ( ) : value = self . spnSpacing . value ( )
else : value = self . spnNumber . value ( )
if self . chkRandom . isChecked ( ) : offset = True
else : offset = False
if self . rdoBoundary . isChecked ( ) :
2010-03-10 01:11:11 +00:00
mLayer = ftools_utils . getMapLayerByName ( unicode ( inName ) )
2010-03-08 02:20:27 +00:00
boundBox = mLayer . extent ( )
crs = mLayer . crs ( )
else :
boundBox = QgsRectangle ( float ( self . xMin . text ( ) ) , float ( self . yMin . text ( ) ) , float ( self . xMax . text ( ) ) , float ( self . yMax . text ( ) ) )
2013-01-03 21:28:48 +01:00
crs = self . mapCanvas . mapRenderer ( ) . destinationCrs ( )
2010-03-08 02:20:27 +00:00
print crs . isValid ( )
if not crs . isValid ( ) : crs = None
self . regularize ( boundBox , outPath , offset , value , self . rdoSpacing . isChecked ( ) , self . spnInset . value ( ) , crs )
2013-05-31 14:57:34 +04:00
addToTOC = QMessageBox . question ( self , self . tr ( " Generate Regular Points " ) , 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 )
2010-03-08 02:20:27 +00:00
if addToTOC == QMessageBox . Yes :
self . vlayer = QgsVectorLayer ( outPath , unicode ( outName ) , " ogr " )
2013-01-04 23:02:44 +01:00
QgsMapLayerRegistry . instance ( ) . addMapLayers ( [ self . vlayer ] )
2011-11-12 13:05:27 +02:00
self . populateLayers ( )
2010-05-13 22:55:59 +00:00
self . progressBar . setValue ( 0 )
self . buttonOk . setEnabled ( True )
2009-02-01 15:44:02 +00:00
2010-03-08 02:20:27 +00:00
def outFile ( self ) :
self . outShape . clear ( )
( self . shapefileName , self . encoding ) = ftools_utils . saveDialog ( self )
if self . shapefileName is None or self . encoding is None :
return
self . outShape . setText ( QString ( self . shapefileName ) )
2011-11-12 13:05:27 +02:00
2010-05-13 22:55:59 +00:00
# Generate list of random points
2010-03-08 02:20:27 +00:00
def simpleRandom ( self , n , bound , xmin , xmax , ymin , ymax ) :
seed ( )
points = [ ]
i = 1
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
2011-11-12 13:05:27 +02:00
return points
2010-03-10 01:11:11 +00:00
2010-03-08 02:20:27 +00:00
def regularize ( self , bound , outPath , offset , value , gridType , inset , crs ) :
area = bound . width ( ) * bound . height ( )
if offset :
seed ( )
if gridType :
pointSpacing = value
else :
# Calculate grid spacing
pointSpacing = sqrt ( area / value )
outFeat = QgsFeature ( )
2013-03-14 13:27:44 +04:00
outFeat . initAttributes ( 1 )
2013-02-09 20:59:19 +01:00
fields = QgsFields ( )
fields . append ( QgsField ( " ID " , QVariant . Int ) )
2013-03-14 13:27:44 +04:00
outFeat . setFields ( fields )
2010-03-08 02:20:27 +00:00
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 = 10.00
add = 90.00 / ( area / pointSpacing )
y = bound . yMaximum ( ) - inset
while y > = bound . yMinimum ( ) :
x = bound . xMinimum ( ) + inset
while x < = bound . xMaximum ( ) :
if offset :
pGeom = QgsGeometry ( ) . fromPoint ( QgsPoint ( uniform ( x - ( pointSpacing / 2.0 ) , x + ( pointSpacing / 2.0 ) ) ,
uniform ( y - ( pointSpacing / 2.0 ) , y + ( pointSpacing / 2.0 ) ) ) )
else :
pGeom = QgsGeometry ( ) . fromPoint ( QgsPoint ( x , y ) )
if pGeom . intersects ( bound ) :
outFeat . setGeometry ( pGeom )
2013-02-03 21:17:23 +01:00
outFeat . setAttribute ( 0 , QVariant ( idVar ) )
2010-03-08 02:20:27 +00:00
writer . addFeature ( outFeat )
idVar = idVar + 1
x = x + pointSpacing
count = count + add
self . progressBar . setValue ( count )
y = y - pointSpacing
del writer