@geospatial-sdk/geocoding 0.0.3-alpha.0

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/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023, Camptocamp
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `geocoding`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const geocoding = require('geocoding');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export {
2
+ queryGeoadmin,
3
+ queryDataGouvFr,
4
+ GeoadminOptions,
5
+ DataGouvFrOptions,
6
+ } from "./lib/providers";
@@ -0,0 +1,6 @@
1
+ import { Geometry } from "geojson";
2
+
3
+ export interface GeocodingResult {
4
+ label: string;
5
+ geom: Geometry | null;
6
+ }
@@ -0,0 +1,92 @@
1
+ import { GeocodingResult } from "../model";
2
+ import { DataGouvFrResponse, queryDataGouvFr } from "./data-gouv-fr.provider";
3
+
4
+ const RESULTS_FIXTURE: DataGouvFrResponse = {
5
+ type: "FeatureCollection",
6
+ version: "draft",
7
+ features: [
8
+ {
9
+ type: "Feature",
10
+ geometry: {
11
+ type: "Point",
12
+ coordinates: [2.290084, 49.897443],
13
+ },
14
+ properties: {
15
+ label: "8 Boulevard du Port 80000 Amiens",
16
+ score: 0.49159121588068583,
17
+ housenumber: "8",
18
+ id: "80021_6590_00008",
19
+ type: "housenumber",
20
+ name: "8 Boulevard du Port",
21
+ postcode: "80000",
22
+ citycode: "80021",
23
+ x: 648952.58,
24
+ y: 6977867.25,
25
+ city: "Amiens",
26
+ context: "80, Somme, Hauts-de-France",
27
+ importance: 0.6706612694243868,
28
+ street: "Boulevard du Port",
29
+ },
30
+ },
31
+ ],
32
+ attribution: "BAN",
33
+ licence: "ODbL 1.0",
34
+ query: "8 bd du port",
35
+ limit: 1,
36
+ };
37
+
38
+ global.fetch = vi.fn(() =>
39
+ Promise.resolve({
40
+ json: () => Promise.resolve(RESULTS_FIXTURE),
41
+ } as Response),
42
+ );
43
+
44
+ describe("queryDataGouvFr", () => {
45
+ let results: GeocodingResult[];
46
+
47
+ beforeEach(() => {
48
+ vi.clearAllMocks();
49
+ });
50
+
51
+ describe("results parsing", () => {
52
+ beforeEach(async () => {
53
+ results = await queryDataGouvFr("hello");
54
+ });
55
+ it("produces geometries, removes html tags from labels", () => {
56
+ expect(results).toEqual([
57
+ {
58
+ geom: {
59
+ type: "Point",
60
+ coordinates: [2.290084, 49.897443],
61
+ },
62
+ label: "8 Boulevard du Port 80000 Amiens",
63
+ },
64
+ ]);
65
+ });
66
+ });
67
+ describe("default options", () => {
68
+ beforeEach(async () => {
69
+ results = await queryDataGouvFr("hello world");
70
+ });
71
+ it("uses default options", () => {
72
+ expect(global.fetch).toHaveBeenCalledWith(
73
+ "https://api-adresse.data.gouv.fr/search/?q=hello+world",
74
+ );
75
+ });
76
+ });
77
+ describe("custom options", () => {
78
+ beforeEach(async () => {
79
+ results = await queryDataGouvFr("hello world", {
80
+ type: "street",
81
+ limit: 32,
82
+ cityCode: "12345",
83
+ postCode: "00000",
84
+ });
85
+ });
86
+ it("uses given options", () => {
87
+ expect(global.fetch).toHaveBeenCalledWith(
88
+ "https://api-adresse.data.gouv.fr/search/?q=hello+world&limit=32&type=street&postcode=00000&citycode=12345",
89
+ );
90
+ });
91
+ });
92
+ });
@@ -0,0 +1,86 @@
1
+ import { GeocodingResult } from "../model";
2
+ import { Geometry } from "geojson";
3
+
4
+ interface DataGouvFrResponseItem {
5
+ type: "Feature";
6
+ geometry: Geometry;
7
+ properties: {
8
+ label: string;
9
+ score: number;
10
+ id: string;
11
+ name: string;
12
+ postcode: string;
13
+ citycode: string;
14
+ x: number;
15
+ y: number;
16
+ city: string;
17
+ district?: string;
18
+ context: string;
19
+ type: string;
20
+ importance: number;
21
+ street: string;
22
+ housenumber?: string;
23
+ };
24
+ }
25
+
26
+ export interface DataGouvFrResponse {
27
+ type: "FeatureCollection";
28
+ version: string;
29
+ features: Array<DataGouvFrResponseItem>;
30
+ attribution: string;
31
+ licence: string;
32
+ query: string;
33
+ filters?: Record<string, string>;
34
+ limit: number;
35
+ }
36
+
37
+ const baseUrl = "https://api-adresse.data.gouv.fr/search/";
38
+
39
+ /**
40
+ * Reference documentation: https://adresse.data.gouv.fr/api-doc/adresse
41
+ * @property type
42
+ * @property postCode
43
+ * @property cityCode
44
+ * @property limit Default value 15
45
+ */
46
+ export interface DataGouvFrOptions {
47
+ type?: "housenumber" | "street" | "locality" | "municipality";
48
+ postCode?: string;
49
+ cityCode?: string;
50
+ limit?: number;
51
+ }
52
+
53
+ export function queryDataGouvFr(
54
+ input: string,
55
+ options?: DataGouvFrOptions,
56
+ ): Promise<GeocodingResult[]> {
57
+ const baseOptions: DataGouvFrOptions = {};
58
+ const finalOptions = options ? { ...baseOptions, ...options } : baseOptions;
59
+
60
+ const url = new URL(baseUrl);
61
+ url.searchParams.set("q", input);
62
+ if (finalOptions.limit) {
63
+ url.searchParams.set("limit", finalOptions.limit.toString());
64
+ }
65
+ if (finalOptions.type) {
66
+ url.searchParams.set("type", finalOptions.type);
67
+ }
68
+ if (finalOptions.postCode) {
69
+ url.searchParams.set("postcode", finalOptions.postCode);
70
+ }
71
+ if (finalOptions.cityCode) {
72
+ url.searchParams.set("citycode", finalOptions.cityCode);
73
+ }
74
+ return fetch(url.toString())
75
+ .then((response) => response.json())
76
+ .then((response: DataGouvFrResponse) =>
77
+ response.features.map((feature) => {
78
+ const label = feature.properties?.label.replace(/<[^>]*>?/gm, "");
79
+ const geom = feature.geometry;
80
+ return {
81
+ label,
82
+ geom,
83
+ };
84
+ }),
85
+ );
86
+ }
@@ -0,0 +1,169 @@
1
+ import { GeoadminResponse, queryGeoadmin } from "./geoadmin.provider";
2
+ import { GeocodingResult } from "../model";
3
+
4
+ const RESULTS_FIXTURE: GeoadminResponse = {
5
+ type: "FeatureCollection",
6
+ bbox: [8.446892, 47.319034, 8.627209, 47.43514],
7
+ features: [
8
+ {
9
+ geometry: {
10
+ type: "Point",
11
+ coordinates: [8.446892, 47.319034],
12
+ },
13
+ properties: {
14
+ origin: "gazetteer",
15
+ geom_quadindex: "021300220330313020221",
16
+ weight: 1,
17
+ zoomlevel: 10,
18
+ lon: 7.459799289703369,
19
+ detail: "wabern koeniz",
20
+ rank: 5,
21
+ lat: 46.925777435302734,
22
+ num: 1,
23
+ y: 601612.0625,
24
+ x: 197186.8125,
25
+ label: "<i>Populated Place</i> <b>Wabern</b> (BE) - Köniz",
26
+ id: 215754,
27
+ },
28
+ type: "Feature",
29
+ id: 215754,
30
+ bbox: [8.446892, 47.319034, 8.627209, 47.43514],
31
+ },
32
+ {
33
+ geometry: {
34
+ type: "Point",
35
+ coordinates: [8.446892, 47.319034],
36
+ },
37
+ properties: {
38
+ origin: "gg25",
39
+ geom_quadindex: "030003",
40
+ weight: 6,
41
+ zoomlevel: 4294967295,
42
+ lon: 8.527311325073242,
43
+ detail: "zurigo zh",
44
+ rank: 2,
45
+ lat: 47.37721252441406,
46
+ num: 1,
47
+ x: 8.527311325073242,
48
+ y: 47.37721252441406,
49
+ label: "<b>Zurigo (ZH)</b>",
50
+ id: 153,
51
+ featureId: "261",
52
+ },
53
+ type: "Feature",
54
+ id: 153,
55
+ bbox: [8.446892, 47.319034, 8.627209, 47.43514],
56
+ },
57
+ ],
58
+ };
59
+
60
+ global.fetch = vi.fn(() =>
61
+ Promise.resolve({
62
+ json: () => Promise.resolve(RESULTS_FIXTURE),
63
+ } as Response),
64
+ );
65
+
66
+ describe("queryGeoadmin", () => {
67
+ let results: GeocodingResult[];
68
+
69
+ beforeEach(() => {
70
+ vi.clearAllMocks();
71
+ });
72
+
73
+ describe("results parsing", () => {
74
+ beforeEach(async () => {
75
+ results = await queryGeoadmin("hello");
76
+ });
77
+ it("produces geometries, removes html tags from labels", () => {
78
+ expect(results).toEqual([
79
+ {
80
+ geom: {
81
+ coordinates: [
82
+ [
83
+ [8.446892, 47.319034],
84
+ [8.446892, 47.43514],
85
+ [8.627209, 47.43514],
86
+ [8.627209, 47.319034],
87
+ [8.446892, 47.319034],
88
+ ],
89
+ ],
90
+ type: "Polygon",
91
+ },
92
+ label: "Populated Place Wabern (BE) - Köniz",
93
+ },
94
+ {
95
+ geom: {
96
+ coordinates: [
97
+ [
98
+ [8.446892, 47.319034],
99
+ [8.446892, 47.43514],
100
+ [8.627209, 47.43514],
101
+ [8.627209, 47.319034],
102
+ [8.446892, 47.319034],
103
+ ],
104
+ ],
105
+ type: "Polygon",
106
+ },
107
+ label: "Zurigo (ZH)",
108
+ },
109
+ ]);
110
+ });
111
+ });
112
+ describe("default options", () => {
113
+ beforeEach(async () => {
114
+ results = await queryGeoadmin("hello world");
115
+ });
116
+ it("uses default options", () => {
117
+ expect(global.fetch).toHaveBeenCalledWith(
118
+ "https://api3.geo.admin.ch/rest/services/api/SearchServer?geometryFormat=geojson&type=locations&searchText=hello+world&lang=en&sr=4326&origins=zipcode%2Cgg25",
119
+ );
120
+ });
121
+ });
122
+ describe("locations search", () => {
123
+ beforeEach(async () => {
124
+ results = await queryGeoadmin("hello world", {
125
+ type: "locations",
126
+ lang: "de",
127
+ origins: ["district", "address"],
128
+ limit: 32,
129
+ sr: "21781",
130
+ });
131
+ });
132
+ it("uses given options", () => {
133
+ expect(global.fetch).toHaveBeenCalledWith(
134
+ "https://api3.geo.admin.ch/rest/services/api/SearchServer?geometryFormat=geojson&type=locations&searchText=hello+world&lang=de&sr=21781&limit=32&origins=district%2Caddress",
135
+ );
136
+ });
137
+ });
138
+ describe("feature search", () => {
139
+ beforeEach(async () => {
140
+ results = await queryGeoadmin("hello world", {
141
+ type: "featuresearch",
142
+ lang: "de",
143
+ features: ["abc", "def"],
144
+ limit: 32,
145
+ sr: "21781",
146
+ });
147
+ });
148
+ it("uses given options", () => {
149
+ expect(global.fetch).toHaveBeenCalledWith(
150
+ "https://api3.geo.admin.ch/rest/services/api/SearchServer?geometryFormat=geojson&type=featuresearch&searchText=hello+world&lang=de&sr=21781&limit=32&features=abc%2Cdef",
151
+ );
152
+ });
153
+ });
154
+ describe("layers search", () => {
155
+ beforeEach(async () => {
156
+ results = await queryGeoadmin("hello world", {
157
+ type: "layers",
158
+ lang: "de",
159
+ limit: 32,
160
+ sr: "21781",
161
+ });
162
+ });
163
+ it("uses given options", () => {
164
+ expect(global.fetch).toHaveBeenCalledWith(
165
+ "https://api3.geo.admin.ch/rest/services/api/SearchServer?geometryFormat=geojson&type=layers&searchText=hello+world&lang=de&sr=21781&limit=32",
166
+ );
167
+ });
168
+ });
169
+ });
@@ -0,0 +1,95 @@
1
+ import { GeocodingResult } from "../model";
2
+ import { BBox, FeatureCollection, Geometry } from "geojson";
3
+
4
+ // from https://github.com/geoblocks/ga-search
5
+ const baseUrl = "https://api3.geo.admin.ch/rest/services/api/SearchServer";
6
+
7
+ export type GeoadminResponse = FeatureCollection;
8
+
9
+ /**
10
+ * Reference documentation: https://api3.geo.admin.ch/services/sdiservices.html#search
11
+ * @property type Default is 'locations'
12
+ * @property sr Defaults to 4326
13
+ * @property limit Default value is 50 for 'locations', 20 for 'featuresearch', 30 for 'layers'
14
+ * @property lang Default is 'en'
15
+ * @property origins Defaults to 'zipcode,gg25'; only applies when type is 'locations'
16
+ * @property features A list of technical layer names; only applies when type is 'featuresearch'
17
+ */
18
+ export interface GeoadminOptions {
19
+ type?: "locations" | "featuresearch" | "layers";
20
+ sr?: "21781" | "2056" | "4326" | "3857";
21
+ origins?: Array<
22
+ | "zipcode"
23
+ | "gg25"
24
+ | "district"
25
+ | "kantone"
26
+ | "gazetteer"
27
+ | "address"
28
+ | "parcel"
29
+ >;
30
+ limit?: number;
31
+ lang?: "de" | "fr" | "it" | "rm" | "en";
32
+ features?: string[];
33
+ }
34
+
35
+ export function queryGeoadmin(
36
+ input: string,
37
+ options?: GeoadminOptions,
38
+ ): Promise<GeocodingResult[]> {
39
+ const baseOptions: GeoadminOptions = {
40
+ type: "locations",
41
+ sr: "4326",
42
+ origins: ["zipcode", "gg25"],
43
+ lang: "en",
44
+ features: [],
45
+ };
46
+ const finalOptions = options ? { ...baseOptions, ...options } : baseOptions;
47
+
48
+ let url = new URL(baseUrl);
49
+ url.searchParams.set("geometryFormat", "geojson");
50
+ url.searchParams.set("type", finalOptions.type!);
51
+ url.searchParams.set("searchText", input);
52
+ url.searchParams.set("lang", finalOptions.lang!);
53
+ url.searchParams.set("sr", finalOptions.sr!);
54
+ if (finalOptions.limit !== undefined) {
55
+ url.searchParams.set("limit", finalOptions.limit.toString());
56
+ }
57
+
58
+ switch (finalOptions.type!) {
59
+ case "locations":
60
+ url.searchParams.set("origins", finalOptions.origins!.join(","));
61
+ break;
62
+ case "featuresearch":
63
+ url.searchParams.set("features", finalOptions.features!.join(","));
64
+ break;
65
+ }
66
+
67
+ return fetch(url.toString())
68
+ .then((response) => response.json())
69
+ .then((response: GeoadminResponse) =>
70
+ response.features.map((feature) => {
71
+ const label = feature.properties?.label.replace(/<[^>]*>?/gm, "");
72
+ const geom = feature.bbox ? bboxToGeometry(feature.bbox) : null;
73
+ return {
74
+ label,
75
+ geom,
76
+ };
77
+ }),
78
+ );
79
+ }
80
+
81
+ function bboxToGeometry(extent: BBox): Geometry {
82
+ const [minX, minY, maxX, maxY] = extent;
83
+ return {
84
+ type: "Polygon",
85
+ coordinates: [
86
+ [
87
+ [minX, minY],
88
+ [minX, maxY],
89
+ [maxX, maxY],
90
+ [maxX, minY],
91
+ [minX, minY],
92
+ ],
93
+ ],
94
+ };
95
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./base.provider";
2
+ export * from "./geoadmin.provider";
3
+ export * from "./data-gouv-fr.provider";
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@geospatial-sdk/geocoding",
3
+ "version": "0.0.3-alpha.0+d226ce9",
4
+ "description": "Geocoding-related components",
5
+ "keywords": [
6
+ "geocoding"
7
+ ],
8
+ "author": "Olivia <olivia.guyot@camptocamp.com>",
9
+ "homepage": "https://github.com/jahow/geospatial-sdk#readme",
10
+ "license": "BSD-3-Clause",
11
+ "main": "index.ts",
12
+ "type": "module",
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "directories": {
17
+ "lib": "lib"
18
+ },
19
+ "files": [
20
+ "lib"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/jahow/geospatial-sdk.git"
25
+ },
26
+ "scripts": {
27
+ "test": "vitest"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/jahow/geospatial-sdk/issues"
31
+ },
32
+ "gitHead": "d226ce97fd7d50287c694ce4ed7182755cdfbb4e"
33
+ }