demarches-normaliennes/app/javascript/shared/carte.js

105 lines
2.3 KiB
JavaScript
Raw Normal View History

import L from 'leaflet';
const MAPS = new WeakMap();
export function drawMap(element, data) {
const map = initMap(element, data);
drawCadastre(map, data);
drawQuartiersPrioritaires(map, data);
drawParcellesAgricoles(map, data);
drawUserSelection(map, data);
}
function initMap(element, { position }) {
2018-10-16 10:49:00 +02:00
if (MAPS.has(element)) {
return MAPS.get(element);
} else {
const map = L.map(element, {
scrollWheelZoom: false
}).setView([position.lat, position.lon], position.zoom);
2018-10-16 10:49:00 +02:00
const loadTilesLayer = process.env.RAILS_ENV != 'test';
if (loadTilesLayer) {
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}
2018-10-16 10:49:00 +02:00
MAPS.set(element, map);
return map;
}
}
2018-10-13 10:33:56 +02:00
function drawUserSelection(map, { selection }) {
2019-04-25 16:30:51 +02:00
if (selection) {
const layer = L.geoJSON(selection, {
style: USER_SELECTION_POLYGON_STYLE
});
layer.addTo(map);
map.fitBounds(layer.getBounds());
2018-10-16 10:49:00 +02:00
}
}
function drawCadastre(map, { cadastres }) {
drawLayer(map, cadastres, noEditStyle(CADASTRE_POLYGON_STYLE));
2018-10-16 10:49:00 +02:00
}
function drawQuartiersPrioritaires(map, { quartiersPrioritaires }) {
drawLayer(map, quartiersPrioritaires, noEditStyle(QP_POLYGON_STYLE));
2018-10-16 10:49:00 +02:00
}
function drawParcellesAgricoles(map, { parcellesAgricoles }) {
drawLayer(map, parcellesAgricoles, noEditStyle(RPG_POLYGON_STYLE));
2019-04-25 16:30:51 +02:00
}
2018-10-13 10:33:23 +02:00
2019-04-25 16:30:51 +02:00
function drawLayer(map, data, style) {
2018-10-13 10:33:23 +02:00
if (Array.isArray(data) && data.length > 0) {
const layer = new L.GeoJSON(undefined, {
interactive: false,
style
});
for (let { geometry } of data) {
layer.addData(geometry);
}
layer.addTo(map);
2018-10-13 10:33:23 +02:00
}
}
2018-10-16 10:49:00 +02:00
function noEditStyle(style) {
2018-10-13 10:34:26 +02:00
return Object.assign({}, style, {
opacity: 0.7,
fillOpacity: 0.5,
color: style.fillColor
});
}
const POLYGON_STYLE = {
weight: 2,
opacity: 0.3,
color: 'white',
dashArray: '3',
fillOpacity: 0.7
};
2018-10-16 10:49:00 +02:00
const CADASTRE_POLYGON_STYLE = Object.assign({}, POLYGON_STYLE, {
2018-10-13 10:34:26 +02:00
fillColor: '#8a6d3b'
});
2018-10-16 10:49:00 +02:00
const QP_POLYGON_STYLE = Object.assign({}, POLYGON_STYLE, {
2018-10-13 10:34:26 +02:00
fillColor: '#31708f'
});
2018-10-23 15:38:20 +02:00
const RPG_POLYGON_STYLE = Object.assign({}, POLYGON_STYLE, {
fillColor: '#31708f'
});
const USER_SELECTION_POLYGON_STYLE = {
color: 'red'
};