/
query.service.ts
241 lines (199 loc) · 7.27 KB
/
query.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { uuid } from '../../utils/uuid';
import { Feature, FeatureType, FeatureFormat, SourceFeatureType,
FeatureService } from '../../feature';
import { DataSource, QueryableDataSource } from '../../datasource';
import { Layer } from '../../layer';
import { QueryFormat } from './query.enum';
import { QueryOptions } from './query.interface';
import ol = require('openlayers');
@Injectable()
export class QueryService {
constructor(private http: HttpClient,
private featureService: FeatureService) { }
query(layers: Layer[], options: QueryOptions): Observable<Feature[]>[] {
return layers
.filter((layer: Layer) => layer.visible && layer.isInResolutionsRange)
.map((layer: Layer) => this.queryDataSource(layer.dataSource, options, layer.zIndex));
}
queryDataSource(dataSource: DataSource, options: QueryOptions,
zIndex: number): Observable<Feature[]> {
const url = (dataSource as any as QueryableDataSource).getQueryUrl(options);
const request = this.http.get(url, {responseType: 'text'});
this.featureService.clear();
return request.map(res => this.extractData(res, dataSource, options, url, zIndex));
}
private extractData(res, dataSource: DataSource, options: QueryOptions,
url: string, zIndex: number): Feature[] {
const queryDataSource = (dataSource as any as QueryableDataSource);
let features = [];
switch (queryDataSource.queryFormat) {
case QueryFormat.GML2:
features = this.extractGML2Data(res, zIndex);
break;
case QueryFormat.GML3:
features = this.extractGML3Data(res, zIndex);
break;
case QueryFormat.JSON:
case QueryFormat.GEOJSON:
features = this.extractGeoJSONData(res);
break;
case QueryFormat.TEXT:
features = this.extractTextData(res);
break;
case QueryFormat.HTML:
features = this.extractHtmlData(res, queryDataSource.queryHtmlTarget, url);
break;
default:
break;
}
return features.map((feature: Feature, index: number) => {
const title = feature.properties[queryDataSource.queryTitle];
return Object.assign(feature, {
id: uuid(),
source: dataSource.title,
sourceType: SourceFeatureType.Query,
order: 1000 - zIndex,
title: title ? title : `${dataSource.title} (${index + 1})`,
projection: options.projection
});
});
}
private extractGML2Data(res, zIndex) {
let parser = new ol.format.GML2();
let features = parser.readFeatures(res);
// Handle non standard GML output (MapServer)
if (features.length === 0) {
parser = new ol.format.WMSGetFeatureInfo();
features = parser.readFeatures(res);
}
return features.map(feature => this.featureToResult(feature, zIndex));
}
private extractGML3Data(res, zIndex) {
const parser = new ol.format.GML3();
const features = parser.readFeatures(res);
return features.map(feature => this.featureToResult(feature, zIndex));
}
private extractGeoJSONData(res) {
let features = [];
try {
features = JSON.parse(res).features;
} catch (e) {
console.warn('query.service: Unable to parse geojson', '\n', res);
}
return features;
}
private extractTextData(res) {
// TODO
return [];
}
private extractHtmlData(res, htmlTarget, url) {
// _blank , modal , innerhtml or undefined
const searchParams = this.getQueryParams(url.toLowerCase());
const bboxRaw = searchParams['bbox'];
const width = parseInt(searchParams['width'], 10);
const height = parseInt(searchParams['height'], 10);
const wmsVersion = searchParams['version'];
let xPosition;
let yPosition;
if (wmsVersion === '1.3.0') {
xPosition = parseInt(searchParams['i'], 10);
yPosition = parseInt(searchParams['j'], 10);
} else {
xPosition = parseInt(searchParams['x'], 10);
yPosition = parseInt(searchParams['y'], 10);
}
const bbox = bboxRaw.split(',');
let threshold = (Math.abs(parseFloat(bbox[0])) - Math.abs(parseFloat(bbox[2]))) * (0.1);
// for context in degree (EPSG:4326,4269...)
if (Math.abs(parseFloat(bbox[0])) < 180) {
threshold = 0.045;
}
const clickx = parseFloat(bbox[0]) + Math.abs(parseFloat(bbox[0]) - parseFloat(bbox[2]))
* xPosition / width - threshold;
const clicky = parseFloat(bbox[1]) + Math.abs(parseFloat(bbox[1]) - parseFloat(bbox[3]))
* yPosition / height - threshold;
const clickx1 = clickx + threshold * 2;
const clicky1 = clicky + threshold * 2;
const wktPoly = 'POLYGON((' + clickx + ' ' + clicky + ', ' + clickx + ' ' + clicky1 +
', ' + clickx1 + ' ' + clicky1 + ', ' + clickx1 + ' ' + clicky +
', ' + clickx + ' ' + clicky + '))';
const format = new ol.format.WKT();
const tenPercentWidthGeom = format.readFeature(wktPoly);
const f = (tenPercentWidthGeom.getGeometry() as any);
let targetIgo2 = '_blank';
let iconHtml = 'link';
switch (htmlTarget) {
case 'newtab':
targetIgo2 = '_blank';
break;
case 'modal':
targetIgo2 = 'modal';
iconHtml = 'place';
break;
case 'innerhtml':
targetIgo2 = 'innerhtml';
iconHtml = 'place';
const bodyTagStart = res.toLowerCase().indexOf('<body>');
const bodyTagEnd = res.toLowerCase().lastIndexOf('</body>') + 7;
// replace \r \n and ' ' with '' to validate if the body is really empty.
const body = res.slice(bodyTagStart, bodyTagEnd).replace(/(\r|\n|\s)/g, '' );
if ( body === '<body></body>' || res === '' ) {
return [];
}
break;
}
return [{
id: 'html1',
source: 'title',
type: FeatureType.Feature,
format: FeatureFormat.GeoJSON,
title: 'title',
icon: iconHtml,
projection: 'EPSG:3857',
properties: {target: targetIgo2, body: res, url: url},
geometry: {type: f.getType(), coordinates: f.getCoordinates()}
}];
}
private getQueryParams(url) {
const queryString = url.split('?');
if (!queryString[1]) {
return;
}
const pairs = queryString[1].split('&');
const result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
result[pair[0]] = decodeURIComponent(pair[1] || '');
});
return result;
}
private featureToResult(feature: ol.Feature, zIndex: number): Feature {
const featureGeometry = (feature.getGeometry() as any);
const properties = Object.assign({}, feature.getProperties());
delete properties['geometry'];
delete properties['boundedBy'];
let geometry;
if (featureGeometry !== undefined) {
geometry = {
type: featureGeometry.getType(),
coordinates: featureGeometry.getCoordinates()
};
}
return {
id: undefined,
source: undefined,
sourceType: SourceFeatureType.Query,
type: FeatureType.Feature,
order: 1000 - zIndex,
format: FeatureFormat.GeoJSON,
title: undefined,
icon: 'place',
projection: undefined,
properties: properties,
geometry: geometry
};
}
}