From 9d9e8c6c3f0fc129661e697328955be76f1cf7d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Lord?= <7397743+pelord@users.noreply.github.com> Date: Tue, 24 May 2022 13:18:41 -0400 Subject: [PATCH] fix(export): export to latin1 with no latin1 equivalent characters (#1042) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(export): export to latin1 with no latin1 equivalent characters * lint * wip Co-authored-by: Pierre-Étienne Lord --- package-lock.json | 5 +++++ package.json | 1 + .../geo/src/lib/import-export/shared/export.service.ts | 10 +++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index e92a68bb95..483a2644e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21040,6 +21040,11 @@ "integrity": "sha512-JcKqAHLPxcdb9KM49dufGXn2x3ssnfjbcaQdLlfZsL9rH9wgDQjUtDxbo8NE0F6SFvydeu1VhZe7hZuHsB2/pw==", "dev": true }, + "windows-1252": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/windows-1252/-/windows-1252-3.0.4.tgz", + "integrity": "sha512-PxP05UYnSeqhkkcHeT3xhpY/BsMOX6URw4ZW9ljOlhkQ6UZRRj/xvS3o5kG+fwefCztUtW36+DHBBNnWfXPIsw==" + }, "windows-release": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/windows-release/-/windows-release-4.0.0.tgz", diff --git a/package.json b/package.json index 70659fe0ce..c8a4c8a856 100644 --- a/package.json +++ b/package.json @@ -129,6 +129,7 @@ "typy": "^3.3.0", "unorm": "^1.4.1", "web-animations-js": "^2.3.2", + "windows-1252": "^3.0.4", "zone.js": "~0.11.4" }, "devDependencies": { 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 bf30aacf2b..dab0a7847e 100644 --- a/packages/geo/src/lib/import-export/shared/export.service.ts +++ b/packages/geo/src/lib/import-export/shared/export.service.ts @@ -8,6 +8,7 @@ import { Observable, Observer } from 'rxjs'; import * as olformat from 'ol/format'; import OlFeature from 'ol/Feature'; import type { default as OlGeometry } from 'ol/geom/Geometry'; +import {encode} from 'windows-1252'; import { ExportFormat, EncodingFormat } from './export.type'; @@ -168,7 +169,7 @@ export class ExportService { projectionIn: string, projectionOut: string ) { - const featuresText: string = new olformat.GeoJSON().writeFeatures( + let featuresText: string = new olformat.GeoJSON().writeFeatures( olFeatures, { dataProjection: projectionOut, @@ -189,6 +190,13 @@ export class ExportService { form.enctype = 'application/x-www-form-urlencoded; charset=utf-8;'; } else if (encodingType === EncodingFormat.LATIN1) { const enctype = 'ISO-8859-1'; + const featuresJson = JSON.parse(featuresText); + featuresJson.features.map(f => { + const encodedProperties = String.fromCharCode + .apply(null, encode(JSON.stringify(f.properties), { mode: 'replacement' })); + f.properties = JSON.parse(encodedProperties); + }); + featuresText = JSON.stringify(featuresJson); const encoding = document.createElement('input'); encoding.setAttribute('type', 'hidden'); encoding.setAttribute('name', 'encoding');