#--------------------------------------------------------------------- # # refmap - A QGIS plugin to display reference maps via web # services. # # Copyright (C) 2009 Aaron Racicot, Z-Pulley Inc. # # EMAIL: aaronr (at) z-pulley.com # WEB : www.z-pulley.com # www.reprojected.com # #--------------------------------------------------------------------- # # licensed under the terms of GNU GPL 2 # # 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. # # This program 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 this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # #--------------------------------------------------------------------- from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgis.gui import * import resources from refmap_ui import Ui_RefMapWindow from providers.config_ui import Ui_RefMapConfigWindow import os import tempfile import math class PythonJS(QObject): def __init__(self,parent): QObject.__init__(self,parent.iface.mainWindow()) self.setObjectName("python") self.parent = parent print "PythonJS init" @pyqtSignature("QString") def alert(self, msg): print "alert pressed" # QMessageBox.information(None, "This is Python", msg) @pyqtSignature("QString") def coords(self, msg): #print "coords updated " + str(msg) self.parent.refmap_gui.mapCoords.setText(msg) @pyqtSignature("double,double") def center(self, lat,lon): # print "lat " + str(lat) + " lon " + str(lon) # Shift the extent rectangle... currentExtent = self.parent.canvas.extent() currentCenter = currentExtent.center() newCenter = QgsPoint(lon,lat) dx = math.fabs(newCenter.x() - currentCenter.x()) dy = math.fabs(newCenter.y() - currentCenter.y()) if (newCenter.x() > currentCenter.x()): currentExtent.setXMinimum( currentExtent.xMinimum() + dx ) currentExtent.setXMaximum( currentExtent.xMaximum() + dx ) else: currentExtent.setXMinimum( currentExtent.xMinimum() - dx ) currentExtent.setXMaximum( currentExtent.xMaximum() - dx ) if (newCenter.y() > currentCenter.y()): currentExtent.setYMaximum( currentExtent.yMaximum() + dy ) currentExtent.setYMinimum( currentExtent.yMinimum() + dy ) else: currentExtent.setYMaximum( currentExtent.yMaximum() - dy ) currentExtent.setYMinimum( currentExtent.yMinimum() - dy ) #self.parent.disconnect(SIGNAL("extentsChanged()")) self.parent.disconnect(self.parent.canvas, SIGNAL("extentsChanged()"), self.parent.coordChange) self.parent.canvas.setExtent(currentExtent) self.parent.canvas.refresh() self.parent.connect(self.parent.canvas, SIGNAL("extentsChanged()"), self.parent.coordChange) class ReferenceMapWindow(QDialog, Ui_RefMapWindow): def __init__(self, iface, fl, plugin): QDialog.__init__(self, iface.mainWindow(), fl) self.setupUi(self) self.iface = iface self.plugin = plugin class ReferenceMapConfigWindow(QDialog, Ui_RefMapConfigWindow): def __init__(self, iface, fl, plugin): QDialog.__init__(self, iface.mainWindow(), fl) self.setupUi(self) self.iface = iface self.plugin = plugin class RefMapPlugin(QObject): def __init__(self, iface): QObject.__init__(self) # Save a reference to the QGIS iface self.iface = iface self.canvas = iface.mapCanvas() self.refmap_gui = None self.googleURL = "http://dev.z-pulley.com/refmap/gmaps/index.html" self.openLayersURL = "http://dev.z-pulley.com/refmap/ol/index.html" self.msVirtualEarthURL = "" def initGui(self): self.helpaction = QAction(QIcon(":/help.png"), "About", self.iface.mainWindow()) self.helpaction.setWhatsThis("RefMap Help") QObject.connect(self.helpaction, SIGNAL("activated()"), self.helprun) self.refmapaction = QAction(QIcon(":/refmapicon.png"), "RefMap", self.iface.mainWindow()) self.refmapaction.setWhatsThis("RefMap Plugin") QObject.connect(self.refmapaction, SIGNAL("activated()"), self.refmaprun) # Add to the main toolbar self.iface.addPluginToMenu("Reference Map", self.helpaction) self.iface.addPluginToMenu("Reference Map", self.refmapaction) self.iface.addToolBarIcon(self.refmapaction) pluginMenu = self.iface.pluginMenu() for action in pluginMenu.actions(): if action.text() == QString("Reference Map"): # update the icon action.setIcon(QIcon(":/refmapicon.png")) # Set up the GUIS needed flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint self.refmap_gui = ReferenceMapWindow(self.iface,flags,self) flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint self.refmapconfig_gui = ReferenceMapConfigWindow(self.iface,flags,self) self.connect(self.refmapconfig_gui.btnCancel, SIGNAL("clicked()"), self.refmapconfig_gui.hide) self.connect(self.refmapconfig_gui.btnOK, SIGNAL("clicked()"), self.refmapconfig_gui.hide) self.connect(self.refmap_gui.btnClose, SIGNAL("clicked()"), self.refmap_gui.hide) self.connect(self.refmap_gui.btnRecenter, SIGNAL("clicked()"), self.recenter) self.connect(self.refmap_gui.btnConfig, SIGNAL("clicked()"), self.config) self.connect(self.refmap_gui.mapSource, SIGNAL("currentIndexChanged(int)"), self.mapSourceChange) def testSplit(self): mainWindow = self.iface.mainWindow() mapCanvas = self.iface.mapCanvas() mapCanvasSize = mapCanvas.size() newWidget = QWidget(mainWindow) newWidget.resize(mapCanvasSize) newWidget.setMinimumSize(mapCanvasSize) #newWidget.setSizePolicy(QSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)) newLayout = QHBoxLayout(newWidget) newCanvas = QgsMapCanvas(mainWindow) newCanvas.setCanvasColor(QColor(255,255,255)) newCanvas.enableAntiAliasing(True) newCanvas.useImageToRender(False) newCanvas.show() newCanvas.resize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) #newCanvas.setSizePolicy(QSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)) #newCanvas.setMinimumSize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) mapCanvas.resize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) #mapCanvas.setSizePolicy(QSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)) oldMapCanvasMinSize = mapCanvas.minimumSize() #mapCanvas.setMinimumSize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) newLayout.addWidget(mapCanvas) newLayout.addWidget(newCanvas) mainWindow.setCentralWidget(newWidget) #newCanvas.setMinimumSize(oldMapCanvasMinSize) #mapCanvas.setMinimumSize(oldMapCanvasMinSize) def config(self): self.refmapconfig_gui.show() def mapSourceChange(self,index): mapSourceSelection = self.refmap_gui.mapSource.itemText(index) if mapSourceSelection == QString("Google"): # Switch to google self.refmap_gui.webView.setUrl(QUrl(self.googleURL)) elif mapSourceSelection == QString("OpenLayers"): # Switch to OL self.refmap_gui.webView.setUrl(QUrl(self.openLayersURL)) def recenter(self,lat=47.65,lon=-122.29): mapSourceSelection = self.refmap_gui.mapSource.itemText(self.refmap_gui.mapSource.currentIndex()) if mapSourceSelection == QString("Google"): # Switch to google self.refmap_gui.webView.page().mainFrame().evaluateJavaScript("map.setCenter(new GLatLng(%f,%f));"%(lat,lon)) elif mapSourceSelection == QString("OpenLayers"): # Switch to OL self.refmap_gui.webView.page().mainFrame().evaluateJavaScript("map.setCenter(new OpenLayers.LonLat(%f,%f).transform(new OpenLayers.Projection(\"EPSG:4326\"),new OpenLayers.Projection(\"EPSG:900913\")),map.getZoom());"%(lon,lat)) # Next need to recenter... def webkitCleared(self): print "Webkit cleared" self.refmap_gui.webView.page().mainFrame().addToJavaScriptWindowObject("python", PythonJS(self)) def coordChange(self): #print "coordChange called on pan" #QObject.connect(self.iface.mapCanvas(), # SIGNAL("extentsChanged()"), # self.coordChange) self.recenter(self.iface.mapCanvas().extent().center().y(), self.iface.mapCanvas().extent().center().x()) def createGoogleMapJS(self,frame): scriptString = """ dv = document.createElement('div'); // create dynamically div tag dv.setAttribute('id',\"map2d\"); //give id to it dv.className=\"mapStyle\"; // set the style classname //set the inner styling of the div tag dv.style.display=\"block\"; dv.style.position=\"absolute\"; dv.style.left=0; dv.style.top=0; dv.style.width=\"100%\"; dv.style.height=\"100%\"; document.body.appendChild(dv); var map = null; if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById(\"map2d\")); map.addMapType(G_PHYSICAL_MAP); map.setMapType(G_PHYSICAL_MAP); map.addControl(new GSmallMapControl()); map.addControl(new GMapTypeControl()); GEvent.addListener(map, \"move\", function() { var center = map.getCenter(); python.coords(center.toString()); }); GEvent.addListener(map, \"moveend\", function() { var center = map.getCenter(); python.center(center.lat(),center.lng()); }); map.setCenter(new GLatLng(47.65,-122.29),3); map.checkResize(); } """ #print scriptString frame.evaluateJavaScript(scriptString) def createOpenLayersMapJS(self,frame): scriptString = """ dv = document.createElement('div'); // create dynamically div tag dv.setAttribute('id',\"map2d\"); //give id to it dv.setAttribute('name',\"map2d\"); //give name to it dv.className=\"mapStyle\"; // set the style classname //set the inner styling of the div tag dv.style.display=\"block\"; dv.style.position=\"absolute\"; dv.style.left=0; dv.style.top=0; dv.style.width=\"100%\"; dv.style.height=\"100%\"; document.body.appendChild(dv); var map_options = { 'units' : \"m\", 'maxResolution' : 156543.0339, 'numZoomLevels' : 22, 'projection' : new OpenLayers.Projection(\"EPSG:900913\"), //'displayProjection' : new OpenLayers.Projection(\"EPSG:4326\"), 'maxExtent' : new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508), 'eventListeners' : { \"mousemove\": moveEvent } } map = new OpenLayers.Map('map2d', map_options); function moveEvent(event) { var lonlat = map.getLonLatFromViewPortPx(event.xy).clone(); lonlat.transform(new OpenLayers.Projection(\"EPSG:900913\"),new OpenLayers.Projection(\"EPSG:4326\")); python.coords(lonlat.lat + \",\" + lonlat.lon); } function moveEndEvent2(event) { var lonlat = map.getCenter().clone(); lonlat.transform(new OpenLayers.Projection(\"EPSG:900913\"),new OpenLayers.Projection(\"EPSG:4326\")); //alert(\"HI\" + lonlat.lat + \" \" + lonlat.lon); python.center(lonlat.lat,lonlat.lon); } //map = new OpenLayers.Map(\"map2d\", { // eventListeners: { // \"mousemove\": moveEvent // } // }); map.events.register(\"moveend\",null,moveEndEvent2); var ol_wms = new OpenLayers.Layer.WMS( \"OpenLayers WMS\", \"http://labs.metacarta.com/wms/vmap0\", {layers: \"basic\"} ); //map.addLayers([ol_wms]); map.layers.base = new OpenLayers.Layer.OSM.Mapnik(\"OpenStreetMap (Mapnik)\"); map.addLayer(map.layers.base); map.addControl(new OpenLayers.Control.LayerSwitcher()); //map.zoomToMaxExtent(); //map.zoomToExtent(new OpenLayers.Bounds(-13644621.04,6028561.02, -13598758.82,6057607.09)); map.setCenter(new OpenLayers.LonLat(-122.29, 47.65).transform(new OpenLayers.Projection(\"EPSG:4326\"),new OpenLayers.Projection(\"EPSG:900913\")),3); map.updateSize(); """ # print scriptString frame.evaluateJavaScript(scriptString) def loadFinished(self): print "loadFinished" mapSourceSelection = self.refmap_gui.mapSource.itemText(self.refmap_gui.mapSource.currentIndex()) if mapSourceSelection == QString("Google"): # Switch to google self.createGoogleMapJS(self.refmap_gui.webView.page().mainFrame()) elif mapSourceSelection == QString("OpenLayers"): # Switch to OL self.createOpenLayersMapJS(self.refmap_gui.webView.page().mainFrame()) self.connect(self.iface.mapCanvas(), SIGNAL("extentsChanged()"), self.coordChange) def refmaprun(self): self.refmap_gui.show() # Connect to the load finished... QObject.connect(self.refmap_gui.webView, SIGNAL("loadFinished(bool)"), self.loadFinished) mapSourceSelection = self.refmap_gui.mapSource.itemText(self.refmap_gui.mapSource.currentIndex()) if mapSourceSelection == QString("Google"): # Switch to google self.refmap_gui.webView.setUrl(QUrl(self.googleURL)) elif mapSourceSelection == QString("OpenLayers"): # Switch to OL self.refmap_gui.webView.setUrl(QUrl(self.openLayersURL)) QObject.connect(self.refmap_gui.webView.page().mainFrame(), SIGNAL("javaScriptWindowObjectCleared ()"), self.webkitCleared) print "refmaprun pressed" def unload(self): # Remove the plugin self.iface.removePluginMenu("Reference Map",self.refmapaction) self.iface.removePluginMenu("Reference Map",self.helpaction) self.iface.removeToolBarIcon(self.refmapaction) def helprun(self): # print "Help pressed..." infoString = QString("Written by Aaron Racicot\naaronr@z-pulley.com\n") infoString = infoString.append("Company - http://www.z-pulley.com\n\n") infoString = infoString.append("Blog - http://www.reprojected.com\n\n") infoString = infoString.append("Source: http://svn.reprojected.com/") infoString = infoString.append("qgisplugins/trunk/refmap\n") infoString = infoString.append("TRAC: http://trac.reprojected.com/") infoString = infoString.append("qgisplugins\n") QMessageBox.information(self.iface.mainWindow(), "RefMap",infoString)