Skip to content

Commit

Permalink
fix(geo): import/export shp file handle invalid style
Browse files Browse the repository at this point in the history
check _style length before export

set ShapefileMaxLength variable to 255

rename const

rename
  • Loading branch information
aziz-access authored and alecarn committed Apr 5, 2024
1 parent 2de5c63 commit aa6d1fd
Show file tree
Hide file tree
Showing 3 changed files with 20 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
8 changes: 8 additions & 0 deletions packages/geo/src/lib/import-export/shared/import.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion packages/geo/src/lib/style/shared/feature/feature-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(): (
Expand All @@ -21,7 +22,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

0 comments on commit aa6d1fd

Please sign in to comment.