-
Notifications
You must be signed in to change notification settings - Fork 31
/
createGroundOverlayGeoTiff.py
189 lines (163 loc) · 7.05 KB
/
createGroundOverlayGeoTiff.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
* *
* 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 os
from qgis.PyQt.QtCore import QObject, QVariant, QCoreApplication, QUrl, pyqtSignal
from qgis.PyQt.QtGui import QIcon
from qgis import processing
from qgis.core import (
QgsCoordinateReferenceSystem, QgsPointXY, QgsRasterLayer, QgsProject,
QgsLineString, QgsMultiLineString, QgsPolygon, QgsMultiPolygon,
QgsFeature, QgsGeometry, QgsFields, QgsField, QgsWkbTypes)
from qgis.core import (
QgsProcessingAlgorithm,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingException,
QgsProcessingParameterFileDestination)
from zipfile import ZipFile
import xml.sax
import xml.sax.handler
import traceback
epsg4326 = QgsCoordinateReferenceSystem("EPSG:4326")
def tr(string):
return QCoreApplication.translate('Processing', string)
class CreateGroundOverlayGeoTiffAlgorithm(QgsProcessingAlgorithm):
"""
Algorithm to import KML and KMZ files.
"""
PrmInput = 'Input'
PrmOutputRaster = 'OutputRaster'
PrmNorthLatitude = 'NorthLatitude'
PrmSouthLatitude = 'SouthLatitude'
PrmEastLongitude = 'EastLongitude'
PrmWestLongitude = 'WestLongitude'
PrmRotation = 'Rotation'
def initAlgorithm(self, config):
self.addParameter(
QgsProcessingParameterRasterLayer(
self.PrmInput,
tr('Input image'))
)
param = QgsProcessingParameterNumber(
self.PrmNorthLatitude,
tr('North latitude'),
QgsProcessingParameterNumber.Double,
defaultValue=0,
optional=False)
param.setMetadata({'widget_wrapper': { 'decimals': 14 }})
self.addParameter(param)
param = QgsProcessingParameterNumber(
self.PrmSouthLatitude,
tr('South latitude'),
QgsProcessingParameterNumber.Double,
defaultValue=0,
optional=False)
param.setMetadata({'widget_wrapper': { 'decimals': 14 }})
self.addParameter(param)
param = QgsProcessingParameterNumber(
self.PrmEastLongitude,
tr('East longitude'),
QgsProcessingParameterNumber.Double,
defaultValue=0,
optional=False)
param.setMetadata({'widget_wrapper': { 'decimals': 14 }})
self.addParameter(param)
param = QgsProcessingParameterNumber(
self.PrmWestLongitude,
tr('West longitude'),
QgsProcessingParameterNumber.Double,
defaultValue=0,
optional=False)
param.setMetadata({'widget_wrapper': { 'decimals': 14 }})
self.addParameter(param)
param = QgsProcessingParameterNumber(
self.PrmRotation,
tr('Rotation'),
QgsProcessingParameterNumber.Double,
defaultValue=0,
optional=False)
param.setMetadata({'widget_wrapper': { 'decimals': 14 }})
self.addParameter(param)
param=QgsProcessingParameterFileDestination(
self.PrmOutputRaster,
tr('Output GeoTIFF Image'),
optional=False)
param.setFileFilter('*.tif')
self.addParameter(param)
def processAlgorithm(self, parameters, context, feedback):
raster = self.parameterAsRasterLayer(parameters, self.PrmInput, context)
out_path = self.parameterAsOutputLayer(parameters, self.PrmOutputRaster, context)
north = self.parameterAsDouble(parameters, self.PrmNorthLatitude, context)
south = self.parameterAsDouble(parameters, self.PrmSouthLatitude, context)
east = self.parameterAsDouble(parameters, self.PrmEastLongitude, context)
west = self.parameterAsDouble(parameters, self.PrmWestLongitude, context)
rotation = self.parameterAsDouble(parameters, self.PrmRotation, context)
if rotation == 0:
status = processing.run("gdal:translate", {'INPUT': raster,
'EXTRA': '-a_srs EPSG:4326 -a_ullr {} {} {} {}'.format(west, north, east, south),
'DATA_TYPE': 0,
'OUTPUT': out_path})
else:
rwidth = raster.width()
rheight = raster.height()
center_x = (east + west) / 2.0
center_y = (north + south)/ 2.0
center_pt = QgsPointXY(center_x, center_y)
ul_pt = QgsPointXY(west, north)
ur_pt = QgsPointXY(east, north)
lr_pt = QgsPointXY(east, south)
ll_pt = QgsPointXY(west, south)
distance = center_pt.distance(ul_pt)
az = center_pt.azimuth(ul_pt) - rotation
pt1 = center_pt.project(distance, az)
az = center_pt.azimuth(ur_pt) - rotation
pt2 = center_pt.project(distance, az)
az = center_pt.azimuth(lr_pt) - rotation
pt3 = center_pt.project(distance, az)
az = center_pt.azimuth(ll_pt) - rotation
pt4 = center_pt.project(distance, az)
gcp1= '-gcp {} {} {} {}'.format(0,0, pt1.x(), pt1.y())
gcp2= '-gcp {} {} {} {}'.format(rwidth,0, pt2.x(), pt2.y())
gcp3= '-gcp {} {} {} {}'.format(rwidth, rheight, pt3.x(), pt3.y())
gcp4= '-gcp {} {} {} {}'.format(0, rheight, pt4.x(), pt4.y())
status = processing.run("gdal:translate", {'INPUT': raster,
'EXTRA': '-a_srs EPSG:4326 -a_nodata 0,0,0 {} {} {} {}'.format(gcp1, gcp2, gcp3, gcp4),
'DATA_TYPE': 0,
'OUTPUT': out_path})
feedback.pushInfo('{}'.format(status))
results = {}
results[self.PrmOutputRaster] = out_path
return (results)
def name(self):
return 'groundoverlay2geotiff'
def icon(self):
return QIcon(os.path.dirname(__file__) + '/icons/gnd_overlay.svg')
def displayName(self):
return tr('Ground Overlay to GeoTIFF Image')
def group(self):
return tr('Raster conversion')
def groupId(self):
return 'rasterconversion'
def helpUrl(self):
file = os.path.dirname(__file__) + '/index.html'
if not os.path.exists(file):
return ''
return QUrl.fromLocalFile(file).toString(QUrl.FullyEncoded)
def shortHelpString(self):
file = os.path.dirname(__file__) + '/doc/gndoverlay2tiff.help'
if not os.path.exists(file):
return ''
with open(file) as helpf:
help = helpf.read()
return help
def createInstance(self):
return CreateGroundOverlayGeoTiffAlgorithm()