Carte Editor sends FeatureCollection to the server

This commit is contained in:
Paul Chavard 2020-04-14 10:24:30 +02:00
parent 99c8300c10
commit bc8217e030
8 changed files with 98 additions and 55 deletions

View file

@ -1,9 +1,10 @@
import L from 'leaflet';
import FreeDraw from 'leaflet-freedraw';
import area from '@turf/area';
import { fire, delegate } from '@utils';
import $ from 'jquery';
import polygonArea from './polygon_area';
import createFeatureCollection from './create-feature-collection';
const MAPS = new WeakMap();
@ -81,13 +82,18 @@ function drawUserSelectionEditor(map, { selection }) {
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) {
input.value = JSON.stringify(latLngs);
} else {
input.value = ERROR_GEO_JSON;
const featureCollection = createFeatureCollection(latLngs);
if (area(featureCollection) < 300000) {
input.value = JSON.stringify(featureCollection);
} else {
input.value = ERROR_GEO_JSON;
}
}
fire(input, 'change');
@ -121,7 +127,7 @@ function getCurrentMap(element) {
}
}
const EMPTY_GEO_JSON = '[]';
const EMPTY_GEO_JSON = '{ "type": "FeatureCollection", "features": [] }';
const ERROR_GEO_JSON = '';
function findInput(selector) {

View file

@ -1,16 +1,16 @@
import area from '@turf/area';
export default function polygonArea(latLngs) {
return area({
export default function createFeatureCollection(latLngs) {
return {
type: 'FeatureCollection',
features: latLngs.map(featurePolygonLatLngs)
});
};
}
function featurePolygonLatLngs(latLngs) {
return {
type: 'Feature',
properties: {},
properties: {
source: 'selection_utilisateur'
},
geometry: {
type: 'Polygon',
coordinates: [latLngs.map(({ lng, lat }) => [lng, lat])]