1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
# -*- coding: utf-8 -*-
#-----------------------------------------------------------
#
# 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.
#
#---------------------------------------------------------------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ftools_utils
from qgis.core import *
from random import *
from math import *
from ui_frmRegPoints import Ui_Dialog
class Dialog(QDialog, Ui_Dialog):
def __init__(self, iface):
QDialog.__init__(self, iface.mainWindow())
self.iface = iface
self.setupUi(self)
self.xMin.setValidator(QDoubleValidator(self.xMin))
self.xMax.setValidator(QDoubleValidator(self.xMax))
self.yMin.setValidator(QDoubleValidator(self.yMin))
self.yMax.setValidator(QDoubleValidator(self.yMax))
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
self.setWindowTitle( self.tr("Regular points") )
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
self.progressBar.setValue(0)
self.mapCanvas = self.iface.mapCanvas()
self.populateLayers()
def populateLayers( self ):
layers = ftools_utils.getLayerNames("all")
self.inShape.clear()
self.inShape.addItems(layers)
def accept(self):
self.buttonOk.setEnabled( False )
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()
outName = ftools_utils.getShapefileName( outPath )
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():
mLayer = ftools_utils.getMapLayerByName(unicode(inName))
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()))
crs = self.mapCanvas.mapRenderer().destinationCrs()
print crs.isValid()
if not crs.isValid(): crs = None
self.regularize(boundBox, outPath, offset, value, self.rdoSpacing.isChecked(), self.spnInset.value(), crs)
if self.addToCanvasCheck.isChecked():
addCanvasCheck = ftools_utils.addShapeToCanvas(unicode(outPath))
if not addCanvasCheck:
QMessageBox.warning( self, self.tr("Generate Regular Points"), self.tr( "Error loading output shapefile:\n%s" ) % ( unicode( outPath ) ))
self.populateLayers()
else:
QMessageBox.information(self, self.tr("Generate Regular Points"),self.tr("Created output shapefile:\n%s" ) % ( unicode( outPath )))
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )
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( self.shapefileName )
# Generate list of random points
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
return points
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()
outFeat.initAttributes(1)
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 = 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)
outFeat.setAttribute(0, idVar)
writer.addFeature(outFeat)
idVar = idVar + 1
x = x + pointSpacing
count = count + add
self.progressBar.setValue(count)
y = y - pointSpacing
del writer
|