Skip to content

Commit

Permalink
Fixes error when manually specifying output coordinates
Browse files Browse the repository at this point in the history
Modifies ui to increase control on output grid extents


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10035 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Jan 27, 2009
1 parent a2fd1a5 commit 7abc55a
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 267 deletions.
81 changes: 48 additions & 33 deletions python/plugins/ftools/tools/doVectorGrid.py
Expand Up @@ -42,52 +42,67 @@ def __init__(self, iface):
self.setupUi(self)
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
QObject.connect(self.spnX, SIGNAL("valueChanged(double)"), self.offset)
#QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.updateInput)
QObject.connect(self.btnUpdate, SIGNAL("clicked()"), self.updateInput)
self.setWindowTitle("Vector grid")
mapCanvas = self.iface.mapCanvas()
for i in range(mapCanvas.layerCount()):
layer = mapCanvas.layer(i)
self.inShape.addItem(layer.name())
self.xMin.setValidator(QDoubleValidator(self.xMin))
self.xMax.setValidator(QDoubleValidator(self.xMax))
self.yMin.setValidator(QDoubleValidator(self.yMin))
self.yMax.setValidator(QDoubleValidator(self.yMax))
layers = ftools_utils.getLayerNames(
[ QGis.Point, QGis.Line, QGis.Polygon ] )
for layer in layers:
self.inShape.addItem( layer )

def offset(self, value):
if self.chkLock.isChecked():
self.spnY.setValue(value)

def updateInput( self ):
mLayerName = self.inShape.currentText()
if not mLayerName == "":
mLayer = self.getMapLayerByName( unicode( mLayerName ) )
self.inLayer = mLayer
boundBox = mLayer.extent()
self.updateExtents( boundBox )

def updateExtents( self, boundBox ):
self.xMin.setText( unicode( boundBox.xMinimum() ) )
self.yMin.setText( unicode( boundBox.yMinimum() ) )
self.xMax.setText( unicode( boundBox.xMaximum() ) )
self.yMax.setText( unicode( boundBox.yMaximum() ) )

def accept(self):
if not self.rdoCoordinates.isChecked() and self.inShape.currentText() == "":
QMessageBox.information(self, "Generate Vector Grid", "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, "Generate Vector Grid", "Please properly specify extent coordinates")
if self.xMin.text() == "" or self.xMax.text() == "" or self.yMin.text() == "" or self.yMax.text() == "":
QMessageBox.information(self, "Vector grid", "Please specify valid extent coordinates")
elif self.outShape.text() == "":
QMessageBox.information(self, "Generate Vector Grid", "Please specify output shapefile")
QMessageBox.information(self, "Vector grid", "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.rdoBoundary.isChecked():
mLayer = self.getMapLayerByName(unicode(inName))
boundBox = mLayer.extent()
else:
boundBox = QgsRect(float(self.xMin.text()), float(self.yMin.text()), float(self.xMax.text()), float(self.yMax.text()))
try:
boundBox = QgsRectangle(
float( self.xMin.text() ),
float( self.yMin.text() ),
float( self.xMax.text() ),
float( self.yMax.text() ) )
except:
QMessageBox.information(self, "Vector grid", "Invalid extent coordinates entered")
xSpace = self.spnX.value()
ySpace = self.spnY.value()
if self.rdoPolygons.isChecked(): polygon = True
else: polygon = False
self.compute(boundBox, outPath, xSpace, ySpace, polygon, self.progressBar)
addToTOC = QMessageBox.question(self, "Generate Vector Grid", "Created output Shapefile:\n" + outPath
+ "\nNote: Layer has no associated coordinate system, please use the Projection Management Tool to specify spatial reference system."
+ "\n\nWould you like to add the new layer to the TOC?", QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
self.outShape.clear()
self.compute( boundBox, xSpace, ySpace, polygon )
addToTOC = QMessageBox.question(self,
"Generate Vector Grid", "Created output Shapefile:\n" + outPath
+ "\nNote: Layer has no associated coordinate system, please use "
+ "the Projection Management Tool to specify spatial reference system."
+ "\n\nWould you like to add the new layer to the TOC?",
QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
if addToTOC == QMessageBox.Yes:
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
self.progressBar.setValue(0)
ftools_utils.addShapeToCanvas( self.shapefileName )
self.progressBar.setValue( 0 )

def compute(self, bound, outPath, xOffset, yOffset, polygon, progressBar):
def compute( self, bound, xOffset, yOffset, polygon ):
if polygon:
fields = {0:QgsField("ID", QVariant.Int), 1:QgsField("XMIN", QVariant.Double), 2:QgsField("XMAX", QVariant.Double),
3:QgsField("YMIN", QVariant.Double), 4:QgsField("YMAX", QVariant.Double)}
Expand All @@ -108,7 +123,7 @@ def compute(self, bound, outPath, xOffset, yOffset, polygon, progressBar):
outFeat = QgsFeature()
outGeom = QgsGeometry()
idVar = 0
progressBar.setRange(0,0)
self.progressBar.setRange( 0, 0 )
if not polygon:
y = bound.yMaximum()
while y >= bound.yMinimum():
Expand Down Expand Up @@ -153,7 +168,7 @@ def compute(self, bound, outPath, xOffset, yOffset, polygon, progressBar):
idVar = idVar + 1
x = x + xOffset
y = y - yOffset
progressBar.setRange(0,100)
self.progressBar.setRange( 0, 100 )
del writer

def outFile(self):
Expand Down
82 changes: 39 additions & 43 deletions python/plugins/ftools/tools/frmVectorGrid.py
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'frmVectorGrid.ui'
#
# Created: Mon Nov 10 00:06:05 2008
# Created: Tue Jan 27 23:10:42 2009
# by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!
Expand All @@ -13,7 +13,7 @@ class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setWindowModality(QtCore.Qt.NonModal)
Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,374,483).size()).expandedTo(Dialog.minimumSizeHint()))
Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,369,459).size()).expandedTo(Dialog.minimumSizeHint()))
Dialog.setSizeGripEnabled(True)

self.gridlayout = QtGui.QGridLayout(Dialog)
Expand All @@ -25,70 +25,76 @@ def setupUi(self, Dialog):
self.gridlayout1 = QtGui.QGridLayout(self.groupBox)
self.gridlayout1.setObjectName("gridlayout1")

self.rdoBoundary = QtGui.QRadioButton(self.groupBox)
self.rdoBoundary.setChecked(True)
self.rdoBoundary.setObjectName("rdoBoundary")
self.gridlayout1.addWidget(self.rdoBoundary,0,0,1,2)
spacerItem = QtGui.QSpacerItem(80,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout1.addItem(spacerItem,1,0,1,1)

self.inShape = QtGui.QComboBox(self.groupBox)
self.inShape.setObjectName("inShape")
self.gridlayout1.addWidget(self.inShape,1,0,1,2)
self.btnUpdate = QtGui.QToolButton(self.groupBox)
self.btnUpdate.setMinimumSize(QtCore.QSize(0,30))
self.btnUpdate.setObjectName("btnUpdate")
self.gridlayout1.addWidget(self.btnUpdate,1,1,1,2)

self.rdoCoordinates = QtGui.QRadioButton(self.groupBox)
self.rdoCoordinates.setObjectName("rdoCoordinates")
self.gridlayout1.addWidget(self.rdoCoordinates,2,0,1,2)
spacerItem1 = QtGui.QSpacerItem(79,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout1.addItem(spacerItem1,1,3,1,1)

self.hboxlayout = QtGui.QHBoxLayout()
self.hboxlayout.setObjectName("hboxlayout")

self.label = QtGui.QLabel(self.groupBox)
self.label.setEnabled(False)
self.label.setEnabled(True)
self.label.setObjectName("label")
self.hboxlayout.addWidget(self.label)

self.xMin = QtGui.QLineEdit(self.groupBox)
self.xMin.setEnabled(True)
self.xMin.setObjectName("xMin")
self.hboxlayout.addWidget(self.xMin)
self.gridlayout1.addLayout(self.hboxlayout,3,0,1,1)
self.gridlayout1.addLayout(self.hboxlayout,2,0,1,2)

self.hboxlayout1 = QtGui.QHBoxLayout()
self.hboxlayout1.setObjectName("hboxlayout1")

self.label_4 = QtGui.QLabel(self.groupBox)
self.label_4.setEnabled(False)
self.label_4.setEnabled(True)
self.label_4.setObjectName("label_4")
self.hboxlayout1.addWidget(self.label_4)

self.yMin = QtGui.QLineEdit(self.groupBox)
self.yMin.setEnabled(True)
self.yMin.setObjectName("yMin")
self.hboxlayout1.addWidget(self.yMin)
self.gridlayout1.addLayout(self.hboxlayout1,3,1,1,1)
self.gridlayout1.addLayout(self.hboxlayout1,2,2,1,2)

self.hboxlayout2 = QtGui.QHBoxLayout()
self.hboxlayout2.setObjectName("hboxlayout2")

self.label_3 = QtGui.QLabel(self.groupBox)
self.label_3.setEnabled(False)
self.label_3.setEnabled(True)
self.label_3.setObjectName("label_3")
self.hboxlayout2.addWidget(self.label_3)

self.xMax = QtGui.QLineEdit(self.groupBox)
self.xMax.setEnabled(True)
self.xMax.setObjectName("xMax")
self.hboxlayout2.addWidget(self.xMax)
self.gridlayout1.addLayout(self.hboxlayout2,4,0,1,1)
self.gridlayout1.addLayout(self.hboxlayout2,3,0,1,2)

self.hboxlayout3 = QtGui.QHBoxLayout()
self.hboxlayout3.setObjectName("hboxlayout3")

self.label_5 = QtGui.QLabel(self.groupBox)
self.label_5.setEnabled(False)
self.label_5.setEnabled(True)
self.label_5.setObjectName("label_5")
self.hboxlayout3.addWidget(self.label_5)

self.yMax = QtGui.QLineEdit(self.groupBox)
self.yMax.setEnabled(True)
self.yMax.setObjectName("yMax")
self.hboxlayout3.addWidget(self.yMax)
self.gridlayout1.addLayout(self.hboxlayout3,4,1,1,1)
self.gridlayout1.addLayout(self.hboxlayout3,3,2,1,2)

self.inShape = QtGui.QComboBox(self.groupBox)
self.inShape.setObjectName("inShape")
self.gridlayout1.addWidget(self.inShape,0,0,1,4)
self.gridlayout.addWidget(self.groupBox,0,0,1,2)

self.gridBox = QtGui.QGroupBox(Dialog)
Expand All @@ -97,8 +103,8 @@ def setupUi(self, Dialog):
self.gridlayout2 = QtGui.QGridLayout(self.gridBox)
self.gridlayout2.setObjectName("gridlayout2")

spacerItem = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout2.addItem(spacerItem,0,0,1,1)
spacerItem2 = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout2.addItem(spacerItem2,0,0,1,1)

self.label_7 = QtGui.QLabel(self.gridBox)
self.label_7.setObjectName("label_7")
Expand All @@ -107,7 +113,7 @@ def setupUi(self, Dialog):
self.spnX = QtGui.QDoubleSpinBox(self.gridBox)
self.spnX.setDecimals(4)
self.spnX.setMinimum(0.0001)
self.spnX.setMaximum(9999.0)
self.spnX.setMaximum(999999999.0)
self.spnX.setSingleStep(0.0001)
self.spnX.setObjectName("spnX")
self.gridlayout2.addWidget(self.spnX,0,2,1,1)
Expand All @@ -117,11 +123,11 @@ def setupUi(self, Dialog):
self.chkLock.setObjectName("chkLock")
self.gridlayout2.addWidget(self.chkLock,0,3,2,1)

spacerItem1 = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout2.addItem(spacerItem1,0,4,1,1)
spacerItem3 = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout2.addItem(spacerItem3,0,4,1,1)

spacerItem2 = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout2.addItem(spacerItem2,1,0,1,1)
spacerItem4 = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.gridlayout2.addItem(spacerItem4,1,0,1,1)

self.label_8 = QtGui.QLabel(self.gridBox)
self.label_8.setEnabled(False)
Expand All @@ -132,7 +138,7 @@ def setupUi(self, Dialog):
self.spnY.setEnabled(False)
self.spnY.setDecimals(4)
self.spnY.setMinimum(0.0001)
self.spnY.setMaximum(9999.0)
self.spnY.setMaximum(999999999.0)
self.spnY.setSingleStep(0.0001)
self.spnY.setObjectName("spnY")
self.gridlayout2.addWidget(self.spnY,1,2,1,1)
Expand Down Expand Up @@ -186,24 +192,14 @@ def setupUi(self, Dialog):
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox_2,QtCore.SIGNAL("accepted()"),Dialog.accept)
QtCore.QObject.connect(self.buttonBox_2,QtCore.SIGNAL("rejected()"),Dialog.close)
QtCore.QObject.connect(self.rdoBoundary,QtCore.SIGNAL("toggled(bool)"),self.inShape.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.xMin.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.xMax.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.yMin.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.yMax.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.label.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.label_3.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.label_4.setEnabled)
QtCore.QObject.connect(self.rdoCoordinates,QtCore.SIGNAL("toggled(bool)"),self.label_5.setEnabled)
QtCore.QObject.connect(self.chkLock,QtCore.SIGNAL("clicked(bool)"),self.spnY.setDisabled)
QtCore.QObject.connect(self.chkLock,QtCore.SIGNAL("toggled(bool)"),self.label_8.setDisabled)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Generate Regular Points", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox.setTitle(QtGui.QApplication.translate("Dialog", "Grid Extent", None, QtGui.QApplication.UnicodeUTF8))
self.rdoBoundary.setText(QtGui.QApplication.translate("Dialog", "Input Boundary Layer", None, QtGui.QApplication.UnicodeUTF8))
self.rdoCoordinates.setText(QtGui.QApplication.translate("Dialog", "Input Coordinates", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox.setTitle(QtGui.QApplication.translate("Dialog", "Grid extent", None, QtGui.QApplication.UnicodeUTF8))
self.btnUpdate.setText(QtGui.QApplication.translate("Dialog", "Update extents from layer", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Dialog", "X Min", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("Dialog", "Y Min", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("Dialog", "X Max", None, QtGui.QApplication.UnicodeUTF8))
Expand All @@ -212,8 +208,8 @@ def retranslateUi(self, Dialog):
self.label_7.setText(QtGui.QApplication.translate("Dialog", "X", None, QtGui.QApplication.UnicodeUTF8))
self.chkLock.setText(QtGui.QApplication.translate("Dialog", "Lock 1:1 ratio", None, QtGui.QApplication.UnicodeUTF8))
self.label_8.setText(QtGui.QApplication.translate("Dialog", "Y", None, QtGui.QApplication.UnicodeUTF8))
self.rdoPolygons.setText(QtGui.QApplication.translate("Dialog", "Output as polygons", None, QtGui.QApplication.UnicodeUTF8))
self.rdoLines.setText(QtGui.QApplication.translate("Dialog", "Output as lines", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Dialog", "Output Shapefile", None, QtGui.QApplication.UnicodeUTF8))
self.rdoPolygons.setText(QtGui.QApplication.translate("Dialog", "Output grid as polygons", None, QtGui.QApplication.UnicodeUTF8))
self.rdoLines.setText(QtGui.QApplication.translate("Dialog", "Output grid as lines", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Dialog", "Output shapefile", None, QtGui.QApplication.UnicodeUTF8))
self.toolOut.setText(QtGui.QApplication.translate("Dialog", "Browse", None, QtGui.QApplication.UnicodeUTF8))

0 comments on commit 7abc55a

Please sign in to comment.