PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/landmask.py

https://bitbucket.org/jkibele/multispectral-land-masker
Python | 114 lines | 82 code | 4 blank | 28 comment | 0 complexity | 149ae7d3ab66637a8d274679c446dbd0 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. LandMask
  5. A QGIS plugin
  6. Create a land (or water) mask for a multispectral raster.
  7. -------------------
  8. begin : 2013-05-21
  9. copyright : (C) 2013 by Jared Kibele
  10. email : jkibele@gmail.com
  11. ***************************************************************************/
  12. /***************************************************************************
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. ***************************************************************************/
  20. """
  21. # Import the PyQt and QGIS libraries
  22. from PyQt4.QtCore import *
  23. from PyQt4.QtGui import *
  24. from qgis.core import *
  25. # Initialize Qt resources from file resources.py
  26. import resources_rc
  27. # Import the code for the dialog
  28. from landmaskdialog import LandMaskDialog
  29. #- Add my imports
  30. from raster_handling import *
  31. class LandMask:
  32. def __init__(self, iface):
  33. # Save reference to the QGIS interface
  34. self.iface = iface
  35. # initialize plugin directory
  36. self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + "/python/plugins/landmask"
  37. # initialize locale
  38. localePath = ""
  39. locale = QSettings().value("locale/userLocale").toString()[0:2]
  40. if QFileInfo(self.plugin_dir).exists():
  41. localePath = self.plugin_dir + "/i18n/landmask_" + locale + ".qm"
  42. if QFileInfo(localePath).exists():
  43. self.translator = QTranslator()
  44. self.translator.load(localePath)
  45. if qVersion() > '4.3.3':
  46. QCoreApplication.installTranslator(self.translator)
  47. # Create the dialog (after translation) and keep reference
  48. self.dlg = LandMaskDialog()
  49. def initGui(self):
  50. # Create action that will start plugin configuration
  51. self.action = QAction(
  52. QIcon(":/plugins/landmask/icon.png"),
  53. u"Land Mask", self.iface.mainWindow())
  54. # connect the action to the run method
  55. QObject.connect(self.action, SIGNAL("triggered()"), self.run)
  56. # Add toolbar button and menu item
  57. self.iface.addToolBarIcon(self.action)
  58. self.iface.addPluginToMenu(u"&Multispectral Land Masker", self.action)
  59. def unload(self):
  60. # Remove the plugin menu item and icon
  61. self.iface.removePluginMenu(u"&Multispectral Land Masker", self.action)
  62. self.iface.removeToolBarIcon(self.action)
  63. # run method that performs all the real work
  64. def run(self):
  65. #- populate the combo box with loaded layers
  66. self.dlg.initLayerCombobox( self.dlg.ui.inputRasterComboBox, 'key_of_default_layer' )
  67. # show the dialog
  68. self.dlg.show()
  69. # Run the dialog event loop
  70. result = self.dlg.exec_()
  71. # See if OK was pressed
  72. if result == 1:
  73. #- get the layer
  74. rast_layer = self.dlg.layerFromComboBox( self.dlg.ui.inputRasterComboBox )
  75. rds = RasterDS( rast_layer )
  76. # get the filename
  77. filename = str( self.dlg.ui.outputRasterLineEdit.text() )
  78. # get the threshold from the dialog
  79. thresh = float( self.dlg.ui.thresholdDoubleSpinBox.text() )
  80. # get connectivity threshold
  81. connthresh = int( self.dlg.ui.connectivitySpinBox.text() )
  82. #mask_band = rds.ward_cluster_land_mask()
  83. mask_band = rds.simple_land_mask(threshold=thresh)
  84. # filter out small patches
  85. mask_band = connectivity_filter(mask_band,threshold=connthresh)
  86. outfile = rds.new_image_from_array(mask_band,outfilename=filename)
  87. if self.dlg.ui.addMaskCheckBox.isChecked():
  88. fileInfo = QFileInfo(filename)
  89. baseName = fileInfo.baseName()
  90. rlayer = QgsRasterLayer(filename, baseName)
  91. if not rlayer.isValid():
  92. #print "Layer failed to load!"
  93. mbox = QMessageBox()
  94. outtext = "Layer failed to load!"
  95. mbox.setText( outtext )
  96. mbox.setWindowTitle( "Error" )
  97. mbox.exec_()
  98. QgsMapLayerRegistry.instance().addMapLayer(rlayer)