From 3459d70327fbe2174925169825c91f7dcf09db33 Mon Sep 17 00:00:00 2001 From: Mikhail Itkin Date: Fri, 18 Oct 2019 12:28:20 +0200 Subject: [PATCH] Ports rasmover to QGIS3 --- __init__.py | 2 +- metadata.txt | 2 +- rasmover.py | 141 +++++++++++++++++++++++++--------------------- rasmoverdialog.py | 8 +-- resources_rc.py | 8 +-- ui_rasmover.py | 18 +++--- 6 files changed, 95 insertions(+), 84 deletions(-) diff --git a/__init__.py b/__init__.py index 0e84ac0..86e5361 100644 --- a/__init__.py +++ b/__init__.py @@ -23,5 +23,5 @@ def classFactory(iface): # load rasmover class from file rasmover - from rasmover import rasmover + from .rasmover import rasmover return rasmover(iface) diff --git a/metadata.txt b/metadata.txt index f27ce6e..576b057 100644 --- a/metadata.txt +++ b/metadata.txt @@ -1,6 +1,6 @@ [general] name = Rasmover -qgisMinimumVersion = 2.0 +qgisMinimumVersion = 3.0 about = Move (translate) manually raster layers description = Move (translate) manually raster layers, the result is a virtual raster (vrt) created using GDAL gdalbuildvrt tool. version = version 0.2 diff --git a/rasmover.py b/rasmover.py index 60e60f0..9a3a34c 100644 --- a/rasmover.py +++ b/rasmover.py @@ -24,26 +24,27 @@ ***************************************************************************/ """ import sip -sip.setapi('QDate', 2) -sip.setapi('QDateTime', 2) -sip.setapi('QString', 2) -sip.setapi('QTextStream', 2) -sip.setapi('QTime', 2) -sip.setapi('QUrl', 2) -sip.setapi('QVariant', 2) +# sip.setapi('QDate', 2) +# sip.setapi('QDateTime', 2) +# sip.setapi('QString', 2) +# sip.setapi('QTextStream', 2) +# sip.setapi('QTime', 2) +# sip.setapi('QUrl', 2) +# sip.setapi('QVariant', 2) # Import the PyQt and QGIS libraries -from PyQt4.QtCore import QSettings, Qt, QTranslator, QCoreApplication, QFileInfo -from PyQt4.QtGui import QAction, QIcon -from qgis.core import QGis, QgsCoordinateReferenceSystem, QgsMapLayerRegistry, QgsRasterLayer, QgsCoordinateTransform -from qgis.gui import QgsRubberBand, QgsMapTool +from PyQt5.QtCore import QSettings, Qt, QTranslator, QCoreApplication, QFileInfo +from PyQt5.QtGui import QIcon +from PyQt5.QtWidgets import QAction +from qgis.core import QgsCoordinateReferenceSystem, QgsProject, QgsRasterLayer, QgsCoordinateTransform, QgsPoint, QgsLineString, QgsWkbTypes +from qgis.gui import QgsRubberBand, QgsMapTool, QgsLayerTreeView from qgis.utils import iface # Initialize Qt resources from file resources.py -import resources_rc +from . import resources_rc # Import the code for the dialog -from rasmoverdialog import rasmoverDialog +from .rasmoverdialog import rasmoverDialog import os.path import math import sys, itertools, os, glob, subprocess, zipfile, zlib, tempfile @@ -57,8 +58,8 @@ except ImportError: import gdal -rb=QgsRubberBand(iface.mapCanvas(),QGis.Point ) -rl=QgsRubberBand(iface.mapCanvas(),QGis.Line ) +rb=QgsRubberBand(iface.mapCanvas(), QgsWkbTypes.PointGeometry) +rl=QgsRubberBand(iface.mapCanvas(), QgsWkbTypes.LineGeometry) premuto= False linea=False point0=iface.mapCanvas().getCoordinateTransform().toMapCoordinates(0, 0) @@ -122,7 +123,7 @@ def canvasPressEvent(self, event): global rb ,premuto ,point0 if not premuto: premuto=True - rb=QgsRubberBand(iface.mapCanvas(),QGis.Point ) + rb=QgsRubberBand(iface.mapCanvas(), QgsWkbTypes.PointGeometry ) rb.setColor ( Qt.red ) point0 = self.canvas.getCoordinateTransform().toMapCoordinates(x, y) rb.addPoint(point0) @@ -141,7 +142,7 @@ def canvasMoveEvent(self, event): else: if linea: point1 = self.canvas.getCoordinateTransform().toMapCoordinates(x, y) - rl.reset(QGis.Line) + rl.reset(QgsWkbTypes.LineGeometry) rl.addPoint(point0) rl.addPoint(point1) @@ -151,84 +152,94 @@ def canvasReleaseEvent(self, event): angle = math.degrees(angle)if angle>0 else (math.degrees(angle) + 180)+180 premuto=False linea=False - actual_crs = self.canvas.mapRenderer().destinationCrs() + # actual_crs = self.canvas.mapRenderer().destinationCrs() + actual_crs = self.canvas.mapSettings().destinationCrs() crsDest = QgsCoordinateReferenceSystem(4326) # WGS 84 / UTM zone 33N - xform = QgsCoordinateTransform(actual_crs, crsDest) + crsDest = QgsCoordinateReferenceSystem(4326) # WGS 84 / UTM zone 33N + xform = QgsCoordinateTransform(actual_crs, crsDest, QgsProject.instance()) pt1 = xform.transform(point0) - dbName = os.getenv("HOME")+'/.qgis2/python/plugins/rasmover/temp/coords' + current_dir = os.path.dirname( + os.path.abspath(__file__) + ) + + temp_dir = os.path.join(current_dir, "temp") + + dbName = os.path.join(temp_dir, "coords") dx = point1.x() - point0.x() dy = point1.y() - point0.y() line = "" - + f2 = open(dbName+'.csv', 'w') - f2.write("dx,dy\n") + f2.write("dx,dy\n") line = ("%lf") %( dx ) line = ("%s,%lf\n") %( line, dy ) - + f2.write(line) f2.close() - out_folder = os.getenv("HOME")+'/.qgis2/python/plugins/rasmover/temp' - if _platform == "win32": - out_folder = out_folder.replace("\\","/") + out_folder = temp_dir + # if _platform == "win32": + # out_folder = out_folder.replace("\\","/") -# print("%s\n") %(out_folder) + listaRaster = os.path.join(out_folder, 'lista.txt') - listaRaster = out_folder + '/lista.txt' - - f5 = open( listaRaster, 'w' ) + f5 = open( listaRaster, 'a' ) toglimi = 0 - + for iLayer in range(self.canvas.layerCount()): layer = self.canvas.layer(iLayer) - + if layer.type() == layer.RasterLayer: if (layer.name() == "moved"): - toglimi = layer.id() - else: - fileIMAGE = str(layer.source()) + toglimi = layer.id() + else: + fileIMAGE = str(layer.source()) scrivimi = ("%s\n") %(fileIMAGE) - f5.write( scrivimi ) + f5.write( scrivimi ) + + # for layer in iface.layerTreeView().selectedLayers(): + # if layer.type() == layer.RasterLayer: + # if (layer.name() == "moved"): + # toglimi = layer.id() + # QgsProject.instance().removeMapLayer(toglimi) + # else: + # fileIMAGE = str(layer.source()) + # scrivimi = ("%s\n") %(fileIMAGE) + # f5.write( scrivimi ) if (toglimi != 0): - QgsMapLayerRegistry.instance().removeMapLayer(toglimi) - #print("HO TOLTO %s\n") %("moved") + QgsProject.instance().removeMapLayer(toglimi) f5.close() - - fileVRT = ("%s") %(out_folder + '/original.vrt') - - subprocess.call([ "gdalbuildvrt", fileVRT, "-input_file_list", listaRaster ]) - if _platform == "win32": - fileVRT = fileVRT.replace("/","\\") - + + fileVRT = os.path.join(out_folder , 'original.vrt') + movedVrt = os.path.join(out_folder, "moved.vrt") + + subprocess.call([ "gdalbuildvrt", fileVRT, "-input_file_list", listaRaster ]) + f1 = open(fileVRT, 'r') + f2 = open(movedVrt, 'w') - f2 = open(out_folder + '/moved.vrt', 'w') - for line in f1: stringa = line - if(string.find(stringa,"") <> -1): + if(stringa.find("") != -1): words1 = stringa.replace(">-","> -"); words2 = words1.replace(",-",", -"); words3 = words2.split(); - -# print("%s") %( words[1].replace(",","") ) -# print("%s") %( words[4].replace(",","") ) coordX = words3[1].replace(",","") - coordY = words3[4].replace(",","") - + coordY = words3[4].replace(",","") + X = float( coordX ) + dx - Y = float( coordY ) + dy + Y = float( coordY ) + dy stringazza = " " stringazza = ("%s %lf, ") %( stringazza, X ) @@ -236,28 +247,28 @@ def canvasReleaseEvent(self, event): stringazza = ("%s %s") %( stringazza, words3[3] ) stringazza = ("%s %lf,") %( stringazza, Y ) stringazza = ("%s %s ") %( stringazza, words3[5] ) - stringazza = ("%s %s ") %( stringazza, words3[6] ) + stringazza = ("%s %s ") %( stringazza, words3[6] ) f2.write(stringazza) - - - else: + + + else: f2.write(stringa) f2.close() - f1.close() + f1.close() rl.reset() - rb.reset() + rb.reset() - fileName = out_folder + '/moved.vrt' + fileName = movedVrt fileInfo = QFileInfo(fileName) baseName = fileInfo.baseName() rlayer = QgsRasterLayer(fileName, baseName) if not rlayer.isValid(): - print "Layer failed to load!" - - QgsMapLayerRegistry.instance().addMapLayer(rlayer) + print("Layer failed to load!") + + QgsProject.instance().addMapLayer(rlayer) def activate(self): @@ -273,4 +284,4 @@ def isTransient(self): return False def isEditTool(self): - return True + return True diff --git a/rasmoverdialog.py b/rasmoverdialog.py index 0779fb4..00af5af 100644 --- a/rasmoverdialog.py +++ b/rasmoverdialog.py @@ -20,14 +20,14 @@ ***************************************************************************/ """ -from PyQt4 import QtCore, QtGui -from ui_rasmover import Ui_rasmover +from PyQt5 import QtCore, QtGui, QtWidgets +from .ui_rasmover import Ui_rasmover # create the dialog for zoom to point -class rasmoverDialog(QtGui.QDialog, Ui_rasmover): +class rasmoverDialog(QtWidgets.QDialog, Ui_rasmover): def __init__(self): - QtGui.QDialog.__init__(self) + QtWidgets.QDialog.__init__(self) # Set up the user interface from Designer. # After setupUI you can access any designer object by doing # self., and you can use autoconnect slots - see diff --git a/resources_rc.py b/resources_rc.py index 875dbf9..dbce339 100644 --- a/resources_rc.py +++ b/resources_rc.py @@ -7,9 +7,9 @@ # # WARNING! All changes made in this file will be lost! -from PyQt4 import QtCore +from PyQt5 import QtCore -qt_resource_data = "\ +qt_resource_data = b"\ \x00\x00\x04\x42\ \x89\ \x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\ @@ -83,7 +83,7 @@ \x82\ " -qt_resource_name = "\ +qt_resource_name = b"\ \x00\x07\ \x07\x3b\xe0\xb3\ \x00\x70\ @@ -98,7 +98,7 @@ \x00\x63\x00\x6f\x00\x6e\x00\x2e\x00\x70\x00\x6e\x00\x67\ " -qt_resource_struct = "\ +qt_resource_struct = b"\ \x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\ \x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\ \x00\x00\x00\x14\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\ diff --git a/ui_rasmover.py b/ui_rasmover.py index e0ed5c7..53b55a3 100644 --- a/ui_rasmover.py +++ b/ui_rasmover.py @@ -7,7 +7,7 @@ # # WARNING! All changes made in this file will be lost! -from PyQt4 import QtCore, QtGui +from PyQt5 import QtCore, QtGui, QtWidgets try: _fromUtf8 = QtCore.QString.fromUtf8 @@ -16,27 +16,27 @@ def _fromUtf8(s): return s try: - _encoding = QtGui.QApplication.UnicodeUTF8 + _encoding = QtWidgets.QApplication.UnicodeUTF8 def _translate(context, text, disambig): - return QtGui.QApplication.translate(context, text, disambig, _encoding) + return QtWidgets.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): - return QtGui.QApplication.translate(context, text, disambig) + return QtWidgets.QApplication.translate(context, text, disambig) class Ui_rasmover(object): def setupUi(self, rasmover): rasmover.setObjectName(_fromUtf8("rasmover")) rasmover.resize(400, 300) - self.buttonBox = QtGui.QDialogButtonBox(rasmover) + self.buttonBox = QtWidgets.QDialogButtonBox(rasmover) self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) - self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) + self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.buttonBox.setObjectName(_fromUtf8("buttonBox")) self.retranslateUi(rasmover) - QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), rasmover.accept) - QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), rasmover.reject) - QtCore.QMetaObject.connectSlotsByName(rasmover) + #QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), rasmover.accept) + #QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), rasmover.reject) + #QtCore.QMetaObject.connectSlotsByName(rasmover) def retranslateUi(self, rasmover): rasmover.setWindowTitle(_translate("rasmover", "rasmover", None))