carto: refactor carto editor using new primitives
This commit is contained in:
parent
ad2c589e4d
commit
55b955f838
1 changed files with 45 additions and 141 deletions
|
@ -1,142 +1,69 @@
|
||||||
import L from 'leaflet';
|
import L from 'leaflet';
|
||||||
import area from '@turf/area';
|
|
||||||
import FreeDraw, { NONE, EDIT, CREATE, DELETE } from 'leaflet-freedraw';
|
import FreeDraw, { NONE, CREATE } from 'leaflet-freedraw';
|
||||||
import $ from 'jquery';
|
import { fire, on, getJSON } from '@utils';
|
||||||
|
|
||||||
import { getData } from '../shared/data';
|
import { getData } from '../shared/data';
|
||||||
import { DEFAULT_POSITION, LAT, LON } from '../shared/carto';
|
import { initMap } from '../shared/carto';
|
||||||
import { qpActive, displayQP, getQP } from './carto/qp';
|
|
||||||
import { cadastreActive, displayCadastre, getCadastre } from './carto/cadastre';
|
import polygonArea from './carto/polygon_area';
|
||||||
|
import drawFactory from './carto/draw';
|
||||||
|
|
||||||
function initialize() {
|
function initialize() {
|
||||||
if ($('#map').length > 0) {
|
if (document.getElementById('map')) {
|
||||||
getPosition(getData('carto').dossierId).then(
|
const data = getData('carto');
|
||||||
position => initializeWithPosition(position),
|
const position = data.position;
|
||||||
() => initializeWithPosition(DEFAULT_POSITION)
|
|
||||||
);
|
const map = initMap(position);
|
||||||
|
const freeDraw = new FreeDraw({
|
||||||
|
mode: NONE,
|
||||||
|
smoothFactor: 4,
|
||||||
|
mergePolygons: false
|
||||||
|
});
|
||||||
|
|
||||||
|
map.addLayer(freeDraw);
|
||||||
|
|
||||||
|
addEventFreeDraw(freeDraw);
|
||||||
|
addEventSearchAddress(map);
|
||||||
|
|
||||||
|
const cartoDrawZones = drawFactory(map, freeDraw);
|
||||||
|
window.DS = { cartoDrawZones };
|
||||||
|
|
||||||
|
cartoDrawZones(data);
|
||||||
|
|
||||||
|
if (freeDraw.polygons[0]) {
|
||||||
|
map.setZoom(18);
|
||||||
|
map.fitBounds(freeDraw.polygons[0].getBounds());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addEventListener('turbolinks:load', initialize);
|
addEventListener('turbolinks:load', initialize);
|
||||||
|
|
||||||
function initializeWithPosition(position) {
|
function addEventFreeDraw(freeDraw) {
|
||||||
const OSM = L.tileLayer(
|
|
||||||
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
||||||
{
|
|
||||||
attribution:
|
|
||||||
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const map = L.map('map', {
|
|
||||||
center: new L.LatLng(position.lat, position.lon),
|
|
||||||
zoom: position.zoom,
|
|
||||||
layers: [OSM],
|
|
||||||
scrollWheelZoom: false
|
|
||||||
});
|
|
||||||
|
|
||||||
if (qpActive()) {
|
|
||||||
displayQP(map, getJsonValue('#quartier_prioritaires'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cadastreActive()) {
|
|
||||||
displayCadastre(map, getJsonValue('#cadastres'));
|
|
||||||
}
|
|
||||||
|
|
||||||
const freeDraw = new FreeDraw({
|
|
||||||
mode: NONE,
|
|
||||||
smoothFactor: 4,
|
|
||||||
mergePolygons: false
|
|
||||||
});
|
|
||||||
|
|
||||||
map.addLayer(freeDraw);
|
|
||||||
|
|
||||||
const latLngs = getJsonValue('#json_latlngs');
|
|
||||||
|
|
||||||
if (latLngs.length) {
|
|
||||||
map.setZoom(18);
|
|
||||||
|
|
||||||
for (let polygon of latLngs) {
|
|
||||||
freeDraw.createPolygon(polygon);
|
|
||||||
}
|
|
||||||
|
|
||||||
map.fitBounds(freeDraw.polygons[0].getBounds());
|
|
||||||
} else if (position.lat == LAT && position.lon == LON) {
|
|
||||||
map.setView(new L.LatLng(position.lat, position.lon), position.zoom);
|
|
||||||
}
|
|
||||||
|
|
||||||
addEventFreeDraw(map, freeDraw);
|
|
||||||
addEventSearchAddress(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getExternalData(map, latLngs) {
|
|
||||||
const { dossierId } = getData('carto');
|
|
||||||
|
|
||||||
if (qpActive()) {
|
|
||||||
getQP(dossierId, latLngs).then(qps => displayQP(map, qps));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cadastreActive()) {
|
|
||||||
const polygons = { type: 'FeatureCollection', features: [] };
|
|
||||||
|
|
||||||
for (let i = 0; i < latLngs.length; i++) {
|
|
||||||
polygons.features.push(featurePolygonLatLngs(latLngs[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (area(polygons) < 300000) {
|
|
||||||
getCadastre(dossierId, latLngs).then(cadastres =>
|
|
||||||
displayCadastre(map, cadastres)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
displayCadastre(map, [{ zoom_error: true }]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function featurePolygonLatLngs(coordinates) {
|
|
||||||
return {
|
|
||||||
type: 'Feature',
|
|
||||||
properties: {},
|
|
||||||
geometry: {
|
|
||||||
type: 'Polygon',
|
|
||||||
coordinates: [JSON.parse(getJsonPolygons([coordinates]))['latLngs']]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function addEventFreeDraw(map, freeDraw) {
|
|
||||||
freeDraw.on('markers', ({ latLngs }) => {
|
freeDraw.on('markers', ({ latLngs }) => {
|
||||||
$('#json_latlngs').val(JSON.stringify(latLngs));
|
const input = document.querySelector('input[name=selection]');
|
||||||
|
|
||||||
addEventEdit(freeDraw);
|
if (polygonArea(latLngs) < 300000) {
|
||||||
|
input.value = JSON.stringify(latLngs);
|
||||||
|
} else {
|
||||||
|
input.value = '{ "error": "TooManyPolygons" }';
|
||||||
|
}
|
||||||
|
|
||||||
getExternalData(map, latLngs);
|
fire(input, 'change');
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#map').on('click', () => {
|
on('#map', 'click', () => {
|
||||||
freeDraw.mode(NONE);
|
freeDraw.mode(NONE);
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#new').on('click', () => {
|
on('#new', 'click', () => {
|
||||||
freeDraw.mode(CREATE);
|
freeDraw.mode(CREATE);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function addEventEdit(freeDraw) {
|
|
||||||
$('.leaflet-container svg').removeAttr('pointer-events');
|
|
||||||
$('.leaflet-container g path').on('click', () => {
|
|
||||||
setTimeout(function() {
|
|
||||||
freeDraw.mode(EDIT | DELETE);
|
|
||||||
}, 50);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPosition(dossierId) {
|
|
||||||
return $.getJSON(`/users/dossiers/${dossierId}/carte/position`);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAddressPoint(map, request) {
|
function getAddressPoint(map, request) {
|
||||||
$.get('/ban/address_point', { request }).then(data => {
|
getJSON('/ban/address_point', { request }).then(data => {
|
||||||
if (data.lat !== null) {
|
if (data.lat !== null) {
|
||||||
map.setView(new L.LatLng(data.lat, data.lon), data.zoom);
|
map.setView(new L.LatLng(data.lat, data.lon), data.zoom);
|
||||||
}
|
}
|
||||||
|
@ -144,34 +71,11 @@ function getAddressPoint(map, request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function addEventSearchAddress(map) {
|
function addEventSearchAddress(map) {
|
||||||
$("#search-by-address input[type='address']").on(
|
on(
|
||||||
|
'#search-by-address input[type=address]',
|
||||||
'autocomplete:select',
|
'autocomplete:select',
|
||||||
(_, seggestion) => {
|
(_, seggestion) => {
|
||||||
getAddressPoint(map, seggestion['label']);
|
getAddressPoint(map, seggestion['label']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getJsonValue(selector) {
|
|
||||||
let data = document.querySelector(selector).value;
|
|
||||||
if (data && data !== '[]') {
|
|
||||||
return JSON.parse(data);
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
function getJsonPolygons(latLngGroups) {
|
|
||||||
var groups = [];
|
|
||||||
|
|
||||||
latLngGroups.forEach(function forEach(latLngs) {
|
|
||||||
var group = [];
|
|
||||||
|
|
||||||
latLngs.forEach(function forEach(latLng) {
|
|
||||||
group.push('[' + latLng.lng + ', ' + latLng.lat + ']');
|
|
||||||
});
|
|
||||||
|
|
||||||
groups.push('{ "latLngs": [' + group.join(', ') + '] }');
|
|
||||||
});
|
|
||||||
|
|
||||||
return groups;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue