forked from qgis2web/qgis2web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
leafletFileScripts.py
193 lines (183 loc) · 7.48 KB
/
leafletFileScripts.py
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
193
# -*- coding: utf-8 -*-
import re
import os
import shutil
import codecs
from utils import replaceInTemplate
def writeFoldersAndFiles(pluginDir, outputProjectFileName, cluster_set,
measure, matchCRS, canvas, mapLibLocation, locate):
jsStore = os.path.join(outputProjectFileName, 'js')
os.makedirs(jsStore)
jsStore += os.sep
jsDir = pluginDir + os.sep + 'js' + os.sep
dataStore = os.path.join(outputProjectFileName, 'data')
os.makedirs(dataStore)
legendStore = os.path.join(outputProjectFileName, 'legend')
os.makedirs(legendStore)
cssStore = os.path.join(outputProjectFileName, 'css')
os.makedirs(cssStore)
cssStore += os.sep
cssDir = pluginDir + os.sep + 'css' + os.sep
markerStore = os.path.join(outputProjectFileName, 'markers')
os.makedirs(markerStore)
if mapLibLocation == "Local":
shutil.copyfile(jsDir + 'leaflet.js', jsStore + 'leaflet.js')
shutil.copyfile(cssDir + 'leaflet.css', cssStore + 'leaflet.css')
if locate:
os.makedirs(os.path.join(jsStore, "images"))
shutil.copyfile(jsDir + 'images/marker-icon.png',
jsStore + 'images/marker-icon.png')
shutil.copyfile(jsDir + 'images/marker-shadow.png',
jsStore + 'images/marker-shadow.png')
shutil.copyfile(jsDir + 'Autolinker.min.js',
jsStore + 'Autolinker.min.js')
shutil.copyfile(jsDir + 'OSMBuildings-Leaflet.js',
jsStore + 'OSMBuildings-Leaflet.js')
shutil.copyfile(jsDir + 'leaflet-hash.js', jsStore + 'leaflet-hash.js')
if len(cluster_set):
shutil.copyfile(jsDir + 'leaflet.markercluster.js',
jsStore + 'leaflet.markercluster.js')
shutil.copyfile(cssDir + 'MarkerCluster.css',
cssStore + 'MarkerCluster.css')
shutil.copyfile(cssDir + 'MarkerCluster.Default.css',
cssStore + 'MarkerCluster.Default.css')
shutil.copyfile(jsDir + 'label.js', jsStore + 'label.js')
shutil.copyfile(cssDir + 'label.css', cssStore + 'label.css')
if measure != "None":
shutil.copyfile(jsDir + 'leaflet.draw.js',
jsStore + 'leaflet.draw.js')
shutil.copyfile(cssDir + 'leaflet.draw.css',
cssStore + 'leaflet.draw.css')
if measure == "Imperial":
shutil.copyfile(jsDir + 'leaflet.measurecontrolFeet.js',
jsStore + 'leaflet.measurecontrol.js')
else:
shutil.copyfile(jsDir + 'leaflet.measurecontrol.js',
jsStore + 'leaflet.measurecontrol.js')
shutil.copyfile(cssDir + 'leaflet.measurecontrol.css',
cssStore + 'leaflet.measurecontrol.css')
shutil.copytree(cssDir + 'images', cssStore + 'images')
if (matchCRS and
canvas.mapRenderer().destinationCrs().authid() != 'EPSG:4326'):
shutil.copyfile(jsDir + 'proj4.js', jsStore + 'proj4.js')
shutil.copyfile(jsDir + 'proj4leaflet.js', jsStore + 'proj4leaflet.js')
return dataStore, cssStore
def writeHTMLstart(outputIndex, webpage_name, cluster_set, address, measure,
matchCRS, canvas, mapLibLocation, qgis2webJS, template):
if webpage_name == "":
pass
else:
webpage_name = unicode(webpage_name)
if mapLibLocation == "Local":
cssAddress = '<link rel="stylesheet" href="css/leaflet.css" />'
jsAddress = '<script src="js/leaflet.js"></script>'
else:
cssAddress = '<link rel="stylesheet" href='
cssAddress += '"http://cdn.leafletjs.com/leaflet/v0.7.7/'
cssAddress += 'leaflet.css" />'
jsAddress = '<script src="http://'
jsAddress += 'cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>'
jsAddress += """
<script src="js/OSMBuildings-Leaflet.js"></script>"""
extracss = '<link rel="stylesheet" type="text/css" '
extracss += """href="css/qgis2web.css">
<link rel="stylesheet" href="css/label.css" />"""
if len(cluster_set):
clusterCSS = '<link rel="stylesheet" '
clusterCSS += """href="css/MarkerCluster.css" />
<link rel="stylesheet" href="css/MarkerCluster.Default.css" />"""
clusterJS = '<script src="js/leaflet.markercluster.js">'
clusterJS += "</script>"
else:
clusterCSS = ""
clusterJS = ""
if address:
addressCSS = """
<link rel="stylesheet" href="""
addressCSS += '"http://k4r573n.github.io/leaflet-control-osm-geocoder/'
addressCSS += 'Control.OSMGeocoder.css" />'
addressJS = """
<script src="http://k4r573n.github.io/leaflet-control-osm-geocoder/"""
addressJS += 'Control.OSMGeocoder.js"></script>'
else:
addressCSS = ""
addressJS = ""
if measure != "None":
measureCSS = """
<link rel="stylesheet" href="css/leaflet.draw.css" />
<link rel="stylesheet" href="css/leaflet.measurecontrol.css" />"""
measureJS = """
<script src="js/leaflet.draw.js"></script>
<script src="js/leaflet.measurecontrol.js"></script>"""
else:
measureCSS = ""
measureJS = ""
extraJS = """<script src="js/leaflet-hash.js"></script>
<script src="js/label.js"></script>
<script src="js/Autolinker.min.js"></script>"""
if (matchCRS and
canvas.mapRenderer().destinationCrs().authid() != 'EPSG:4326'):
crsJS = """
<script src="js/proj4.js"></script>
<script src="js/proj4leaflet.js"></script>"""
else:
crsJS = ""
canvasSize = canvas.size()
values = {"@PAGETITLE@": webpage_name,
"@CSSADDRESS@": cssAddress,
"@EXTRACSS@": extracss,
"@JSADDRESS@": jsAddress,
"@LEAFLET_CLUSTERCSS@": clusterCSS,
"@LEAFLET_CLUSTERJS@": clusterJS,
"@LEAFLET_ADDRESSCSS@": addressCSS,
"@LEAFLET_MEASURECSS@": measureCSS,
"@LEAFLET_EXTRAJS@": extraJS,
"@LEAFLET_ADDRESSJS@": addressJS,
"@LEAFLET_MEASUREJS@": measureJS,
"@LEAFLET_CRSJS@": crsJS,
"@QGIS2WEBJS@": qgis2webJS,
"@MAP_WIDTH@": unicode(canvasSize.width()) + "px",
"@MAP_HEIGHT@": unicode(canvasSize.height()) + "px",
"@OL3_BACKGROUNDCOLOR@": "",
"@OL3_STYLEVARS@": "",
"@OL3_POPUP@": "",
"@OL3_GEOJSONVARS@": "",
"@OL3_WFSVARS@": "",
"@OL3_PROJ4@": "",
"@OL3_PROJDEF@": "",
"@OL3_GEOCODINGLINKS@": "",
"@OL3_LAYERSWITCHER@": "",
"@OL3_LAYERS@": "",
"@OL3_MEASURESTYLE@": ""}
with codecs.open(outputIndex, 'w', encoding='utf-8') as f:
base = replaceInTemplate(template + ".html", values)
base = re.sub('\n[\s_]+\n', '\n', base)
f.write(unicode(base))
f.close()
def writeCSS(cssStore, backgroundColor):
with open(cssStore + 'qgis2web.css', 'w') as f_css:
text = """
#map {
background-color: """ + backgroundColor + """
}
th {
text-align: left;
vertical-align: top;
}
.info {
padding: 6px 8px;
font: 14px/16px Arial, Helvetica, sans-serif;
background: white;
background: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.info h2 {
margin: 0 0 5px;
color: #777;
}
.leaflet-container {
background: #fff;
}"""
f_css.write(text)
f_css.close()