2017-01-20 39 views
-1

我爲不同的用戶創建了一個python插件,它具有每個人需要在QGIS中使用和使用的圖層。如何在QGIS的Python代碼中獲得谷歌底圖?

我沒有問題使用下面的代碼添加從利用Geoserver柵格:

 wmsTicketDSM= QgsRasterLayer('contextualWMSLegend=0&crs=EPSG:27700&dpiMode=7&featureCount=10&format=image/png&layers=Ticket_DSM&password=XXXXXXXXXXXX&styles=&url=http://63.12.21.231:8080/geoserver/exchange_maps/wms&username=admin', 'Ticket_DSM', 'wms') 
     if not wmsTicketDSM.isValid(): 
      print "Layer wmsTicketDSM failed to load!" 
     else: 
      print "Raster Layer wmsTicketDSM loaded!" 
     QgsMapLayerRegistry.instance().addMapLayer(wmsTicketDSM,False) 

現在我需要添加底圖。如何在python上添加Google衛星地圖?

回答

1

谷歌地圖可以用作Qgis中的底圖,將它們加載爲TMS(Tiled Map Service)。來自QGIS 2.18對TMS的本地支持已通過Lutra添加到Qgis。

import requests 
service_url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}" 
service_uri = "type=xyz&zmin=0&zmax=21&url="+requests.utils.quote(service_url) 
tms_layer = core.QgsRasterLayer(service_uri, "Google Hybrid", "wms") 

其中lyrs=y費的混合式地圖,lyrs=s SAT的地圖和lyrs=m的路線圖。

注意URI的URL參數必須%的代碼編碼

QGIS通過Minoru Akagi暴露所需的方法與以往的版本,可以支持利用的TileLayerPlugin

plugin = qgis.utils.plugins.get("TileLayerPlugin") 
if plugin: 
    from TileLayerPlugin.tiles import BoundingBox, TileLayerDefinition 
    bbox = None # BoundingBox(-180, -85.05, 180, 85.05) 
    layerdef = TileLayerDefinition(u"title", 
           u"attribution", 
           "http://example.com/xyz/{z}/{x}/{y}.png", 
           zmin=1, 
           zmax=18, 
           bbox=bbox) 
    plugin.addTileLayer(layerdef) 
else: 
    from PyQt4.QtGui import QMessageBox 
    QMessageBox.warning(None, 
         u"TileLayerPlugin not installed", 
         u"Please install it and try again.") 

對於谷歌底圖替換適當的「歸因」行和url行與以下內容:

"https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}" 
+0

我試着直接在qgis和g上使用行ot下面的消息「服務的訪問受到TOS的限制。請按照服務條款「??? – Luffydude

+0

TOS =服務條款 –

+0

是的我知道,但有沒有辦法自動跳過信息? – Luffydude