Verified Commit 93981310 authored by Michael Auer's avatar Michael Auer
Browse files

closes #9 added keepColumn option with renameing/alias option

parent d8b6375e
import {FeatureTypeConfig} from './config_types_interfaces';
import {FeatureCollection} from '@turf/helpers';
import {PgColumnTypes} from './PgColumnTypes';
//abstract class and factory for postgis, GeoJSON, ...
export abstract class FeatureType {
private config: FeatureTypeConfig<any>;
protected config: FeatureTypeConfig<any>;
protected keepColumns: { name:string; as?:string }[];
protected constructor(config: FeatureTypeConfig<any>) {
this.config = config;
if ('keepColumns' in config) {
//sourceConfig
this.keepColumns = config.keepColumns || [];
} else {
//targetConfig
this.keepColumns = [];
}
}
/**
......@@ -20,12 +28,18 @@ export abstract class FeatureType {
*/
abstract async getIdJsType(): Promise<string>
/**
* Returns a look-up object with column names as keys and their data types as values
*/
abstract async getPgColumnTypes(): Promise<{[column:string]: string}>
/**
* @return GeoJSON<FeatureCollection>
**/
abstract async getFeatures(): Promise<FeatureCollection>;
abstract async writeFeatures(featureCollection: FeatureCollection): Promise<void>;
abstract async writeFeatures(featureCollection: FeatureCollection, pgColumnTypes?: PgColumnTypes): Promise<void>;
/**
* physically deletes the feature type from the store (e.g. file from disk or table from database)
......
This diff is collapsed.
This diff is collapsed.
import moment = require('moment');
export class PgColumnTypes {
private sourceTypes: { [column: string]: string };
private queryType: string;
private transformToWebmercator: boolean
private horizontalTimestamps: boolean;
private extendedTypes: { [column: string]: string };
/**
*
* @param sourceTypes sourceType after modification by keepColumns
* @param queryType the ohsomeAPI endpoint (queryType string)
* @param transformToWebmercator
*/
constructor({
sourceTypes,
queryType,
transformToWebmercator,
horizontalTimestamps
}: {
sourceTypes: { [column: string]: string };
queryType: string;
transformToWebmercator: boolean;
horizontalTimestamps: boolean;
}) {
this.sourceTypes = sourceTypes;
this.queryType = queryType;
this.transformToWebmercator = transformToWebmercator;
this.horizontalTimestamps = horizontalTimestamps;
this.extendedTypes = {...this.sourceTypes,...this.getColumnTypesByQueryType(this.queryType)};
// set geometry projection
if(this.transformToWebmercator && "geom" in this.extendedTypes){
this.extendedTypes.geom = this.extendedTypes.geom.replace(/\d+/, "3857");
}
}
//reserved column names
// public static DEFAULT = {
// id: 'int4',
// value: 'double precision',
// value2: 'double precision',
// value_per_area: 'double precision',
// ratio: 'double precision',
// timestamp: 'timestamptz',
// from_timestamp: 'timestamptz',
// to_timestamp: 'timestamptz',
// tag: 'text',
// geom: 'geometry'
// }
/**
* Use with columnNames array. Will order id first, geom last and rest of columns alphabetically
* @param a
* @param b
*/
public static columnOrderFn(a:string, b:string) {
if (a == 'id' || b == 'geom'){
return -1;
}
if (a == 'geom' || b == 'id'){
return 1;
}
if(a < b) {
return -1;
}
if(a > b) {
return 1;
}
return 0;
}
getTypeByColumnName(name:string){
if(this.extendedTypes[name]){
return this.extendedTypes[name]
} else {
return 'float8'; //double precision for 'value_per_area' and horizontal timstamps
}
// if(this.isIsoDateTimeOrDateTimeRange(name)){
// // for horizontal timesstamps
// return 'double precision';
// } else if (name === 'value_per_area'){
// return 'double precision';
// } else {
// return this.extendedTypes[name];
// }
}
private isIsoDateTimeOrDateTimeRange(columnName:string){
// console.log(columnName)
return columnName.split('/').every((datetime) => moment(datetime).isValid());
}
private getColumnTypesByQueryType(queryType: string){
function tvint() {
return {timestamp: 'timestamptz', value: 'int4'}
}
function tvfloat() {
return {timestamp: 'timestamptz', value: 'float8'}
}
function tvfinttag(){
return {timestamp: 'timestamptz', value: 'int4', tag: 'text'}
}
function tvfloattag(){
return {timestamp: 'timestamptz', value: 'float8', tag: 'text'}
}
const tvv2intratio = () => {
let verticalTsCols = {timestamp: 'timestamptz', value: 'int4', value2: 'int4', ratio: 'float8'};
let horizontalTsCols = {attrName: 'text'};
return (this.horizontalTimestamps)? horizontalTsCols : verticalTsCols;
}
const tvv2floatratio = () => {
let verticalTsCols = {timestamp: 'timestamptz', value: 'float8', value2: 'float8', ratio: 'float8'};
let horizontalTsCols = {attrName: 'text'};
return (this.horizontalTimestamps)? horizontalTsCols : verticalTsCols;
}
function ftttvfloat(){
return {from_timestamp: 'timestamptz', to_timestamp: 'timestamptz', value: 'float8'}
}
function ftttvint(){
return {from_timestamp: 'timestamptz', to_timestamp: 'timestamptz', value: 'int4'}
}
switch (queryType) {
case "elements/count/density/groupBy/boundary":
case "elements/area/density/groupBy/boundary":
case "elements/area/groupBy/boundary":
case "elements/length/density/groupBy/boundary":
case "elements/length/groupBy/boundary":
case "elements/perimeter/density/groupBy/boundary":
case "elements/perimeter/groupBy/boundary":
return tvfloat();
case "elements/count/density/groupBy/boundary/groupBy/tag":
case "elements/area/density/groupBy/boundary/groupBy/tag":
case "elements/area/groupBy/boundary/groupBy/tag":
case "elements/length/density/groupBy/boundary/groupBy/tag":
case "elements/length/groupBy/boundary/groupBy/tag":
case "elements/perimeter/density/groupBy/boundary/groupBy/tag":
case "elements/perimeter/groupBy/boundary/groupBy/tag":
return tvfloattag();
case "elements/count/groupBy/boundary":
return tvint();
case "elements/count/groupBy/boundary/groupBy/tag":
return tvfinttag();
case "elements/count/ratio/groupBy/boundary":
return tvv2intratio();
case "elements/area/ratio/groupBy/boundary":
case "elements/length/ratio/groupBy/boundary":
case "elements/perimeter/ratio/groupBy/boundary":
return tvv2floatratio();
case "users/count/density/groupBy/boundary":
case "contributions/count/density/groupBy/boundary":
return ftttvfloat();
case "users/count/groupBy/boundary":
case "contributions/count/groupBy/boundary":
return ftttvint();
default:
throw Error(`Query Type not implemented: ${queryType}`)
}
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -23,10 +23,15 @@ export interface PostgisStore extends Store {
export interface FeatureTypeConfig<T extends GeoJSONStore | PostgisStore> {
name? : string;
store: T;// GeoJSONStore | PostgisStore;
keepColumns?: { name: string; as?: string }[];
}
export interface SourceFeatureTypeConfig {
geometryId: string|number;
geometryId: string;
//not reserved column names as id, timestamp, value, value2, ratio etc.
// input column names can be renamed using `{name: 'origname', as: 'newname'}` syntax
// e.g. [{name: 'res', as: 'resolution'}]
keepColumns?: { name:string; as?:string }[];
}
export interface TargetFeatureTypeConfig {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment