Add geocoder cache
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,3 +11,4 @@ baselineEToData/*.png
|
|||||||
baselineEToData/*.tif
|
baselineEToData/*.tif
|
||||||
baselineEToData/dataPreparer[.exe]
|
baselineEToData/dataPreparer[.exe]
|
||||||
observations.json
|
observations.json
|
||||||
|
geocoderCache.json
|
||||||
|
|||||||
@@ -1,11 +1,49 @@
|
|||||||
|
import fs = require("fs");
|
||||||
|
|
||||||
import { GeoCoordinates } from "../../types";
|
import { GeoCoordinates } from "../../types";
|
||||||
|
|
||||||
export abstract class Geocoder {
|
export abstract class Geocoder {
|
||||||
|
|
||||||
|
private static cacheFile: string = __dirname + "/../../../geocoderCache.json";
|
||||||
|
|
||||||
|
private cache: Map<string, GeoCoordinates>;
|
||||||
|
|
||||||
|
public constructor() {
|
||||||
|
// Load the cache from disk.
|
||||||
|
if ( fs.existsSync( Geocoder.cacheFile ) ) {
|
||||||
|
this.cache = new Map( JSON.parse( fs.readFileSync( Geocoder.cacheFile, "utf-8" ) ) );
|
||||||
|
} else {
|
||||||
|
this.cache = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the cache to disk every 5 minutes.
|
||||||
|
setInterval( () => {
|
||||||
|
this.saveCache();
|
||||||
|
}, 5 * 60 * 1000 );
|
||||||
|
}
|
||||||
|
|
||||||
|
private saveCache(): void {
|
||||||
|
fs.writeFileSync( Geocoder.cacheFile, JSON.stringify( Array.from( this.cache.entries() ) ) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a location name to geographic coordinates.
|
* Converts a location name to geographic coordinates.
|
||||||
* @param location A location name.
|
* @param location A location name.
|
||||||
* @return A Promise that will be resolved with the GeoCoordinates of the specified location, or rejected with a
|
* @return A Promise that will be resolved with the GeoCoordinates of the specified location, or rejected with a
|
||||||
* CodedError.
|
* CodedError.
|
||||||
*/
|
*/
|
||||||
public abstract geocodeLocation( location: string ): Promise<GeoCoordinates>;
|
protected abstract geocodeLocation( location: string ): Promise<GeoCoordinates>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a location name to geographic coordinates, first checking the cache and updating it if necessary.
|
||||||
|
*/
|
||||||
|
public async getLocation( location: string ): Promise<GeoCoordinates> {
|
||||||
|
if ( this.cache.has( location ) ) {
|
||||||
|
return this.cache.get( location );
|
||||||
|
}
|
||||||
|
|
||||||
|
const coords: GeoCoordinates = await this.geocodeLocation( location );
|
||||||
|
this.cache.set( location, coords );
|
||||||
|
return coords;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export async function resolveCoordinates( location: string ): Promise< GeoCoordi
|
|||||||
const split: string[] = location.split( "," );
|
const split: string[] = location.split( "," );
|
||||||
return [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ];
|
return [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ];
|
||||||
} else {
|
} else {
|
||||||
return GEOCODER.geocodeLocation( location );
|
return GEOCODER.getLocation( location );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user