[Qgis-developer] GoogleLayers
Ivan Mincik
ivan.mincik at gista.sk
Tue May 25 16:15:22 EDT 2010
> Under Linux (ubuntu) things seems to work better, but if I save a
> project that includes one or more tiles, then when I try to reopen the
> project qgis crashes with the following message
>
> -----------
> Error:
> GDAL Error 6: No translation an empty SRS to PROJ.4 format is known.
> Segmentation fault
> -----------
I have found the reason of this problem. Would You please try to
replace original 'GoogleLayers.py' file with file attached in this
mail and test on Ubuntu?
Ivan
-------------- next part --------------
"""
/***************************************************************************
GoogleLayers
A QGIS plugin
Add Google Layers into QGIS
-------------------
begin : 2010-02-04
copyright : (C) 2010 by yao
email : yao at chu.url.com.tw
changes by : Ivan Mincik, ivan.mincik at gista.sk
***************************************************************************/
/***************************************************************************
* *
* 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 __future__ import with_statement
import sys
import os
import urllib
import random
from datetime import datetime
from itertools import *
from math import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import resources
from GoogleLayersDialog import GoogleLayersDialog
currentPath = os.path.dirname( __file__ )
count=0
maptype=""
t = datetime.now()
session_id = t.strftime("%d%m%Y_%H%M%S%s")
def writeWorldFile(outputFile,xResolution,yResolution,angleDeg,rotatedUlXcoord,rotatedUlYcoord):
angleRad = -radians(angleDeg)
line1 = xResolution * cos(angleRad)
line2 = -xResolution * sin(angleRad)
line3 = -yResolution * sin(angleRad)
line4 = -yResolution * cos(angleRad)
line5 = rotatedUlXcoord
line6 = rotatedUlYcoord
try:
newWorldfFile = open(outputFile,"w")
except:
print "couldn't open file"
fileText = str(line1) + '\n' + str(line2) + '\n' + str(line3)+ '\n' + str(line4) + '\n' \
+ str(line5) + '\n' + str(line6) + '\n'
newWorldfFile.write(fileText)
newWorldfFile.close()
def Google2WGS84(lon,lat):
f=QgsCoordinateReferenceSystem()
f.createFromProj4("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 \
+k=1.0 +units=m +nadgrids=@null +wktext +no_defs")
t=QgsCoordinateReferenceSystem()
t.createFromEpsg(4326)
transformer = QgsCoordinateTransform(f,t)
Qpoint = transformer.transform(lon,lat)
return Qpoint
def readConfig():
global maptype
cf = open(os.path.join(currentPath, "GoogleLayers.ini"),"r")
maptype=cf.readline().strip()
cf.close()
def writeConfig(MapType):
cf = open(os.path.join(currentPath, "GoogleLayers.ini"),"w")
cf.writelines(MapType)
cf.close()
class GoogleLayers:
def __init__(self, iface):
self.iface = iface
def initGui(self):
self.action = QAction(QIcon(":/plugins/GoogleLayers/icon.png"), "Google Layers", self.iface.mainWindow())
self.actionSettings = QAction(QIcon(":/plugins/GoogleLayers/setting.png"), "Settings", self.iface.mainWindow())
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
QObject.connect( self.actionSettings, SIGNAL( "activated()" ), self.config )
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu("&Google Layers", self.action)
self.iface.addPluginToMenu("&Google Layers", self.actionSettings )
# read config
self.config = QSettings('ItOpen', 'GoogleLayers');
def unload(self):
self.iface.removePluginMenu("&Google Layer", self.action)
self.iface.removePluginMenu("&Google Layer", self.actionSettings )
self.iface.removeToolBarIcon(self.action)
def config(self):
dlg = GoogleLayersDialog()
dlg.show()
result = dlg.exec_()
if result == 1:
if dlg.ui.radioRoadMap.isChecked():
MapType='roadmap'
if dlg.ui.radioMobileMap.isChecked():
MapType='mobile'
if dlg.ui.radioTerrainMap.isChecked():
MapType='terrain'
if dlg.ui.radioSatellite.isChecked():
MapType='satellite'
if dlg.ui.radioHybrid.isChecked():
MapType='hybrid'
writeConfig(MapType)
def run(self):
readConfig()
global maptype
if maptype=="":
maptype="roadmap"
mapCanvas = self.iface.mapCanvas()
width=640
height=640
lat1=mapCanvas.extent().yMinimum()
lon1=mapCanvas.extent().xMinimum()
lat2=mapCanvas.extent().yMaximum()
lon2=mapCanvas.extent().xMaximum()
clat=(lat1+lat2)/2
clon=(lon1+lon2)/2
cwgs1 = Google2WGS84(lon1, lat1)
cwgs2 = Google2WGS84(lon2, lat2)
bbox=[cwgs1.x(), cwgs1.y(), cwgs2.x(), cwgs2.y()]
lon1, lat1, lon2, lat2 = imap(radians, bbox)
x1, y1 = lon1 / 2.0 / pi + 0.5, log((1.0 + sin(lat1)) / (1.0 - sin(lat1))) / 4.0 / pi + 0.5
x2, y2 = lon2 / 2.0 / pi + 0.5, log((1.0 + sin(lat2)) / (1.0 - sin(lat2))) / 4.0 / pi + 0.5
z = min(max(int(ceil(log(min(width / ((x2 - x1) * 256), height / ((y2 - y1) * 256)), 2))), 0), 19)
if z>=0 and z<=19:
initialResolution = 2 * pi * 6378137 / 256
widthOffset = width * initialResolution / pow(2, z)
heightOffset = height * initialResolution / pow(2, z)
cx=clon
cy=clat
W = cx - widthOffset/2
E = cx + widthOffset/2
S = cy - heightOffset/2
N = cy + heightOffset/2
XPixelWidth=(E-W)/width
YPixelWidth=(N-S)/height
cwgs = Google2WGS84(clon, clat)
# load image from Google
url = "http://maps.google.com/staticmap?center=%s,%s&zoom=%s&size=%sx%s&sensor=false&format=png&maptype=%s" % (str(cwgs.y()), \
str(cwgs.x()), str(z), str(width), str(height), maptype)
google_image = urllib.urlopen(url).read()
# write image file + world file to disk
global count
global session_id
count=count+1
fn = '%s_z%s_%s_%s' % (maptype, str(z), str(count), session_id)
writeWorldFile(os.path.join(currentPath, "temp", fn + ".pngw"),XPixelWidth,YPixelWidth,0,W,N)
rasterfile_path = (os.path.join(currentPath, "temp", fn + ".png"))
file = open(rasterfile_path, "wb")
file.write(google_image)
file.close()
# load image file to QGIS
glayer = self.iface.addRasterLayer(rasterfile_path, "Google Map(z:" + str(z) + ") " + str(count))
srs = QgsCoordinateReferenceSystem()
google_proj = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 \
+units=m +nadgrids=@null +no_defs"
srs.createFromProj4(google_proj)
glayer.setCrs(srs)
self.iface.zoomToActiveLayer()
else:
re = QMessageBox.question(None, "Warning", "No Google Layer in this scale.\n Zoom Level:" + str(z), QMessageBox.Yes, QMessageBox.No)
More information about the Qgis-developer
mailing list