diff --git a/qgis2leaf_exec.py b/qgis2leaf_exec.py index 9beead8..30b36e8 100644 --- a/qgis2leaf_exec.py +++ b/qgis2leaf_exec.py @@ -20,223 +20,195 @@ ***************************************************************************/ """ -from PyQt4.QtCore import QFileInfo -import osgeo.ogr, osgeo.osr #we will need some packages -from osgeo import ogr from qgis.core import * import qgis.utils import os #for file writing/folder actions import shutil #for reverse removing directories import urllib # to get files from the web +from string import Template + +styletext = """ + """ + +basemaps = {'OSM Standard': + """ + map.attributionControl.addAttribution('© OpenStreetMap contributors,CC-BY-SA'); + L.tileLayer('http://a.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map); + """, + 'OSM Black & White': + """ + map.attributionControl.addAttribution('© OpenStreetMap contributors,CC-BY-SA'); + L.tileLayer('http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png').addTo(map); + """, + 'Stamen Toner': + """ + map.attributionControl.addAttribution('Map tiles by Stamen Design, CC BY 3.0 — Map data: © OpenStreetMap contributors,CC-BY-SA'); + L.tileLayer('http://a.tile.stamen.com/toner/{z}/{x}/{y}.png').addTo(map); + """ } + +layerblock = """ + function pop_${name}(feature, layer) { + var popupContent = ' + + ${rows} +
attributevalue
'; + layer.bindPopup(popupContent); + } + + var ${name}JSON = new L.geoJson(${name},{ + onEachFeature: pop_${name}, + pointToLayer: function (feature, latlng) { + return L.marker(latlng); + } + }); + feature_group.addLayer(${name}JSON); + ${name}JSON.addTo(map); + """ +htmltemplate = """ + + + QGIS2leaf webmap + + + + + +
+ + $sources + + $map + $basemap + var feature_group = new L.featureGroup([]); + $layerblocks + L.control.layers({},{$controllayers}).addTo(map); + $fitbounds + + + """ + +indextemplate = Template(htmltemplate) +layertemplate = Template(layerblock) + +def exportlayer(layer, datastore): + layerjsfile = os.path.join(datastore, '{}.js'.format(layer.name())) + qgis.core.QgsVectorFileWriter.writeAsVectorFormat(layer, layerjsfile, 'utf-8', exp_crs, 'GeoJson') + #now change the data structure to work with leaflet: + with open (layerjsfile, "r+") as jsfile: + code = jsfile.read () # read everything in the file + jsfile.seek (0) # rewind + jsfile.write("var {name} = {code}".format(name=i.name(), code=code)) # write the new line before + + return layerjsfile + +def generatelayerblock(layer): + fields = layer.pendingFields () + field_names = [field.name () for field in fields] + rows = [] + for field in field_names: + row = """{field}' + feature.properties.{field} + '""".format(field=field) + rows.append(row) + + return layertemplate.substitute(name=layer.name(), rows='\n'.join(rows)) + +def generateindexhtml(layers, bounds, dataStore, basemap, extenttype): + sources = [] + layerblocks = [] + controllayers = [] + + for layer in layers: + exportedlayer = exportlayer(layer, dataStore) + #now add the js files as data input for our map + new_src = """ - """ - f_html.write(base) - f_html.close() - # let's create the js files in the data folder of input vector files: - canvas = qgis.utils.iface.mapCanvas() - allLayers = canvas.layers() - exp_crs = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) - for i in allLayers: - if i.type() != 0 : - print(i.name() + " skipped as it is not a vector layer") - if i.type() == 0 : - - qgis.core.QgsVectorFileWriter.writeAsVectorFormat(i,dataStore + os.sep + str(i.name()) + '.js', 'utf-8', exp_crs, 'GeoJson') - #now change the data structure to work with leaflet: - with open(dataStore + os.sep + str(i.name()) + '.js', "r+") as f2: - old = f2.read() # read everything in the file - f2.seek(0) # rewind - f2.write("var " + str(i.name()) + " = " + old) # write the new line before - f2.close - - #now add the js files as data input for our map - with open(os.path.join(os.getcwd(),outputProjectFileName) + os.sep + 'index.html', 'a') as f3: - new_src = """ - - """ - # store everything in the file - f3.write(new_src) - f3.close() - #now determine the canvas bounding box - #####now with viewcontrol - if extent == 'canvas extent': - pt0 = canvas.extent() - crsSrc = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs() # WGS 84 - crsDest = QgsCoordinateReferenceSystem(4326) # WGS 84 / UTM zone 33N - xform = QgsCoordinateTransform(crsSrc, crsDest) - pt1 = xform.transform(pt0) - bbox_canvas = [pt1.yMinimum(), pt1.yMaximum(),pt1.xMinimum(), pt1.xMaximum()] - bounds = '[[' + str(pt1.yMinimum()) + ',' + str(pt1.xMinimum()) + '],[' + str(pt1.yMaximum()) + ',' + str(pt1.xMaximum()) +']]' - middle = """ - - - - """ - if extent == 'canvas extent': - end = """ - - - - """ - with open(os.path.join(os.getcwd(),outputProjectFileName) + os.sep + 'index.html', 'a') as f9: - f9.write(end) - f9.close()