Skip to content

Commit

Permalink
Version 0.1.0 of the QGIS Elevation Plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Macke committed Aug 20, 2013
1 parent c08f09b commit c2dbe25
Show file tree
Hide file tree
Showing 11 changed files with 2,044 additions and 4 deletions.
3 changes: 3 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Steffen Macke <[email protected]>
Dr. Alessandro Pasotti <[email protected]> - parts taken from GeoCoding plugin

166 changes: 166 additions & 0 deletions Elevation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#! /usr/bin/env python
#
# This file is part of the QGIS Elevation Plugin
#
# Elevation.py - load Elevation class from file Elevation.py
#
# Copyright 2010 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 *

# Standard imports. simplejson is imported later
import sys,os,httplib, webbrowser, urllib
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))

# GeoCoding Utils
from Utils import *
# Elevation imports
import resources, numericmarkers

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 = QString(QCoreApplication.translate('Elevation', "QGIS Elevation Plugin<br />This plugin allows to mark point elevations in Google Maps.<br />Copyright (c) 2010 Steffen Macke<br /><a href=\"http://polylinie.de/elevation\">polylinie.de/elevation</a><br/>In order to use, you have to accept the <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 obtain_action(self, point) :
try:
import simplejson
# reverse lat/lon
pt = pointToWGS84(point)
conn = httplib.HTTPConnection("maps.google.com")
conn.request("GET", "/maps/api/elevation/json?locations=" + str(pt[1])+","+str(pt[0])+"&sensor=false")
response = conn.getresponse()

jsonresult = response.read()
elevation = int(round(simplejson.loads(jsonresult).get('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.numericmarkers.has_key(elevation+x) :
marker = numericmarkers.numericmarkers.get(elevation+x)
break
if numericmarkers.numericmarkers.has_key(elevation-x):
marker = numericmarkers.numericmarkers.get(elevation-x)
break
# create map
webbrowser.open('http://maps.google.com/maps/api/staticmap?size=512x512&maptype=terrain\&markers=icon:'+marker+'|'+str(pt[1])+','+str(pt[0])+'&mobile=true&sensor=false')
except ImportError, e:
QCoreApplication.translate('Elevation', "'simplejson' module is required. Please install simplejson with 'easy_install simplejson'")
return

# save point to file, point is in project's crs
def save_point(self, point, elevation):
qDebug('Saving point ' + str(point[1]) + ' ' + str(point[0]))
# 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", "Elevation Plugin Results", "memory")
self.provider = self.layer.dataProvider()
p = QgsProject.instance()
(proj4string,ok) = p.readEntry("SpatialRefSys","ProjectCRSProj4String")
crs = QgsCoordinateReferenceSystem()
crs.createFromProj4(proj4string)
self.layer.setCrs(crs)

# add fields
self.provider.addAttributes( { "elevation" : "int" } )

# 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.setGeometry(QgsGeometry.fromPoint(point))
fet.setAttributeMap( { 0 : QVariant(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,ok) = p.readEntry("SpatialRefSys", "ProjectCRSProj4String")
if not ok :
error += unicode(QCoreApplication.translate('Elevation', "Project projection is not set: Elevation needs explicit projection set. Please set project CRS."))

try:
import simplejson
except ImportError, e:
error += QCoreApplication.translate('Elevation', "'simplejson' module is required. Please install simplejson with 'easy_install simplejson'")

return error

if __name__ == "__main__":
pass

22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
qgis-elevation
==============

Quantum GIS Elevation Plugin
Quantum GIS Elevation plugin.

The QGIS Elevation Plugin is available from http://polylinie.de/elevation

Quantum GIS is available from http://www.qgis.org

Functionality: The plugin will obtain and display point elevation data using
the Google Maps API.

Use of the extension requires acceptance of the Google Maps Terms of Service:
http://code.google.com/intl/en/apis/maps/terms.html

Installation

The installation can be installed using the Quantum GIS PluginInstaller from
the "QGIS Contributed Repository".

Installation from the source tarball is also possible.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Work with all selected records of the active layer.
65 changes: 65 additions & 0 deletions Utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
***************************************************************************
Name : Geocoding
Description : Geocoding and reverse Geocoding using Google
Date : 28/May/09
copyright : (C) 2009 by ItOpen
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""

from qgis.core import *
from qgis.gui import *

class ClickTool(QgsMapTool):
def __init__(self,iface, callback):
QgsMapTool.__init__(self,iface.mapCanvas())
self.iface = iface
self.callback = callback
self.canvas = iface.mapCanvas()
return None


def canvasReleaseEvent(self,e):
point = self.canvas.getCoordinateTransform().toMapPoint(e.pos().x(),e.pos().y())
self.callback(point)
return None


def pointToWGS84(point):
p = QgsProject.instance()
(proj4string,ok) = p.readEntry("SpatialRefSys","ProjectCRSProj4String")
if not ok:
return point
t=QgsCoordinateReferenceSystem()
t.createFromEpsg(4326)
f=QgsCoordinateReferenceSystem()
f.createFromProj4(proj4string)
transformer = QgsCoordinateTransform(f,t)
pt = transformer.transform(point)
return pt

def pointFromWGS84(point):
p = QgsProject.instance()
(proj4string,ok) = p.readEntry("SpatialRefSys","ProjectCRSProj4String")
if not ok:
return point
f=QgsCoordinateReferenceSystem()
f.createFromEpsg(4326)
t=QgsCoordinateReferenceSystem()
t.createFromProj4(proj4string)
transformer = QgsCoordinateTransform(f,t)
pt = transformer.transform(point)
return pt



37 changes: 37 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#! /usr/bin/env python
#
# This file is part of the QGIS Elevation Plugin
#
# __init__.py - load Elevation class from file Elevation.py
#
# Copyright 2010 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
#
def name():
return "Elevation"
def description():
return "Obtain and display point elevation data using Google Maps"
def version():
return "Version 0.1.0"
def qgisMinimumVersion():
return "1.0"
def classFactory(iface):
# load Elevation class from file Elevation
from Elevation import Elevation
return Elevation(iface)
Binary file added about_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added elevation_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c2dbe25

Please sign in to comment.