diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 1ff5980c6..84f5de313 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -115,7 +115,7 @@ class Champs::CarteController < ApplicationController end def params_features - params[:feature_collection][:features] + params[:features] end def save_geometry!(geo_area, feature) diff --git a/app/javascript/components/MapEditor/MapEditor.js b/app/javascript/components/MapEditor/MapEditor.js index 56b33d08f..904cb8c05 100644 --- a/app/javascript/components/MapEditor/MapEditor.js +++ b/app/javascript/components/MapEditor/MapEditor.js @@ -6,6 +6,7 @@ import DrawControl from 'react-mapbox-gl-draw'; import SwitchMapStyle from './SwitchMapStyle'; import SearchInput from './SearchInput'; import { getJSON, ajax } from '@utils'; +import { gpx } from '@tmcw/togeojson/dist/togeojson.es.js'; import ortho from './styles/ortho.json'; import vector from './styles/vector.json'; import { polygonCadastresFill, polygonCadastresLine } from './utils'; @@ -28,9 +29,8 @@ const MapEditor = ({ featureCollection, url }) => { const [coords, setCoords] = useState([1.7, 46.9]); const [zoom, setZoom] = useState([5]); const [currentMap, setCurrentMap] = useState({}); - + const [bbox, setBbox] = useState(featureCollection.bbox); const mapStyle = style === 'ortho' ? ortho : vector; - const bbox = featureCollection.bbox; const cadastresFeatureCollection = filterFeatureCollection( featureCollection, 'cadastre' @@ -93,6 +93,29 @@ const MapEditor = ({ featureCollection, url }) => { } }; + const onGpxImport = e => { + let reader = new FileReader(); + reader.readAsText(e.target.files[0], 'UTF-8'); + reader.onload = async event => { + 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); + }; + }; + useEffect(() => { addEventListener('cadastres:update', onCadastresUpdate); return () => removeEventListener('cadastres:update', onCadastresUpdate); @@ -110,6 +133,16 @@ const MapEditor = ({ featureCollection, url }) => { return ( <> +
+ Importer un fichier GPX +
+