forked from qgis2web/qgis2web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleafletWriter.py
More file actions
192 lines (178 loc) · 8.34 KB
/
Copy pathleafletWriter.py
File metadata and controls
192 lines (178 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
"""
/***************************************************************************
qgis2web
(c) Tom Chadwin
leafletWriter.py original by:
-------------------
begin : 2014-04-29
copyright : (C) 2013 by Riccardo Klinger
email : riccardo.klinger@geolicious.com
***************************************************************************/
/***************************************************************************
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from qgis.core import *
import qgis.utils
import os
import time
import re
from basemaps import basemapLeaflet
from leafletFileScripts import *
from leafletLayerScripts import *
from leafletScriptStrings import *
from utils import ALL_ATTRIBUTES, removeSpaces
def writeLeaflet(iface, outputProjectFileName, layer_list, visible, cluster,
json, params, popup):
legends = {}
canvas = iface.mapCanvas()
project = QgsProject.instance()
mapSettings = canvas.mapSettings()
title = project.title()
pluginDir = os.path.dirname(os.path.realpath(__file__))
outputProjectFileName = os.path.join(outputProjectFileName,
'qgis2web_' + unicode(time.time()))
outputIndex = os.path.join(outputProjectFileName, 'index.html')
cluster_num = 1
mapLibLocation = params["Data export"]["Mapping library location"]
minify = params["Data export"]["Minify GeoJSON files"]
precision = params["Data export"]["Precision"]
extent = params["Scale/Zoom"]["Extent"]
minZoom = params["Scale/Zoom"]["Min zoom level"]
maxZoom = params["Scale/Zoom"]["Max zoom level"]
restrictToExtent = params["Scale/Zoom"]["Restrict to extent"]
basemapList = params["Appearance"]["Base layer"]
matchCRS = params["Appearance"]["Match project CRS"]
addressSearch = params["Appearance"]["Add address search"]
locate = params["Appearance"]["Geolocate user"]
measure = params["Appearance"]["Measure tool"]
highlight = params["Appearance"]["Highlight on hover"]
layerSearch = params["Appearance"]["Layer search"]
popupsOnHover = params["Appearance"]["Show popups on hover"]
template = params["Appearance"]["Template"]
usedFields = [ALL_ATTRIBUTES] * len(popup)
QgsApplication.initQgis()
dataStore, cssStore = writeFoldersAndFiles(pluginDir,
outputProjectFileName, cluster,
measure, matchCRS, layerSearch,
canvas, mapLibLocation, locate)
writeCSS(cssStore, mapSettings.backgroundColor().name())
wfsLayers = ""
scaleDependentLayers = ""
labelVisibility = ""
new_src = ""
crs = QgsCoordinateReferenceSystem.EpsgCrsId
exp_crs = QgsCoordinateReferenceSystem(4326, crs)
lyrCount = 0
for layer, jsonEncode, eachPopup in zip(layer_list, json, popup):
rawLayerName = layer.name()
safeLayerName = re.sub('[\W_]+', '', rawLayerName) + unicode(lyrCount)
lyrCount += 1
dataPath = os.path.join(dataStore, 'json_' + safeLayerName)
tmpFileName = dataPath + '.json'
layerFileName = dataPath + '.js'
if layer.providerType() != 'WFS' or jsonEncode is True and layer:
if layer.type() == QgsMapLayer.VectorLayer:
exportJSONLayer(layer, eachPopup, precision, tmpFileName,
exp_crs, layerFileName, safeLayerName, minify,
canvas)
new_src += jsonScript(safeLayerName)
scaleDependentLayers = scaleDependentLabelScript(layer,
safeLayerName)
labelVisibility += scaleDependentLayers
elif layer.type() == QgsMapLayer.RasterLayer:
if layer.dataProvider().name() != "wms":
exportRasterLayer(layer, safeLayerName, dataPath)
if layer.hasScaleBasedVisibility():
scaleDependentLayers += scaleDependentLayerScript(layer,
safeLayerName)
if scaleDependentLayers != "":
scaleDependentLayers = scaleDependentScript(scaleDependentLayers)
crsSrc = mapSettings.destinationCrs()
crsAuthId = crsSrc.authid()
crsProj4 = crsSrc.toProj4()
middle = openScript()
if highlight or popupsOnHover:
selectionColor = mapSettings.selectionColor().name()
middle += highlightScript(highlight, popupsOnHover, selectionColor)
if extent == "Canvas extent":
pt0 = canvas.extent()
crsDest = QgsCoordinateReferenceSystem(4326)
xform = QgsCoordinateTransform(crsSrc, crsDest)
pt1 = xform.transform(pt0)
bbox_canvas = [pt1.yMinimum(), pt1.yMaximum(),
pt1.xMinimum(), pt1.xMaximum()]
bounds = '[[' + unicode(pt1.yMinimum()) + ','
bounds += unicode(pt1.xMinimum()) + '],['
bounds += unicode(pt1.yMaximum()) + ','
bounds += unicode(pt1.xMaximum()) + ']]'
if matchCRS and crsAuthId != 'EPSG:4326':
middle += crsScript(crsAuthId, crsProj4)
middle += mapScript(extent, matchCRS, crsAuthId, measure, maxZoom,
minZoom, bounds, locate)
else:
if matchCRS and crsAuthId != 'EPSG:4326':
middle += crsScript(crsAuthId, crsProj4)
middle += mapScript(extent, matchCRS, crsAuthId, measure, maxZoom,
minZoom, 0, locate)
middle += featureGroupsScript()
if (len(basemapList) == 0 or matchCRS):
basemapText = ""
else:
basemapText = basemapsScript(basemapList, maxZoom)
layerOrder = layerOrderScript(extent, restrictToExtent)
new_src += middle
new_src += basemapText
new_src += layerOrder
lyrCount = 0
for count, layer in enumerate(layer_list):
rawLayerName = layer.name()
safeLayerName = re.sub('[\W_]+', '', rawLayerName) + unicode(lyrCount)
lyrCount += 1
if layer.type() == QgsMapLayer.VectorLayer:
(new_src,
legends,
wfsLayers) = writeVectorLayer(layer, safeLayerName, usedFields,
highlight, popupsOnHover, popup,
count, outputProjectFileName,
wfsLayers, cluster, cluster_num,
visible, json, legends, new_src,
canvas, lyrCount)
elif layer.type() == QgsMapLayer.RasterLayer:
if layer.dataProvider().name() == "wms":
new_obj = wmsScript(layer, safeLayerName)
else:
new_obj = rasterScript(layer, safeLayerName)
if visible[count]:
new_obj += """
raster_group.addLayer(overlay_""" + safeLayerName + """);"""
new_src += new_obj
new_src += """
raster_group.addTo(map);
feature_group.addTo(map);"""
new_src += scaleDependentLayers
if title != "":
titleStart = unicode(titleSubScript(title).decode("utf-8"))
new_src += unicode(titleStart)
if addressSearch:
address_text = addressSearchScript()
new_src += address_text
if params["Appearance"]["Add layers list"]:
new_src += addLayersList(basemapList, matchCRS, layer_list, cluster,
legends)
if project.readBoolEntry("ScaleBar", "/Enabled", False)[0]:
end = scaleBar()
else:
end = ''
end += endHTMLscript(wfsLayers, layerSearch, labelVisibility)
new_src += end
writeHTMLstart(outputIndex, title, cluster, addressSearch, measure,
matchCRS, layerSearch, canvas, mapLibLocation, locate,
new_src, template)
return outputIndex