2021-05-06 18:52:02 +02:00
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
import ReactMapboxGl, { ZoomControl, GeoJSONLayer } from 'react-mapbox-gl';
|
2020-04-07 18:17:30 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2021-05-06 18:52:02 +02:00
|
|
|
import 'mapbox-gl/dist/mapbox-gl.css';
|
2020-04-07 18:17:30 +02:00
|
|
|
|
2021-05-06 18:52:02 +02:00
|
|
|
import MapStyleControl, { useMapStyle } from '../shared/mapbox/MapStyleControl';
|
2020-06-09 17:23:24 +02:00
|
|
|
import {
|
|
|
|
filterFeatureCollection,
|
2021-05-06 18:52:02 +02:00
|
|
|
filterFeatureCollectionByGeometryType
|
2020-10-15 17:22:08 +02:00
|
|
|
} from '../shared/mapbox/utils';
|
2021-05-06 18:52:02 +02:00
|
|
|
import { useMapbox } from './useMapbox';
|
2020-04-07 18:17:30 +02:00
|
|
|
|
2021-05-06 18:52:02 +02:00
|
|
|
const Mapbox = ReactMapboxGl({});
|
2020-06-09 17:23:24 +02:00
|
|
|
|
2021-05-06 18:52:02 +02:00
|
|
|
const MapReader = ({ featureCollection, options }) => {
|
2021-08-31 18:14:04 +02:00
|
|
|
const { isSupported, onLoad, onStyleChange, onMouseEnter, onMouseLeave } =
|
|
|
|
useMapbox(featureCollection);
|
|
|
|
const { style, layers, setStyle, setLayerEnabled, setLayerOpacity } =
|
|
|
|
useMapStyle(options.layers, { onStyleChange });
|
2021-05-06 18:52:02 +02:00
|
|
|
|
|
|
|
if (!isSupported) {
|
2020-04-07 18:20:53 +02:00
|
|
|
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 (
|
2020-10-15 17:22:08 +02:00
|
|
|
<Mapbox
|
2021-05-06 18:52:02 +02:00
|
|
|
onStyleLoad={(map) => onLoad(map)}
|
|
|
|
style={style}
|
2021-07-01 18:50:35 +02:00
|
|
|
containerStyle={{ height: '500px' }}
|
2020-04-07 18:17:30 +02:00
|
|
|
>
|
2021-05-06 18:52:02 +02:00
|
|
|
<SelectionUtilisateurPolygonLayer
|
|
|
|
featureCollection={featureCollection}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
2020-04-07 18:17:30 +02:00
|
|
|
/>
|
2021-05-06 18:52:02 +02:00
|
|
|
<SelectionUtilisateurLineLayer
|
|
|
|
featureCollection={featureCollection}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
2020-05-12 15:43:20 +02:00
|
|
|
/>
|
2021-05-06 18:52:02 +02:00
|
|
|
<SelectionUtilisateurPointLayer
|
|
|
|
featureCollection={featureCollection}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
2020-05-12 15:43:20 +02:00
|
|
|
/>
|
2021-05-06 18:52:02 +02:00
|
|
|
|
2021-06-30 10:28:02 +02:00
|
|
|
<MapStyleControl
|
|
|
|
style={style.id}
|
|
|
|
layers={layers}
|
|
|
|
setStyle={setStyle}
|
|
|
|
setLayerEnabled={setLayerEnabled}
|
|
|
|
setLayerOpacity={setLayerOpacity}
|
|
|
|
/>
|
2020-04-07 18:17:30 +02:00
|
|
|
<ZoomControl />
|
2020-10-15 17:22:08 +02:00
|
|
|
</Mapbox>
|
2020-04-07 18:17:30 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-06 18:52:02 +02:00
|
|
|
const polygonSelectionFill = {
|
|
|
|
'fill-color': '#EC3323',
|
|
|
|
'fill-opacity': 0.5
|
|
|
|
};
|
|
|
|
const polygonSelectionLine = {
|
|
|
|
'line-color': 'rgba(255, 0, 0, 1)',
|
|
|
|
'line-width': 4
|
|
|
|
};
|
|
|
|
const lineStringSelectionLine = {
|
|
|
|
'line-color': 'rgba(55, 42, 127, 1.00)',
|
|
|
|
'line-width': 3
|
|
|
|
};
|
|
|
|
const pointSelectionFill = {
|
|
|
|
'circle-color': '#EC3323'
|
|
|
|
};
|
|
|
|
|
|
|
|
function SelectionUtilisateurPolygonLayer({
|
|
|
|
featureCollection,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave
|
|
|
|
}) {
|
|
|
|
const data = useMemo(
|
|
|
|
() =>
|
|
|
|
filterFeatureCollectionByGeometryType(
|
|
|
|
filterFeatureCollection(featureCollection, 'selection_utilisateur'),
|
|
|
|
'Polygon'
|
|
|
|
),
|
|
|
|
[featureCollection]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GeoJSONLayer
|
|
|
|
data={data}
|
|
|
|
fillPaint={polygonSelectionFill}
|
|
|
|
linePaint={polygonSelectionLine}
|
|
|
|
fillOnMouseEnter={onMouseEnter}
|
|
|
|
fillOnMouseLeave={onMouseLeave}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function SelectionUtilisateurLineLayer({
|
|
|
|
featureCollection,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave
|
|
|
|
}) {
|
|
|
|
const data = useMemo(
|
|
|
|
() =>
|
|
|
|
filterFeatureCollectionByGeometryType(
|
|
|
|
filterFeatureCollection(featureCollection, 'selection_utilisateur'),
|
|
|
|
'LineString'
|
|
|
|
),
|
|
|
|
[featureCollection]
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<GeoJSONLayer
|
|
|
|
data={data}
|
|
|
|
linePaint={lineStringSelectionLine}
|
|
|
|
lineOnMouseEnter={onMouseEnter}
|
|
|
|
lineOnMouseLeave={onMouseLeave}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function SelectionUtilisateurPointLayer({
|
|
|
|
featureCollection,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave
|
|
|
|
}) {
|
|
|
|
const data = useMemo(
|
|
|
|
() =>
|
|
|
|
filterFeatureCollectionByGeometryType(
|
|
|
|
filterFeatureCollection(featureCollection, 'selection_utilisateur'),
|
|
|
|
'Point'
|
|
|
|
),
|
|
|
|
[featureCollection]
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<GeoJSONLayer
|
|
|
|
data={data}
|
|
|
|
circlePaint={pointSelectionFill}
|
|
|
|
circleOnMouseEnter={onMouseEnter}
|
|
|
|
circleOnMouseLeave={onMouseLeave}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectionUtilisateurPolygonLayer.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
|
|
|
}),
|
2021-05-06 18:52:02 +02:00
|
|
|
onMouseEnter: PropTypes.func,
|
|
|
|
onMouseLeave: PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
SelectionUtilisateurLineLayer.propTypes = {
|
|
|
|
featureCollection: PropTypes.shape({
|
|
|
|
type: PropTypes.string,
|
|
|
|
bbox: PropTypes.array,
|
|
|
|
features: PropTypes.array
|
|
|
|
}),
|
|
|
|
onMouseEnter: PropTypes.func,
|
|
|
|
onMouseLeave: PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
SelectionUtilisateurPointLayer.propTypes = {
|
|
|
|
featureCollection: PropTypes.shape({
|
|
|
|
type: PropTypes.string,
|
|
|
|
bbox: PropTypes.array,
|
|
|
|
features: PropTypes.array
|
|
|
|
}),
|
|
|
|
onMouseEnter: PropTypes.func,
|
|
|
|
onMouseLeave: PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
MapReader.propTypes = {
|
|
|
|
featureCollection: PropTypes.shape({
|
|
|
|
bbox: PropTypes.array,
|
|
|
|
features: PropTypes.array
|
|
|
|
}),
|
|
|
|
options: PropTypes.shape({ layers: PropTypes.array })
|
2020-04-07 18:17:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MapReader;
|