2020-05-28 17:43:04 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-04-07 18:17:30 +02:00
|
|
|
import ReactMapboxGl, { ZoomControl, GeoJSONLayer } from 'react-mapbox-gl';
|
2020-04-09 17:32:20 +02:00
|
|
|
import mapboxgl from 'mapbox-gl';
|
2020-05-28 17:43:04 +02:00
|
|
|
import SwitchMapStyle from './SwitchMapStyle';
|
|
|
|
import ortho from '../MapStyles/ortho.json';
|
|
|
|
import orthoCadastre from '../MapStyles/orthoCadastre.json';
|
|
|
|
import vector from '../MapStyles/vector.json';
|
|
|
|
import vectorCadastre from '../MapStyles/vectorCadastre.json';
|
2020-04-07 18:17:30 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
const Map = ReactMapboxGl({});
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
const MapReader = ({ featureCollection }) => {
|
2020-05-28 17:43:04 +02:00
|
|
|
const [style, setStyle] = useState('ortho');
|
|
|
|
const hasCadastres = featureCollection.features.find(
|
|
|
|
(feature) => feature.properties.source === 'cadastre'
|
|
|
|
);
|
|
|
|
let mapStyle = style === 'ortho' ? ortho : vector;
|
|
|
|
|
|
|
|
if (hasCadastres) {
|
|
|
|
mapStyle = style === 'ortho' ? orthoCadastre : vectorCadastre;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-05-12 15:43:20 +02:00
|
|
|
const cadastresFeatureCollection = {
|
2020-04-09 17:32:20 +02:00
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: []
|
|
|
|
};
|
2020-05-12 15:43:20 +02:00
|
|
|
|
|
|
|
const selectionsLineFeatureCollection = {
|
2020-04-09 17:32:20 +02:00
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: []
|
2020-04-07 18:17:30 +02:00
|
|
|
};
|
|
|
|
|
2020-05-12 15:43:20 +02:00
|
|
|
const selectionsPolygonFeatureCollection = {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: []
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectionsPointFeatureCollection = {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: []
|
|
|
|
};
|
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-05-12 15:43:20 +02:00
|
|
|
for (let feature of featureCollection.features) {
|
|
|
|
switch (feature.properties.source) {
|
|
|
|
case 'selection_utilisateur':
|
|
|
|
switch (feature.geometry.type) {
|
|
|
|
case 'LineString':
|
|
|
|
selectionsLineFeatureCollection.features.push(feature);
|
|
|
|
break;
|
|
|
|
case 'Polygon':
|
|
|
|
selectionsPolygonFeatureCollection.features.push(feature);
|
|
|
|
break;
|
|
|
|
case 'Point':
|
|
|
|
selectionsPointFeatureCollection.features.push(feature);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'cadastre':
|
|
|
|
cadastresFeatureCollection.features.push(feature);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-05-12 15:43:20 +02:00
|
|
|
<GeoJSONLayer
|
|
|
|
data={selectionsLineFeatureCollection}
|
|
|
|
linePaint={lineStringSelectionLine}
|
|
|
|
/>
|
|
|
|
<GeoJSONLayer
|
|
|
|
data={selectionsPointFeatureCollection}
|
|
|
|
circlePaint={pointSelectionFill}
|
|
|
|
/>
|
2020-04-07 18:17:30 +02:00
|
|
|
<GeoJSONLayer
|
2020-04-09 17:32:20 +02:00
|
|
|
data={cadastresFeatureCollection}
|
2020-04-07 18:17:30 +02:00
|
|
|
fillPaint={polygonCadastresFill}
|
|
|
|
linePaint={polygonCadastresLine}
|
|
|
|
/>
|
2020-05-28 17:43:04 +02:00
|
|
|
|
|
|
|
<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>
|
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-04-07 18:17:30 +02:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MapReader;
|