forked from geodrinx/rasmover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rasmover.py
287 lines (222 loc) · 9.85 KB
/
rasmover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# -*- coding: utf-8 -*-
"""
/***************************************************************************
rasmover
A QGIS plugin
rasmover
-------------------
begin : 2014-03-24
copyright : (C) 2014 by geodrinx
email : [email protected]
Roberto (BobMaX) Angeletti 2014-03-24 idea and first version
Giovanni Manghi 2014-12-17 fix installation on Linux, fix bug on Windows when using some projections
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
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)
# Import the PyQt and QGIS libraries
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
from . import resources_rc
# Import the code for the dialog
from .rasmoverdialog import rasmoverDialog
import os.path
import math
import sys, itertools, os, glob, subprocess, zipfile, zlib, tempfile
import platform
import string
import subprocess
try:
from osgeo import gdal
from sys import platform as _platform
except ImportError:
import gdal
rb=QgsRubberBand(iface.mapCanvas(), QgsWkbTypes.PointGeometry)
rl=QgsRubberBand(iface.mapCanvas(), QgsWkbTypes.LineGeometry)
premuto= False
linea=False
point0=iface.mapCanvas().getCoordinateTransform().toMapCoordinates(0, 0)
point1=iface.mapCanvas().getCoordinateTransform().toMapCoordinates(0, 0)
class rasmover:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value("locale/userLocale")[0:2]
localePath = os.path.join(self.plugin_dir, 'i18n', 'rasmover_{}.qm'.format(locale))
if os.path.exists(localePath):
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.dlg = rasmoverDialog()
def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(
QIcon(":/plugins/rasmover/icon.png"),
u"rasmover", self.iface.mainWindow())
# connect the action to the run method
self.action.triggered.connect(self.run)
# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu(u"&rasmover", self.action)
def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu(u"&rasmover", self.action)
self.iface.removeToolBarIcon(self.action)
# run method that performs all the real work
def run(self):
tool = PointTool(self.iface.mapCanvas())
self.iface.mapCanvas().setMapTool(tool)
class PointTool(QgsMapTool):
def __init__(self, canvas):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
def canvasPressEvent(self, event):
x = event.pos().x()
y = event.pos().y()
global rb ,premuto ,point0
if not premuto:
premuto=True
rb=QgsRubberBand(iface.mapCanvas(), QgsWkbTypes.PointGeometry )
rb.setColor ( Qt.red )
point0 = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
rb.addPoint(point0)
def canvasMoveEvent(self, event):
x = event.pos().x()
y = event.pos().y()
global premuto,point0,point1,linea,rl
if premuto:
if not linea:
rl.setColor ( Qt.red )
poin1 = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
rl.addPoint(point0)
rl.addPoint(point1)
linea=True
else:
if linea:
point1 = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
rl.reset(QgsWkbTypes.LineGeometry)
rl.addPoint(point0)
rl.addPoint(point1)
def canvasReleaseEvent(self, event):
global premuto,linea,rb,rl,point1,point0
angle = math.atan2(point1.x() - point0.x(), point1.y() - point0.y())
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.mapSettings().destinationCrs()
crsDest = QgsCoordinateReferenceSystem.fromEpsgId(32633) # WGS 84 / UTM zone 33N
xform = QgsCoordinateTransform(actual_crs, crsDest, QgsProject.instance())
pt0 = xform.transform(point0)
pt1 = xform.transform(point1)
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 = pt1.x() - pt0.x()
dy = pt1.y() - pt0.y()
line = ""
f2 = open(dbName+'.csv', 'w')
f2.write("dx,dy\n")
line = ("%lf") %( dx )
line = ("%s,%lf\n") %( line, dy )
f2.write(line)
f2.close()
out_folder = temp_dir
# if _platform == "win32":
# out_folder = out_folder.replace("\\","/")
listaRaster = os.path.join(out_folder, 'lista.txt')
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())
scrivimi = ("%s\n") %(fileIMAGE)
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):
QgsProject.instance().removeMapLayer(toglimi)
f5.close()
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')
for line in f1:
stringa = line
if(stringa.find("<GeoTransform>") != -1):
words1 = stringa.replace(">-","> -");
words2 = words1.replace(",-",", -");
words3 = words2.split();
coordX = words3[1].replace(",","")
coordY = words3[4].replace(",","")
X = float( coordX ) + dx
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] )
f2.write(stringazza)
else:
f2.write(stringa)
f2.close()
f1.close()
rl.reset()
rb.reset()
fileName = movedVrt
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)
if not rlayer.isValid():
print("Layer failed to load!")
QgsProject.instance().addMapLayer(rlayer)
def activate(self):
pass
def deactivate(self):
pass
def isZoomTool(self):
return False
def isTransient(self):
return False
def isEditTool(self):
return True