Skip to content

Commit

Permalink
Ports rasmover to QGIS3
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Itkin authored and Mikhail Itkin committed Oct 18, 2019
1 parent b2c48ac commit 3459d70
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 84 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@

def classFactory(iface):
# load rasmover class from file rasmover
from rasmover import rasmover
from .rasmover import rasmover
return rasmover(iface)
2 changes: 1 addition & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
141 changes: 76 additions & 65 deletions rasmover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -151,113 +152,123 @@ 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,"<GeoTransform>") <> -1):
if(stringa.find("<GeoTransform>") != -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 = " <GeoTransform>"
stringazza = ("%s %lf, ") %( stringazza, X )
stringazza = ("%s %s") %( stringazza, words3[2] )
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):
Expand All @@ -273,4 +284,4 @@ def isTransient(self):
return False

def isEditTool(self):
return True
return True
8 changes: 4 additions & 4 deletions rasmoverdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.<objectname>, and you can use autoconnect slots - see
Expand Down
8 changes: 4 additions & 4 deletions resources_rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down Expand Up @@ -83,7 +83,7 @@
\x82\
"

qt_resource_name = "\
qt_resource_name = b"\
\x00\x07\
\x07\x3b\xe0\xb3\
\x00\x70\
Expand All @@ -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\
Expand Down
18 changes: 9 additions & 9 deletions ui_rasmover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 3459d70

Please sign in to comment.