def getBasemaps(basemapList): basemaps = [basemapOL()[item] for _, item in enumerate(basemapList)] if len(basemapList) > 1: baseGroup = "Base maps" else: baseGroup = "" baseLayer = """var baseLayer = new ol.layer.Group({ 'title': '%s', layers: [%s\n] });""" % (baseGroup, ','.join(basemaps)) return baseLayer
def writeLayersAndGroups(layers, groups, visible, folder, popup, settings, json, matchCRS, clustered, iface, restrictToExtent, extent): canvas = iface.mapCanvas() basemapList = settings["Appearance"]["Base layer"] basemaps = [basemapOL()[item] for _, item in enumerate(basemapList)] if len(basemapList) > 1: baseGroup = "Base maps" else: baseGroup = "" baseLayer = """var baseLayer = new ol.layer.Group({ 'title': '%s', layers: [%s\n] });""" % (baseGroup, ','.join(basemaps)) layerVars = "" layer_names_id = {} for count, (layer, encode2json, cluster) in enumerate(zip(layers, json, clustered)): layer_names_id[layer.id()] = str(count) try: if is25d(layer, canvas, restrictToExtent, extent): pass else: layerVars += "\n".join([layerToJavascript(iface, layer, encode2json, matchCRS, cluster, restrictToExtent, extent, count)]) except: try: layerVars += "\n".join([layerToJavascript(iface, layer, encode2json, matchCRS, cluster, restrictToExtent, extent, count)]) except: pass groupVars = "" groupedLayers = {} for group, groupLayers in groups.iteritems(): groupLayerObjs = "" for layer in groupLayers: try: if isinstance(layer, TileLayer): continue except: pass groupLayerObjs += ("lyr_" + safeName(layer.name()) + layer_names_id[layer.id()] + ",") groupVars += ('''var %s = new ol.layer.Group({ layers: [%s], title: "%s"});\n''' % ("group_" + safeName(group), groupLayerObjs, group)) for layer in groupLayers: groupedLayers[layer.id()] = safeName(group) mapLayers = ["baseLayer"] usedGroups = [] osmb = "" for count, layer in enumerate(layers): try: renderer = layer.rendererV2() if is25d(layer, canvas, restrictToExtent, extent): shadows = "" renderer = layer.rendererV2() renderContext = QgsRenderContext.fromMapSettings( canvas.mapSettings()) fields = layer.pendingFields() renderer.startRender(renderContext, fields) for feat in layer.getFeatures(): if isinstance(renderer, QgsCategorizedSymbolRendererV2): classAttribute = renderer.classAttribute() attrValue = feat.attribute(classAttribute) catIndex = renderer.categoryIndexForValue(attrValue) categories = renderer.categories() symbol = categories[catIndex].symbol() elif isinstance(renderer, QgsGraduatedSymbolRendererV2): classAttribute = renderer.classAttribute() attrValue = feat.attribute(classAttribute) ranges = renderer.ranges() for range in ranges: if (attrValue >= range.lowerValue() and attrValue <= range.upperValue()): symbol = range.symbol().clone() else: symbol = renderer.symbolForFeature2(feat, renderContext) symbolLayer = symbol.symbolLayer(0) if not symbolLayer.paintEffect().effectList()[0].enabled(): shadows = "'2015-07-15 10:00:00'" renderer.stopRender(renderContext) osmb = """ var osmb = new OSMBuildings(map).date(new Date({shadows})); osmb.set(json_{sln}{count});""".format(shadows=shadows, sln=safeName(layer.name()), count=unicode(count)) else: try: if not isinstance(layer, TileLayer): mapLayers.append("lyr_" + safeName(layer.name()) + unicode(count)) except: mapLayers.append("lyr_" + safeName(layer.name()) + unicode(count)) except: QgsMessageLog.logMessage(traceback.format_exc(), "qgis2web", level=QgsMessageLog.CRITICAL) try: if not isinstance(layer, TileLayer): mapLayers.append("lyr_" + safeName(layer.name()) + unicode(count)) except: mapLayers.append("lyr_" + safeName(layer.name()) + unicode(count)) visibility = "" for layer, v in zip(mapLayers[1:], visible): visibility += "\n".join(["%s.setVisible(%s);" % (layer, unicode(v).lower())]) group_list = ["baseLayer"] if len(basemapList) else [] no_group_list = [] for count, layer in enumerate(layers): try: if is25d(layer, canvas, restrictToExtent, extent): pass else: if layer.id() in groupedLayers: groupName = groupedLayers[layer.id()] if groupName not in usedGroups: group_list.append("group_" + safeName(groupName)) usedGroups.append(groupName) else: no_group_list.append("lyr_" + safeName(layer.name()) + unicode(count)) except: if layer.id() in groupedLayers: groupName = groupedLayers[layer.id()] if groupName not in usedGroups: group_list.append("group_" + safeName(groupName)) usedGroups.append(groupName) else: no_group_list.append("lyr_" + safeName(layer.name()) + unicode(count)) layersList = [] for layer in (group_list + no_group_list): layersList.append(layer) layersListString = "var layersList = [" + ",".join(layersList) + "];" fieldAliases = "" fieldImages = "" fieldLabels = "" blend_mode = "" for count, (layer, labels) in enumerate(zip(layers, popup)): sln = safeName(layer.name()) + unicode(count) if layer.type() == layer.VectorLayer and not is25d(layer, canvas, restrictToExtent, extent): fieldList = layer.pendingFields() aliasFields = "" imageFields = "" labelFields = "" for field, label in zip(labels.keys(), labels.values()): labelFields += "'%(field)s': '%(label)s', " % ( {"field": field, "label": label}) labelFields = "{%(labelFields)s});\n" % ( {"labelFields": labelFields}) labelFields = "lyr_%(name)s.set('fieldLabels', " % ( {"name": sln}) + labelFields fieldLabels += labelFields for f in fieldList: fieldIndex = fieldList.indexFromName(unicode(f.name())) aliasFields += "'%(field)s': '%(alias)s', " % ( {"field": f.name(), "alias": layer.attributeDisplayName(fieldIndex)}) widget = layer.editFormConfig().widgetType(fieldIndex) imageFields += "'%(field)s': '%(image)s', " % ( {"field": f.name(), "image": widget}) aliasFields = "{%(aliasFields)s});\n" % ( {"aliasFields": aliasFields}) aliasFields = "lyr_%(name)s.set('fieldAliases', " % ( {"name": sln}) + aliasFields fieldAliases += aliasFields imageFields = "{%(imageFields)s});\n" % ( {"imageFields": imageFields}) imageFields = "lyr_%(name)s.set('fieldImages', " % ( {"name": sln}) + imageFields fieldImages += imageFields blend_mode = """lyr_%(name)s.on('precompose', function(evt) { evt.context.globalCompositeOperation = '%(blend)s'; });""" % ( {"name": sln, "blend": BLEND_MODES[layer.blendMode()]}) path = os.path.join(folder, "layers", "layers.js") with codecs.open(path, "w", "utf-8") as f: if basemapList: f.write(baseLayer + "\n") f.write(layerVars + "\n") f.write(groupVars + "\n") f.write(visibility + "\n") f.write(layersListString + "\n") f.write(fieldAliases) f.write(fieldImages) f.write(fieldLabels) f.write(blend_mode) return osmb
def writeLayersAndGroups(layers, groups, visible, folder, popup, settings, json, matchCRS, clustered, iface): canvas = iface.mapCanvas() basemapList = settings["Appearance"]["Base layer"] basemaps = [basemapOL()[item.text()] for _, item in enumerate(basemapList)] baseLayer = """var baseLayer = new ol.layer.Group({ 'title': 'Base maps', layers: [%s\n] });""" % ','.join(basemaps) layerVars = "" for layer, encode2json, cluster in zip(layers, json, clustered): try: if is25d(layer, canvas): pass else: layerVars += "\n".join([layerToJavascript(iface, layer, encode2json, matchCRS, cluster)]) except: layerVars += "\n".join([layerToJavascript(iface, layer, encode2json, matchCRS, cluster)]) groupVars = "" groupedLayers = {} for group, groupLayers in groups.iteritems(): groupVars += ('''var %s = new ol.layer.Group({ layers: [%s], title: "%s"});\n''' % ("group_" + safeName(group), ",".join(["lyr_" + safeName(layer.name()) for layer in groupLayers]), group)) for layer in groupLayers: groupedLayers[layer.id()] = safeName(group) mapLayers = ["baseLayer"] usedGroups = [] osmb = "" for layer in layers: try: renderer = layer.rendererV2() if is25d(layer, canvas): shadows = "" renderer = layer.rendererV2() renderContext = QgsRenderContext.fromMapSettings( canvas.mapSettings()) fields = layer.pendingFields() renderer.startRender(renderContext, fields) for feat in layer.getFeatures(): if isinstance(renderer, QgsCategorizedSymbolRendererV2): classAttribute = renderer.classAttribute() attrValue = feat.attribute(classAttribute) catIndex = renderer.categoryIndexForValue(attrValue) categories = renderer.categories() symbol = categories[catIndex].symbol() elif isinstance(renderer, QgsGraduatedSymbolRendererV2): classAttribute = renderer.classAttribute() attrValue = feat.attribute(classAttribute) ranges = renderer.ranges() for range in ranges: if (attrValue >= range.lowerValue() and attrValue <= range.upperValue()): symbol = range.symbol().clone() else: symbol = renderer.symbolForFeature2(feat, renderContext) symbolLayer = symbol.symbolLayer(0) if not symbolLayer.paintEffect().effectList()[0].enabled(): shadows = "'2015-07-15 10:00:00'" renderer.stopRender(renderContext) osmb = """ var osmb = new OSMBuildings(map).date(new Date({shadows})); osmb.set(geojson_{sln});""".format(shadows=shadows, sln=safeName(layer.name())) else: mapLayers.append("lyr_" + safeName(layer.name())) except: mapLayers.append("lyr_" + safeName(layer.name())) visibility = "" for layer, v in zip(mapLayers[1:], visible): visibility += "\n".join(["%s.setVisible(%s);" % (layer, unicode(v).lower())]) group_list = ["baseLayer"] if len(basemapList) else [] no_group_list = [] for layer in layers: try: if is25d(layer, canvas): pass else: if layer.id() in groupedLayers: groupName = groupedLayers[layer.id()] if groupName not in usedGroups: group_list.append("group_" + safeName(groupName)) usedGroups.append(groupName) else: no_group_list.append("lyr_" + safeName(layer.name())) except: if layer.id() in groupedLayers: groupName = groupedLayers[layer.id()] if groupName not in usedGroups: group_list.append("group_" + safeName(groupName)) usedGroups.append(groupName) else: no_group_list.append("lyr_" + safeName(layer.name())) layersList = [] for layer in (group_list + no_group_list): layersList.append(layer) layersListString = "var layersList = [" + ",".join(layersList) + "];" fieldAliases = "" fieldImages = "" fieldLabels = "" for layer, labels in zip(layers, popup): if layer.type() == layer.VectorLayer: fieldList = layer.pendingFields() aliasFields = "" imageFields = "" labelFields = "" for field, label in zip(labels.keys(), labels.values()): labelFields += "'%(field)s': '%(label)s', " % ( {"field": field, "label": label}) labelFields = "{%(labelFields)s});\n" % ( {"labelFields": labelFields}) labelFields = "lyr_%(name)s.set('fieldLabels', " % ( {"name": safeName(layer.name())}) + labelFields fieldLabels += labelFields for f in fieldList: fieldIndex = fieldList.indexFromName(unicode(f.name())) aliasFields += "'%(field)s': '%(alias)s', " % ( {"field": f.name(), "alias": layer.attributeDisplayName(fieldIndex)}) try: widget = layer.editFormConfig().widgetType(fieldIndex) except: widget = layer.editorWidgetV2(fieldIndex) imageFields += "'%(field)s': '%(image)s', " % ( {"field": f.name(), "image": widget}) aliasFields = "{%(aliasFields)s});\n" % ( {"aliasFields": aliasFields}) aliasFields = "lyr_%(name)s.set('fieldAliases', " % ( {"name": safeName(layer.name())}) + aliasFields fieldAliases += aliasFields imageFields = "{%(imageFields)s});\n" % ( {"imageFields": imageFields}) imageFields = "lyr_%(name)s.set('fieldImages', " % ( {"name": safeName(layer.name())}) + imageFields fieldImages += imageFields path = os.path.join(folder, "layers", "layers.js") with codecs.open(path, "w", "utf-8") as f: if basemapList: f.write(baseLayer + "\n") f.write(layerVars + "\n") f.write(groupVars + "\n") f.write(visibility + "\n") f.write(layersListString + "\n") f.write(fieldAliases) f.write(fieldImages) f.write(fieldLabels) return osmb
import codecs import os import re import math import time import shutil from qgis.core import * from utils import exportLayers, safeName from qgis.utils import iface from PyQt4.QtCore import * from PyQt4.QtGui import * from utils import ALL_ATTRIBUTES from basemaps import basemapOL, basemapAttributions baseLayers = basemapOL() basemapAttributions = basemapAttributions() baseLayerGroup = "var baseLayer = new ol.layer.Group({'title': 'Base maps',layers: [%s]});" def writeOL(iface, layers, groups, popup, visible, json, cluster, labels, settings, folder): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) folder = os.path.join(folder, 'qgis2web_' + unicode(time.strftime("%Y_%m_%d-%H_%M_%S"))) # folder = os.path.join(os.getcwd(),folder) try: dst = os.path.join(folder, "resources") if not os.path.exists(dst): shutil.copytree(os.path.join(os.path.dirname(__file__), "resources"), dst) precision = settings["Data export"]["Precision"] optimize = settings["Data export"]["Minify GeoJSON files"]
def writeLayersAndGroups(layers, groups, visible, folder, popup, settings, json, matchCRS, clustered, iface): canvas = iface.mapCanvas() basemapList = settings["Appearance"]["Base layer"] basemaps = [basemapOL()[item.text()] for _, item in enumerate(basemapList)] baseLayer = """var baseLayer = new ol.layer.Group({ 'title': 'Base maps', layers: [%s\n] });""" % ','.join(basemaps) layerVars = "" for layer, encode2json, cluster in zip(layers, json, clustered): try: if is25d(layer, canvas): pass else: layerVars += "\n".join([ layerToJavascript(iface, layer, encode2json, matchCRS, cluster) ]) except: layerVars += "\n".join([ layerToJavascript(iface, layer, encode2json, matchCRS, cluster) ]) groupVars = "" groupedLayers = {} for group, groupLayers in groups.iteritems(): groupVars += ( '''var %s = new ol.layer.Group({ layers: [%s], title: "%s"});\n''' % ("group_" + safeName(group), ",".join( ["lyr_" + safeName(layer.name()) for layer in groupLayers]), group)) for layer in groupLayers: groupedLayers[layer.id()] = safeName(group) mapLayers = ["baseLayer"] usedGroups = [] osmb = "" for layer in layers: try: renderer = layer.rendererV2() if is25d(layer, canvas): shadows = "" renderer = layer.rendererV2() renderContext = QgsRenderContext.fromMapSettings( canvas.mapSettings()) fields = layer.pendingFields() renderer.startRender(renderContext, fields) for feat in layer.getFeatures(): if isinstance(renderer, QgsCategorizedSymbolRendererV2): classAttribute = renderer.classAttribute() attrValue = feat.attribute(classAttribute) catIndex = renderer.categoryIndexForValue(attrValue) categories = renderer.categories() symbol = categories[catIndex].symbol() elif isinstance(renderer, QgsGraduatedSymbolRendererV2): classAttribute = renderer.classAttribute() attrValue = feat.attribute(classAttribute) ranges = renderer.ranges() for range in ranges: if (attrValue >= range.lowerValue() and attrValue <= range.upperValue()): symbol = range.symbol().clone() else: symbol = renderer.symbolForFeature2( feat, renderContext) symbolLayer = symbol.symbolLayer(0) if not symbolLayer.paintEffect().effectList()[0].enabled(): shadows = "'2015-07-15 10:00:00'" renderer.stopRender(renderContext) osmb = """ var osmb = new OSMBuildings(map).date(new Date({shadows})); osmb.set(geojson_{sln});""".format(shadows=shadows, sln=safeName(layer.name())) else: mapLayers.append("lyr_" + safeName(layer.name())) except: mapLayers.append("lyr_" + safeName(layer.name())) visibility = "" for layer, v in zip(mapLayers[1:], visible): visibility += "\n".join( ["%s.setVisible(%s);" % (layer, unicode(v).lower())]) group_list = ["baseLayer"] if len(basemapList) else [] no_group_list = [] for layer in layers: try: if is25d(layer, canvas): pass else: if layer.id() in groupedLayers: groupName = groupedLayers[layer.id()] if groupName not in usedGroups: group_list.append("group_" + safeName(groupName)) usedGroups.append(groupName) else: no_group_list.append("lyr_" + safeName(layer.name())) except: if layer.id() in groupedLayers: groupName = groupedLayers[layer.id()] if groupName not in usedGroups: group_list.append("group_" + safeName(groupName)) usedGroups.append(groupName) else: no_group_list.append("lyr_" + safeName(layer.name())) layersList = [] for layer in (group_list + no_group_list): layersList.append(layer) layersListString = "var layersList = [" + ",".join(layersList) + "];" fieldAliases = "" fieldImages = "" fieldLabels = "" for layer, labels in zip(layers, popup): if layer.type() == layer.VectorLayer: fieldList = layer.pendingFields() aliasFields = "" imageFields = "" labelFields = "" for field, label in zip(labels.keys(), labels.values()): labelFields += "'%(field)s': '%(label)s', " % ({ "field": field, "label": label }) labelFields = "{%(labelFields)s});\n" % ({ "labelFields": labelFields }) labelFields = "lyr_%(name)s.set('fieldLabels', " % ({ "name": safeName(layer.name()) }) + labelFields fieldLabels += labelFields for f in fieldList: fieldIndex = fieldList.indexFromName(unicode(f.name())) aliasFields += "'%(field)s': '%(alias)s', " % ( { "field": f.name(), "alias": layer.attributeDisplayName(fieldIndex) }) try: widget = layer.editFormConfig().widgetType(fieldIndex) except: widget = layer.editorWidgetV2(fieldIndex) imageFields += "'%(field)s': '%(image)s', " % ( { "field": f.name(), "image": widget }) aliasFields = "{%(aliasFields)s});\n" % ({ "aliasFields": aliasFields }) aliasFields = "lyr_%(name)s.set('fieldAliases', " % ({ "name": safeName(layer.name()) }) + aliasFields fieldAliases += aliasFields imageFields = "{%(imageFields)s});\n" % ({ "imageFields": imageFields }) imageFields = "lyr_%(name)s.set('fieldImages', " % ({ "name": safeName(layer.name()) }) + imageFields fieldImages += imageFields path = os.path.join(folder, "layers", "layers.js") with codecs.open(path, "w", "utf-8") as f: if basemapList: f.write(baseLayer + "\n") f.write(layerVars + "\n") f.write(groupVars + "\n") f.write(visibility + "\n") f.write(layersListString + "\n") f.write(fieldAliases) f.write(fieldImages) f.write(fieldLabels) return osmb
import os import re import math import time import shutil import traceback from qgis.core import * from utils import exportLayers, safeName, replaceInTemplate from qgis.utils import iface from PyQt4.QtCore import * from PyQt4.QtGui import * from olScriptStrings import * from utils import ALL_ATTRIBUTES from basemaps import basemapOL, basemapAttributions baseLayers = basemapOL() basemapAttributions = basemapAttributions() baseLayerGroup = "var baseLayer = " baseLayerGroup += "new ol.layer.Group({'title': 'Base maps',layers: [%s]});" def writeOL(iface, layers, groups, popup, visible, json, clustered, settings, folder): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) stamp = time.strftime("%Y_%m_%d-%H_%M_%S") folder = os.path.join(folder, 'qgis2web_' + unicode(stamp)) try: dst = os.path.join(folder, "resources") if not os.path.exists(dst): shutil.copytree(