Commit af31fbce authored by Michael Auer's avatar Michael Auer
Browse files

resolves #2 by removing empty params completely from query params

parent f92d8f69
import {
Ohsome2XConfig,
OhsomeQueryConfig,
OhsomeQueryConfig, OhsomeQueryConfigFormat,
TargetPostgisFeatureTypeConfig
} from './config_types_interfaces';
import normalizeUrl from 'normalize-url';
......@@ -42,8 +42,11 @@ class Ohsome2X {
this.sourceFeatureType = null;
this.targetFeatureType = null;
this.ohsomeApiUrl = config.ohsomeApiUrl || OHSOME_API_URL;
//default use csv because its way faster than parsing json
this.config.ohsomeQuery.format = (this.config.ohsomeQuery.format == null || this.config.ohsomeQuery.format.trim() === '')? 'csv' : this.config.ohsomeQuery.format.trim() as 'csv' | 'json';
this.log_start = new Date();
this.log_end = new Date();
console.log('Start at: ' + this.log_start.toLocaleString());
......@@ -263,7 +266,7 @@ class Ohsome2X {
);
}
console.timeEnd('computeValuePerArea');
console.log(JSON.stringify(targetFeatureCollection, undefined, 2));
// console.log(JSON.stringify(targetFeatureCollection, undefined, 2));
await this.targetFeatureType.writeFeatures(targetFeatureCollection);
//await this.targetFeatureType.writeOhsomeFeatures(ohsomeResults.data, false);
......@@ -345,26 +348,23 @@ class Ohsome2X {
async getOhsomeResults(ohsomeQuery: OhsomeQueryConfig): Promise<AxiosResponse> {
// axios.interceptors.request.use(request => {
// console.log('Starting Request', request)
// return request
// });
const keys = ohsomeQuery.keys || '';
const values = ohsomeQuery.values || '';
const format = ohsomeQuery.format || 'csv';
// set empty strings to null
const keys = (ohsomeQuery.keys != null && ohsomeQuery.keys.trim() != '')? ohsomeQuery.keys.trim() : undefined;
const values = (ohsomeQuery.values != null && ohsomeQuery.values.trim() != '')? ohsomeQuery.values.trim() : undefined;
const format: OhsomeQueryConfigFormat = (ohsomeQuery.format != null && ohsomeQuery.format.trim() != '')? ohsomeQuery.format.trim() as 'csv'|'json' : undefined;
try {
console.log('----------------------------------');
console.log('Stringify Cells');
console.time('stringify');
if (ohsomeQuery.bpolys == null) {
throw Error('bpolys undefined in OhsomeQueryConfig');
}
let bpolyString = JSON.stringify(ohsomeQuery.bpolys);
console.timeEnd('stringify');
console.log('----------------------------------');
console.log('Stringify querystring');
console.time('querystring');
let dataString = querystring.stringify({
let bpolyString = (typeof ohsomeQuery.bpolys === 'object')? JSON.stringify(ohsomeQuery.bpolys) : ohsomeQuery.bpolys.trim();
//default query object, empty values will be removed due to issue with ohsomeAPI: https://gitlab.gistools.geog.uni-heidelberg.de/giscience/big-data/ohsome/ohsome-api/issues/72
let dataObject: {} = {
bpolys: bpolyString,
keys: keys,
values: values,
......@@ -372,15 +372,18 @@ class Ohsome2X {
types: ohsomeQuery.types,
showMetadata: true,
format: format
});
console.timeEnd('querystring');
console.log('----------------------------------');
}
// removes properties with null or undefined
dataObject = Object.entries(dataObject).reduce((a,[k,v]) => (v == null ? a : {...a, [k]:v}), {});
let dataString = querystring.stringify(dataObject);
// always make a group by boundary query for backward compatibility
// complete ohsomeAPI resource path should be used
let queryType = ohsomeQuery.queryType.replace(/(.*)(\/)?(\/groupBy\/boundary)(\/)?/, "$1");
console.log(`Querying ohsome-API: ${this.ohsomeApiUrl}/${queryType}/groupBy/boundary`);
console.log('with params:', dataObject);
const stats = await axios(<AxiosRequestConfig>{
url: `${this.ohsomeApiUrl}/${queryType}/groupBy/boundary`,
method: 'post',
......@@ -388,9 +391,12 @@ class Ohsome2X {
maxContentLength: 1024 * 1024 * 1024 * 1024,
data: dataString
});
console.log('----------------------------------');
console.log('Response Metadata', JSON.stringify(stats.data.metadata));
console.log('----------------------------------');
//only for json requests with showMetadata=true, not csv
if (typeof stats.data === 'object' && "metadata" in stats.data){
console.log('----------------------------------');
console.log('Response Metadata', JSON.stringify(stats.data.metadata));
console.log('----------------------------------');
}
return stats;
} catch (e) {
console.log('Ohsome API request failed.');
......
......@@ -63,18 +63,19 @@ export interface TargetPostgisFeatureTypeConfig extends PostgisFeatureTypeConfig
export type OhsomeQueryConfigTypes = 'node' | 'way' | 'relation' | 'node,way' | 'node,relation' | 'way,relation' | 'node,way,relation' | 'point' | 'line' | 'polygon' | 'point,line' | 'point,polygon' | 'line,polygon' | 'point,line,polygon' | '';
export type OhsomeQueryConfigFormat = 'json' | 'csv';
export type OhsomeQueryConfigFormat = 'json' | 'csv' | undefined;
export interface OhsomeQueryConfig {
queryType: string; // e.g. elements/count for elements/count/groupBy/boundary
keys: string; // comma seperated values e.g.['highway','maxspeed'].join(),
values: string; // comma seperated values e.g. ['','50'].join(),
keys?: string; // comma seperated values e.g.['highway','maxspeed'].join(),
values?: string; // comma seperated values e.g. ['','50'].join(),
types: OhsomeQueryConfigTypes;
time: string; // e.g. ISO8601 start/end/interval_period '2015-01-01/2017-01-01/P1Y'
bpolys?: FeatureCollection | string;
bboxes?: string;
bcircles?: string
bcircles?: string;
format?: OhsomeQueryConfigFormat; // default is 'csv'. Others are 'json' | ('geojson' -> not yet supported)
showMetadata?: boolean;
}
export interface Ohsome2XConfig {
......
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