@geospatial-sdk/geocoding 0.0.5-alpha.0 → 0.0.5-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/providers/geoadmin.provider.d.ts +0 -1
- package/dist/providers/geoadmin.provider.d.ts.map +1 -1
- package/dist/providers/geoadmin.provider.js +0 -4
- package/dist/providers/geonames.provider.d.ts +24 -0
- package/dist/providers/geonames.provider.d.ts.map +1 -0
- package/dist/providers/geonames.provider.js +53 -0
- package/dist/providers/geonames.provider.test.d.ts +2 -0
- package/dist/providers/geonames.provider.test.d.ts.map +1 -0
- package/dist/providers/geonames.provider.test.js +120 -0
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/lib/index.ts +1 -0
- package/lib/providers/geoadmin.provider.ts +0 -6
- package/lib/providers/geonames.provider.test.ts +129 -0
- package/lib/providers/geonames.provider.ts +93 -0
- package/lib/providers/index.ts +1 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -19,5 +19,4 @@ export interface GeoadminOptions {
|
|
|
19
19
|
features?: string[];
|
|
20
20
|
}
|
|
21
21
|
export declare function queryGeoadmin(input: string, options?: GeoadminOptions): Promise<GeocodingResult[]>;
|
|
22
|
-
export declare function sayHello(): string;
|
|
23
22
|
//# sourceMappingURL=geoadmin.provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"geoadmin.provider.d.ts","sourceRoot":"","sources":["../../lib/providers/geoadmin.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAQ,iBAAiB,EAAY,MAAM,SAAS,CAAC;AAK5D,MAAM,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAEjD;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,WAAW,GAAG,eAAe,GAAG,QAAQ,CAAC;IAChD,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;IACxC,OAAO,CAAC,EAAE,KAAK,CACX,SAAS,GACT,MAAM,GACN,UAAU,GACV,SAAS,GACT,WAAW,GACX,SAAS,GACT,QAAQ,CACX,CAAC;IACF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,eAAe,EAAE,CAAC,CAyC5B
|
|
1
|
+
{"version":3,"file":"geoadmin.provider.d.ts","sourceRoot":"","sources":["../../lib/providers/geoadmin.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAQ,iBAAiB,EAAY,MAAM,SAAS,CAAC;AAK5D,MAAM,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAEjD;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,WAAW,GAAG,eAAe,GAAG,QAAQ,CAAC;IAChD,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;IACxC,OAAO,CAAC,EAAE,KAAK,CACX,SAAS,GACT,MAAM,GACN,UAAU,GACV,SAAS,GACT,WAAW,GACX,SAAS,GACT,QAAQ,CACX,CAAC;IACF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,eAAe,EAAE,CAAC,CAyC5B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { GeocodingResult } from "../model";
|
|
2
|
+
/**
|
|
3
|
+
* Reference documentation: http://www.geonames.org/export/geonames-search.html
|
|
4
|
+
* @property lang Default is English. Either "local" or iso2 country code should be used
|
|
5
|
+
* @property maxRows The maximal number of rows in the document returned by the service. Default is 100
|
|
6
|
+
* @property country Default is all countries. The country parameter may occur more than once, example: country=FR&country=GP
|
|
7
|
+
* @property style verbosity of returned xml document, default = MEDIUM
|
|
8
|
+
* @property type The format type of the returned document, default = xml
|
|
9
|
+
* @property username The parameter 'username' needs to be passed with each request. (https://www.geonames.org/login)
|
|
10
|
+
* @property east, west, north, south Restrict the search to the given bounding box
|
|
11
|
+
*/
|
|
12
|
+
export interface GeonamesOptions {
|
|
13
|
+
lang?: string;
|
|
14
|
+
maxRows?: number;
|
|
15
|
+
country?: string | string[];
|
|
16
|
+
style?: "SHORT" | "MEDIUM" | "LONG" | "FULL";
|
|
17
|
+
username: string;
|
|
18
|
+
east?: number;
|
|
19
|
+
west?: number;
|
|
20
|
+
north?: number;
|
|
21
|
+
south?: number;
|
|
22
|
+
}
|
|
23
|
+
export declare function queryGeonames(input: string, options?: GeonamesOptions): Promise<GeocodingResult[]>;
|
|
24
|
+
//# sourceMappingURL=geonames.provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geonames.provider.d.ts","sourceRoot":"","sources":["../../lib/providers/geonames.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAK3C;;;;;;;;;GASG;AAEH,MAAM,WAAW,eAAe;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,KAAK,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB;AAQD,wBAAgB,aAAa,CACzB,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GAC1B,OAAO,CAAC,eAAe,EAAE,CAAC,CAuD5B"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const baseUrl = "https://secure.geonames.org/searchJSON";
|
|
2
|
+
export function queryGeonames(input, options) {
|
|
3
|
+
if (!input.trim()) {
|
|
4
|
+
throw new Error("Input query is required and cannot be empty.");
|
|
5
|
+
}
|
|
6
|
+
const baseOptions = {
|
|
7
|
+
lang: "en",
|
|
8
|
+
maxRows: 10,
|
|
9
|
+
style: "FULL",
|
|
10
|
+
username: "gn_ui",
|
|
11
|
+
};
|
|
12
|
+
const finalOptions = Object.assign(Object.assign({}, baseOptions), options);
|
|
13
|
+
const url = new URL(baseUrl);
|
|
14
|
+
url.searchParams.set("q", input.trim());
|
|
15
|
+
url.searchParams.set("username", finalOptions.username);
|
|
16
|
+
finalOptions.maxRows && url.searchParams.set("maxRows", finalOptions.maxRows.toString());
|
|
17
|
+
if (typeof finalOptions.country === "string") {
|
|
18
|
+
url.searchParams.set("country", finalOptions.country);
|
|
19
|
+
}
|
|
20
|
+
else if (Array.isArray(finalOptions.country)) {
|
|
21
|
+
finalOptions.country.forEach(c => url.searchParams.append("country", c));
|
|
22
|
+
}
|
|
23
|
+
finalOptions.lang && url.searchParams.set("lang", finalOptions.lang);
|
|
24
|
+
finalOptions.style && url.searchParams.set("style", finalOptions.style);
|
|
25
|
+
url.searchParams.set("type", "json");
|
|
26
|
+
if (finalOptions.east !== undefined && finalOptions.west !== undefined &&
|
|
27
|
+
finalOptions.north !== undefined && finalOptions.south !== undefined &&
|
|
28
|
+
finalOptions.east > finalOptions.west && finalOptions.north > finalOptions.south) {
|
|
29
|
+
url.searchParams.set("east", finalOptions.east.toString());
|
|
30
|
+
url.searchParams.set("west", finalOptions.west.toString());
|
|
31
|
+
url.searchParams.set("north", finalOptions.north.toString());
|
|
32
|
+
url.searchParams.set("south", finalOptions.south.toString());
|
|
33
|
+
}
|
|
34
|
+
return fetch(url.toString())
|
|
35
|
+
.then(response => {
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
throw new Error(`Geonames API returned ${response.status}: ${response.statusText}`);
|
|
38
|
+
}
|
|
39
|
+
return response.json();
|
|
40
|
+
})
|
|
41
|
+
.then((data) => {
|
|
42
|
+
if (!data.geonames) {
|
|
43
|
+
throw new Error('Invalid response from Geonames API: no geonames property');
|
|
44
|
+
}
|
|
45
|
+
return data.geonames.map(geoname => ({
|
|
46
|
+
label: geoname.name,
|
|
47
|
+
geom: {
|
|
48
|
+
type: "Point",
|
|
49
|
+
coordinates: [parseFloat(geoname.lng), parseFloat(geoname.lat)],
|
|
50
|
+
},
|
|
51
|
+
}));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geonames.provider.test.d.ts","sourceRoot":"","sources":["../../lib/providers/geonames.provider.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { queryGeonames } from "./geonames.provider";
|
|
11
|
+
import { describe } from "vitest";
|
|
12
|
+
const MOCK_DATA = {
|
|
13
|
+
totalResultsCount: 35,
|
|
14
|
+
geonames: [
|
|
15
|
+
{
|
|
16
|
+
adminCode1: "ZH",
|
|
17
|
+
lng: "8.55",
|
|
18
|
+
geonameId: 2657896,
|
|
19
|
+
toponymName: "Zürich",
|
|
20
|
+
countryId: "2658434",
|
|
21
|
+
fcl: "P",
|
|
22
|
+
population: 341730,
|
|
23
|
+
countryCode: "CH",
|
|
24
|
+
name: "Zurich",
|
|
25
|
+
fclName: "city, village...",
|
|
26
|
+
adminCodes1: { ISO3166_2: "ZH" },
|
|
27
|
+
countryName: "Switzerland",
|
|
28
|
+
fcodeName: "seat of a first-order administrative division",
|
|
29
|
+
adminName1: "Zurich",
|
|
30
|
+
lat: "47.36667",
|
|
31
|
+
fcode: "PPLA"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
adminCode1: "ZH",
|
|
35
|
+
lng: "8.66667",
|
|
36
|
+
geonameId: 2657895,
|
|
37
|
+
toponymName: "Kanton Zürich",
|
|
38
|
+
countryId: "2658434",
|
|
39
|
+
fcl: "A",
|
|
40
|
+
population: 1553423,
|
|
41
|
+
countryCode: "CH",
|
|
42
|
+
name: "Zurich",
|
|
43
|
+
fclName: "country, state, region...",
|
|
44
|
+
adminCodes1: { ISO3166_2: "ZH" },
|
|
45
|
+
countryName: "Switzerland",
|
|
46
|
+
fcodeName: "first-order administrative division",
|
|
47
|
+
adminName1: "Zurich",
|
|
48
|
+
lat: "47.41667",
|
|
49
|
+
fcode: "ADM1"
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
};
|
|
53
|
+
vi.stubGlobal('fetch', vi.fn(() => Promise.resolve({
|
|
54
|
+
ok: true, // Ensure the mock reflects a successful response
|
|
55
|
+
json: () => Promise.resolve(MOCK_DATA),
|
|
56
|
+
})));
|
|
57
|
+
describe("queryGeonames", () => {
|
|
58
|
+
let results;
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
vi.clearAllMocks();
|
|
61
|
+
});
|
|
62
|
+
describe("results parsing", () => {
|
|
63
|
+
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
+
results = yield queryGeonames("Zurich");
|
|
65
|
+
}));
|
|
66
|
+
it("correctly processes Geonames API response", () => {
|
|
67
|
+
expect(results).toEqual([
|
|
68
|
+
{
|
|
69
|
+
label: "Zurich",
|
|
70
|
+
geom: {
|
|
71
|
+
type: "Point",
|
|
72
|
+
coordinates: [8.55, 47.36667],
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
label: "Zurich",
|
|
77
|
+
geom: {
|
|
78
|
+
type: "Point",
|
|
79
|
+
coordinates: [8.66667, 47.41667],
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe("default options", () => {
|
|
86
|
+
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
87
|
+
results = yield queryGeonames("Zurich");
|
|
88
|
+
}));
|
|
89
|
+
it("uses default options", () => {
|
|
90
|
+
expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining("https://secure.geonames.org/searchJSON?q=Zurich&username=gn_ui&maxRows=10"));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
describe("custom options", () => {
|
|
94
|
+
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
95
|
+
results = yield queryGeonames("Zurich", {
|
|
96
|
+
lang: "de",
|
|
97
|
+
maxRows: 5,
|
|
98
|
+
country: "CH",
|
|
99
|
+
username: "customUser",
|
|
100
|
+
});
|
|
101
|
+
}));
|
|
102
|
+
it("uses given options for custom search", () => {
|
|
103
|
+
expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining("https://secure.geonames.org/searchJSON?q=Zurich&username=customUser&maxRows=5&country=CH&lang=de&style=FULL&type=json"));
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe("bounding box search", () => {
|
|
107
|
+
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
108
|
+
results = yield queryGeonames("Zurich", {
|
|
109
|
+
east: 10,
|
|
110
|
+
west: 5,
|
|
111
|
+
north: 50,
|
|
112
|
+
south: 45,
|
|
113
|
+
username: "customUser",
|
|
114
|
+
});
|
|
115
|
+
}));
|
|
116
|
+
it("uses given options for bounding box search", () => {
|
|
117
|
+
expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining("https://secure.geonames.org/searchJSON?q=Zurich&username=customUser&maxRows=10&lang=en&style=FULL&type=json&east=10&west=5&north=50&south=45"));
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/providers/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/providers/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC"}
|
package/dist/providers/index.js
CHANGED
package/lib/index.ts
CHANGED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {queryGeonames} from "./geonames.provider";
|
|
2
|
+
import { GeocodingResult } from "../model";
|
|
3
|
+
import {describe} from "vitest";
|
|
4
|
+
|
|
5
|
+
const MOCK_DATA = {
|
|
6
|
+
totalResultsCount: 35,
|
|
7
|
+
geonames: [
|
|
8
|
+
{
|
|
9
|
+
adminCode1: "ZH",
|
|
10
|
+
lng: "8.55",
|
|
11
|
+
geonameId: 2657896,
|
|
12
|
+
toponymName: "Zürich",
|
|
13
|
+
countryId: "2658434",
|
|
14
|
+
fcl: "P",
|
|
15
|
+
population: 341730,
|
|
16
|
+
countryCode: "CH",
|
|
17
|
+
name: "Zurich",
|
|
18
|
+
fclName: "city, village...",
|
|
19
|
+
adminCodes1: { ISO3166_2: "ZH" },
|
|
20
|
+
countryName: "Switzerland",
|
|
21
|
+
fcodeName: "seat of a first-order administrative division",
|
|
22
|
+
adminName1: "Zurich",
|
|
23
|
+
lat: "47.36667",
|
|
24
|
+
fcode: "PPLA"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
adminCode1: "ZH",
|
|
28
|
+
lng: "8.66667",
|
|
29
|
+
geonameId: 2657895,
|
|
30
|
+
toponymName: "Kanton Zürich",
|
|
31
|
+
countryId: "2658434",
|
|
32
|
+
fcl: "A",
|
|
33
|
+
population: 1553423,
|
|
34
|
+
countryCode: "CH",
|
|
35
|
+
name: "Zurich",
|
|
36
|
+
fclName: "country, state, region...",
|
|
37
|
+
adminCodes1: { ISO3166_2: "ZH" },
|
|
38
|
+
countryName: "Switzerland",
|
|
39
|
+
fcodeName: "first-order administrative division",
|
|
40
|
+
adminName1: "Zurich",
|
|
41
|
+
lat: "47.41667",
|
|
42
|
+
fcode: "ADM1"
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
vi.stubGlobal('fetch', vi.fn(() =>
|
|
48
|
+
Promise.resolve({
|
|
49
|
+
ok: true, // Ensure the mock reflects a successful response
|
|
50
|
+
json: () => Promise.resolve(MOCK_DATA),
|
|
51
|
+
}) as unknown as Response
|
|
52
|
+
));
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
describe("queryGeonames", () => {
|
|
56
|
+
let results: GeocodingResult[];
|
|
57
|
+
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
vi.clearAllMocks();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("results parsing", () => {
|
|
63
|
+
beforeEach(async () => {
|
|
64
|
+
results = await queryGeonames("Zurich");
|
|
65
|
+
});
|
|
66
|
+
it("correctly processes Geonames API response", () => {
|
|
67
|
+
expect(results).toEqual([
|
|
68
|
+
{
|
|
69
|
+
label: "Zurich",
|
|
70
|
+
geom: {
|
|
71
|
+
type: "Point",
|
|
72
|
+
coordinates: [8.55, 47.36667],
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
label: "Zurich",
|
|
77
|
+
geom: {
|
|
78
|
+
type: "Point",
|
|
79
|
+
coordinates: [8.66667, 47.41667],
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe("default options", () => {
|
|
86
|
+
beforeEach(async () => {
|
|
87
|
+
results = await queryGeonames("Zurich");
|
|
88
|
+
});
|
|
89
|
+
it("uses default options", () => {
|
|
90
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
91
|
+
expect.stringContaining("https://secure.geonames.org/searchJSON?q=Zurich&username=gn_ui&maxRows=10")
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("custom options", () => {
|
|
97
|
+
beforeEach(async () => {
|
|
98
|
+
results = await queryGeonames("Zurich", {
|
|
99
|
+
lang: "de",
|
|
100
|
+
maxRows: 5,
|
|
101
|
+
country: "CH",
|
|
102
|
+
username: "customUser",
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
it("uses given options for custom search", () => {
|
|
106
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
107
|
+
expect.stringContaining("https://secure.geonames.org/searchJSON?q=Zurich&username=customUser&maxRows=5&country=CH&lang=de&style=FULL&type=json")
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("bounding box search", () => {
|
|
113
|
+
beforeEach(async () => {
|
|
114
|
+
results = await queryGeonames("Zurich", {
|
|
115
|
+
east: 10,
|
|
116
|
+
west: 5,
|
|
117
|
+
north: 50,
|
|
118
|
+
south: 45,
|
|
119
|
+
username: "customUser",
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
it("uses given options for bounding box search", () => {
|
|
123
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
124
|
+
expect.stringContaining("https://secure.geonames.org/searchJSON?q=Zurich&username=customUser&maxRows=10&lang=en&style=FULL&type=json&east=10&west=5&north=50&south=45")
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { GeocodingResult } from "../model";
|
|
2
|
+
import { Geometry } from "geojson";
|
|
3
|
+
|
|
4
|
+
const baseUrl = "https://secure.geonames.org/searchJSON";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Reference documentation: http://www.geonames.org/export/geonames-search.html
|
|
8
|
+
* @property lang Default is English. Either "local" or iso2 country code should be used
|
|
9
|
+
* @property maxRows The maximal number of rows in the document returned by the service. Default is 100
|
|
10
|
+
* @property country Default is all countries. The country parameter may occur more than once, example: country=FR&country=GP
|
|
11
|
+
* @property style verbosity of returned xml document, default = MEDIUM
|
|
12
|
+
* @property type The format type of the returned document, default = xml
|
|
13
|
+
* @property username The parameter 'username' needs to be passed with each request. (https://www.geonames.org/login)
|
|
14
|
+
* @property east, west, north, south Restrict the search to the given bounding box
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export interface GeonamesOptions {
|
|
18
|
+
lang?: string;
|
|
19
|
+
maxRows?: number;
|
|
20
|
+
country?: string | string[]; // Now supports multiple countries
|
|
21
|
+
style?: "SHORT" | "MEDIUM" | "LONG" | "FULL";
|
|
22
|
+
username: string;
|
|
23
|
+
east?: number;
|
|
24
|
+
west?: number;
|
|
25
|
+
north?: number;
|
|
26
|
+
south?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface GeonameResponse {
|
|
30
|
+
name: string;
|
|
31
|
+
lng: string;
|
|
32
|
+
lat: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function queryGeonames(
|
|
36
|
+
input: string,
|
|
37
|
+
options?: GeonamesOptions,
|
|
38
|
+
): Promise<GeocodingResult[]> {
|
|
39
|
+
if (!input.trim()) {
|
|
40
|
+
throw new Error("Input query is required and cannot be empty.");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const baseOptions: GeonamesOptions = {
|
|
44
|
+
lang: "en",
|
|
45
|
+
maxRows: 10,
|
|
46
|
+
style: "FULL",
|
|
47
|
+
username: "gn_ui",
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const finalOptions = { ...baseOptions, ...options };
|
|
51
|
+
|
|
52
|
+
const url = new URL(baseUrl);
|
|
53
|
+
url.searchParams.set("q", input.trim());
|
|
54
|
+
url.searchParams.set("username", finalOptions.username);
|
|
55
|
+
finalOptions.maxRows && url.searchParams.set("maxRows", finalOptions.maxRows.toString());
|
|
56
|
+
if (typeof finalOptions.country === "string") {
|
|
57
|
+
url.searchParams.set("country", finalOptions.country);
|
|
58
|
+
} else if (Array.isArray(finalOptions.country)) {
|
|
59
|
+
finalOptions.country.forEach(c => url.searchParams.append("country", c));
|
|
60
|
+
}
|
|
61
|
+
finalOptions.lang && url.searchParams.set("lang", finalOptions.lang);
|
|
62
|
+
finalOptions.style && url.searchParams.set("style", finalOptions.style);
|
|
63
|
+
url.searchParams.set("type", "json");
|
|
64
|
+
|
|
65
|
+
if (finalOptions.east !== undefined && finalOptions.west !== undefined &&
|
|
66
|
+
finalOptions.north !== undefined && finalOptions.south !== undefined &&
|
|
67
|
+
finalOptions.east > finalOptions.west && finalOptions.north > finalOptions.south) {
|
|
68
|
+
url.searchParams.set("east", finalOptions.east.toString());
|
|
69
|
+
url.searchParams.set("west", finalOptions.west.toString());
|
|
70
|
+
url.searchParams.set("north", finalOptions.north.toString());
|
|
71
|
+
url.searchParams.set("south", finalOptions.south.toString());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return fetch(url.toString())
|
|
75
|
+
.then(response => {
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
throw new Error(`Geonames API returned ${response.status}: ${response.statusText}`);
|
|
78
|
+
}
|
|
79
|
+
return response.json();
|
|
80
|
+
})
|
|
81
|
+
.then((data: { geonames: GeonameResponse[] }) => {
|
|
82
|
+
if (!data.geonames) {
|
|
83
|
+
throw new Error('Invalid response from Geonames API: no geonames property');
|
|
84
|
+
}
|
|
85
|
+
return data.geonames.map(geoname => ({
|
|
86
|
+
label: geoname.name,
|
|
87
|
+
geom: {
|
|
88
|
+
type: "Point",
|
|
89
|
+
coordinates: [parseFloat(geoname.lng), parseFloat(geoname.lat)],
|
|
90
|
+
} as Geometry,
|
|
91
|
+
}));
|
|
92
|
+
});
|
|
93
|
+
}
|
package/lib/providers/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geospatial-sdk/geocoding",
|
|
3
|
-
"version": "0.0.5-alpha.
|
|
3
|
+
"version": "0.0.5-alpha.2",
|
|
4
4
|
"description": "Geocoding-related components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"geocoding"
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"bugs": {
|
|
33
33
|
"url": "https://github.com/jahow/geospatial-sdk/issues"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "c61f5acb8eac69b081ef21127092e264306ee99d"
|
|
36
36
|
}
|