|
| 1 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 2 | +import os.path |
| 3 | +from PyQt4 import QtGui |
| 4 | +from PyQt4.QtCore import * |
| 5 | +from PyQt4.QtGui import * |
| 6 | +from qgis.core import * |
| 7 | +from sextante.parameters.ParameterVector import ParameterVector |
| 8 | +from sextante.core.QGisLayers import QGisLayers |
| 9 | +from sextante.outputs.OutputVector import OutputVector |
| 10 | + |
| 11 | +class ExtentFromLayer(GeoAlgorithm): |
| 12 | + |
| 13 | + INPUT = "INPUT" |
| 14 | + OUTPUT = "OUTPUT" |
| 15 | + |
| 16 | + def getIcon(self): |
| 17 | + return QtGui.QIcon(os.path.dirname(__file__) + "/icons/layer_extent.png") |
| 18 | + |
| 19 | + def processAlgorithm(self, progress): |
| 20 | + settings = QSettings() |
| 21 | + systemEncoding = settings.value( "/UI/encoding", "System" ).toString() |
| 22 | + output = self.getOutputValue(ExtentFromLayer.OUTPUT) |
| 23 | + vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(ExtentFromLayer.INPUT)) |
| 24 | + fields = { |
| 25 | + 0 : QgsField( "MINX", QVariant.Double ), |
| 26 | + 1 : QgsField( "MINY", QVariant.Double ), |
| 27 | + 2 : QgsField( "MAXX", QVariant.Double ), |
| 28 | + 3 : QgsField( "MAXY", QVariant.Double ), |
| 29 | + 4 : QgsField( "CNTX", QVariant.Double ), |
| 30 | + 5 : QgsField( "CNTY", QVariant.Double ), |
| 31 | + 6 : QgsField( "AREA", QVariant.Double ), |
| 32 | + 7 : QgsField( "PERIM", QVariant.Double ), |
| 33 | + 8 : QgsField( "HEIGHT", QVariant.Double ), |
| 34 | + 9 : QgsField( "WIDTH", QVariant.Double ) } |
| 35 | + |
| 36 | + writer = QgsVectorFileWriter(output, systemEncoding, fields, QGis.WKBPolygon, vlayer.crs() ) |
| 37 | + rect = vlayer.extent() |
| 38 | + minx = rect.xMinimum() |
| 39 | + miny = rect.yMinimum() |
| 40 | + maxx = rect.xMaximum() |
| 41 | + maxy = rect.yMaximum() |
| 42 | + height = rect.height() |
| 43 | + width = rect.width() |
| 44 | + cntx = minx + ( width / 2.0 ) |
| 45 | + cnty = miny + ( height / 2.0 ) |
| 46 | + area = width * height |
| 47 | + perim = ( 2 * width ) + (2 * height ) |
| 48 | + rect = [ |
| 49 | + QgsPoint( minx, miny ), |
| 50 | + QgsPoint( minx, maxy ), |
| 51 | + QgsPoint( maxx, maxy ), |
| 52 | + QgsPoint( maxx, miny ), |
| 53 | + QgsPoint( minx, miny ) ] |
| 54 | + geometry = QgsGeometry().fromPolygon( [ rect ] ) |
| 55 | + feat = QgsFeature() |
| 56 | + feat.setGeometry( geometry ) |
| 57 | + feat.setAttributeMap( { |
| 58 | + 0 : QVariant( minx ), |
| 59 | + 1 : QVariant( miny ), |
| 60 | + 2 : QVariant( maxx ), |
| 61 | + 3 : QVariant( maxy ), |
| 62 | + 4 : QVariant( cntx ), |
| 63 | + 5 : QVariant( cnty ), |
| 64 | + 6 : QVariant( area ), |
| 65 | + 7 : QVariant( perim ), |
| 66 | + 8 : QVariant( height ), |
| 67 | + 9 : QVariant( width ) } ) |
| 68 | + writer.addFeature( feat ) |
| 69 | + del writer |
| 70 | + |
| 71 | + |
| 72 | + def defineCharacteristics(self): |
| 73 | + self.name = "Extent from layer" |
| 74 | + self.group = "Research tools" |
| 75 | + self.addParameter(ParameterVector(ExtentFromLayer.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY)) |
| 76 | + self.addOutput(OutputVector(ExtentFromLayer.OUTPUT, "Extent layer")) |
| 77 | + #========================================================= |
0 commit comments