Skip to content

Commit

Permalink
fix(import-export): fix with ogre api
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Sep 20, 2019
1 parent 9bdde0a commit 555bb1e
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
</div>

<div class="igo-form-button-group">
<button mat-raised-button type="button" (click)="fileInput.click()" [disabled]="loading">
<button mat-raised-button type="button" (click)="fileInput.click()" [disabled]="loading$ | async">
{{'igo.geo.importExportForm.importButton' | translate}}
</button>
<igo-spinner [shown]="loading"></igo-spinner>
<igo-spinner [shown]="loading$ | async"></igo-spinner>
<input
#fileInput
type="file"
Expand Down Expand Up @@ -64,11 +64,11 @@ <h4>{{'igo.geo.importExportForm.importClarifications' | translate}}</h4>
<button
mat-raised-button
type="button"
[disabled]="!form.valid || loading"
[disabled]="!form.valid || (loading$ | async)"
(click)="handleExportFormSubmit(form.value)">
{{'igo.geo.importExportForm.exportButton' | translate}}
</button>
<igo-spinner [shown]="loading"></igo-spinner>
<igo-spinner [shown]="loading$ | async"></igo-spinner>
</div>

</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ mat-form-field {
width: 100%;
}

h4 {
padding: 0 5px;
}

.igo-form {
padding: 5px;
padding: 15px 5px;
}

.igo-form-button-group {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Subscription, throwError } from 'rxjs';
import { Subscription, BehaviorSubject } from 'rxjs';

import { MessageService, LanguageService } from '@igo2/core';

Expand Down Expand Up @@ -29,7 +29,7 @@ export class ImportExportComponent implements OnDestroy, OnInit {
public formats = ExportFormat;
public layers: VectorLayer[];
public inputProj: string = 'EPSG:4326';
public loading = false;
public loading$ = new BehaviorSubject(false);

private layers$$: Subscription;

Expand Down Expand Up @@ -58,26 +58,30 @@ export class ImportExportComponent implements OnDestroy, OnInit {
}

importFiles(files: File[]) {
this.loading = true;
this.loading$.next(true);
for (const file of files) {
this.importService
.import(file, this.inputProj)
.subscribe(
(features: Feature[]) => this.onFileImportSuccess(file, features),
(error: Error) => this.onFileImportError(file, error)
);
this.importService.import(file, this.inputProj).subscribe(
(features: Feature[]) => this.onFileImportSuccess(file, features),
(error: Error) => this.onFileImportError(file, error),
() => {
this.loading$.next(false);
}
);
}
}

handleExportFormSubmit(data: ExportOptions) {
this.loading = true;
this.loading$.next(true);
const layer = this.map.getLayerById(data.layer);
const olFeatures = layer.dataSource.ol.getFeatures();
this.exportService
.export(olFeatures, data.format, layer.title, this.map.projection)
.subscribe(
() => { this.loading = false;},
(error: Error) => this.onFileExportError(error)
() => {},
(error: Error) => this.onFileExportError(error),
() => {
this.loading$.next(false);
}
);
}

Expand All @@ -89,7 +93,6 @@ export class ImportExportComponent implements OnDestroy, OnInit {
}

private onFileImportSuccess(file: File, features: Feature[]) {
this.loading = false;
handleFileImportSuccess(
file,
features,
Expand All @@ -100,7 +103,7 @@ export class ImportExportComponent implements OnDestroy, OnInit {
}

private onFileImportError(file: File, error: Error) {
this.loading = false;
this.loading$.next(false);
handleFileImportError(
file,
error,
Expand All @@ -110,7 +113,7 @@ export class ImportExportComponent implements OnDestroy, OnInit {
}

private onFileExportError(error: Error) {
this.loading = false;
this.loading$.next(false);
handleFileExportError(error, this.messageService, this.languageService);
}
}
77 changes: 60 additions & 17 deletions packages/geo/src/lib/import-export/shared/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import OlFeature from 'ol/Feature';

import { downloadContent } from './export.utils';
import { ExportFormat } from './export.type';
import { ExportInvalidFileError, ExportNothingToExportError } from './export.errors';
import {
ExportInvalidFileError,
ExportNothingToExportError
} from './export.errors';

@Injectable({
providedIn: 'root'
})
export class ExportService {

static ogreFormats = {
GML: 'gml',
GPX: 'gpx',
Expand All @@ -39,15 +41,26 @@ export class ExportService {
projectionOut = 'EPSG:4326'
): Observable<void> {
const exportOlFeatures = olFeatures.map((olFeature: OlFeature) => {
const keys = olFeature.getKeys().filter((key: string) => !key.startsWith('_'));
const properties = keys.reduce((acc: object, key: string) => {
acc[key] = olFeature.get(key);
return acc;
}, {geometry: olFeature.getGeometry()});
const keys = olFeature
.getKeys()
.filter((key: string) => !key.startsWith('_'));
const properties = keys.reduce(
(acc: object, key: string) => {
acc[key] = olFeature.get(key);
return acc;
},
{ geometry: olFeature.getGeometry() }
);
return new OlFeature(properties);
});

return this.exportAsync(exportOlFeatures, format, title, projectionIn, projectionOut);
return this.exportAsync(
exportOlFeatures,
format,
title,
projectionIn,
projectionOut
);
}

private exportAsync(
Expand All @@ -68,15 +81,36 @@ export class ExportService {
if (ogreFormats.indexOf(format) >= 0) {
if (this.ogreUrl === undefined) {
if (ExportService.noOgreFallbacks.indexOf(format) >= 0) {
this.exportToFile(olFeatures, observer, format, title, projectionIn, projectionOut);
this.exportToFile(
olFeatures,
observer,
format,
title,
projectionIn,
projectionOut
);
} else {
observer.error(new ExportInvalidFileError());
}
return;
}
this.exportWithOgre(olFeatures, observer, format, title, projectionIn, projectionOut);
this.exportWithOgre(
olFeatures,
observer,
format,
title,
projectionIn,
projectionOut
);
} else {
this.exportToFile(olFeatures, observer, format, title, projectionIn, projectionOut);
this.exportToFile(
olFeatures,
observer,
format,
title,
projectionIn,
projectionOut
);
}
};

Expand Down Expand Up @@ -120,9 +154,10 @@ export class ExportService {
featureNS: 'http://example.com/feature'
});

const url = `${this.ogreUrl}/convert`;
const url = `${this.ogreUrl}/convertJson`;
const form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('target', '_blank');
form.setAttribute('action', url);

const geojsonField = document.createElement('input');
Expand All @@ -132,7 +167,10 @@ export class ExportService {
form.appendChild(geojsonField);

const outputNameField = document.createElement('input');
const outputName = format === 'Shapefile' ? `${title}.zip` : title;
const outputName =
format === 'Shapefile'
? `${title}.zip`
: `${title}.${format.toLowerCase()}`;
outputNameField.setAttribute('type', 'hidden');
outputNameField.setAttribute('name', 'outputName');
outputNameField.setAttribute('value', outputName);
Expand All @@ -141,7 +179,7 @@ export class ExportService {
const ogreFormat = ExportService.ogreFormats[format];
const outputFormatField = document.createElement('input');
outputFormatField.setAttribute('type', 'hidden');
outputFormatField.setAttribute('name', 'formatOutput');
outputFormatField.setAttribute('name', 'format');
outputFormatField.setAttribute('value', ogreFormat);
form.appendChild(outputFormatField);

Expand All @@ -153,10 +191,15 @@ export class ExportService {
}

private nothingToExport(olFeatures: OlFeature[], format: string): boolean {
if (olFeatures.length === 0) { return true; }
if (olFeatures.length === 0) {
return true;
}
if (format === 'GPX') {
const pointOrLine = olFeatures.find((olFeature) => {
return ['Point', 'LineString'].indexOf(olFeature.getGeometry().getType()) >= 0;
const pointOrLine = olFeatures.find(olFeature => {
return (
['Point', 'LineString'].indexOf(olFeature.getGeometry().getType()) >=
0
);
});
return pointOrLine === undefined;
}
Expand Down
Loading

0 comments on commit 555bb1e

Please sign in to comment.