forked from sdteffen/qgis-elevation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElevation.py
174 lines (152 loc) · 7.19 KB
/
Elevation.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
#
# This file is part of the QGIS Elevation Plugin
#
# Elevation.py - load Elevation class from file Elevation.py
#
# Copyright 2010, 2013, 2014 Steffen Macke <[email protected]>
#
# The QGIS Elevation plugin 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, or (at your option) any later version.
#
# The QGIS Elevation plugin is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with program; see the file COPYING. If not,
# write to the Free Software Foundation, Inc., 59 Temple Place
# - Suite 330, Boston, MA 02111-1307, USA.
#
# The QGIS Python bindings are required to run this file
#
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import sys, os, httplib, json, tempfile, urllib
# GeoCoding Utils
from Utils import *
# Elevation imports
from resources import *
from numericmarkers import *
from ImageDialog import *
class Elevation:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
self.canvas = iface.mapCanvas()
# store layer id
self.layerid = ''
def initGui(self):
self.obtainAction = QAction(QIcon(":/plugins/elevation/elevation_icon.png"), QCoreApplication.translate('Elevation', "&Obtain Elevation"), self.iface.mainWindow())
self.aboutAction = QAction(QIcon(":/plugins/elevation/about_icon.png"), QCoreApplication.translate('Elevation', "&About"), self.iface.mainWindow())
self.iface.addPluginToMenu("Elevation", self.obtainAction)
self.iface.addPluginToMenu("Elevation", self.aboutAction)
self.iface.addToolBarIcon(self.obtainAction)
QObject.connect(self.obtainAction, SIGNAL("triggered()"), self.obtain)
QObject.connect(self.aboutAction, SIGNAL("triggered()"), self.about)
def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu("Elevation", self.obtainAction)
self.iface.removePluginMenu("Elevation", self.aboutAction)
self.iface.removeToolBarIcon(self.obtainAction)
def about(self):
infoString = QCoreApplication.translate('Elevation', "QGIS Elevation Plugin 0.4.0<br />This plugin allows to mark point elevations in Google Maps.<br />Copyright (c) 2010, 2013 Steffen Macke<br /><a href=\"http://polylinie.de/elevation\">polylinie.de/elevation</a><br/>You have to accept the<br/><a href=\"http://code.google.com/intl/en/apis/maps/terms.html\">Google Maps APIs Terms of Service</a>\n")
QMessageBox.information(self.iface.mainWindow(), "About Elevation", infoString)
# Obtain elevation
def obtain(self):
chk = self.check_settings()
if len(chk) :
QMessageBox.information(self.iface.mainWindow(), QCoreApplication.translate('Elevation', "Elevation plugin error"), chk)
return
sb = self.iface.mainWindow().statusBar()
sb.showMessage(QCoreApplication.translate('Elevation', "Click on the map to obtain the elevation"))
ct = ClickTool(self.iface, self.obtain_action);
self.iface.mapCanvas().setMapTool(ct)
def get_elevation(self,point):
epsg4326 = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
self.reprojectgeographic = QgsCoordinateTransform(self.iface.mapCanvas().mapRenderer().destinationCrs(), epsg4326)
pt = self.reprojectgeographic.transform(point)
conn = httplib.HTTPConnection("maps.googleapis.com")
QgsMessageLog.instance().logMessage( "http://maps.googleapis.com/maps/api/elevation/json?locations=" + str(pt[1])+","+str(pt[0])+"&sensor=false", "Elevation")
conn.request("GET", "/maps/api/elevation/json?locations=" + str(pt[1])+","+str(pt[0])+"&sensor=false")
response = conn.getresponse()
jsonresult = response.read()
try:
results = json.loads(jsonresult).get('results')
if 0 < len(results):
return int(round(results[0].get('elevation')))
else:
QMessageBox.warning(self.iface.mainWindow(), 'Elevation', 'HTTP GET Request failed.', QMessageBox.Ok, QMessageBox.Ok)
except ValueError, e:
QMessageBox.warning(self.iface.mainWindow(), 'Elevation', 'JSON decode failed: '+str(jsonresult), QMessageBox.Ok, QMessageBox.Ok)
def obtain_action(self, point) :
epsg4326 = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
self.reprojectgeographic = QgsCoordinateTransform(self.iface.mapCanvas().mapRenderer().destinationCrs(), epsg4326)
pt = self.reprojectgeographic.transform(point)
elevation = self.get_elevation(point)
if elevation == None:
QMessageBox.warning(self.iface.mainWindow(), 'Elevation', 'Failed to get elevation.', QMessageBox.Ok, QMessageBox.Ok)
else:
# save point
self.save_point(point, elevation)
#find marker
marker = 'http://bit.ly/aUwrKs'
for x in range(0, 1000):
if numericmarkers.has_key(elevation+x) :
marker = numericmarkers.get(elevation+x)
break
if numericmarkers.has_key(elevation-x):
marker = numericmarkers.get(elevation-x)
break
# create map
image = tempfile.mkstemp(suffix='png')
os.close(image[0])
urllib.urlretrieve('http://maps.google.com/maps/api/staticmap?size=640x480&maptype=terrain\&markers=icon:'+marker+'|'+str(pt[1])+','+str(pt[0])+'&mobile=true&sensor=false', image[1])
QgsMessageLog.instance().logMessage('http://maps.google.com/maps/api/staticmap?size=640x4802&maptype=terrain\&markers=icon:'+marker+'|'+str(pt[1])+','+str(pt[0])+'&mobile=true&sensor=false')
dlg = ImageDialog()
dlg.image.setPixmap(QPixmap(image[1]))
dlg.show()
dlg.exec_()
if os.path.exists(image[1]):
os.unlink(image[1])
# save point to file, point is in project's crs
def save_point(self, point, elevation):
# create and add the point layer if not exists or not set
if not QgsMapLayerRegistry.instance().mapLayer(self.layerid) :
# create layer with same CRS as project
self.layer = QgsVectorLayer("Point?crs=epsg:4326", "Elevation Plugin Results", "memory")
self.provider = self.layer.dataProvider()
# add fields
self.provider.addAttributes( [QgsField("elevation", QVariant.Double)] )
self.layer.updateFields()
# Labels on
label = self.layer.label()
label.setLabelField(QgsLabel.Text, 0)
self.layer.enableLabels(True)
# add layer if not already
QgsMapLayerRegistry.instance().addMapLayer(self.layer)
# store layer id
self.layerid = QgsMapLayerRegistry.instance().mapLayers().keys()[-1]
# add a feature
fet = QgsFeature()
fet.initAttributes(1)
fet.setGeometry(QgsGeometry.fromPoint(self.reprojectgeographic.transform(point)))
fet.setAttribute(0, elevation)
self.provider.addFeatures( [ fet ] )
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
self.layer.updateExtents()
self.canvas.refresh()
# check project settings before obtaining elevations
# return an error string
def check_settings (self) :
p = QgsProject.instance()
error = ''
proj4string = self.iface.mapCanvas().mapRenderer().destinationCrs().toProj4()
return error
if __name__ == "__main__":
pass