Skip to content

Commit

Permalink
fix(geo): import and export shp file (#1665)
Browse files Browse the repository at this point in the history
* fix(geo): transfer isValidJSON function to utils pakage
  • Loading branch information
aziz-access committed May 15, 2024
1 parent ca633c1 commit 67725f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/geo/src/lib/import-export/shared/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
} from './export.errors';
import { EncodingFormat, ExportFormat } from './export.type';

const SHAPEFILE_FIELD_MAX_LENGHT = 255;

@Injectable({
providedIn: 'root'
})
Expand Down Expand Up @@ -78,9 +80,16 @@ export class ExportService {
}

return olFeatures.map((olFeature: OlFeature<OlGeometry>) => {
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);
Expand Down
4 changes: 3 additions & 1 deletion packages/geo/src/lib/style/shared/feature/feature-style.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isValidJSON } from '@igo2/utils';

import olFeature from 'ol/Feature';
import * as olGeom from 'ol/geom';
import type { default as OlGeometry } from 'ol/geom/Geometry';
Expand All @@ -21,7 +23,7 @@ export function featureRandomStyleFunction(): (
});
return (olFeature: olFeature<OlGeometry>, resolution: number) => {
const customStyle = olFeature.get('_style');
if (customStyle) {
if (customStyle && isValidJSON(customStyle)) {
if (
customStyle.circle &&
olFeature.get('rad') &&
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/src/lib/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ export function downloadFromUri(uri: string, fileName: string) {

document.body.removeChild(element);
}

/**
* Validate if string is valid json object
* @param jsonString
* @return boolean
*/
export function isValidJSON(jsonString: string): boolean {
try {
return JSON.parse(jsonString) && !!jsonString;
} catch (e) {
return false;
}
}

0 comments on commit 67725f7

Please sign in to comment.