2020-04-16 17:39:41 +02:00
|
|
|
import React, { useState, useRef, useEffect } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import mapboxgl from 'mapbox-gl';
|
|
|
|
import ReactMapboxGl, { GeoJSONLayer, ZoomControl } from 'react-mapbox-gl';
|
|
|
|
import DrawControl from 'react-mapbox-gl-draw';
|
|
|
|
import SwitchMapStyle from './SwitchMapStyle';
|
|
|
|
import SearchInput from './SearchInput';
|
2020-05-05 15:09:29 +02:00
|
|
|
import { getJSON, ajax } from '@utils';
|
2020-05-07 18:30:19 +02:00
|
|
|
import { gpx } from '@tmcw/togeojson/dist/togeojson.es.js';
|
2020-04-16 17:39:41 +02:00
|
|
|
import ortho from './styles/ortho.json';
|
|
|
|
import vector from './styles/vector.json';
|
2020-05-05 15:09:29 +02:00
|
|
|
import { polygonCadastresFill, polygonCadastresLine } from './utils';
|
2020-04-16 17:39:41 +02:00
|
|
|
import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css';
|
|
|
|
|
|
|
|
const Map = ReactMapboxGl({});
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
function filterFeatureCollection(featureCollection, source) {
|
|
|
|
return {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: featureCollection.features.filter(
|
2020-04-30 15:42:29 +02:00
|
|
|
(feature) => feature.properties.source === source
|
2020-05-05 15:09:29 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:36:25 +02:00
|
|
|
function noop() {}
|
|
|
|
|
|
|
|
function MapEditor({ featureCollection, url, preview }) {
|
2020-04-16 17:39:41 +02:00
|
|
|
const drawControl = useRef(null);
|
|
|
|
const [style, setStyle] = useState('ortho');
|
|
|
|
const [coords, setCoords] = useState([1.7, 46.9]);
|
|
|
|
const [zoom, setZoom] = useState([5]);
|
|
|
|
const [currentMap, setCurrentMap] = useState({});
|
2020-05-07 18:30:19 +02:00
|
|
|
const [bbox, setBbox] = useState(featureCollection.bbox);
|
2020-05-05 15:09:29 +02:00
|
|
|
const mapStyle = style === 'ortho' ? ortho : vector;
|
|
|
|
const cadastresFeatureCollection = filterFeatureCollection(
|
|
|
|
featureCollection,
|
|
|
|
'cadastre'
|
2020-04-16 17:39:41 +02:00
|
|
|
);
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
function updateFeaturesList(features) {
|
|
|
|
const cadastres = features.find(
|
|
|
|
({ geometry }) => geometry.type === 'Polygon'
|
|
|
|
);
|
|
|
|
ajax({ url, type: 'get', data: cadastres ? 'cadastres=update' : '' });
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onDrawCreate({ features }) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
updateFeaturesList(features);
|
|
|
|
}
|
2020-04-16 17:39:41 +02:00
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
async function onDrawUpdate({ features }) {
|
|
|
|
for (const feature of features) {
|
|
|
|
let { id } = feature.properties;
|
|
|
|
await getJSON(`${url}/${id}`, { feature }, 'patch');
|
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-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-04-30 15:42:29 +02:00
|
|
|
const 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-04-16 17:39:41 +02:00
|
|
|
};
|
|
|
|
|
2020-04-30 15:42:29 +02:00
|
|
|
const onCadastresUpdate = (evt) => {
|
2020-04-16 17:39:41 +02:00
|
|
|
if (currentMap) {
|
|
|
|
currentMap
|
|
|
|
.getSource('cadastres-layer')
|
2020-05-05 15:09:29 +02:00
|
|
|
.setData(
|
|
|
|
filterFeatureCollection(evt.detail.featureCollection, 'cadastre')
|
|
|
|
);
|
2020-04-16 17:39:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-30 15:42:29 +02:00
|
|
|
const onGpxImport = (e) => {
|
2020-05-07 18:30:19 +02:00
|
|
|
let reader = new FileReader();
|
|
|
|
reader.readAsText(e.target.files[0], 'UTF-8');
|
2020-04-30 15:42:29 +02:00
|
|
|
reader.onload = async (event) => {
|
2020-05-07 18:30:19 +02:00
|
|
|
const featureCollection = gpx(
|
|
|
|
new DOMParser().parseFromString(event.target.result, 'text/xml')
|
|
|
|
);
|
|
|
|
const resultFeatureCollection = await getJSON(
|
|
|
|
`${url}/import`,
|
|
|
|
featureCollection,
|
|
|
|
'post'
|
|
|
|
);
|
|
|
|
drawControl.current.draw.set(
|
|
|
|
filterFeatureCollection(
|
|
|
|
resultFeatureCollection,
|
|
|
|
'selection_utilisateur'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
updateFeaturesList(resultFeatureCollection.features);
|
|
|
|
setBbox(resultFeatureCollection.bbox);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-16 17:39:41 +02:00
|
|
|
useEffect(() => {
|
2020-05-05 15:09:29 +02:00
|
|
|
addEventListener('cadastres:update', onCadastresUpdate);
|
|
|
|
return () => removeEventListener('cadastres:update', onCadastresUpdate);
|
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 (
|
|
|
|
<>
|
2020-05-07 18:30:19 +02:00
|
|
|
<div className="file-import" style={{ marginBottom: '20px' }}>
|
|
|
|
<div>
|
|
|
|
<p style={{ fontWeight: 'bolder', marginBottom: '10px' }}>
|
|
|
|
Importer un fichier GPX
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<input type="file" accept=".gpx" onChange={onGpxImport} />
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-04-16 17:39:41 +02:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
marginBottom: '62px'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SearchInput
|
2020-04-30 15:42:29 +02:00
|
|
|
getCoords={(searchTerm) => {
|
2020-04-16 17:39:41 +02:00
|
|
|
setCoords(searchTerm);
|
|
|
|
setZoom([17]);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Map
|
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'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<GeoJSONLayer
|
|
|
|
id="cadastres-layer"
|
|
|
|
data={cadastresFeatureCollection}
|
|
|
|
fillPaint={polygonCadastresFill}
|
|
|
|
linePaint={polygonCadastresLine}
|
|
|
|
/>
|
|
|
|
<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
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
className="style-switch"
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
bottom: 0,
|
|
|
|
left: 0
|
|
|
|
}}
|
|
|
|
onClick={() =>
|
|
|
|
style === 'ortho' ? setStyle('vector') : setStyle('ortho')
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<SwitchMapStyle isVector={style === 'vector' ? true : false} />
|
|
|
|
</div>
|
|
|
|
<ZoomControl />
|
|
|
|
</Map>
|
|
|
|
</>
|
|
|
|
);
|
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,
|
|
|
|
preview: PropTypes.bool
|
2020-04-16 17:39:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MapEditor;
|