Skip to content

Commit

Permalink
feat(ConfigFileToGeoDBService): using zipped geojson (#1140)
Browse files Browse the repository at this point in the history
* feat(ConfigFileToGeoDBService): using zipped geojson

* wip

* refactor(geoDB): typo in variables

Co-authored-by: Pierre-Étienne Lord <[email protected]>
  • Loading branch information
pelord and Pierre-Étienne Lord authored Nov 30, 2022
1 parent f9e3fe0 commit 8c7dc93
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
39 changes: 35 additions & 4 deletions packages/geo/src/lib/offline/geoDB/configFileToGeoDB.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { catchError, concatMap } from 'rxjs/operators';
import { GeoDBService } from './geoDB.service';
import { of, zip } from 'rxjs';
import { DatasToIDB, GeoDBData } from './geoDB.interface';
import { default as JSZip } from 'jszip';
import { InsertSourceInsertDBEnum } from './geoDB.enums';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -39,7 +41,7 @@ export class ConfigFileToGeoDBService {
}
if (currentDate >= geoData.triggerDate) {
if (geoData.action === 'update') {
const insertEvent = `${geoData.source || 'automatedDataUpdate'} (${geoData.triggerDate})`;
const insertEvent = `${geoData.source || InsertSourceInsertDBEnum.System} (${geoData.triggerDate})`;
geoData.urls.map((url) => {
datas$.push(
this.geoDBService.getByID(url).pipe(concatMap((res: GeoDBData) => {
Expand All @@ -50,8 +52,34 @@ export class ConfigFileToGeoDBService {
{ disableTimeOut: true, progressBar: false, closeButton: true, tapToDismiss: false });
firstDownload = false;
}
return this.http.get(url)
.pipe(concatMap(r => this.geoDBService.update(url, url as any, r, 'system' as any, insertEvent)));
let responseType: any = 'json';
const isZip = this.isZip(url);
if (isZip) {
responseType = 'arraybuffer';
}
return this.http.get(url, { responseType }).pipe(concatMap(r => {
if (isZip) {
const observables$ = [this.geoDBService.update(url, url, {}, InsertSourceInsertDBEnum.System, insertEvent)];
JSZip.loadAsync(r)
.then((zipped) => {
zipped.forEach((relativePath) => {
if (relativePath.toLocaleLowerCase().endsWith('.geojson')) {
zipped.file(relativePath).async("base64").then((r) => {
const geojson = JSON.parse(atob(r));
const subUrl = geoData.zippedBaseUrl || '';
const zippedUrl = subUrl + (subUrl.endsWith('/') ? '' : '/') + relativePath;
observables$.push(
this.geoDBService.update(zippedUrl, url, geojson, InsertSourceInsertDBEnum.System, insertEvent)
);
}
);
}
});
});
return zip(observables$);
}
return this.geoDBService.update(url, url, r, InsertSourceInsertDBEnum.System, insertEvent);
}));
} else {
return of(false);
}
Expand All @@ -78,5 +106,8 @@ export class ConfigFileToGeoDBService {
});
}


private isZip(value) {
const regex = /(zip)$/;
return typeof value === 'string' && regex.test(value.toLowerCase());
}
}
1 change: 1 addition & 0 deletions packages/geo/src/lib/offline/geoDB/geoDB.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface GeoDataToIDB {
action: "delete" | 'update';
urls: string[];
source?: string;
zippedBaseUrl?: string;
}

export interface DatasToIDB {
Expand Down
22 changes: 11 additions & 11 deletions packages/geo/src/lib/offline/geoDB/geoDB.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { InsertSourceInsertDBEnum } from './geoDB.enums';
export class GeoDBService {
readonly dbName: string = 'geoData';
public collisionsMap: Map<number, string[]> = new Map();
public _newTiles: number = 0;
public _newData: number = 0;

constructor(
private ngxIndexedDBService: NgxIndexedDBService,
Expand Down Expand Up @@ -56,7 +56,7 @@ export class GeoDBService {
}),
concatMap((dbObject: GeoDBData) => {
if (!dbObject) {
this._newTiles++;
this._newData++;
return this.ngxIndexedDBService.add(this.dbName, geoDBData);
} else {
const currentRegionID = dbObject.regionID;
Expand Down Expand Up @@ -116,11 +116,11 @@ export class GeoDBService {
return this.ngxIndexedDBService.deleteByKey(this.dbName, url);
}

getRegionTileCountByID(id: number): Observable<number> {
getRegionCountByID(id: number): Observable<number> {
const subject: Subject<number> = new Subject();
const dbRequest = this.getRegionByID(id)
.subscribe((tiles) => {
subject.next(tiles.length);
.subscribe((datas) => {
subject.next(datas.length);
subject.complete();
});
return subject;
Expand All @@ -143,9 +143,9 @@ export class GeoDBService {

const IDBKey: IDBKeyRange = IDBKeyRange.only(id);
const dbRequest = this.ngxIndexedDBService.getAllByIndex(this.dbName, 'regionID', IDBKey);
dbRequest.subscribe((tiles: GeoDBData[]) => {
tiles.forEach((tile) => {
this.ngxIndexedDBService.deleteByKey(this.dbName, tile.url);
dbRequest.subscribe((datas: GeoDBData[]) => {
datas.forEach((data) => {
this.ngxIndexedDBService.deleteByKey(this.dbName, data.url);
});
});
return dbRequest;
Expand All @@ -161,7 +161,7 @@ export class GeoDBService {

resetCounters() {
this.resetCollisionsMap();
this._newTiles = 0;
this._newData = 0;
}

resetCollisionsMap() {
Expand All @@ -182,7 +182,7 @@ export class GeoDBService {
}
}

get newTiles(): number {
return this._newTiles;
get newData(): number {
return this._newData;
}
}

0 comments on commit 8c7dc93

Please sign in to comment.