From a46c71350710c15091bb3e015588d1fc9d85da60 Mon Sep 17 00:00:00 2001 From: aziz Date: Thu, 4 Apr 2024 22:21:22 +0200 Subject: [PATCH 1/5] fix(geo): import and export shp file --- .../geo/src/lib/import-export/shared/export.service.ts | 8 +++++++- packages/geo/src/lib/import-export/shared/import.utils.ts | 8 ++++++++ .../geo/src/lib/style/shared/feature/feature-style.ts | 4 +++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/geo/src/lib/import-export/shared/export.service.ts b/packages/geo/src/lib/import-export/shared/export.service.ts index d81e70f975..6799fe4d98 100644 --- a/packages/geo/src/lib/import-export/shared/export.service.ts +++ b/packages/geo/src/lib/import-export/shared/export.service.ts @@ -78,9 +78,15 @@ export class ExportService { } return olFeatures.map((olFeature: OlFeature) => { - const keys = olFeature + let keys = olFeature .getKeys() .filter((key: string) => !key.startsWith(excludePrefix)); + + if (format === ExportFormat.Shapefile && olFeature.get('_style')) { + const style = JSON.stringify(olFeature.get('_style')); + keys = keys.filter((key) => key !== '_style'); + } + const properties = keys.reduce( (acc: object, key: string) => { acc[key] = olFeature.get(key); diff --git a/packages/geo/src/lib/import-export/shared/import.utils.ts b/packages/geo/src/lib/import-export/shared/import.utils.ts index 7099bd2b32..f75854ce0e 100644 --- a/packages/geo/src/lib/import-export/shared/import.utils.ts +++ b/packages/geo/src/lib/import-export/shared/import.utils.ts @@ -411,3 +411,11 @@ export function getFileExtension(file: File): string { export function computeLayerTitleFromFile(file: File): string { return file.name.substr(0, file.name.lastIndexOf('.')); } + +export function isValidJSON(jsonString: string) { + try { + return JSON.parse(jsonString) && !!jsonString; + } catch (e) { + return false; + } +} diff --git a/packages/geo/src/lib/style/shared/feature/feature-style.ts b/packages/geo/src/lib/style/shared/feature/feature-style.ts index 01c27c1e74..e2936e0ed8 100644 --- a/packages/geo/src/lib/style/shared/feature/feature-style.ts +++ b/packages/geo/src/lib/style/shared/feature/feature-style.ts @@ -3,6 +3,7 @@ import * as olGeom from 'ol/geom'; import type { default as OlGeometry } from 'ol/geom/Geometry'; import * as olStyle from 'ol/style'; +import { isValidJSON } from '../../../import-export'; import { StyleService } from '../../style-service/style.service'; export function featureRandomStyleFunction(): ( @@ -21,7 +22,8 @@ export function featureRandomStyleFunction(): ( }); return (olFeature: olFeature, resolution: number) => { const customStyle = olFeature.get('_style'); - if (customStyle) { + console.log('customStyle', customStyle); + if (customStyle && isValidJSON(customStyle)) { if ( customStyle.circle && olFeature.get('rad') && From 659ead816309857a80f5686de9c4bd4d7726fe80 Mon Sep 17 00:00:00 2001 From: aziz Date: Thu, 4 Apr 2024 22:28:35 +0200 Subject: [PATCH 2/5] check _style length before export --- packages/geo/src/lib/import-export/shared/export.service.ts | 2 +- packages/geo/src/lib/style/shared/feature/feature-style.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/geo/src/lib/import-export/shared/export.service.ts b/packages/geo/src/lib/import-export/shared/export.service.ts index 6799fe4d98..1c1112bf39 100644 --- a/packages/geo/src/lib/import-export/shared/export.service.ts +++ b/packages/geo/src/lib/import-export/shared/export.service.ts @@ -84,7 +84,7 @@ export class ExportService { if (format === ExportFormat.Shapefile && olFeature.get('_style')) { const style = JSON.stringify(olFeature.get('_style')); - keys = keys.filter((key) => key !== '_style'); + if (style.length > 256) keys = keys.filter((key) => key !== '_style'); } const properties = keys.reduce( diff --git a/packages/geo/src/lib/style/shared/feature/feature-style.ts b/packages/geo/src/lib/style/shared/feature/feature-style.ts index e2936e0ed8..8ed2cf9e31 100644 --- a/packages/geo/src/lib/style/shared/feature/feature-style.ts +++ b/packages/geo/src/lib/style/shared/feature/feature-style.ts @@ -22,7 +22,6 @@ export function featureRandomStyleFunction(): ( }); return (olFeature: olFeature, resolution: number) => { const customStyle = olFeature.get('_style'); - console.log('customStyle', customStyle); if (customStyle && isValidJSON(customStyle)) { if ( customStyle.circle && From 3190f7ee917868a5e13006f9f8c69167b9c2b287 Mon Sep 17 00:00:00 2001 From: aziz Date: Fri, 5 Apr 2024 17:52:11 +0200 Subject: [PATCH 3/5] set ShapefileMaxLength variable to 255 --- packages/geo/src/lib/import-export/shared/export.service.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/geo/src/lib/import-export/shared/export.service.ts b/packages/geo/src/lib/import-export/shared/export.service.ts index 1c1112bf39..7c697886f1 100644 --- a/packages/geo/src/lib/import-export/shared/export.service.ts +++ b/packages/geo/src/lib/import-export/shared/export.service.ts @@ -34,6 +34,8 @@ export class ExportService { private ogreUrl: string; private aggregateInComment: boolean = true; + private ShapefileMaxLength: number = 255; + constructor(private config: ConfigService) { this.ogreUrl = this.config.getConfig('importExport.url'); const gpxAggregateInComment = this.config.getConfig( @@ -84,7 +86,8 @@ export class ExportService { if (format === ExportFormat.Shapefile && olFeature.get('_style')) { const style = JSON.stringify(olFeature.get('_style')); - if (style.length > 256) keys = keys.filter((key) => key !== '_style'); + if (style.length > this.ShapefileMaxLength) + keys = keys.filter((key) => key !== '_style'); } const properties = keys.reduce( From aa6d1fddc562f3f6a0d15e0c22047966afd56119 Mon Sep 17 00:00:00 2001 From: aziz Date: Thu, 4 Apr 2024 22:21:22 +0200 Subject: [PATCH 4/5] fix(geo): import/export shp file handle invalid style check _style length before export set ShapefileMaxLength variable to 255 rename const rename --- .../src/lib/import-export/shared/export.service.ts | 11 ++++++++++- .../geo/src/lib/import-export/shared/import.utils.ts | 8 ++++++++ .../geo/src/lib/style/shared/feature/feature-style.ts | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/geo/src/lib/import-export/shared/export.service.ts b/packages/geo/src/lib/import-export/shared/export.service.ts index d81e70f975..5089a4f04b 100644 --- a/packages/geo/src/lib/import-export/shared/export.service.ts +++ b/packages/geo/src/lib/import-export/shared/export.service.ts @@ -16,6 +16,8 @@ import { } from './export.errors'; import { EncodingFormat, ExportFormat } from './export.type'; +const SHAPEFILE_FIELD_MAX_LENGHT = 255; + @Injectable({ providedIn: 'root' }) @@ -78,9 +80,16 @@ export class ExportService { } return olFeatures.map((olFeature: OlFeature) => { - const keys = olFeature + let keys = olFeature .getKeys() .filter((key: string) => !key.startsWith(excludePrefix)); + + if (format === ExportFormat.Shapefile && olFeature.get('_style')) { + const style = JSON.stringify(olFeature.get('_style')); + if (style.length > SHAPEFILE_FIELD_MAX_LENGHT) + keys = keys.filter((key) => key !== '_style'); + } + const properties = keys.reduce( (acc: object, key: string) => { acc[key] = olFeature.get(key); diff --git a/packages/geo/src/lib/import-export/shared/import.utils.ts b/packages/geo/src/lib/import-export/shared/import.utils.ts index 7099bd2b32..f75854ce0e 100644 --- a/packages/geo/src/lib/import-export/shared/import.utils.ts +++ b/packages/geo/src/lib/import-export/shared/import.utils.ts @@ -411,3 +411,11 @@ export function getFileExtension(file: File): string { export function computeLayerTitleFromFile(file: File): string { return file.name.substr(0, file.name.lastIndexOf('.')); } + +export function isValidJSON(jsonString: string) { + try { + return JSON.parse(jsonString) && !!jsonString; + } catch (e) { + return false; + } +} diff --git a/packages/geo/src/lib/style/shared/feature/feature-style.ts b/packages/geo/src/lib/style/shared/feature/feature-style.ts index 01c27c1e74..8ed2cf9e31 100644 --- a/packages/geo/src/lib/style/shared/feature/feature-style.ts +++ b/packages/geo/src/lib/style/shared/feature/feature-style.ts @@ -3,6 +3,7 @@ import * as olGeom from 'ol/geom'; import type { default as OlGeometry } from 'ol/geom/Geometry'; import * as olStyle from 'ol/style'; +import { isValidJSON } from '../../../import-export'; import { StyleService } from '../../style-service/style.service'; export function featureRandomStyleFunction(): ( @@ -21,7 +22,7 @@ export function featureRandomStyleFunction(): ( }); return (olFeature: olFeature, resolution: number) => { const customStyle = olFeature.get('_style'); - if (customStyle) { + if (customStyle && isValidJSON(customStyle)) { if ( customStyle.circle && olFeature.get('rad') && From 8610c5f2a9adcbf3dd6543fc5a480ba9747dac54 Mon Sep 17 00:00:00 2001 From: aziz Date: Mon, 8 Apr 2024 18:23:34 +0200 Subject: [PATCH 5/5] fix(geo): transfer isValidJSON function to utils pakage --- packages/geo/src/lib/import-export/shared/export.service.ts | 2 -- packages/utils/src/lib/file.ts | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/geo/src/lib/import-export/shared/export.service.ts b/packages/geo/src/lib/import-export/shared/export.service.ts index 8313cf3724..5089a4f04b 100644 --- a/packages/geo/src/lib/import-export/shared/export.service.ts +++ b/packages/geo/src/lib/import-export/shared/export.service.ts @@ -36,8 +36,6 @@ export class ExportService { private ogreUrl: string; private aggregateInComment: boolean = true; - private ShapefileMaxLength: number = 255; - constructor(private config: ConfigService) { this.ogreUrl = this.config.getConfig('importExport.url'); const gpxAggregateInComment = this.config.getConfig( diff --git a/packages/utils/src/lib/file.ts b/packages/utils/src/lib/file.ts index 4fd5f01fcd..a727c7e428 100644 --- a/packages/utils/src/lib/file.ts +++ b/packages/utils/src/lib/file.ts @@ -34,9 +34,9 @@ export function downloadFromUri(uri: string, fileName: string) { } /** - * + * Validate if string is valid json object * @param jsonString - * @returns boolean + * @return boolean */ export function isValidJSON(jsonString: string): boolean { try {