|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +/*************************************************************************** |
| 4 | +Name : GdalTools |
| 5 | +Description : Integrate gdal tools into qgis |
| 6 | +Date : 17/Sep/09 |
| 7 | +copyright : (C) 2009 by Lorenzo Masini (Faunalia) |
| 8 | +email : lorenxo86@gmail.com |
| 9 | + ***************************************************************************/ |
| 10 | +
|
| 11 | +/*************************************************************************** |
| 12 | + * * |
| 13 | + * This program is free software; you can redistribute it and/or modify * |
| 14 | + * it under the terms of the GNU General Public License as published by * |
| 15 | + * the Free Software Foundation; either version 2 of the License, or * |
| 16 | + * (at your option) any later version. * |
| 17 | + * * |
| 18 | + ***************************************************************************/ |
| 19 | +""" |
| 20 | +# Import the PyQt and QGIS libraries |
| 21 | +from PyQt4.QtCore import * |
| 22 | +from PyQt4.QtGui import * |
| 23 | +from qgis.core import * |
| 24 | + |
| 25 | +# Initialize Qt resources from file resources_rc.py |
| 26 | +import resources_rc |
| 27 | + |
| 28 | +# Import required modules and if missing show the right name of them |
| 29 | +req_mods = { "osgeo": "osgeo [python-gdal]" } |
| 30 | +for k, v in req_mods.iteritems(): |
| 31 | + try: |
| 32 | + exec( "import %s" % k ) |
| 33 | + except ImportError, e: |
| 34 | + errorStr = str(e) |
| 35 | + if len(v) > 0: |
| 36 | + errorStr = errorStr.replace( k, v ) |
| 37 | + raise ImportError( errorStr ) |
| 38 | + |
| 39 | +# Set up current path, so that we know where to look for modules |
| 40 | +import os.path, sys |
| 41 | +currentPath = os.path.dirname( __file__ ) |
| 42 | +sys.path.append( os.path.abspath(os.path.dirname( __file__ ) + '/tools' ) ) |
| 43 | +import doBuildVRT, doContour, doRasterize, doPolygonize, doMerge, doSieve, doProximity, doNearBlack, doWarp, doGrid, doTranslate, doClipper |
| 44 | +import doInfo, doProjection, doOverview, doRgbPct, doPctRgb, doSettings, doAbout |
| 45 | + |
| 46 | +import GdalTools_utils as Utils |
| 47 | + |
| 48 | +class GdalTools: |
| 49 | + |
| 50 | + def __init__( self, iface ): |
| 51 | + # Save reference to the QGIS interface |
| 52 | + self.iface = iface |
| 53 | + try: |
| 54 | + self.QgisVersion = unicode( QGis.QGIS_VERSION_INT ) |
| 55 | + except: |
| 56 | + self.QgisVersion = unicode( QGis.qgisVersion )[ 0 ] |
| 57 | + |
| 58 | + # For i18n support |
| 59 | + userPluginPath = QFileInfo( QgsApplication.qgisUserDbFilePath() ).path() + "/python/plugins/GdalTools" |
| 60 | + systemPluginPath = QgsApplication.prefixPath() + "/python/plugins/GdalTools" |
| 61 | + |
| 62 | + overrideLocale = QSettings().value( "locale/overrideFlag", QVariant( False ) ).toBool() |
| 63 | + if not overrideLocale: |
| 64 | + localeFullName = QLocale.system().name() |
| 65 | + else: |
| 66 | + localeFullName = QSettings().value( "locale/userLocale", QVariant( "" ) ).toString() |
| 67 | + |
| 68 | + if QFileInfo( userPluginPath ).exists(): |
| 69 | + translationPath = userPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" |
| 70 | + else: |
| 71 | + translationPath = systemPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" |
| 72 | + |
| 73 | + self.localePath = translationPath |
| 74 | + if QFileInfo( self.localePath ).exists(): |
| 75 | + self.translator = QTranslator() |
| 76 | + self.translator.load( self.localePath ) |
| 77 | + QCoreApplication.installTranslator( self.translator ) |
| 78 | + |
| 79 | + def initGui( self ): |
| 80 | + if int( self.QgisVersion ) < 1: |
| 81 | + QMessageBox.warning( self.iface.getMainWindow(), "Gdal Tools", |
| 82 | + QCoreApplication.translate( "GdalTools", "Quantum GIS version detected: " ) +unicode( self.QgisVersion )+".xx\n" |
| 83 | + + QCoreApplication.translate( "GdalTools", "This version of Gdal Tools requires at least QGIS version 1.0.0\nPlugin will not be enabled." ) ) |
| 84 | + return None |
| 85 | + |
| 86 | + self.GdalVersion = Utils.Version( Utils.GdalConfig.version() ) |
| 87 | + |
| 88 | + self.menu = QMenu() |
| 89 | + self.menu.setTitle( QCoreApplication.translate( "GdalTools", "&Raster" ) ) |
| 90 | + |
| 91 | + if self.GdalVersion >= "1.6": |
| 92 | + self.buildVRT = QAction( QIcon(":/icons/vrt.png"), QCoreApplication.translate( "GdalTools", "Build Virtual Raster (catalog)" ), self.iface.mainWindow() ) |
| 93 | + self.buildVRT.setStatusTip( QCoreApplication.translate( "GdalTools", "Builds a VRT from a list of datasets") ) |
| 94 | + QObject.connect( self.buildVRT, SIGNAL( "triggered()" ), self.doBuildVRT ) |
| 95 | + self.menu.addAction(self.buildVRT) |
| 96 | + |
| 97 | + if self.GdalVersion >= "1.6": |
| 98 | + self.contour = QAction( QIcon(":/icons/contour.png"), QCoreApplication.translate( "GdalTools", "Contour" ), self.iface.mainWindow() ) |
| 99 | + self.contour.setStatusTip( QCoreApplication.translate( "GdalTools", "Builds vector contour lines from a DEM") ) |
| 100 | + QObject.connect( self.contour, SIGNAL( "triggered()" ), self.doContour ) |
| 101 | + self.menu.addAction(self.contour) |
| 102 | + |
| 103 | + if self.GdalVersion >= "1.3": |
| 104 | + self.rasterize = QAction( QIcon(":/icons/rasterize.png"), QCoreApplication.translate( "GdalTools", "Rasterize" ), self.iface.mainWindow() ) |
| 105 | + self.rasterize.setStatusTip( QCoreApplication.translate( "GdalTools", "Burns vector geometries into a raster") ) |
| 106 | + QObject.connect( self.rasterize, SIGNAL( "triggered()" ), self.doRasterize ) |
| 107 | + self.menu.addAction(self.rasterize) |
| 108 | + |
| 109 | + if self.GdalVersion >= "1.6": |
| 110 | + self.polygonize = QAction( QIcon(":/icons/polygonize.png"), QCoreApplication.translate( "GdalTools", "Polygonize" ), self.iface.mainWindow() ) |
| 111 | + self.polygonize.setStatusTip( QCoreApplication.translate( "GdalTools", "Produces a polygon feature layer from a raster") ) |
| 112 | + QObject.connect( self.polygonize, SIGNAL( "triggered()" ), self.doPolygonize ) |
| 113 | + self.menu.addAction(self.polygonize) |
| 114 | + |
| 115 | + self.merge = QAction( QIcon(":/icons/merge.png"), QCoreApplication.translate( "GdalTools", "Merge" ), self.iface.mainWindow() ) |
| 116 | + self.merge.setStatusTip( QCoreApplication.translate( "GdalTools", "Build a quick mosaic from a set of images") ) |
| 117 | + QObject.connect( self.merge, SIGNAL( "triggered()" ), self.doMerge ) |
| 118 | + self.menu.addAction(self.merge) |
| 119 | + |
| 120 | + if self.GdalVersion >= "1.6": |
| 121 | + self.sieve = QAction( QIcon(":/icons/sieve.png"), QCoreApplication.translate( "GdalTools", "Sieve" ), self.iface.mainWindow() ) |
| 122 | + self.sieve.setStatusTip( QCoreApplication.translate( "GdalTools", "Removes small raster polygons") ) |
| 123 | + QObject.connect( self.sieve, SIGNAL( "triggered()" ), self.doSieve ) |
| 124 | + self.menu.addAction(self.sieve) |
| 125 | + |
| 126 | + if self.GdalVersion >= "1.6": |
| 127 | + self.proximity = QAction( QIcon(":/icons/proximity.png"), QCoreApplication.translate( "GdalTools", "Proximity" ), self.iface.mainWindow() ) |
| 128 | + self.proximity.setStatusTip( QCoreApplication.translate( "GdalTools", "Produces a raster proximity map") ) |
| 129 | + QObject.connect( self.proximity, SIGNAL( "triggered()" ), self.doProximity ) |
| 130 | + self.menu.addAction(self.proximity) |
| 131 | + |
| 132 | + if self.GdalVersion >= "1.5": |
| 133 | + self.nearBlack = QAction( QIcon(":/icons/nearblack.png"), QCoreApplication.translate( "GdalTools", "Near black" ), self.iface.mainWindow() ) |
| 134 | + self.nearBlack.setStatusTip( QCoreApplication.translate( "GdalTools", "Convert nearly black/white borders to exact value") ) |
| 135 | + QObject.connect( self.nearBlack, SIGNAL( "triggered()" ), self.doNearBlack ) |
| 136 | + self.menu.addAction(self.nearBlack) |
| 137 | + |
| 138 | + self.warp = QAction( QIcon(":/icons/warp.png"), QCoreApplication.translate( "GdalTools", "Warp" ), self.iface.mainWindow() ) |
| 139 | + self.warp.setStatusTip( QCoreApplication.translate( "GdalTools", "Warp an image into a new coordinate system") ) |
| 140 | + QObject.connect( self.warp, SIGNAL( "triggered()" ), self.doWarp ) |
| 141 | + self.menu.addAction(self.warp) |
| 142 | + |
| 143 | + if self.GdalVersion >= "1.5": |
| 144 | + self.grid = QAction( QIcon(":/icons/grid.png"), QCoreApplication.translate( "GdalTools", "Grid" ), self.iface.mainWindow() ) |
| 145 | + self.grid.setStatusTip( QCoreApplication.translate( "GdalTools", "Create raster from the scattered data") ) |
| 146 | + QObject.connect( self.grid, SIGNAL( "triggered()" ), self.doGrid ) |
| 147 | + self.menu.addAction(self.grid) |
| 148 | + |
| 149 | + self.translate = QAction( QIcon(":/icons/translate.png"), QCoreApplication.translate( "GdalTools", "Translate" ), self.iface.mainWindow() ) |
| 150 | + self.translate.setStatusTip( QCoreApplication.translate( "GdalTools", "Converts raster data between different formats") ) |
| 151 | + QObject.connect( self.translate, SIGNAL( "triggered()" ), self.doTranslate ) |
| 152 | + self.menu.addAction(self.translate) |
| 153 | + |
| 154 | + self.info = QAction( QIcon( ":/icons/raster-info.png" ), QCoreApplication.translate( "GdalTools", "Information" ), self.iface.mainWindow() ) |
| 155 | + self.info.setStatusTip( QCoreApplication.translate( "GdalTools", "Lists information about raster dataset" ) ) |
| 156 | + QObject.connect( self.info, SIGNAL("triggered()"), self.doInfo ) |
| 157 | + self.menu.addAction( self.info ) |
| 158 | + |
| 159 | + self.projection = QAction( QIcon( ":icons/projection-add.png" ), QCoreApplication.translate( "GdalTools", "Assign projection" ), self.iface.mainWindow() ) |
| 160 | + self.projection.setStatusTip( QCoreApplication.translate( "GdalTools", "Add projection info to the raster" ) ) |
| 161 | + QObject.connect( self.projection, SIGNAL( "triggered()" ), self.doProjection ) |
| 162 | + self.menu.addAction( self.projection ) |
| 163 | + |
| 164 | + self.overview = QAction( QIcon( ":icons/raster-overview.png" ), QCoreApplication.translate( "GdalTools", "Build overviews" ), self.iface.mainWindow() ) |
| 165 | + self.overview.setStatusTip( QCoreApplication.translate( "GdalTools", "Builds or rebuilds overview images" ) ) |
| 166 | + QObject.connect( self.overview, SIGNAL( "triggered()" ), self.doOverview ) |
| 167 | + self.menu.addAction( self.overview ) |
| 168 | + |
| 169 | + self.clipper = QAction( QIcon( ":icons/raster-clip.png" ), QCoreApplication.translate( "GdalTools", "Clipper" ), self.iface.mainWindow() ) |
| 170 | + #self.clipper.setStatusTip( QCoreApplication.translate( "GdalTools", "Converts raster data between different formats") ) |
| 171 | + QObject.connect( self.clipper, SIGNAL( "triggered()" ), self.doClipper ) |
| 172 | + self.menu.addAction(self.clipper) |
| 173 | + |
| 174 | + self.paletted = QAction( QIcon( ":icons/raster-paletted.png" ), QCoreApplication.translate( "GdalTools", "RGB to PCT" ), self.iface.mainWindow() ) |
| 175 | + self.paletted.setStatusTip( QCoreApplication.translate( "GdalTools", "Convert a 24bit RGB image to 8bit paletted" ) ) |
| 176 | + QObject.connect( self.paletted, SIGNAL( "triggered()" ), self.doPaletted ) |
| 177 | + self.menu.addAction(self.paletted) |
| 178 | + |
| 179 | + self.rgb = QAction( QIcon( ":icons/raster-paletted.png" ), QCoreApplication.translate( "GdalTools", "PCT to RGB" ), self.iface.mainWindow() ) |
| 180 | + self.rgb.setStatusTip( QCoreApplication.translate( "GdalTools", "Convert an 8bit paletted image to 24bit RGB" ) ) |
| 181 | + QObject.connect( self.rgb, SIGNAL( "triggered()" ), self.doRGB ) |
| 182 | + self.menu.addAction(self.rgb) |
| 183 | + |
| 184 | + self.settings = QAction( QCoreApplication.translate( "GdalTools", "GdalTools settings" ), self.iface.mainWindow() ) |
| 185 | + self.settings.setStatusTip( QCoreApplication.translate( "GdalTools", "Various settings for Gdal Tools" ) ) |
| 186 | + QObject.connect( self.settings, SIGNAL( "triggered()" ), self.doSettings ) |
| 187 | + self.menu.addAction( self.settings ) |
| 188 | + |
| 189 | + self.about = QAction( QIcon( ":icons/about.png" ), QCoreApplication.translate( "GdalTools", "About GdalTools" ), self.iface.mainWindow() ) |
| 190 | + self.about.setStatusTip( QCoreApplication.translate( "GdalTools", "Displays information about Gdal Tools" ) ) |
| 191 | + QObject.connect( self.about, SIGNAL( "triggered()" ), self.doAbout ) |
| 192 | + self.menu.addSeparator() |
| 193 | + self.menu.addAction( self.about ) |
| 194 | + |
| 195 | + menu_bar = self.iface.mainWindow().menuBar() |
| 196 | + actions = menu_bar.actions() |
| 197 | + lastAction = actions[ len( actions ) - 1 ] |
| 198 | + menu_bar.insertMenu( lastAction, self.menu ) |
| 199 | + |
| 200 | + def unload( self ): |
| 201 | + pass |
| 202 | + |
| 203 | + def doBuildVRT( self ): |
| 204 | + d = doBuildVRT.GdalToolsDialog( self.iface ) |
| 205 | + d.show_() |
| 206 | + |
| 207 | + def doContour( self ): |
| 208 | + d = doContour.GdalToolsDialog( self.iface ) |
| 209 | + d.show_() |
| 210 | + |
| 211 | + def doRasterize( self ): |
| 212 | + d = doRasterize.GdalToolsDialog( self.iface ) |
| 213 | + d.show_() |
| 214 | + |
| 215 | + def doPolygonize( self ): |
| 216 | + d = doPolygonize.GdalToolsDialog( self.iface ) |
| 217 | + d.show_() |
| 218 | + |
| 219 | + def doMerge( self ): |
| 220 | + d = doMerge.GdalToolsDialog( self.iface ) |
| 221 | + d.show_() |
| 222 | + |
| 223 | + def doSieve( self ): |
| 224 | + d = doSieve.GdalToolsDialog( self.iface ) |
| 225 | + d.show_() |
| 226 | + |
| 227 | + def doProximity( self ): |
| 228 | + d = doProximity.GdalToolsDialog( self.iface ) |
| 229 | + d.show_() |
| 230 | + |
| 231 | + def doNearBlack( self ): |
| 232 | + d = doNearBlack.GdalToolsDialog( self.iface ) |
| 233 | + d.show_() |
| 234 | + |
| 235 | + def doWarp( self ): |
| 236 | + d = doWarp.GdalToolsDialog( self.iface ) |
| 237 | + d.show_() |
| 238 | + |
| 239 | + def doGrid( self ): |
| 240 | + d = doGrid.GdalToolsDialog( self.iface ) |
| 241 | + d.show_() |
| 242 | + |
| 243 | + def doTranslate( self ): |
| 244 | + d = doTranslate.GdalToolsDialog( self.iface ) |
| 245 | + d.show_() |
| 246 | + |
| 247 | + def doInfo( self ): |
| 248 | + d = doInfo.GdalToolsDialog( self.iface ) |
| 249 | + d.show_() |
| 250 | + |
| 251 | + def doProjection( self ): |
| 252 | + d = doProjection.GdalToolsDialog( self.iface ) |
| 253 | + d.show_() |
| 254 | + |
| 255 | + def doOverview( self ): |
| 256 | + d = doOverview.GdalToolsDialog( self.iface ) |
| 257 | + d.show_() |
| 258 | + |
| 259 | + def doClipper( self ): |
| 260 | + d = doClipper.GdalToolsDialog( self.iface ) |
| 261 | + d.show_() |
| 262 | + |
| 263 | + def doPaletted( self ): |
| 264 | + d = doRgbPct.GdalToolsDialog( self.iface ) |
| 265 | + d.show_() |
| 266 | + |
| 267 | + def doRGB( self ): |
| 268 | + d = doPctRgb.GdalToolsDialog( self.iface ) |
| 269 | + d.show_() |
| 270 | + |
| 271 | + def doSettings( self ): |
| 272 | + d = doSettings.GdalToolsSettingsDialog( self.iface ) |
| 273 | + d.exec_() |
| 274 | + |
| 275 | + def doAbout( self ): |
| 276 | + d = doAbout.GdalToolsAboutDialog( self.iface ) |
| 277 | + d.exec_() |
0 commit comments