-
Notifications
You must be signed in to change notification settings - Fork 91
/
main.js
240 lines (213 loc) · 6.79 KB
/
main.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* global L Papa */
/*
* Script to display two tables from Google Sheets as point and geometry layers using Leaflet
* The Sheets are then imported using PapaParse and overwrite the initially laded layers
*/
// PASTE YOUR URLs HERE
// these URLs come from Google Sheets 'shareable link' form
// the first is the geometry layer and the second the points
let geomURL =
"https://docs.google.com/spreadsheets/d/e/2PACX-1vTsAyA0Hpk_-WpKyN1dfqi5IPEIC3rqEiL-uwElxJpw_U7BYntc8sDw-8sWsL87JCDU4lVg2aNi65ES/pub?output=csv";
let pointsURL =
"https://docs.google.com/spreadsheets/d/e/2PACX-1vSFQw9sVY16eQmN5TIjOH7CUaxeZnl_v6LcdE2goig1pSe9I3hipeOn1sOwmC4fS0AURefRWwcKExct/pub?output=csv";
window.addEventListener("DOMContentLoaded", init);
let map;
let sidebar;
let panelID = "my-info-panel";
/*
* init() is called when the page has loaded
*/
function init() {
// Create a new Leaflet map centered on the continental US
map = L.map("map").setView([51.5, -0.1], 14);
// This is the Carto Positron basemap
L.tileLayer(
"https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}{r}.png",
{
attribution:
"© <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a> © <a href='http://cartodb.com/attributions'>CartoDB</a>",
subdomains: "abcd",
maxZoom: 19,
}
).addTo(map);
sidebar = L.control
.sidebar({
container: "sidebar",
closeButton: true,
position: "right",
})
.addTo(map);
let panelContent = {
id: panelID,
tab: "<i class='fa fa-bars active'></i>",
pane: "<p id='sidebar-content'></p>",
title: "<h2 id='sidebar-title'>Nothing selected</h2>",
};
sidebar.addPanel(panelContent);
map.on("click", function () {
sidebar.close(panelID);
});
// Use PapaParse to load data from Google Sheets
// And call the respective functions to add those to the map.
Papa.parse(geomURL, {
download: true,
header: true,
complete: addGeoms,
});
Papa.parse(pointsURL, {
download: true,
header: true,
complete: addPoints,
});
}
/*
* Expects a JSON representation of the table with properties columns
* and a 'geometry' column that can be parsed by parseGeom()
*/
function addGeoms(data) {
data = data.data;
// Need to convert the PapaParse JSON into a GeoJSON
// Start with an empty GeoJSON of type FeatureCollection
// All the rows will be inserted into a single GeoJSON
let fc = {
type: "FeatureCollection",
features: [],
};
for (let row in data) {
// The Sheets data has a column 'include' that specifies if that row should be mapped
if (data[row].include == "y") {
let features = parseGeom(JSON.parse(data[row].geometry));
features.forEach((el) => {
el.properties = {
name: data[row].name,
description: data[row].description,
};
fc.features.push(el);
});
}
}
// The geometries are styled slightly differently on mouse hovers
let geomStyle = { color: "#2ca25f", fillColor: "#99d8c9", weight: 2 };
let geomHoverStyle = { color: "green", fillColor: "#2ca25f", weight: 3 };
L.geoJSON(fc, {
onEachFeature: function (feature, layer) {
layer.on({
mouseout: function (e) {
e.target.setStyle(geomStyle);
},
mouseover: function (e) {
e.target.setStyle(geomHoverStyle);
},
click: function (e) {
// This zooms the map to the clicked geometry
// Uncomment to enable
// map.fitBounds(e.target.getBounds());
// if this isn't added, then map.click is also fired!
L.DomEvent.stopPropagation(e);
document.getElementById("sidebar-title").innerHTML =
e.target.feature.properties.name;
document.getElementById("sidebar-content").innerHTML =
e.target.feature.properties.description;
sidebar.open(panelID);
},
});
},
style: geomStyle,
}).addTo(map);
}
/*
* addPoints is a bit simpler, as no GeoJSON is needed for the points
*/
function addPoints(data) {
data = data.data;
let pointGroupLayer = L.layerGroup().addTo(map);
// Choose marker type. Options are:
// (these are case-sensitive, defaults to marker!)
// marker: standard point with an icon
// circleMarker: a circle with a radius set in pixels
// circle: a circle with a radius set in meters
let markerType = "marker";
// Marker radius
// Wil be in pixels for circleMarker, metres for circle
// Ignore for point
let markerRadius = 100;
for (let row = 0; row < data.length; row++) {
let marker;
if (markerType == "circleMarker") {
marker = L.circleMarker([data[row].lat, data[row].lon], {
radius: markerRadius,
});
} else if (markerType == "circle") {
marker = L.circle([data[row].lat, data[row].lon], {
radius: markerRadius,
});
} else {
marker = L.marker([data[row].lat, data[row].lon]);
}
marker.addTo(pointGroupLayer);
// UNCOMMENT THIS LINE TO USE POPUPS
//marker.bindPopup('<h2>' + data[row].name + '</h2>There's a ' + data[row].description + ' here');
// COMMENT THE NEXT GROUP OF LINES TO DISABLE SIDEBAR FOR THE MARKERS
marker.feature = {
properties: {
name: data[row].name,
description: data[row].description,
},
};
marker.on({
click: function (e) {
L.DomEvent.stopPropagation(e);
document.getElementById("sidebar-title").innerHTML =
e.target.feature.properties.name;
document.getElementById("sidebar-content").innerHTML =
e.target.feature.properties.description;
sidebar.open(panelID);
},
});
// COMMENT UNTIL HERE TO DISABLE SIDEBAR FOR THE MARKERS
// AwesomeMarkers is used to create fancier icons
let icon = L.AwesomeMarkers.icon({
icon: "info-circle",
iconColor: "white",
markerColor: data[row].color,
prefix: "fa",
extraClasses: "fa-rotate-0",
});
if (!markerType.includes("circle")) {
marker.setIcon(icon);
}
}
}
/*
* Accepts any GeoJSON-ish object and returns an Array of
* GeoJSON Features. Attempts to guess the geometry type
* when a bare coordinates Array is supplied.
*/
function parseGeom(gj) {
// FeatureCollection
if (gj.type == "FeatureCollection") {
return gj.features;
}
// Feature
else if (gj.type == "Feature") {
return [gj];
}
// Geometry
else if ("type" in gj) {
return [{ type: "Feature", geometry: gj }];
}
// Coordinates
else {
let type;
if (typeof gj[0] == "number") {
type = "Point";
} else if (typeof gj[0][0] == "number") {
type = "LineString";
} else if (typeof gj[0][0][0] == "number") {
type = "Polygon";
} else {
type = "MultiPolygon";
}
return [{ type: "Feature", geometry: { type: type, coordinates: gj } }];
}
}