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

Import export naming #631

Merged
merged 2 commits into from
May 4, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ <h4>{{'igo.geo.importExportForm.importClarifications' | translate}}</h4>
</mat-form-field>
</div>

<div class="igo-input-container" *ngIf="forceNaming">
<mat-form-field>
<input matInput formControlName="name" placeholder="{{'igo.geo.importExportForm.exportFileNamePlaceholder' | translate}}">
</mat-form-field>
</div>

<div class="igo-form-button-group">
<button
mat-raised-button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Subscription, BehaviorSubject } from 'rxjs';

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

import { Feature } from '../../feature/shared/feature.interfaces';
import { IgoMap } from '../../map/shared/map';
Expand All @@ -29,10 +30,11 @@ import { StyleListService } from '../style-list/style-list.service';
})
export class ImportExportComponent implements OnDestroy, OnInit {
public form: FormGroup;
public formats = ExportFormat;
public formats;
public layers: VectorLayer[];
public inputProj: string = 'EPSG:4326';
public loading$ = new BehaviorSubject(false);
public forceNaming = false;

private layers$$: Subscription;

Expand All @@ -52,6 +54,7 @@ export class ImportExportComponent implements OnDestroy, OnInit {
private formBuilder: FormBuilder,
private config: ConfigService
) {
this.chargerConfiguration();
this.buildForm();
}

Expand Down Expand Up @@ -92,12 +95,17 @@ export class ImportExportComponent implements OnDestroy, OnInit {
handleExportFormSubmit(data: ExportOptions) {
this.loading$.next(true);
const layer = this.map.getLayerById(data.layer);
let filename = layer.title;
if (data.name !== undefined) {
filename = data.name;
}

let olFeatures = layer.dataSource.ol.getFeatures();
if (layer.dataSource instanceof ClusterDataSource) {
olFeatures = olFeatures.flatMap((cluster: any) => cluster.get('features'));
}
this.exportService
.export(olFeatures, data.format, layer.title, this.map.projection)
.export(olFeatures, data.format, filename, this.map.projection)
.subscribe(
() => {},
(error: Error) => this.onFileExportError(error),
Expand All @@ -108,10 +116,18 @@ export class ImportExportComponent implements OnDestroy, OnInit {
}

private buildForm() {
this.form = this.formBuilder.group({
format: ['', [Validators.required]],
layer: ['', [Validators.required]]
});
if (this.forceNaming) {
this.form = this.formBuilder.group({
format: ['', [Validators.required]],
layer: ['', [Validators.required]],
name: ['', [Validators.required]],
});
} else {
this.form = this.formBuilder.group({
format: ['', [Validators.required]],
layer: ['', [Validators.required]],
});
}
}

private onFileImportSuccess(file: File, features: Feature[]) {
Expand Down Expand Up @@ -151,4 +167,55 @@ export class ImportExportComponent implements OnDestroy, OnInit {
this.loading$.next(false);
handleFileExportError(error, this.messageService, this.languageService);
}

private chargerConfiguration() {
if (this.config.getConfig('importExport.forceNaming') !== undefined) {
this.forceNaming = this.config.getConfig('importExport.forceNaming');
}

if (this.config.getConfig('importExport.formats') !== undefined) {
const liste = this.validerListeFormat(this.config.getConfig('importExport.formats'));
this.formats = strEnum(liste);
} else {
this.formats = ExportFormat;
}
}

private validerListeFormat(liste: string[]): string[] {
return liste.filter(format => {
if (format.toUpperCase() === ExportFormat.CSV.toUpperCase() ||
format.toUpperCase() === ExportFormat.GML.toUpperCase() ||
format.toUpperCase() === ExportFormat.GPX.toUpperCase() ||
format.toUpperCase() === ExportFormat.GeoJSON.toUpperCase() ||
format.toUpperCase() === ExportFormat.KML.toUpperCase() ||
format.toUpperCase() === ExportFormat.Shapefile.toUpperCase()) {
return format;
}
}).map(format => {
if (format.toUpperCase() === ExportFormat.CSV.toUpperCase()) {
format = ExportFormat.CSV;
return format;
}
if (format.toUpperCase() === ExportFormat.GML.toUpperCase()) {
format = ExportFormat.GML;
return format;
}
if (format.toUpperCase() === ExportFormat.GPX.toUpperCase()) {
format = ExportFormat.GPX;
return format;
}
if (format.toUpperCase() === ExportFormat.GeoJSON.toUpperCase()) {
format = ExportFormat.GeoJSON;
return format;
}
if (format.toUpperCase() === ExportFormat.KML.toUpperCase()){
format = ExportFormat.KML;
return format;
}
if (format.toUpperCase() === ExportFormat.Shapefile.toUpperCase()) {
format = ExportFormat.Shapefile;
return format;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { ExportFormat } from './export.type';
export interface ExportOptions {
format: ExportFormat;
layer: string;
name: string;
}
2 changes: 2 additions & 0 deletions packages/geo/src/lib/import-export/shared/import.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface ImportExportServiceOptions {
url: string;
clientSideFileSizeMaxMb?: number;
forceNaming?: boolean;
formats?: string[];
}
1 change: 1 addition & 0 deletions packages/geo/src/locale/en.geo.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"exportButton": "Export",
"exportFormatPlaceholder": "Format",
"exportLayerPlaceholder": "Layer",
"exportFileNamePlaceholder": "Filename",
"exportTabTitle": "Export",
"importButton": "Import",
"importProjPlaceholder": "Coordinate system",
Expand Down
1 change: 1 addition & 0 deletions packages/geo/src/locale/fr.geo.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"exportButton": "Exporter",
"exportFormatPlaceholder": "Format",
"exportLayerPlaceholder": "Données géospatiales",
"exportFileNamePlaceholder": "Nom du fichier",
"exportTabTitle": "Exporter",
"importButton": "Importer",
"importProjPlaceholder": "Système de coordonnées",
Expand Down