-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_0-100.html
288 lines (240 loc) · 10.8 KB
/
index_0-100.html
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leaflet with GeoJSON from ArcGIS</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<style>
#map { height: 500px; }
#form { margin-top: 20px; }
.label-marker .label {
font-family: 'Open Sans', sans-serif;
font-size: 12px; /* Initial font size */
}
.info {
padding: 6px 8px;
font: 14px/16px Arial, Helvetica, sans-serif;
background: white;
background: rgba(255,255,255,0.95);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.info h4 {
margin: 0 0 5px;
color: #777;
}
.legend {
line-height: 18px;
color: #555;
}
.legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 8px;
opacity: 0.9;
}
</style>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Overpass:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
</head>
<body>
<div id="map"></div>
<div id="form">
<form id="dataForm">
<label for="csvInput">CSV Data:</label>
<textarea id="csvInput" name="csvInput" rows="3" required placeholder="44114,20\n44113,10"></textarea>
<button type="button" onclick="refreshMap()">Submit</button>
<button type="button" onclick="copyToClipboard()">Copy GeoJSON to Clipboard</button>
<!-- Added input box for polygon fill color -->
<label for="fillColorInput">Polygon Fill Color:</label>
<input type="color" id="fillColorInput" name="fillColorInput" value="#008080"><br>
<input id="propertyName" name="propertyName" value="" placeholder="Legend">
</form>
</div>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<script src="https://unpkg.com/leaflet.draggable/dist/leaflet.draggable.js"></script>
<script>
var map = L.map('map', { zoomSnap: 0.25, scrollWheelZoom: false }).setView([0, 0], 2);
var geojsonLayer;
var minValue;
var maxValue;
var propertyName;
map.on('zoomend', function () {
updateLabelFont();
});
// create a street map layer and add it to the map, making it the default basemap
let openstreetmaps = L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' )
var Stadia_AlidadeSmooth = L.tileLayer('https://tiles.stadiamaps.com/tiles/alidade_smooth/{z}/{x}/{y}{r}.{ext}', {
minZoom: 0,
maxZoom: 20,
attribution: '© <a href="https://www.stadiamaps.com/" target="_blank">Stadia Maps</a> © <a href="https://openmaptiles.org/" target="_blank">OpenMapTiles</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
ext: 'png'
});
var Stadia_StamenTonerLite = L.tileLayer('https://tiles.stadiamaps.com/tiles/stamen_toner_lite/{z}/{x}/{y}{r}.{ext}', {
minZoom: 0,
maxZoom: 20,
attribution: '© <a href="https://www.stadiamaps.com/" target="_blank">Stadia Maps</a> © <a href="https://www.stamen.com/" target="_blank">Stamen Design</a> © <a href="https://openmaptiles.org/" target="_blank">OpenMapTiles</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
ext: 'png'
});
var CartoDB_Positron = L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 20
}).addTo(map);
// create an object to hold layer names as you want them to appear in the basemap switcher list
let basemapControl = {
"CartoDB_Positron": CartoDB_Positron,
"OSM": openstreetmaps,
"Stadia_AlidadeSmooth": Stadia_AlidadeSmooth,
"Stadia_StamenTonerLite": Stadia_StamenTonerLite,
}
// display the control (switcher list) on the map, by default in the top right corner
L.control.layers( basemapControl ).addTo( map )
function refreshMap() {
// map.removeLayer(geojsonLayer);
map.eachLayer((layer) => {
if (layer instanceof L.Marker) {
layer.remove();
}
});
var csvInput = document.getElementById('csvInput').value;
// Parse CSV data
var rows = csvInput.split('\n');
propertyName = rows[0].split(',')[1]
var data = [];
for (var i = 0; i < rows.length; i++) {
var columns = rows[i].split(',');
var zipCode = columns[0].trim();
var propertyValue = parseFloat(columns[1].trim());
data.push({ zipCode: zipCode, propertyValue: propertyValue });
}
// Create a comma-separated list of zip codes
var zipCodeList = data.map(entry => entry.zipCode).join(',');
// ArcGIS layer URL with the list of zip codes
var arcgisLayerUrl = 'https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Boundaries_2022/FeatureServer/3/query?where=ZIP_CODE+IN+(' + zipCodeList + ')&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&relationParam=&returnGeodetic=false&outFields=ZIP_CODE&returnGeometry=true&returnCentroid=false&returnEnvelope=false&featureEncoding=esriDefault&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&defaultSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pgeojson&token=';
// Make a GET request to ArcGIS layer URL to fetch GeoJSON data
fetch(arcgisLayerUrl)
.then(response => response.json())
.then(geojson => {
// Clear existing layers
map.eachLayer(function (layer) {
if (layer instanceof L.GeoJSON) {
map.removeLayer(layer);
}
});
// Determine minValue and maxValue
minValue = Math.min(...data.map(entry => entry.propertyValue));
maxValue = Math.max(...data.map(entry => entry.propertyValue));
// Add GeoJSON data to the map with styled polygons and labels
geojsonLayer = L.geoJSON(geojson, {
style: function (feature) {
var zipCode = feature.properties.ZIP_CODE;
var propertyValue = getProperty(data, zipCode);
var color = getColor(propertyValue);
//feature.properties.fill = color; // Add fillColor to properties
feature.properties.value = propertyValue;
return { fillColor: color, color: '#fff', weight: 1, fillOpacity: 1 };
},
onEachFeature: function (feature, layer) {
var zipCode = feature.properties.ZIP_CODE;
var propertyValue = getProperty(data, zipCode);
var label = zipCode;
var center = layer.getBounds().getCenter();
L.marker(center, {
icon: L.divIcon({
className: 'label',
html: '<div style="font-family: \'Overpass\', sans-serif;"><center>' + label + '<br><b>'+propertyValue+'%</b></center></div>',
iconSize: [30, 15]
})
}).addTo(map);
}
}).addTo(map);
// Fit the map to the GeoJSON data extent
map.fitBounds(geojsonLayer.getBounds());
removeElementsByClass('legend')
var legend = L.control({ position: 'bottomright' });
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend');
var grades = [0, 25, 50, 75, 100]; // Adjust these values as needed
var labels = [];
div.innerHTML += document.getElementById('propertyName').value + '<br>'
for (var i = 0; i < grades.length - 1; i++) {
var from = grades[i];
var to = grades[i + 1];
var color = interpolateColor('#FFFFFF', document.getElementById('fillColorInput').value, to / 100); // Adjust colors based on your gradient
div.innerHTML +=
'<div><i style="background:' + color + '"></i> ' +
from + '–' + to + '%</div>';
}
var draggable = new L.Draggable(div);
draggable.enable()
return div;
};
legend.addTo(map);
})
.catch(error => console.error('Error fetching GeoJSON data:', error));
}
function getProperty(data, zipCode) {
// Find the property value for the given zip code
var entry = data.find(entry => entry.zipCode === zipCode);
return entry ? entry.propertyValue : 0;
}
function updateLabelFont() {
var labels = document.getElementsByClassName('label');
for (var i = 0; i < labels.length; i++) {
var fontSize = 22 * (map.getZoom()-12);
if (fontSize < 0) {
fontSize = 0;
}
labels[i].style.setProperty('font-size', fontSize + 'px');
// boxParaRulesetProperty("font-size", fontSize);
// console.log(fontSize + 'px !important')
}
}
function copyToClipboard() {
if (geojsonLayer) {
var geojsonString = JSON.stringify(geojsonLayer.toGeoJSON());
navigator.clipboard.writeText(geojsonString)
.then(function() {
alert('GeoJSON copied to clipboard!');
})
.catch(function(err) {
console.error('Unable to copy GeoJSON to clipboard', err);
});
} else {
console.warn('No GeoJSON data available to copy.');
}
}
function getColor(value) {
// Define a gradient based on property values (teal gradient for positive values)
var gradientColor = interpolateColor('#FFFFFF', document.getElementById('fillColorInput').value, value / 100);
return gradientColor;
}
function interpolateColor(color1, color2, ratio) {
var hex = function(x) {
x = x.toString(16);
return (x.length === 1) ? '0' + x : x;
};
var r1 = parseInt(color1.substring(1, 3), 16);
var g1 = parseInt(color1.substring(3, 5), 16);
var b1 = parseInt(color1.substring(5, 7), 16);
var r2 = parseInt(color2.substring(1, 3), 16);
var g2 = parseInt(color2.substring(3, 5), 16);
var b2 = parseInt(color2.substring(5, 7), 16);
var r = Math.round(r1 * (1 - ratio) + r2 * ratio);
var g = Math.round(g1 * (1 - ratio) + g2 * ratio);
var b = Math.round(b1 * (1 - ratio) + b2 * ratio);
return '#' + hex(r) + hex(g) + hex(b);
}
function removeElementsByClass(className){
const elements = document.getElementsByClassName(className);
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}
}
</script>
</body>
</html>