Skip to content

Commit

Permalink
rasterize
Browse files Browse the repository at this point in the history
  • Loading branch information
patricev committed Apr 6, 2016
1 parent 0ab55c6 commit e2e36d8
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 15 deletions.
6 changes: 3 additions & 3 deletions dialogs/posttelemacpropertiesdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,14 @@ def delete_def_variables(self):
if self.layer.parametres[index][2]:
self.layer.param_displayed = index-1
self.layer.parametres[index:index+1] = []
self.populatecombobox_param()
self.setTreeWidgetIndex(self.treeWidget_parameters,0,index-1)
#checkkeysparameter
self.layer.parametreh = None
self.layer.parametrevx = None
self.layer.parametrevy = None

#update all
self.layer.updateSelafinValues()
self.populatecombobox_param()
self.setTreeWidgetIndex(self.treeWidget_parameters,0,index-1)

#Display tools - contour - color ramp things ***********************************************

Expand Down
Binary file added icons/tools/layer_raster_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
68 changes: 62 additions & 6 deletions libs/posttelemac_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from posttelemac_util_flow import *
from posttelemac_util_get_max import *
from posttelemac_util_getcomparevalue import *
from posttelemac_util_rasterize import *


class PostTelemacUtils():
Expand Down Expand Up @@ -636,8 +637,13 @@ def workerFinishedHillshade(self,strpath):
self.selafinlayer.propertiesdialog.normalMessage(str(os.path.basename(strpath).split('.')[0]) + self.tr(" created"))

def rasterCreation(self):
#print 'ok'
#Points
self.initclass=InitRasterize()
self.initclass.status.connect(self.selafinlayer.propertiesdialog.textBrowser_2.append)
self.initclass.finished1.connect(self.rasterCreationFinished)
self.selafinlayer.propertiesdialog.normalMessage('Raster creation started')
self.initclass.start(self.selafinlayer)

"""
layerstring = self.selafinlayer.hydraufilepath+'[' + str(self.selafinlayer.time_displayed) + ']'
print layerstring
layerpoint = QgsVectorLayer(layerstring, 'test', "ogr")
Expand Down Expand Up @@ -667,10 +673,60 @@ def rasterCreation(self):
test = qgis.analysis.QgsGridFileWriter(itp,rasterfilepath,rect,ncol,ncol,res,res)
print 'ok1'
test.writeFile(False)




"""
"""
#Extent evaluation
if self.selafinlayer.propertiesdialog.comboBox_rasterextent.currentIndex() == 0 :
rect = iface.mapCanvas().extent()
elif self.selafinlayer.propertiesdialog.comboBox_rasterextent.currentIndex() == 1 :
rect = self.selafinlayer.extent()
#res
res = self.selafinlayer.propertiesdialog.spinBox_rastercellsize.value()
#grid creation
xmin,xmax,ymin,ymax = [int(rect.xMinimum()), int(rect.xMaximum()), int(rect.yMinimum()), int(rect.yMaximum()) ]
xi, yi = np.meshgrid(np.arange(xmin, xmax, res), np.arange(ymin, ymax, res))
self.selafinlayer.initTriinterpolator()
paramindex = self.selafinlayer.propertiesdialog.comboBox_parametreschooser_2.currentIndex()
zi = self.selafinlayer.triinterp[paramindex](xi, yi)
nrows,ncols = np.shape(zi)
#xres = (xmax-xmin)/float(ncols)
#yres = (ymax-ymin)/float(nrows)
xres = res
yres = res
geotransform=(xmin,xres,0,ymin,0, yres)
raster_ut = os.path.join(os.path.dirname(self.selafinlayer.hydraufilepath),str(os.path.basename(self.selafinlayer.hydraufilepath).split('.')[0] ) + '_raster_'+str(self.selafinlayer.parametres[paramindex][1])+'.tif')
#output_raster = gdal.GetDriverByName('GTiff').Create(raster_ut,ncols, nrows, 1 ,gdal.GDT_Float32,['TFW=YES', 'COMPRESS=PACKBITS']) # Open the file, see here for information about compression: http://gis.stackexchange.com/questions/1104/should-gdal-be-set-to-produce-geotiff-files-with-compression-which-algorithm-sh
output_raster = gdal.GetDriverByName('GTiff').Create(raster_ut,ncols, nrows, 1 ,gdal.GDT_Float32,['TFW=YES', 'COMPRESS=PACKBITS']) # Open the file, see here for information about compression: http://gis.stackexchange.com/questions/1104/should-gdal-be-set-to-produce-geotiff-files-with-compression-which-algorithm-sh
output_raster.SetGeoTransform(geotransform) # Specify its coordinates
srs = osr.SpatialReference() # Establish its coordinate encoding
srs.ImportFromEPSG(2154) # This one specifies SWEREF99 16 30
output_raster.SetProjection( srs.ExportToWkt() ) # Exports the coordinate system to the file
output_raster.GetRasterBand(1).WriteArray(zi) # Writes my array to the raster
"""
"""
try:
print str(raster_ut)
print str(os.path.basename(raster_ut).split('.')[0])
rlayer = QgsRasterLayer(raster_ut, os.path.basename(raster_ut).split('.')[0])
print str(rlayer.isValid())
print str(rlayer.crs().authid())
QgsMapLayerRegistry.instance().addMapLayer(rlayer)
except Exception, e:
print str(e)
"""
def rasterCreationFinished(self,strpath):
if strpath != None:
rlayer = QgsRasterLayer(strpath, os.path.basename(strpath).split('.')[0])
QgsMapLayerRegistry.instance().addMapLayer(rlayer)
self.selafinlayer.propertiesdialog.normalMessage(str(os.path.basename(strpath).split('.')[0]) + self.tr(" created"))



Expand Down
162 changes: 162 additions & 0 deletions libs/posttelemac_util_rasterize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# -*- coding: utf-8 -*-

#import qgis
from qgis.core import *
from qgis.gui import *
from qgis.utils import *
#import PyQT
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, Qt
from PyQt4 import QtCore, QtGui
#import numpy
from numpy import *
import numpy as np
#import gdal
from osgeo import gdal
from osgeo import osr
#imports divers
from time import ctime
import math
import sys
import os.path



#*********************************************************************************************
#*************** Classe de traitement **********************************************************
#********************************************************************************************


class rasterize(QtCore.QObject):

def __init__(self, selafin):

QtCore.QObject.__init__(self)
self.selafinlayer = selafin
#self.points = qgspoints
#self.skdtree = None
#self.compare = compare


def createRaster(self):
try:
if self.selafinlayer.propertiesdialog.comboBox_rasterextent.currentIndex() == 0 :
rect = self.selafinlayer.xform.transform(iface.mapCanvas().extent(), QgsCoordinateTransform.ReverseTransform)
elif self.selafinlayer.propertiesdialog.comboBox_rasterextent.currentIndex() == 1 :
rect = self.selafinlayer.xform.transform(self.selafinlayer.extent(), QgsCoordinateTransform.ReverseTransform)
#res
res = self.selafinlayer.propertiesdialog.spinBox_rastercellsize.value()
#grid creation
xmin,xmax,ymin,ymax = [int(rect.xMinimum()), int(rect.xMaximum()), int(rect.yMinimum()), int(rect.yMaximum()) ]


try:
xi, yi = np.meshgrid(np.arange(xmin, xmax, res), np.arange(ymin, ymax, res))
except Exception, e:
self.status.emit(str(e))
self.finished.emit(None)

self.selafinlayer.initTriinterpolator()
paramindex = self.selafinlayer.propertiesdialog.comboBox_parametreschooser_2.currentIndex()
zi = self.selafinlayer.triinterp[paramindex](xi, yi)

nrows,ncols = np.shape(zi)
self.status.emit('Raster creation - nrows : ' + str(nrows)+' - ncols : ' + str(ncols))

#xres = (xmax-xmin)/float(ncols)
#yres = (ymax-ymin)/float(nrows)
xres = res
yres = res
geotransform=(xmin,xres,0,ymin,0, yres)


raster_ut = os.path.join(os.path.dirname(self.selafinlayer.hydraufilepath),str(os.path.basename(self.selafinlayer.hydraufilepath).split('.')[0] ) + '_raster_'+str(self.selafinlayer.parametres[paramindex][1])+'.tif')

#output_raster = gdal.GetDriverByName('GTiff').Create(raster_ut,ncols, nrows, 1 ,gdal.GDT_Float32,['TFW=YES', 'COMPRESS=PACKBITS']) # Open the file, see here for information about compression: http://gis.stackexchange.com/questions/1104/should-gdal-be-set-to-produce-geotiff-files-with-compression-which-algorithm-sh
output_raster = gdal.GetDriverByName('GTiff').Create(raster_ut,ncols, nrows, 1 ,gdal.GDT_Float32,['TFW=YES']) # Open the file, see here for information about compression: http://gis.stackexchange.com/questions/1104/should-gdal-be-set-to-produce-geotiff-files-with-compression-which-algorithm-sh
output_raster.SetGeoTransform(geotransform) # Specify its coordinates
srs = osr.SpatialReference() # Establish its coordinate encoding
crstemp = self.selafinlayer.crs().authid()
if crstemp.startswith('EPSG:'):
crsnumber = int(crstemp[5:])
else:
self.status.emit(str('Please choose a EPSG crs'))
self.finished.emit(None)

srs.ImportFromEPSG(crsnumber) # This one specifies SWEREF99 16 30
output_raster.SetProjection( srs.ExportToWkt() ) # Exports the coordinate system to the file
output_raster.GetRasterBand(1).WriteArray(zi) # Writes my array to the raster


self.finished.emit(raster_ut)
except Exception, e:
self.status.emit(str(e))
self.finished.emit(None)

progress = QtCore.pyqtSignal(int)
status = QtCore.pyqtSignal(str)
error = QtCore.pyqtSignal(str)
killed = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal(str)



#*********************************************************************************************
#*************** Classe de lancement du thread **********************************************************
#********************************************************************************************


class InitRasterize(QtCore.QObject):

def __init__(self):
QtCore.QObject.__init__(self)
self.thread = None
self.worker = None
self.processtype = 0


def start(self, selafin):

#Launch worker
self.thread = QtCore.QThread()
self.worker = rasterize(selafin)
#self.graphtemp.points = qgspoints
#self.worker = self.graphtemp

self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.createRaster)
self.worker.status.connect(self.writeOutput)
self.worker.error.connect(self.raiseError)
self.worker.finished.connect(self.workerFinished)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.finished.connect(self.thread.quit)
self.thread.start()



def raiseError(self,str):
if self.processtype ==0:
self.status.emit(str)
elif self.processtype in [1,2,3]:
raise GeoAlgorithmExecutionException(str)
elif self.processtype == 4:
print str
sys.exit(0)

def writeOutput(self,str1):
self.status.emit(str(str1))

def workerFinished(self,str):
self.finished1.emit(str)




status = QtCore.pyqtSignal(str)
error = QtCore.pyqtSignal(str)
finished1 = QtCore.pyqtSignal(str)



3 changes: 2 additions & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name=PostTelemac
about= Post treatment of open Telemac-Mascaret results. Requires matplotlib, shapely, numpy, scipy and networkx libraries.
qgisMinimumVersion=2.10.0
description=Post Treatment for Telemac
version=0.1.3
version=0.1.4
author=Artelia Eau et Environnement
[email protected]

Expand All @@ -21,6 +21,7 @@ [email protected]

# Uncomment the following line and add your changelog:
changelog=
0.1.4 : Rasterize and automatic water depth recognition
0.1.3 : bugfix. Code still messy.
0.1.2 : bugfix, max res improvement. Code still messy.
0.1.1 : color management, faster rendering. Code still messy.
Expand Down
1 change: 1 addition & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
<file>icons/tools/layer_shaded_relief_add.png</file>
<file>icons/tools/Files_Copy.png</file>
<file>icons/tools/Wizard_48x48.png</file>
<file>icons/tools/layer_raster_add.png</file>
</qresource>
</RCC>
47 changes: 43 additions & 4 deletions resources_rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Resource object code
#
# Created: lun. 18. janv. 11:25:43 2016
# Created: mer. 6. avr. 13:52:43 2016
# by: The Resource Compiler for PyQt (Qt v4.8.5)
#
# WARNING! All changes made in this file will be lost!
Expand Down Expand Up @@ -4934,6 +4934,39 @@
\x09\x16\x98\x10\x09\x85\xff\x17\xf1\x3a\x78\x91\xd3\xc5\x03\xe0\
\x5c\xe3\x7f\x01\x06\x00\xba\x74\x31\xfb\xc3\x91\x66\xa4\x00\x00\
\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\
\x00\x00\x01\xeb\
\x89\
\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\
\x00\x00\x18\x00\x00\x00\x18\x08\x06\x00\x00\x00\xe0\x77\x3d\xf8\
\x00\x00\x00\x04\x73\x42\x49\x54\x08\x08\x08\x08\x7c\x08\x64\x88\
\x00\x00\x00\x09\x70\x48\x59\x73\x00\x00\x0d\xd7\x00\x00\x0d\xd7\
\x01\x42\x28\x9b\x78\x00\x00\x00\x19\x74\x45\x58\x74\x53\x6f\x66\
\x74\x77\x61\x72\x65\x00\x77\x77\x77\x2e\x69\x6e\x6b\x73\x63\x61\
\x70\x65\x2e\x6f\x72\x67\x9b\xee\x3c\x1a\x00\x00\x01\x68\x49\x44\
\x41\x54\x48\x89\xe5\x96\x31\x4f\xc2\x50\x14\x46\x4f\x4b\xd3\xcd\
\x45\x1d\x34\x31\xe8\xa4\x83\x9a\x18\x07\x26\x8d\x03\x09\x03\x13\
\x33\x6e\xc4\x85\xc9\x01\x06\x7f\x04\x1d\x9c\x58\x0c\x9b\x0e\x4e\
\x4d\xba\x90\x6e\x46\x26\x06\x34\xb2\x4b\x52\x26\xb5\x85\xa4\x03\
\x8b\xa4\x75\x30\x10\x5a\x5a\xaa\x45\x06\xf5\x9b\xde\xed\xbd\xb9\
\xe7\xbe\xf7\xbd\xb4\x15\x5c\xd7\x65\x91\x92\xa2\x0a\x8e\x4f\xcf\
\x3d\x13\x64\x32\x19\x4f\x5e\xd7\x75\x4f\x7c\x7f\x7d\x29\x4c\xc6\
\x62\xfc\xd9\xbe\xa6\xdf\x0f\x18\x7b\x50\xad\x56\x03\xdd\x8e\x3a\
\x73\x7f\x3e\x14\x00\x90\xcb\xe5\xa6\x0a\xae\xf4\x56\xd4\x90\x33\
\xf5\x87\x3c\x08\x93\xff\xcc\xfd\x3a\xcb\x1c\xa2\xaa\x2a\xc5\x62\
\x51\x08\xca\x8b\x61\xe6\xfe\x94\xfe\x81\x07\xb7\x95\x8b\xc5\x02\
\xc2\xd4\x79\xeb\xa0\xb5\x34\x0c\xcb\x00\xa0\xa1\x34\xfc\x5e\x36\
\x5d\xdc\x52\x6c\x80\xd6\xd2\x18\xbc\x0f\xd8\xde\xd8\x41\x12\x25\
\xb2\x07\x59\x1c\xc7\xa1\xfe\x54\x67\xe8\x0c\xe9\xd9\x56\xca\xb4\
\x4d\x25\x36\xc0\xb0\x0c\xf6\xb6\xf6\x91\xc4\xcf\x16\xc9\x95\x24\
\x00\xb2\x24\x23\x23\x23\x2f\xcb\x98\xb6\x99\x9a\xcb\xe4\x51\xf3\
\x59\xb9\xd8\x3b\x00\x48\xef\xa6\xd9\x5c\xdd\xf4\x3c\x2b\x9c\x14\
\x00\xe8\x5a\x5d\x1e\x9f\x1f\xe6\x03\xf4\xed\x1e\xeb\x4b\x6b\x81\
\xb9\xd7\xfe\x0b\xe0\xdb\x81\xaa\xaa\xdf\x02\xd4\xee\x6a\xe3\x75\
\x25\x5f\x01\xa0\x7c\x53\xf6\xd4\x8c\x01\x61\xef\x92\x30\x05\x5c\
\xcb\x40\x49\x10\xfe\xb1\x89\x80\x36\x81\xd4\x28\x68\x77\xdb\x24\
\xc4\xc4\x54\x8d\x10\xf7\xb7\x25\xaf\xe4\x8f\x04\x04\x65\x12\xe2\
\x6f\xee\xe2\x96\x3e\x00\xe9\x72\x71\x7b\xce\x19\xf2\x1f\x00\x00\
\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\
\x00\x00\x0e\xa5\
\x89\
\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\
Expand Down Expand Up @@ -5542,6 +5575,11 @@
\x00\x4c\
\x00\x69\x00\x6e\x00\x65\x00\x5f\x00\x47\x00\x72\x00\x61\x00\x70\x00\x68\x00\x5f\x00\x34\x00\x38\x00\x78\x00\x34\x00\x38\x00\x2e\
\x00\x70\x00\x6e\x00\x67\
\x00\x14\
\x05\x00\xe4\x87\
\x00\x6c\
\x00\x61\x00\x79\x00\x65\x00\x72\x00\x5f\x00\x72\x00\x61\x00\x73\x00\x74\x00\x65\x00\x72\x00\x5f\x00\x61\x00\x64\x00\x64\x00\x2e\
\x00\x70\x00\x6e\x00\x67\
\x00\x1c\
\x02\x93\x2c\x07\
\x00\x4c\
Expand All @@ -5566,7 +5604,7 @@
\x00\x00\x00\x30\x00\x02\x00\x00\x00\x10\x00\x00\x00\x04\
\x00\x00\x01\x90\x00\x00\x00\x00\x00\x01\x00\x00\x54\x3f\
\x00\x00\x01\x0c\x00\x00\x00\x00\x00\x01\x00\x00\x2e\x44\
\x00\x00\x00\xdc\x00\x02\x00\x00\x00\x0b\x00\x00\x00\x14\
\x00\x00\x00\xdc\x00\x02\x00\x00\x00\x0c\x00\x00\x00\x14\
\x00\x00\x00\x86\x00\x00\x00\x00\x00\x01\x00\x00\x11\x42\
\x00\x00\x00\xec\x00\x00\x00\x00\x00\x01\x00\x00\x28\x9d\
\x00\x00\x01\xac\x00\x00\x00\x00\x00\x01\x00\x00\x94\x43\
Expand All @@ -5580,15 +5618,16 @@
\x00\x00\x01\x56\x00\x00\x00\x00\x00\x01\x00\x00\x4f\x02\
\x00\x00\x01\xfc\x00\x00\x00\x00\x00\x01\x00\x00\xb7\xd7\
\x00\x00\x02\x40\x00\x00\x00\x00\x00\x01\x00\x00\xcc\x29\
\x00\x00\x04\x48\x00\x01\x00\x00\x00\x01\x00\x01\x4d\xa1\
\x00\x00\x04\x76\x00\x01\x00\x00\x00\x01\x00\x01\x4f\x90\
\x00\x00\x04\x00\x00\x00\x00\x00\x00\x01\x00\x01\x32\x79\
\x00\x00\x03\xd2\x00\x00\x00\x00\x00\x01\x00\x01\x30\x8a\
\x00\x00\x00\xac\x00\x00\x00\x00\x00\x01\x00\x00\xe4\x77\
\x00\x00\x02\x9a\x00\x00\x00\x00\x00\x01\x00\x00\xde\x25\
\x00\x00\x02\xf8\x00\x00\x00\x00\x00\x01\x00\x00\xfc\x45\
\x00\x00\x03\x30\x00\x00\x00\x00\x00\x01\x00\x01\x08\xc1\
\x00\x00\x03\xa4\x00\x00\x00\x00\x00\x01\x00\x01\x26\x7b\
\x00\x00\x03\x7a\x00\x01\x00\x00\x00\x01\x00\x01\x24\xf2\
\x00\x00\x04\x10\x00\x00\x00\x00\x00\x01\x00\x01\x3f\x33\
\x00\x00\x04\x3e\x00\x00\x00\x00\x00\x01\x00\x01\x41\x22\
\x00\x00\x03\x56\x00\x00\x00\x00\x00\x01\x00\x01\x1a\x36\
\x00\x00\x02\xbc\x00\x00\x00\x00\x00\x01\x00\x00\xf8\x0a\
"
Expand Down
Loading

0 comments on commit e2e36d8

Please sign in to comment.