2021-03-11 12:09:27 +01:00
|
|
|
|
import React, {
|
|
|
|
|
useState,
|
|
|
|
|
useCallback,
|
|
|
|
|
useRef,
|
|
|
|
|
useMemo,
|
|
|
|
|
useEffect
|
|
|
|
|
} from 'react';
|
2020-04-16 17:39:41 +02:00
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import mapboxgl from 'mapbox-gl';
|
2020-10-15 17:22:08 +02:00
|
|
|
|
import { GeoJSONLayer, ZoomControl } from 'react-mapbox-gl';
|
2020-04-16 17:39:41 +02:00
|
|
|
|
import DrawControl from 'react-mapbox-gl-draw';
|
|
|
|
|
import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css';
|
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
|
import { getJSON, ajax, fire } from '@utils';
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2020-10-15 17:22:08 +02:00
|
|
|
|
import Mapbox from '../shared/mapbox/Mapbox';
|
|
|
|
|
import { getMapStyle } from '../shared/mapbox/styles';
|
|
|
|
|
import SwitchMapStyle from '../shared/mapbox/SwitchMapStyle';
|
2021-03-11 12:09:27 +01:00
|
|
|
|
import { FlashMessage } from '../shared/FlashMessage';
|
2020-06-09 17:23:24 +02:00
|
|
|
|
|
2020-10-07 17:44:04 +02:00
|
|
|
|
import ComboAdresseSearch from '../ComboAdresseSearch';
|
2020-09-20 13:19:09 +02:00
|
|
|
|
import {
|
|
|
|
|
polygonCadastresFill,
|
|
|
|
|
polygonCadastresLine,
|
|
|
|
|
readGeoFile
|
|
|
|
|
} from './utils';
|
2020-06-09 17:23:24 +02:00
|
|
|
|
import {
|
|
|
|
|
noop,
|
|
|
|
|
filterFeatureCollection,
|
|
|
|
|
fitBounds,
|
|
|
|
|
generateId,
|
|
|
|
|
useEvent,
|
|
|
|
|
findFeature
|
2020-10-15 17:22:08 +02:00
|
|
|
|
} from '../shared/mapbox/utils';
|
2020-05-14 13:36:25 +02:00
|
|
|
|
|
2020-09-02 12:56:45 +02:00
|
|
|
|
function MapEditor({ featureCollection, url, preview, options }) {
|
2020-04-16 17:39:41 +02:00
|
|
|
|
const drawControl = useRef(null);
|
2020-06-09 17:23:24 +02:00
|
|
|
|
const [currentMap, setCurrentMap] = useState(null);
|
|
|
|
|
|
2021-03-11 12:09:27 +01:00
|
|
|
|
const [errorMessage, setErrorMessage] = useState();
|
2020-04-16 17:39:41 +02:00
|
|
|
|
const [style, setStyle] = useState('ortho');
|
|
|
|
|
const [coords, setCoords] = useState([1.7, 46.9]);
|
|
|
|
|
const [zoom, setZoom] = useState([5]);
|
2020-05-07 18:30:19 +02:00
|
|
|
|
const [bbox, setBbox] = useState(featureCollection.bbox);
|
2020-05-15 15:26:49 +02:00
|
|
|
|
const [importInputs, setImportInputs] = useState([]);
|
2020-06-09 17:23:24 +02:00
|
|
|
|
const [cadastresFeatureCollection, setCadastresFeatureCollection] = useState(
|
|
|
|
|
filterFeatureCollection(featureCollection, 'cadastre')
|
|
|
|
|
);
|
2020-10-15 17:22:08 +02:00
|
|
|
|
const mapStyle = useMemo(() => getMapStyle(style, options.layers), [
|
|
|
|
|
style,
|
|
|
|
|
options
|
|
|
|
|
]);
|
|
|
|
|
const hasCadastres = useMemo(() => options.layers.includes('cadastres'));
|
2020-06-09 17:23:24 +02:00
|
|
|
|
|
2021-03-11 12:09:27 +01:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
const timer = setTimeout(() => setErrorMessage(null), 5000);
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, [errorMessage]);
|
|
|
|
|
|
2020-09-01 16:41:02 +02:00
|
|
|
|
const translations = [
|
|
|
|
|
['.mapbox-gl-draw_line', 'Tracer une ligne'],
|
|
|
|
|
['.mapbox-gl-draw_polygon', 'Dessiner un polygone'],
|
|
|
|
|
['.mapbox-gl-draw_point', 'Ajouter un point'],
|
|
|
|
|
['.mapbox-gl-draw_trash', 'Supprimer']
|
|
|
|
|
];
|
|
|
|
|
for (const [selector, translation] of translations) {
|
|
|
|
|
const element = document.querySelector(selector);
|
|
|
|
|
if (element) {
|
|
|
|
|
element.setAttribute('title', translation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
|
const onFeatureFocus = useCallback(
|
|
|
|
|
({ detail }) => {
|
|
|
|
|
const { id } = detail;
|
|
|
|
|
const featureCollection = drawControl.current.draw.getAll();
|
|
|
|
|
const feature = findFeature(featureCollection, id);
|
|
|
|
|
if (feature) {
|
|
|
|
|
fitBounds(currentMap, feature);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[currentMap, drawControl.current]
|
|
|
|
|
);
|
2020-05-29 12:41:56 +02:00
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
|
const onFeatureUpdate = useCallback(
|
|
|
|
|
async ({ detail }) => {
|
|
|
|
|
const { id, properties } = detail;
|
|
|
|
|
const featureCollection = drawControl.current.draw.getAll();
|
|
|
|
|
const feature = findFeature(featureCollection, id);
|
2020-05-29 12:41:56 +02:00
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
|
if (feature) {
|
|
|
|
|
getJSON(`${url}/${id}`, { feature: { properties } }, 'patch');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[url, drawControl.current]
|
2020-04-16 17:39:41 +02:00
|
|
|
|
);
|
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
|
const updateFeaturesList = useCallback(
|
|
|
|
|
async (features) => {
|
|
|
|
|
const cadastres = features.find(
|
|
|
|
|
({ geometry }) => geometry.type === 'Polygon'
|
|
|
|
|
);
|
|
|
|
|
await ajax({
|
|
|
|
|
url,
|
|
|
|
|
type: 'get',
|
|
|
|
|
data: cadastres ? 'cadastres=update' : ''
|
|
|
|
|
});
|
|
|
|
|
fire(document, 'ds:page:update');
|
|
|
|
|
},
|
|
|
|
|
[url]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onCadastresUpdate = useCallback(({ detail }) => {
|
|
|
|
|
setCadastresFeatureCollection(
|
|
|
|
|
filterFeatureCollection(detail.featureCollection, 'cadastre')
|
2020-05-05 15:09:29 +02:00
|
|
|
|
);
|
2020-06-09 17:23:24 +02:00
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEvent('map:feature:focus', onFeatureFocus);
|
|
|
|
|
useEvent('map:feature:update', onFeatureUpdate);
|
|
|
|
|
useEvent('cadastres:update', onCadastresUpdate);
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
|
function setFeatureId(lid, feature) {
|
|
|
|
|
const draw = drawControl.current.draw;
|
|
|
|
|
draw.setFeatureProperty(lid, 'id', feature.properties.id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
|
function updateImportInputs(inputs, inputId) {
|
2020-05-15 15:26:49 +02:00
|
|
|
|
const updatedInputs = inputs.filter((input) => input.id !== inputId);
|
|
|
|
|
setImportInputs(updatedInputs);
|
2020-06-09 17:23:24 +02:00
|
|
|
|
}
|
2020-05-15 15:26:49 +02:00
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
|
async function onDrawCreate({ features }) {
|
2021-03-11 12:09:27 +01:00
|
|
|
|
try {
|
|
|
|
|
for (const feature of features) {
|
|
|
|
|
const data = await getJSON(url, { feature }, 'post');
|
|
|
|
|
setFeatureId(feature.id, data.feature);
|
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2021-03-11 12:09:27 +01:00
|
|
|
|
updateFeaturesList(features);
|
|
|
|
|
} catch {
|
|
|
|
|
setErrorMessage('Le polygone dessiné n’est pas valide.');
|
|
|
|
|
}
|
2020-05-05 15:09:29 +02:00
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
|
async function onDrawUpdate({ features }) {
|
2021-03-11 12:09:27 +01:00
|
|
|
|
try {
|
|
|
|
|
for (const feature of features) {
|
|
|
|
|
const { id } = feature.properties;
|
|
|
|
|
if (id) {
|
|
|
|
|
await getJSON(`${url}/${id}`, { feature }, 'patch');
|
|
|
|
|
} else {
|
|
|
|
|
const data = await getJSON(url, { feature }, 'post');
|
|
|
|
|
setFeatureId(feature.id, data.feature);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2021-03-11 12:09:27 +01:00
|
|
|
|
updateFeaturesList(features);
|
|
|
|
|
} catch {
|
|
|
|
|
setErrorMessage('Le polygone dessiné n’est pas valide.');
|
|
|
|
|
}
|
2020-05-05 15:09:29 +02:00
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
|
async function onDrawDelete({ features }) {
|
|
|
|
|
for (const feature of features) {
|
|
|
|
|
const { id } = feature.properties;
|
|
|
|
|
await getJSON(`${url}/${id}`, null, 'delete');
|
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
|
updateFeaturesList(features);
|
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
|
function onMapLoad(map) {
|
2020-04-16 17:39:41 +02:00
|
|
|
|
setCurrentMap(map);
|
2020-05-05 15:09:29 +02:00
|
|
|
|
|
|
|
|
|
drawControl.current.draw.set(
|
|
|
|
|
filterFeatureCollection(featureCollection, 'selection_utilisateur')
|
|
|
|
|
);
|
2020-06-09 17:23:24 +02:00
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
2021-03-11 12:09:27 +01:00
|
|
|
|
const onFileImport = async (e, inputId) => {
|
|
|
|
|
try {
|
|
|
|
|
const featureCollection = await readGeoFile(e.target.files[0]);
|
2020-05-07 18:30:19 +02:00
|
|
|
|
const resultFeatureCollection = await getJSON(
|
|
|
|
|
`${url}/import`,
|
|
|
|
|
featureCollection,
|
|
|
|
|
'post'
|
|
|
|
|
);
|
2020-05-15 15:26:49 +02:00
|
|
|
|
let inputs = [...importInputs];
|
|
|
|
|
const setInputs = inputs.map((input) => {
|
|
|
|
|
if (input.id === inputId) {
|
|
|
|
|
input.disabled = true;
|
|
|
|
|
input.hasValue = true;
|
2020-06-18 16:28:46 +02:00
|
|
|
|
resultFeatureCollection.features.forEach((resultFeature) => {
|
|
|
|
|
featureCollection.features.forEach((feature) => {
|
|
|
|
|
if (
|
|
|
|
|
JSON.stringify(resultFeature.geometry) ===
|
|
|
|
|
JSON.stringify(feature.geometry)
|
|
|
|
|
) {
|
|
|
|
|
input.featureIds.push(resultFeature.properties.id);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-05-15 15:26:49 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return input;
|
|
|
|
|
});
|
|
|
|
|
|
2020-05-07 18:30:19 +02:00
|
|
|
|
drawControl.current.draw.set(
|
|
|
|
|
filterFeatureCollection(
|
|
|
|
|
resultFeatureCollection,
|
|
|
|
|
'selection_utilisateur'
|
|
|
|
|
)
|
|
|
|
|
);
|
2020-05-15 15:26:49 +02:00
|
|
|
|
|
2020-05-07 18:30:19 +02:00
|
|
|
|
updateFeaturesList(resultFeatureCollection.features);
|
2020-05-15 15:26:49 +02:00
|
|
|
|
setImportInputs(setInputs);
|
2020-05-07 18:30:19 +02:00
|
|
|
|
setBbox(resultFeatureCollection.bbox);
|
2021-03-11 12:09:27 +01:00
|
|
|
|
} catch {
|
|
|
|
|
setErrorMessage('Le fichier importé contient des polygones invalides.');
|
|
|
|
|
}
|
2020-05-07 18:30:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-15 15:26:49 +02:00
|
|
|
|
const addInputFile = (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
let inputs = [...importInputs];
|
|
|
|
|
inputs.push({
|
|
|
|
|
id: generateId(),
|
|
|
|
|
disabled: false,
|
2020-06-18 16:28:46 +02:00
|
|
|
|
featureIds: [],
|
2020-05-15 15:26:49 +02:00
|
|
|
|
hasValue: false
|
|
|
|
|
});
|
|
|
|
|
setImportInputs(inputs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const removeInputFile = async (e, inputId) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const draw = drawControl.current.draw;
|
|
|
|
|
const featureCollection = draw.getAll();
|
|
|
|
|
let inputs = [...importInputs];
|
|
|
|
|
const inputToRemove = inputs.find((input) => input.id === inputId);
|
|
|
|
|
|
|
|
|
|
for (const feature of featureCollection.features) {
|
2020-06-18 16:28:46 +02:00
|
|
|
|
if (inputToRemove.featureIds.includes(feature.properties.id)) {
|
2020-05-29 12:41:56 +02:00
|
|
|
|
const featureToRemove = draw.get(feature.id);
|
2020-06-18 16:28:46 +02:00
|
|
|
|
await getJSON(`${url}/${feature.properties.id}`, null, 'delete');
|
2020-05-29 12:41:56 +02:00
|
|
|
|
draw.delete(feature.id).getAll();
|
|
|
|
|
updateFeaturesList([featureToRemove]);
|
2020-05-15 15:26:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
updateImportInputs(inputs, inputId);
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-16 17:39:41 +02:00
|
|
|
|
if (!mapboxgl.supported()) {
|
|
|
|
|
return (
|
|
|
|
|
<p>
|
|
|
|
|
Nous ne pouvons pas afficher notre éditeur de carte car il est
|
|
|
|
|
imcompatible avec votre navigateur. Nous vous conseillons de le mettre à
|
|
|
|
|
jour ou utiliser les dernières versions de Chrome, Firefox ou Safari
|
|
|
|
|
</p>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2021-03-11 12:09:27 +01:00
|
|
|
|
{errorMessage && (
|
|
|
|
|
<FlashMessage message={errorMessage} level="alert" fixed={true} />
|
|
|
|
|
)}
|
2020-05-28 17:43:04 +02:00
|
|
|
|
<div>
|
|
|
|
|
<p style={{ marginBottom: '20px' }}>
|
|
|
|
|
Besoin d'aide ?
|
|
|
|
|
<a
|
|
|
|
|
href="https://doc.demarches-simplifiees.fr/pour-aller-plus-loin/cartographie"
|
|
|
|
|
target="_blank"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
>
|
|
|
|
|
consulter les tutoriels video
|
|
|
|
|
</a>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2020-05-07 18:30:19 +02:00
|
|
|
|
<div className="file-import" style={{ marginBottom: '20px' }}>
|
2020-05-15 15:26:49 +02:00
|
|
|
|
<button className="button send primary" onClick={addInputFile}>
|
|
|
|
|
Ajouter un fichier GPX ou KML
|
|
|
|
|
</button>
|
2020-05-07 18:30:19 +02:00
|
|
|
|
<div>
|
2020-05-15 15:26:49 +02:00
|
|
|
|
{importInputs.map((input) => (
|
|
|
|
|
<div key={input.id}>
|
|
|
|
|
<input
|
|
|
|
|
title="Choisir un fichier gpx ou kml"
|
|
|
|
|
style={{ marginTop: '15px' }}
|
|
|
|
|
id={input.id}
|
|
|
|
|
type="file"
|
|
|
|
|
accept=".gpx, .kml"
|
|
|
|
|
disabled={input.disabled}
|
|
|
|
|
onChange={(e) => onFileImport(e, input.id)}
|
|
|
|
|
/>
|
|
|
|
|
{input.hasValue && (
|
|
|
|
|
<span
|
|
|
|
|
title="Supprimer le fichier"
|
|
|
|
|
className="icon refuse"
|
|
|
|
|
style={{
|
|
|
|
|
cursor: 'pointer'
|
|
|
|
|
}}
|
|
|
|
|
onClick={(e) => removeInputFile(e, input.id)}
|
|
|
|
|
></span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
2020-05-07 18:30:19 +02:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-04-16 17:39:41 +02:00
|
|
|
|
<div
|
|
|
|
|
style={{
|
2020-05-28 17:43:04 +02:00
|
|
|
|
marginBottom: '50px'
|
2020-04-16 17:39:41 +02:00
|
|
|
|
}}
|
|
|
|
|
>
|
2020-10-07 17:44:04 +02:00
|
|
|
|
<ComboAdresseSearch
|
|
|
|
|
placeholder="Rechercher une adresse : saisissez au moins 2 caractères"
|
|
|
|
|
allowInputValues={false}
|
|
|
|
|
onChange={(_, { geometry: { coordinates } }) => {
|
|
|
|
|
setCoords(coordinates);
|
2020-04-16 17:39:41 +02:00
|
|
|
|
setZoom([17]);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2020-10-15 17:22:08 +02:00
|
|
|
|
<Mapbox
|
2020-04-30 15:42:29 +02:00
|
|
|
|
onStyleLoad={(map) => onMapLoad(map)}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
fitBounds={bbox}
|
|
|
|
|
fitBoundsOptions={{ padding: 100 }}
|
|
|
|
|
center={coords}
|
|
|
|
|
zoom={zoom}
|
|
|
|
|
style={mapStyle}
|
|
|
|
|
containerStyle={{
|
|
|
|
|
height: '500px'
|
|
|
|
|
}}
|
|
|
|
|
>
|
2020-10-15 17:22:08 +02:00
|
|
|
|
{hasCadastres ? (
|
2020-07-29 15:27:08 +02:00
|
|
|
|
<GeoJSONLayer
|
|
|
|
|
data={cadastresFeatureCollection}
|
|
|
|
|
fillPaint={polygonCadastresFill}
|
|
|
|
|
linePaint={polygonCadastresLine}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
<DrawControl
|
|
|
|
|
ref={drawControl}
|
2020-05-14 13:36:25 +02:00
|
|
|
|
onDrawCreate={preview ? noop : onDrawCreate}
|
|
|
|
|
onDrawUpdate={preview ? noop : onDrawUpdate}
|
|
|
|
|
onDrawDelete={preview ? noop : onDrawDelete}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
displayControlsDefault={false}
|
|
|
|
|
controls={{
|
|
|
|
|
point: true,
|
|
|
|
|
line_string: true,
|
|
|
|
|
polygon: true,
|
|
|
|
|
trash: true
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2020-09-02 12:56:45 +02:00
|
|
|
|
<SwitchMapStyle style={style} setStyle={setStyle} ign={options.ign} />
|
2020-04-16 17:39:41 +02:00
|
|
|
|
<ZoomControl />
|
2020-10-15 17:22:08 +02:00
|
|
|
|
</Mapbox>
|
2020-04-16 17:39:41 +02:00
|
|
|
|
</>
|
|
|
|
|
);
|
2020-05-14 13:36:25 +02:00
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
|
|
|
|
|
MapEditor.propTypes = {
|
|
|
|
|
featureCollection: PropTypes.shape({
|
|
|
|
|
bbox: PropTypes.array,
|
|
|
|
|
features: PropTypes.array,
|
|
|
|
|
id: PropTypes.number
|
2020-05-05 15:09:29 +02:00
|
|
|
|
}),
|
2020-05-14 13:36:25 +02:00
|
|
|
|
url: PropTypes.string,
|
2020-05-29 12:41:56 +02:00
|
|
|
|
preview: PropTypes.bool,
|
2020-09-02 12:56:45 +02:00
|
|
|
|
options: PropTypes.shape({
|
2020-10-15 17:22:08 +02:00
|
|
|
|
layers: PropTypes.array,
|
2020-09-02 12:56:45 +02:00
|
|
|
|
ign: PropTypes.bool
|
|
|
|
|
})
|
2020-04-16 17:39:41 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default MapEditor;
|