Skip to content

Commit

Permalink
fix(context): crash on context export with vector data (#1188)
Browse files Browse the repository at this point in the history
* fix(context): crash on context export with vector data (infra-geo-ouverte/igo2#711)

* wip
  • Loading branch information
pelord authored and cbourget committed Mar 21, 2023
1 parent 750d461 commit e94770e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 10 additions & 3 deletions packages/context/src/lib/context-manager/shared/context.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {

import { AuthService } from '@igo2/auth';
import type { IgoMap, Layer, LayerOptions } from '@igo2/geo';
import { ExportService } from '@igo2/geo';

import { TypePermission } from './context.enum';
import {
Expand Down Expand Up @@ -76,6 +77,7 @@ export class ContextService {
private config: ConfigService,
private messageService: MessageService,
private storageService: StorageService,
private exportService: ExportService,
@Optional() private route: RouteService
) {
this.options = Object.assign(
Expand Down Expand Up @@ -579,17 +581,22 @@ export class ContextService {
const writer = new GeoJSON();
if (layer.ol.getSource() instanceof Cluster) {
const clusterSource = layer.ol.getSource() as Cluster;
let olFeatures = clusterSource.getFeatures();
olFeatures = (olFeatures as any).flatMap((cluster: any) => cluster.get('features'));
const cleanedOlFeatures = this.exportService.generateFeature(olFeatures, 'GeoJSON', '_featureStore');
features = writer.writeFeatures(
clusterSource.getFeatures(),
cleanedOlFeatures,
{
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
}
);
} else {
const source = layer.ol.getSource() as any;
const source = layer.ol.getSource() as olVectorSource;
const olFeatures = source.getFeatures();
const cleanedOlFeatures = this.exportService.generateFeature(olFeatures, 'GeoJSON', '_featureStore');
features = writer.writeFeatures(
source.getFeatures(),
cleanedOlFeatures,
{
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
Expand Down
7 changes: 4 additions & 3 deletions packages/geo/src/lib/import-export/shared/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ export class ExportService {
return this.exportAsync(exportOlFeatures, format, title, encoding, projectionIn, projectionOut);
}

private generateFeature(
public generateFeature(
olFeatures: OlFeature<OlGeometry>[],
format: ExportFormat
format: ExportFormat,
excludePrefix: string = '_'
): OlFeature<OlGeometry>[] {
if (format === ExportFormat.GPX && this.aggregateInComment) {
return this.generateAggregatedFeature(olFeatures);
Expand All @@ -69,7 +70,7 @@ export class ExportService {
return olFeatures.map((olFeature: OlFeature<OlGeometry>) => {
const keys = olFeature
.getKeys()
.filter((key: string) => !key.startsWith('_'));
.filter((key: string) => !key.startsWith(excludePrefix));
const properties = keys.reduce(
(acc: object, key: string) => {
acc[key] = olFeature.get(key);
Expand Down

0 comments on commit e94770e

Please sign in to comment.