def __init__(self, plugin, layerDef, creditVisibility=1): QgsPluginLayer.__init__(self, TileLayer.LAYER_TYPE, layerDef.title) self.plugin = plugin self.iface = plugin.iface self.layerDef = layerDef self.creditVisibility = 1 if creditVisibility else 0 self.tiles = None # set attribution property self.setAttribution(layerDef.attribution) # set custom properties self.setCustomProperty("title", layerDef.title) self.setCustomProperty("credit", layerDef.attribution) self.setCustomProperty("serviceUrl", layerDef.serviceUrl) self.setCustomProperty("yOriginTop", layerDef.yOriginTop) self.setCustomProperty("zmin", layerDef.zmin) self.setCustomProperty("zmax", layerDef.zmax) if layerDef.bbox: self.setCustomProperty("bbox", layerDef.bbox.toString()) self.setCustomProperty("creditVisibility", self.creditVisibility) # set crs if plugin.crs3857 is None: # create a QgsCoordinateReferenceSystem instance if plugin has no instance yet plugin.crs3857 = QgsCoordinateReferenceSystem(3857) self.setCrs(plugin.crs3857) # set extent if layerDef.bbox: self.setExtent(BoundingBox.degreesToMercatorMeters(layerDef.bbox).toQgsRectangle()) else: self.setExtent(QgsRectangle(-layerDef.TSIZE1, -layerDef.TSIZE1, layerDef.TSIZE1, layerDef.TSIZE1)) # set styles self.setTransparency(0) self.setBlendModeByName(self.DEFAULT_BLEND_MODE) self.setSmoothRender(self.DEFAULT_SMOOTH_RENDER) # downloader self.maxConnections = HonestAccess.maxConnections(layerDef.serviceUrl) self.cacheExpiry = QSettings().value("/qgis/defaultTileExpiry", 24, type=int) self.userAgent = "QGIS/{0} TileLayerPlugin/{1}".format(QGis.QGIS_VERSION, self.plugin.VERSION) # will be overwritten in QgsNetworkAccessManager::createRequest() since 2.2 self.downloader = Downloader(self, self.maxConnections, self.cacheExpiry, self.userAgent) # TOS violation warning if HonestAccess.restrictedByTOS(layerDef.serviceUrl): QMessageBox.warning(None, u"{0} - {1}".format(self.tr("TileLayerPlugin"), layerDef.title), self.tr("Access to the service is restricted by the TOS. Please follow the TOS.")) # multi-thread rendering if self.iface: self.statusSignal.connect(self.showStatusMessageSlot) self.messageBarSignal.connect(self.showMessageBarSlot) self.setValid(True)
def importFromTsv(self, filename): # append file item rootItem = self.model.invisibleRootItem() basename = os.path.basename(filename) parent = QStandardItem(os.path.splitext(basename)[0]) rootItem.appendRow([parent]) # load service info from tsv file try: with codecs.open(filename, "r", "utf-8") as f: lines = f.readlines() except: QgsMessageLog.logMessage( self.tr("Fail to read: {}").format(basename), self.tr("TileLayerPlugin")) return False for i, line in enumerate(lines): if line.startswith("#"): continue vals = line.split("\t") nvals = len(vals) try: if nvals < 3: raise title, credit, url = vals[0:3] if nvals < 4: serviceInfo = TileServiceInfo(title, credit, url) else: yOriginTop = int(vals[3]) if nvals < 6: serviceInfo = TileServiceInfo(title, credit, url, yOriginTop) else: zmin, zmax = map(int, vals[4:6]) if nvals < 10: serviceInfo = TileServiceInfo( title, credit, url, yOriginTop, zmin, zmax) else: bbox = BoundingBox.fromString(",".join(vals[6:10])) serviceInfo = TileServiceInfo( title, credit, url, yOriginTop, zmin, zmax, bbox) except: QgsMessageLog.logMessage( self.tr("Invalid line format: {} line {}").format( basename, i + 1), self.tr("TileLayerPlugin")) continue # append the service info into the tree vals = serviceInfo.toArrayForTreeView() + [ len(self.serviceInfoList) ] rowItems = map(QStandardItem, map(unicode, vals)) parent.appendRow(rowItems) self.serviceInfoList.append(serviceInfo) return True
def importFromTsv(self, filename): # append file item rootItem = self.model.invisibleRootItem() basename = os.path.basename(filename) parent = QStandardItem(os.path.splitext(basename)[0]) rootItem.appendRow([parent]) # load service info from tsv file try: with codecs.open(filename, "r", "utf-8") as f: lines = f.readlines() except Exception as e: QgsMessageLog.logMessage(self.tr("Fail to read {0}: {1}").format(basename, unicode(e)), self.tr("TileLayerPlugin")) return False for i, line in enumerate(lines): if line.startswith("#"): continue vals = line.rstrip().split("\t") nvals = len(vals) try: if nvals < 3: raise title, attribution, url = vals[0:3] if not url: raise if nvals < 4: serviceInfo = TileLayerDefinition(title, attribution, url) else: yOriginTop = int(vals[3]) if nvals < 6: serviceInfo = TileLayerDefinition(title, attribution, url, yOriginTop) else: zmin, zmax = map(int, vals[4:6]) if nvals < 10: serviceInfo = TileLayerDefinition(title, attribution, url, yOriginTop, zmin, zmax) else: bbox = BoundingBox.fromString(",".join(vals[6:10])) epsg = None try: epsg = int(vals[10]) except Exception as e: i = 0 serviceInfo = TileLayerDefinition(title, attribution, url, yOriginTop, zmin, zmax, bbox, epsg) except: QgsMessageLog.logMessage(self.tr("Invalid line format: {} line {}").format(basename, i + 1), self.tr("TileLayerPlugin")) continue # append the service info into the tree vals = serviceInfo.toArrayForTreeView() + [len(self.serviceInfoList)] rowItems = map(QStandardItem, map(unicode, vals)) parent.appendRow(rowItems) self.serviceInfoList.append(serviceInfo) return True
def readXml(self, node): self.readCustomProperties(node) self.layerDef.title = self.customProperty("title", "") self.layerDef.attribution = self.customProperty("credit", "") if self.layerDef.attribution == "": self.layerDef.attribution = self.customProperty("providerName", "") # for compatibility with 0.11 self.layerDef.serviceUrl = self.customProperty("serviceUrl", "") self.layerDef.yOriginTop = int(self.customProperty("yOriginTop", 1)) self.layerDef.zmin = int(self.customProperty("zmin", TileDefaultSettings.ZMIN)) self.layerDef.zmax = int(self.customProperty("zmax", TileDefaultSettings.ZMAX)) bbox = self.customProperty("bbox", None) if bbox: self.layerDef.bbox = BoundingBox.fromString(bbox) self.setExtent(BoundingBox.degreesToMercatorMeters(self.layerDef.bbox).toQgsRectangle()) # layer style self.setTransparency(int(self.customProperty("transparency", 0))) self.setBlendModeByName(self.customProperty("blendMode", self.DEFAULT_BLEND_MODE)) self.setSmoothRender(int(self.customProperty("smoothRender", self.DEFAULT_SMOOTH_RENDER))) self.creditVisibility = int(self.customProperty("creditVisibility", 1)) # max connections of downloader self.maxConnections = HonestAccess.maxConnections(self.layerDef.serviceUrl) return True