-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathNominatimSearchProvider.ts
More file actions
146 lines (130 loc) · 4.38 KB
/
NominatimSearchProvider.ts
File metadata and controls
146 lines (130 loc) · 4.38 KB
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
import { Feature, Point } from "geojson";
import { makeObservable, runInAction } from "mobx";
import CesiumMath from "terriajs-cesium/Source/Core/Math";
import Rectangle from "terriajs-cesium/Source/Core/Rectangle";
import Resource from "terriajs-cesium/Source/Core/Resource";
import {
Category,
SearchAction
} from "../../Core/AnalyticEvents/analyticEvents";
import loadJson from "../../Core/loadJson";
import LocationSearchProviderMixin from "../../ModelMixins/SearchProviders/LocationSearchProviderMixin";
import NominatimSearchProviderTraits from "../../Traits/SearchProviders/NominatimSearchProviderTraits";
import CreateModel from "../Definition/CreateModel";
import Terria from "../Terria";
import SearchProviderResult from "./SearchProviderResults";
import SearchResult from "./SearchResult";
export default class NominatimSearchProvider extends LocationSearchProviderMixin(
CreateModel(NominatimSearchProviderTraits)
) {
static readonly type = "nominatim-search-provider";
get type() {
return NominatimSearchProvider.type;
}
constructor(uniqueId: string | undefined, terria: Terria) {
super(uniqueId, terria);
console.warn(
"%c" +
"This map is using the Nominatim search provider. It is not recommended for production use, consider using a different search provider instead.",
"color: white; font-size: 24px; font-weight: bold; font-family: Helvetica, sans-serif;"
);
makeObservable(this);
}
protected logEvent(searchText: string) {
this.terria.analytics?.logEvent(
Category.search,
SearchAction.nominatim,
searchText
);
}
protected async doSearch(
searchText: string,
searchResults: SearchProviderResult
): Promise<void> {
searchResults.results.length = 0;
searchResults.message = undefined;
const view = this.terria.currentViewer.getCurrentCameraView();
const bboxStr =
CesiumMath.toDegrees(view.rectangle.west) +
", " +
CesiumMath.toDegrees(view.rectangle.north) +
", " +
CesiumMath.toDegrees(view.rectangle.east) +
", " +
CesiumMath.toDegrees(view.rectangle.south);
const promise = loadJson(
new Resource({
url: this.url,
queryParameters: {
q: searchText,
viewbox: bboxStr,
bounded: 0,
format: "geojson",
countrycodes: this.countryCodes,
limit: this.maxResults
}
})
);
return promise
.then((result) => {
if (searchResults.isCanceled) {
// A new search has superseded this one, so ignore the result.
return;
}
if (
!result?.features ||
!Array.isArray(result.features) ||
result.features.length === 0
) {
searchResults.message = {
content: "translate#viewModels.searchNoLocations"
};
return;
}
const locations: SearchResult[] = (result.features as Feature<Point>[])
.filter(
(feat) =>
feat.properties && feat.geometry && feat.properties.display_name
)
.sort((a, b) => b.properties!.importance - a.properties!.importance)
.map((feat) => {
return new SearchResult({
name: feat.properties!.display_name,
clickAction: createZoomToFunction(this, feat),
location: {
latitude: feat.geometry.coordinates[1],
longitude: feat.geometry.coordinates[0]
}
});
});
runInAction(() => {
searchResults.results.push(...locations);
});
if (searchResults.results.length === 0) {
searchResults.message = {
content: "translate#viewModels.searchNoLocations"
};
}
})
.catch(() => {
if (searchResults.isCanceled) {
// A new search has superseded this one, so ignore the result.
return;
}
searchResults.message = {
content: "translate#viewModels.searchErrorOccurred"
};
});
}
}
function createZoomToFunction(
model: NominatimSearchProvider,
resource: Feature<Point>
) {
const [west, south, east, north] = resource.bbox!;
const rectangle = Rectangle.fromDegrees(west, south, east, north);
return function () {
const terria = model.terria;
terria.currentViewer.zoomTo(rectangle, model.flightDurationSeconds);
};
}