2020-06-09 17:23:24 +02:00
|
|
|
import React, { useState, useCallback, useMemo } from 'react';
|
2020-04-07 18:17:30 +02:00
|
|
|
import ReactMapboxGl, { ZoomControl, GeoJSONLayer } from 'react-mapbox-gl';
|
2020-06-09 17:23:24 +02:00
|
|
|
import mapboxgl, { Popup } from 'mapbox-gl';
|
2020-04-07 18:17:30 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2020-07-29 15:27:08 +02:00
|
|
|
import { getMapStyle, SwitchMapStyle } from '../MapStyles';
|
2020-06-09 17:23:24 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
filterFeatureCollection,
|
|
|
|
filterFeatureCollectionByGeometryType,
|
|
|
|
useEvent,
|
|
|
|
findFeature,
|
|
|
|
fitBounds,
|
|
|
|
getCenter
|
|
|
|
} from '../shared/map';
|
|
|
|
|
2020-04-07 18:17:30 +02:00
|
|
|
const Map = ReactMapboxGl({});
|
|
|
|
|
2020-09-02 12:56:45 +02:00
|
|
|
const MapReader = ({ featureCollection, options }) => {
|
2020-06-09 17:23:24 +02:00
|
|
|
const [currentMap, setCurrentMap] = useState(null);
|
2020-05-28 17:43:04 +02:00
|
|
|
const [style, setStyle] = useState('ortho');
|
2020-06-09 17:23:24 +02:00
|
|
|
const cadastresFeatureCollection = useMemo(
|
|
|
|
() => filterFeatureCollection(featureCollection, 'cadastre'),
|
|
|
|
[featureCollection]
|
|
|
|
);
|
|
|
|
const selectionsUtilisateurFeatureCollection = useMemo(
|
|
|
|
() => filterFeatureCollection(featureCollection, 'selection_utilisateur'),
|
|
|
|
[featureCollection]
|
|
|
|
);
|
|
|
|
const selectionsLineFeatureCollection = useMemo(
|
|
|
|
() =>
|
|
|
|
filterFeatureCollectionByGeometryType(
|
|
|
|
selectionsUtilisateurFeatureCollection,
|
|
|
|
'LineString'
|
|
|
|
),
|
|
|
|
[selectionsUtilisateurFeatureCollection]
|
|
|
|
);
|
|
|
|
const selectionsPolygonFeatureCollection = useMemo(
|
|
|
|
() =>
|
|
|
|
filterFeatureCollectionByGeometryType(
|
|
|
|
selectionsUtilisateurFeatureCollection,
|
|
|
|
'Polygon'
|
|
|
|
),
|
|
|
|
[selectionsUtilisateurFeatureCollection]
|
|
|
|
);
|
|
|
|
const selectionsPointFeatureCollection = useMemo(
|
|
|
|
() =>
|
|
|
|
filterFeatureCollectionByGeometryType(
|
|
|
|
selectionsUtilisateurFeatureCollection,
|
|
|
|
'Point'
|
|
|
|
),
|
|
|
|
[selectionsUtilisateurFeatureCollection]
|
|
|
|
);
|
2020-07-29 15:27:08 +02:00
|
|
|
const hasCadastres = !!cadastresFeatureCollection.length;
|
2020-09-02 12:56:45 +02:00
|
|
|
const mapStyle = useMemo(
|
|
|
|
() => getMapStyle(style, hasCadastres, options.mnhn),
|
|
|
|
[style, options, cadastresFeatureCollection]
|
|
|
|
);
|
2020-06-09 17:23:24 +02:00
|
|
|
const popup = useMemo(
|
|
|
|
() =>
|
|
|
|
new Popup({
|
|
|
|
closeButton: false,
|
|
|
|
closeOnClick: false
|
|
|
|
})
|
2020-05-28 17:43:04 +02:00
|
|
|
);
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
const onMouseEnter = useCallback(
|
|
|
|
(event) => {
|
|
|
|
const feature = event.features[0];
|
|
|
|
if (feature.properties && feature.properties.description) {
|
|
|
|
const coordinates = getCenter(feature.geometry, event.lngLat);
|
|
|
|
const description = feature.properties.description;
|
|
|
|
currentMap.getCanvas().style.cursor = 'pointer';
|
|
|
|
popup.setLngLat(coordinates).setHTML(description).addTo(currentMap);
|
|
|
|
} else {
|
|
|
|
popup.remove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[currentMap, popup]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onMouseLeave = useCallback(() => {
|
|
|
|
currentMap.getCanvas().style.cursor = '';
|
|
|
|
popup.remove();
|
|
|
|
}, [currentMap, popup]);
|
|
|
|
|
|
|
|
const onFeatureFocus = useCallback(
|
|
|
|
({ detail }) => {
|
|
|
|
const feature = findFeature(featureCollection, detail.id);
|
|
|
|
if (feature) {
|
|
|
|
fitBounds(currentMap, feature);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[currentMap, featureCollection]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEvent('map:feature:focus', onFeatureFocus);
|
2020-05-28 17:43:04 +02:00
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
const [a1, a2, b1, b2] = featureCollection.bbox;
|
|
|
|
const boundData = [
|
|
|
|
[a1, a2],
|
|
|
|
[b1, b2]
|
|
|
|
];
|
2020-04-07 18:17:30 +02:00
|
|
|
|
|
|
|
const polygonSelectionFill = {
|
|
|
|
'fill-color': '#EC3323',
|
|
|
|
'fill-opacity': 0.5
|
|
|
|
};
|
|
|
|
|
|
|
|
const polygonSelectionLine = {
|
|
|
|
'line-color': 'rgba(255, 0, 0, 1)',
|
|
|
|
'line-width': 4
|
|
|
|
};
|
|
|
|
|
2020-05-12 15:43:20 +02:00
|
|
|
const lineStringSelectionLine = {
|
|
|
|
'line-color': 'rgba(55, 42, 127, 1.00)',
|
|
|
|
'line-width': 3
|
|
|
|
};
|
|
|
|
|
|
|
|
const pointSelectionFill = {
|
|
|
|
'circle-color': '#EC3323'
|
|
|
|
};
|
|
|
|
|
2020-04-07 18:17:30 +02:00
|
|
|
const polygonCadastresFill = {
|
2020-05-28 17:43:04 +02:00
|
|
|
'fill-color': '#FAD859',
|
2020-04-07 18:17:30 +02:00
|
|
|
'fill-opacity': 0.5
|
|
|
|
};
|
|
|
|
|
|
|
|
const polygonCadastresLine = {
|
|
|
|
'line-color': 'rgba(156, 160, 144, 255)',
|
|
|
|
'line-width': 2,
|
|
|
|
'line-dasharray': [1, 1]
|
|
|
|
};
|
|
|
|
|
2020-06-09 17:23:24 +02:00
|
|
|
function onMapLoad(map) {
|
|
|
|
setCurrentMap(map);
|
2020-05-12 15:43:20 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:20:53 +02:00
|
|
|
if (!mapboxgl.supported()) {
|
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
Nous ne pouvons pas afficher la carte car elle est imcompatible avec
|
|
|
|
votre navigateur. Nous vous conseillons de le mettre à jour ou utiliser
|
|
|
|
les dernières versions de Chrome, Firefox ou Safari
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:17:30 +02:00
|
|
|
return (
|
|
|
|
<Map
|
2020-06-09 17:23:24 +02:00
|
|
|
onStyleLoad={(map) => onMapLoad(map)}
|
2020-04-07 18:17:30 +02:00
|
|
|
fitBounds={boundData}
|
|
|
|
fitBoundsOptions={{ padding: 100 }}
|
2020-05-28 17:43:04 +02:00
|
|
|
style={mapStyle}
|
2020-04-07 18:17:30 +02:00
|
|
|
containerStyle={{
|
|
|
|
height: '400px',
|
|
|
|
width: '100%'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<GeoJSONLayer
|
2020-05-12 15:43:20 +02:00
|
|
|
data={selectionsPolygonFeatureCollection}
|
2020-04-07 18:17:30 +02:00
|
|
|
fillPaint={polygonSelectionFill}
|
|
|
|
linePaint={polygonSelectionLine}
|
2020-06-09 17:23:24 +02:00
|
|
|
fillOnMouseEnter={onMouseEnter}
|
|
|
|
fillOnMouseLeave={onMouseLeave}
|
2020-04-07 18:17:30 +02:00
|
|
|
/>
|
2020-05-12 15:43:20 +02:00
|
|
|
<GeoJSONLayer
|
|
|
|
data={selectionsLineFeatureCollection}
|
|
|
|
linePaint={lineStringSelectionLine}
|
2020-06-09 17:23:24 +02:00
|
|
|
lineOnMouseEnter={onMouseEnter}
|
|
|
|
lineOnMouseLeave={onMouseLeave}
|
2020-05-12 15:43:20 +02:00
|
|
|
/>
|
|
|
|
<GeoJSONLayer
|
|
|
|
data={selectionsPointFeatureCollection}
|
|
|
|
circlePaint={pointSelectionFill}
|
2020-06-09 17:23:24 +02:00
|
|
|
circleOnMouseEnter={onMouseEnter}
|
|
|
|
circleOnMouseLeave={onMouseLeave}
|
2020-05-12 15:43:20 +02:00
|
|
|
/>
|
2020-07-29 15:27:08 +02:00
|
|
|
{hasCadastres ? (
|
|
|
|
<GeoJSONLayer
|
|
|
|
data={cadastresFeatureCollection}
|
|
|
|
fillPaint={polygonCadastresFill}
|
|
|
|
linePaint={polygonCadastresLine}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
|
2020-09-02 12:56:45 +02:00
|
|
|
<SwitchMapStyle style={style} setStyle={setStyle} ign={options.ign} />
|
2020-04-07 18:17:30 +02:00
|
|
|
<ZoomControl />
|
|
|
|
</Map>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
MapReader.propTypes = {
|
2020-04-09 17:32:20 +02:00
|
|
|
featureCollection: PropTypes.shape({
|
|
|
|
type: PropTypes.string,
|
|
|
|
bbox: PropTypes.array,
|
|
|
|
features: PropTypes.array
|
2020-07-30 17:10:26 +02:00
|
|
|
}),
|
2020-09-02 12:56:45 +02:00
|
|
|
options: PropTypes.shape({
|
|
|
|
ign: PropTypes.bool,
|
|
|
|
mnhn: PropTypes.bool
|
|
|
|
})
|
2020-04-07 18:17:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MapReader;
|