Skip to content

Commit

Permalink
feat(import): ajout de l'import par fichier géographique
Browse files Browse the repository at this point in the history
  • Loading branch information
thom4parisot committed May 13, 2024
1 parent d470636 commit f197111
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/cartobio-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ export function setAuthorization (userToken) {
}
}


/**
* Turn a geographical file into a GeoJSON
*
* @param {File} archive
* @returns {Promise<CartoBioFeatureCollection>}
*/
export async function convertGeographicalFileToGeoJSON (archive) {
const form = new FormData()
form.append('archive', archive)
const { data: geojson } = await apiClient.post(`/v2/convert/anygeo/geojson`, form)
return geojson
}

/**
* Turn a zipped Shapefile into a GeoJSON
*
Expand Down
62 changes: 62 additions & 0 deletions src/components/OperatorSetup/Sources/AnyGeo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<template>
<div>
<p>
Vous pouvez importer un fichier géographique exporté depuis
CartoBio, le <a href="https://www.geoportail.gouv.fr/" target="_blank" rel="noopener noreferrer">Geoportail</a>
ou encore <a href="https://qgis.org" target="_blank" rel="noopener noreferrer">QGIS</a>.
</p>

<p>
Les formats acceptés sont
<abbr>KML</abbr> (<code>.kml</code> ou <code>.kmz</code>),
GeoJSON (<code>.geojson</code>),
GeoPackage (<code>.gpkg</code>)
et Shapefile (<code>.shp</code> ou <code>.zip</code>).
</p>

<div class="fr-upload-group fr-mb-5w">
<input type="file" ref="fileInput" accept=".geojson,.gpkg,.json,.kml,.kmz,.shp,.shp.zip,.zip" @change="handleFileUpload" hidden />
<button class="fr-btn fr-icon-upload-line fr-btn--icon-left" @click="fileInput.click()">
Sélectionner mon fichier
</button>
</div>

<div v-if="erreur" class="fr-alert fr-alert--error fr-mb-6w">
<h3 class="fr-alert__title">Échec de l'import</h3>
<p>{{ erreur }}</p>
</div>
</div>
</template>

<script setup>
import { ref } from 'vue'
import { convertGeographicalFileToGeoJSON } from '@/cartobio-api.js'
const emit = defineEmits(['upload:start', 'upload:complete'])
const fileInput = ref(null)
const source = 'anygeo'
const erreur = ref('')
async function handleFileUpload () {
const warnings = []
const [archive] = fileInput.value.files
emit('upload:start')
try {
const geojson = await convertGeographicalFileToGeoJSON(archive)
const metadata = {
}
emit('upload:complete', { geojson, source, warnings, metadata })
} catch (error) {
if (error.response?.status >= 400 && error.response?.status < 500) {
erreur.value = error.response.data.message
} else {
erreur.value = 'Erreur inconnue, merci de réessayer plus tard.'
throw error
}
}
}
</script>
25 changes: 15 additions & 10 deletions src/components/OperatorSetup/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { markRaw } from 'vue'
import { sources } from '@/referentiels/imports.js'

import AnyGeoFeaturesImport from '@/components/OperatorSetup/Sources/AnyGeo.vue'
import GeofoliaFeaturesImport from '@/components/OperatorSetup/Sources/Geofolia.vue'
import RPGFeaturesImport from '@/components/OperatorSetup/Sources/RPG.vue'
import CviFeaturesImport from '@/components/OperatorSetup/Sources/Cvi.vue'
Expand All @@ -10,25 +11,29 @@ import TelepacFeaturesImport from '@/components/OperatorSetup/Sources/Telepac.vu
const { VUE_APP_PRELOADED_CAMPAGNE_PAC: CAMPAGNE_PAC } = import.meta.env

export default {
[sources.TELEPAC]: {
label: 'Telepac',
component: markRaw(TelepacFeaturesImport),
[sources.ANYGEO]: {
label: 'Fichier géographique',
component: markRaw(AnyGeoFeaturesImport),
},
[sources.CVI]: {
label: 'ProDouanes (CVI)',
component: markRaw(CviFeaturesImport),
},
[sources.GEOFOLIA]: {
label: 'Geofolia',
component: markRaw(GeofoliaFeaturesImport),
},
[sources.MESPARCELLES]: {
label: 'MesParcelles',
component: markRaw(MesParcellesFeaturesImport),
},
[sources.RPG]: {
label: `RPG ${CAMPAGNE_PAC}`,
component: markRaw(RPGFeaturesImport),
},
[sources.CVI]: {
label: 'ProDouanes (CVI)',
component: markRaw(CviFeaturesImport),
},
[sources.MESPARCELLES]: {
label: 'MesParcelles',
component: markRaw(MesParcellesFeaturesImport),
[sources.TELEPAC]: {
label: 'Telepac',
component: markRaw(TelepacFeaturesImport),
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/pages/exploitations/[numeroBio]/import.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const operatorSetupActions = [
selector: markRaw(ActionFromSource),
wizzard: defineAsyncComponent(() => import('@/components/OperatorSetup/Flows/MultiSources.vue')),
extraProps: {
sources: [sources.GEOFOLIA, sources.MESPARCELLES, sources.TELEPAC, ...(permissions.isOc ? [sources.RPG] : []), sources.CVI]
sources: [
sources.GEOFOLIA,
sources.MESPARCELLES,
sources.TELEPAC,
sources.CVI,
sources.ANYGEO,
...(permissions.isOc ? [sources.RPG] : [])
]
}
},
]
Expand Down
3 changes: 2 additions & 1 deletion src/pages/exploitations/[numeroBio]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ const operatorSetupActions = [
sources.MESPARCELLES,
sources.TELEPAC,
sources.RPG,
sources.CVI
sources.CVI,
sources.ANYGEO
]
}
},
Expand Down
1 change: 1 addition & 0 deletions src/referentiels/imports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @enum string */
export const sources = Object.freeze({
ANYGEO: 'anygeo',
GEOFOLIA: 'geofolia',
MANUAL: '',
MESPARCELLES: 'mesparcelles',
Expand Down
3 changes: 2 additions & 1 deletion widget/Notification.ce.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const operatorSetupActions = [
sources.GEOFOLIA,
sources.MESPARCELLES,
sources.TELEPAC,
sources.CVI
sources.CVI,
sources.ANYGEO
]
}
},
Expand Down

0 comments on commit f197111

Please sign in to comment.