function createMap(map_id, zoomControl, map_center) {
const map = {};
map.map_id = map_id;
let Esri_WorldGrayCanvas = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri — Esri, DeLorme, NAVTEQ',
maxZoom: 16
});
var Stamen_TopOSMRelief = L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toposm-color-relief/{z}/{x}/{y}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
subdomains: 'abcd',
minZoom: 0,
maxZoom: 20,
ext: 'jpg',
bounds: [[22, -132], [51, -56]]
});
var Esri_WorldImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
});
map.map = L.map(container, {
layers: [
Stamen_TopOSMRelief,
Esri_WorldImagery,
Esri_WorldGrayCanvas
],
center: map_center,
zoom: 11,
zoomControl: zoomControl
});
let baseMaps = {
'Stamen_TopOSMRelief': Stamen_TopOSMRelief,
'Esri_WorldImagery': Esri_WorldImagery,
'Esri_WorldGrayCanvas': Esri_WorldGrayCanvas
};
L.control.layers(baseMaps).addTo(map.map);
map.drawMap = function(data, diameter) {
L.svg({clickable:true}).addTo(map.map);
const overlay = d3.select(map.map.getPanes().overlayPane);
const svg = overlay.select('svg').attr("pointer-events", "auto");
const g = svg.append('g').attr('class', 'leaflet-zoom-hide');
const projectPoint = function(x, y) {
const point = map.map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}
const projection = d3.geoTransform({point: projectPoint});
const pathCreator = d3.geoPath().projection(projection) ;
let myColor = map.colorInterpolateInteger(data.features, diameter);
let init_diameter = function(d) {
return d.properties[diameter] / 3 + 'px'
}
function filterPipes(data) {
let isZommed = map.map.getZoom() >= 15;
return data.features.filter(d => isZommed || d.properties[diameter] >= 12);
}
let filteredData = filterPipes(data)
const lines_bound = g.selectAll('path').data(filteredData).join('path')
drawPipeSelection(lines_bound)
function drawPipeSelection(selection) {
selection = selection
.attr('class', 'pipe')
.attr('d', pathCreator)
.attr("stroke-width", d => init_diameter(d))
.attr("stroke", d => myColor(d.properties[diameter]))
.attr("fill", "none")
.attr("style", "pointer-events: auto;")
.on("mouseover", function(e, d) {
d3.select(e.target).attr('stroke-width', '5px');
map.map.dragging.disable();
map.tooltip.tooltipMouseover(e, d);
})
.on('mouseout', function(e, d) {
d3.select(e.target)
.attr('stroke-width', d => init_diameter(d));
map.map.dragging.enable();
map.tooltip.tooltipMouseout(e, d);
});
}
function onZoom() {
console.log('==== NEW ZOOM ===', map.map.getZoom());
filteredData = filterPipes(data);
lines_bound.data(filteredData)
.join(
function(enter) {
enter.attr('class', 'pipe')
.attr('d', pathCreator)
.attr("stroke-width", d => init_diameter(d))
.attr("stroke", d => myColor(d.properties[diameter]))
.attr("fill", "none")
.attr("style", "pointer-events: auto;")
.on("mouseover", function(e, d) {
d3.select(e.target).attr('stroke-width', '5px');
map.map.dragging.disable();
map.tooltip.tooltipMouseover(e, d);
})
.on('mouseout', function(e, d) {
d3.select(e.target)
.attr('stroke-width', d => init_diameter(d));
map.map.dragging.enable();
map.tooltip.tooltipMouseout(e, d);
})
},
update => drawPipeSelection(update),
exit => exit.remove()
)
}
onZoom();
map.map.on('zoomend', onZoom);
map.map.on('moveend', onZoom);
}
map.colorInterpolateInteger = function(data, param) {
let all_data = data.map(d => d.properties[param])
let unique_data = all_data.filter((v, i, a) => a.indexOf(v) === i);
unique_data = unique_data.sort(function(a, b){return a - b})
if (param.substring(0, 4).toLowerCase() === 'diam')
unique_data = unique_data.filter(x => x > 5.9);
return d3.scaleOrdinal().domain(unique_data).range(d3.schemeYlGnBu[unique_data.length]);
}
map.colorInteger = function(param, data) {
let myColor = map.colorInterpolateInteger(data, param);
d3.select('#map' + map.map_id)
.selectAll('.pipe')
.attr("stroke", d => myColor(d['properties'][param]));
map.legendInteger(data, param);
}
return map;
}