Skip to content

Commit

Permalink
Elevation.get_elevation added
Browse files Browse the repository at this point in the history
Added Elevation.get_elevation so other scripts can get the elevation for any point through code.
  • Loading branch information
dolfandringa committed Apr 28, 2015
1 parent 92a6578 commit 3beae2d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
64 changes: 35 additions & 29 deletions Elevation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def __init__(self, iface):
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.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)
Expand All @@ -70,13 +70,13 @@ def obtain(self):
chk = self.check_settings()
if len(chk) :
QMessageBox.information(self.iface.mainWindow(), QCoreApplication.translate('Elevation', "Elevation plugin error"), chk)
return
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 obtain_action(self, point) :
def get_elevation(self,point):
epsg4326 = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
self.reprojectgeographic = QgsCoordinateTransform(self.iface.mapCanvas().mapRenderer().destinationCrs(), epsg4326)
pt = self.reprojectgeographic.transform(point)
Expand All @@ -88,34 +88,40 @@ def obtain_action(self, point) :
try:
results = json.loads(jsonresult).get('results')
if 0 < len(results):
elevation = int(round(results[0].get('elevation')))
# 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])
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) :
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
Expand Down Expand Up @@ -162,4 +168,4 @@ def check_settings (self) :

if __name__ == "__main__":
pass
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ The installation can be installed by selecting Plugins->Manage and Install Plugi
followed by a click on "Get more".

Installation from the source tarball is also possible.

Usage in python scripts
=======================

You can also get the elevation from python scripts.

```
from elevation.Elevation import Elevation #import the plugin
elev = Elevation(iface) #initialise the plugin
ext = iface.mapCanvas().extent() #get the current map canvas extent
point = QgsPoint(ext.xMinimum(),ext.yMinimum()) #make a point for the bottom left corner
elev.get_elevation(point) #get the elevation for the point
```
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def name():
def description():
return "Obtain and display point elevation data using Google Maps"
def version():
return "Version 0.4.0"
return "Version 0.5.0"
def qgisMinimumVersion():
return "2.0"
def classFactory(iface):
Expand Down

0 comments on commit 3beae2d

Please sign in to comment.