From 2ca3398de58298349dcefe32b7d94dc2b69ee899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr> Date: Fri, 22 Dec 2023 12:16:40 +0100 Subject: [PATCH] add json import (adapting geojson) for non geographical features --- src/store/modules/feature-type.store.js | 15 +++++++-------- src/utils/index.js | 1 + src/views/FeatureType/FeatureTypeDetail.vue | 11 ++++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/store/modules/feature-type.store.js b/src/store/modules/feature-type.store.js index 78381230..b822ac4c 100644 --- a/src/store/modules/feature-type.store.js +++ b/src/store/modules/feature-type.store.js @@ -9,14 +9,14 @@ const getColorsStyles = (customForms) => customForms }); const pending2draftFeatures = (features) => { - const result = []; for (const el of features) { - if (el.properties.status === 'pending') { + if (el.properties && el.properties.status === 'pending') { el.properties.status = 'draft'; + } else if (el.status === 'pending') { + el.status = 'draft'; } - result.push(el); } - return result; + return features; }; const feature_type = { @@ -199,16 +199,15 @@ const feature_type = { if (!name && state.fileToImport) { name = state.fileToImport.name; } - if (rootState.projects.project.moderation) { if (state.fileToImport && state.fileToImport.size > 0) { //* if data in a binary file, read it as text const textFile = await state.fileToImport.text(); geojson = JSON.parse(textFile); } - const unmoderatedFeatures = pending2draftFeatures(geojson.features); - geojson= { + const unmoderatedFeatures = pending2draftFeatures(geojson.features || geojson); + geojson = geojson.features ? { type: 'FeatureCollection', features: unmoderatedFeatures - }; + } : unmoderatedFeatures; } const fileToImport = new File([JSON.stringify(geojson)], name, { type }); diff --git a/src/utils/index.js b/src/utils/index.js index 12a54ae6..66fa3538 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,5 +1,6 @@ import featureAPI from '@/services/feature-api'; import { isNil } from 'lodash'; + export function formatStringDate(stringDate) { const date = new Date(stringDate); if (date instanceof Date && !isNaN(date.valueOf())) { diff --git a/src/views/FeatureType/FeatureTypeDetail.vue b/src/views/FeatureType/FeatureTypeDetail.vue index 8d780b12..d6acca8f 100644 --- a/src/views/FeatureType/FeatureTypeDetail.vue +++ b/src/views/FeatureType/FeatureTypeDetail.vue @@ -123,8 +123,9 @@ @change="onGeojsonFileChange" > </div> + <div - v-if="structure.geom_type === 'point'" + v-if="structure.geom_type === 'point' || structure.geom_type === 'none'" class="field" > <label @@ -568,7 +569,7 @@ export default { return this.selectedPrerecordedListValues[listName].some((el) => el.label === fieldLabel); }, - async isValidTypes(data) { + async isValidTypes(features) { this.importError = ''; const fields = this.structure.customfield_set.map((el) => { return { @@ -578,7 +579,7 @@ export default { }; }); - for (const feature of data.features) { + for (const feature of features) { for (const { name, field_type, options } of fields) { const properties = feature.properties || feature; @@ -708,7 +709,7 @@ export default { const jsonFromCsv = await csv({ delimiter }).fromString(csvString); // Validate the types of values in the JSON object - const validity = await this.isValidTypes({ features: jsonFromCsv }); + const validity = await this.isValidTypes(jsonFromCsv); return validity; }, @@ -751,7 +752,7 @@ export default { // If the file is smaller than 10 Mo, check its validity try { const json = JSON.parse(fileContent); - jsonValidity = await this.isValidTypes(json); + jsonValidity = await this.isValidTypes(json.features || json); } catch (error) { this.DISPLAY_MESSAGE({ comment: error, level: 'negative' }); jsonValidity = false; -- GitLab