Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(geo): import and export shp file #1665

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading