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

215 lines
4.8 KiB
JavaScript
Raw Normal View History

import L from 'leaflet';
2018-10-16 10:49:00 +02:00
import FreeDraw, { NONE, EDIT, DELETE } from 'leaflet-freedraw';
import { fire, getJSON, delegate } from '@utils';
import polygonArea from './polygon_area';
2018-10-03 10:39:35 +02:00
const LAYERS = {};
2018-10-16 10:49:00 +02:00
const MAPS = new WeakMap();
export function initMap(element, position, editable = false) {
if (MAPS.has(element)) {
return MAPS.get(element);
} else {
const map = L.map(element, {
scrollWheelZoom: false
}).setView([position.lat, position.lon], editable ? 18 : position.zoom);
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);
if (editable) {
const freeDraw = new FreeDraw({
mode: NONE,
smoothFactor: 4,
mergePolygons: false
});
map.addLayer(freeDraw);
map.freeDraw = freeDraw;
}
2018-10-16 10:49:00 +02:00
MAPS.set(element, map);
return map;
}
}
2018-10-13 10:33:56 +02:00
2018-10-16 10:49:00 +02:00
export function drawCadastre(map, { cadastres }, editable = false) {
drawLayer(
map,
cadastres,
editable ? CADASTRE_POLYGON_STYLE : noEditStyle(CADASTRE_POLYGON_STYLE),
'cadastres'
);
}
2018-10-13 10:33:56 +02:00
2018-10-16 10:49:00 +02:00
export function drawQuartiersPrioritaires(
map,
{ quartiersPrioritaires },
editable = false
) {
drawLayer(
map,
quartiersPrioritaires,
editable ? QP_POLYGON_STYLE : noEditStyle(QP_POLYGON_STYLE),
'quartiersPrioritaires'
);
}
2018-10-23 15:38:20 +02:00
export function drawParcellesAgricoles(
map,
{ parcellesAgricoles },
editable = false
) {
drawLayer(
map,
parcellesAgricoles,
editable ? RPG_POLYGON_STYLE : noEditStyle(RPG_POLYGON_STYLE),
'parcellesAgricoles'
);
}
2018-10-16 10:49:00 +02:00
export function drawUserSelection(map, { selection }, editable = false) {
2018-11-30 13:19:19 +01:00
if (selection) {
const coordinates = toLatLngs(selection);
2018-10-16 10:49:00 +02:00
2018-11-30 13:19:19 +01:00
if (editable) {
coordinates.forEach(polygon => map.freeDraw.create(polygon));
const polygon = map.freeDraw.all()[0];
2018-10-16 10:49:00 +02:00
if (polygon) {
map.fitBounds(polygon.getBounds());
}
2018-11-30 13:19:19 +01:00
} else {
const polygon = L.polygon(coordinates, {
color: 'red',
zIndex: 3
}).addTo(map);
2018-10-16 10:49:00 +02:00
2018-11-30 13:19:19 +01:00
map.fitBounds(polygon.getBounds());
}
2018-10-16 10:49:00 +02:00
}
}
export function geocodeAddress(map, query) {
getJSON('/address/geocode', { request: query }).then(data => {
if (data.lat !== null) {
map.setView(new L.LatLng(data.lat, data.lon), data.zoom);
}
});
2018-10-13 10:33:56 +02:00
}
2018-10-16 10:49:00 +02:00
export function getCurrentMap(input) {
let element = input.closest('.toolbar').parentElement.querySelector('.carte');
if (MAPS.has(element)) {
return MAPS.get(element);
}
}
const EMPTY_GEO_JSON = '[]';
const ERROR_GEO_JSON = '';
2018-10-16 10:49:00 +02:00
export function addFreeDrawEvents(map, selector) {
const input = findInput(selector);
map.freeDraw.on('markers', ({ latLngs }) => {
if (latLngs.length === 0) {
input.value = EMPTY_GEO_JSON;
} else if (polygonArea(latLngs) < 300000) {
2018-10-16 10:49:00 +02:00
input.value = JSON.stringify(latLngs);
} else {
input.value = ERROR_GEO_JSON;
2018-10-16 10:49:00 +02:00
}
fire(input, 'change');
});
}
2018-11-30 13:19:19 +01:00
function toLatLngs({ coordinates }) {
return coordinates.map(polygon =>
polygon[0].map(point => ({ lng: point[0], lat: point[1] }))
);
}
2018-10-16 10:49:00 +02:00
function findInput(selector) {
return typeof selector === 'string'
? document.querySelector(selector)
: selector;
}
function createLayer(map, layerName) {
const layer = (LAYERS[layerName] = new L.GeoJSON(undefined, {
interactive: false
}));
layer.addTo(map);
return layer;
}
function removeLayer(map, layerName) {
const layer = LAYERS[layerName];
if (layer) {
delete LAYERS[layerName];
map.removeLayer(layer);
}
}
function drawLayer(map, data, style, layerName = 'default') {
2018-10-13 10:33:23 +02:00
removeLayer(map, layerName);
if (Array.isArray(data) && data.length > 0) {
const layer = createLayer(map, layerName);
data.forEach(function(item) {
layer.addData(item.geometry);
});
layer.setStyle(style).addTo(map);
}
}
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'
});
2018-10-16 10:49:00 +02:00
delegate('click', '.carte.edit', event => {
let element = event.target;
let isPath = element.matches('.leaflet-container g path');
let map = element.matches('.carte') ? element : element.closest('.carte');
let freeDraw = MAPS.has(map) ? MAPS.get(map).freeDraw : null;
2018-10-16 10:49:00 +02:00
if (freeDraw) {
if (isPath) {
setTimeout(() => {
freeDraw.mode(EDIT | DELETE);
}, 50);
} else {
freeDraw.mode(NONE);
}
}
2018-10-16 10:49:00 +02:00
});