From 1cc04092f489c390217c7c0c5a35e8a720301123 Mon Sep 17 00:00:00 2001 From: kara Diaby Date: Thu, 7 May 2020 18:30:19 +0200 Subject: [PATCH] import GPX file in browser [Carto] --- app/controllers/champs/carte_controller.rb | 2 +- .../components/MapEditor/MapEditor.js | 37 ++++++++++++++++++- .../components/MapEditor/SearchInput.js | 2 +- package.json | 1 + .../champs/carte_controller_spec.rb | 5 +-- yarn.lock | 5 +++ 6 files changed, 45 insertions(+), 7 deletions(-) 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 +

+
+
+ +
+
{ return (