File : core/lib/NominatimDataLoader.js

1
/*
2
Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
*/
18
/*
19
Changes:
20
    - v4.0.0:
21
        - created from v3.6.0
22
Doc reviewed 202208
23
 */
24
25
import theConfig from '../../data/Config.js';
26
27
import { ZERO, ONE, HTTP_STATUS_OK } from '../../main/Constants.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
This class search an address with Nominatim starting from a lat and lng
32
*/
33
/* ------------------------------------------------------------------------------------------------------------------------- */
34
35
class NominatimDataLoader {
36
37
    /** The status of the requests
38
    @type {Boolean}
39
    */
40
41
    #nominatimStatusOk = false;
42
43
    /**
44
    The name found by Nominatim
45
    @type {String}
46
    */
47
48
    #name = '';
49
50
    /**
51
    The street found by Nominatim
52
    @type {String}
53
    */
54
55
    #street = '';
56
57
    /**
58
    The city found by Nominatim
59
    @type {String}
60
    */
61
62
    #city = '';
63
64
    /**
65
    The country found by Nominatim
66
    @type {String}
67
    */
68
69
    #country = '';
70
71
    /**
72
    This method parse the data received from Nominatim
73
    @param {Object} nominatimDataZoom10 the data received from Nomination using the zoom factor 10
74
    @param {Object} nominatimDataZoom18 the data received from Nomination using the zoom factor 18
75
    */
76
77
    #parseNominatimData ( nominatimDataZoom10, nominatimDataZoom18 ) {
78
        if ( nominatimDataZoom10.error || nominatimDataZoom18.error ) {
79
            this.#nominatimStatusOk = false;
80
        }
81
        else {
82
            this.#name = nominatimDataZoom18.namedetails.name;
83
            if ( nominatimDataZoom18.address.house_number ) {
84
                this.#street = nominatimDataZoom18.address.house_number + ' ';
85
            }
86
            if ( nominatimDataZoom18.address.road ) {
87
                this.#street += nominatimDataZoom18.address.road + ' ';
88
            }
89
            else if ( nominatimDataZoom18.address.pedestrian ) {
90
                this.#street += nominatimDataZoom18.address.pedestrian + ' ';
91
            }
92
            this.#city =
93
                nominatimDataZoom10.address.city
94
                ||
95
                nominatimDataZoom10.address.town
96
                ||
97
                nominatimDataZoom10.address.municipality
98
                ||
99
                nominatimDataZoom10.address.village
100
                ||
101
                '';
102
            this.#country = nominatimDataZoom18.address.country;
103
            this.#nominatimStatusOk = true;
104
        }
105
    }
106
107
    /**
108
    The constructor
109
    */
110
111
    constructor ( ) {
112
        Object.freeze ( this );
113
    }
114
115
    /**
116
    This method call Nominatim
117
    @param {Array.<Number>} latLng The latitude and longitude of the point for witch the address is searched
118
    */
119
120
    async loadData ( latLng ) {
121
122
        this.#nominatimStatusOk = false;
123
124
        // url
125
        const nominatimUrl =
126
            theConfig.nominatim.url + 'reverse?format=json&lat=' +
127
            latLng [ ZERO ] + '&lon=' + latLng [ ONE ];
128
129
        // We do a request with zoom factor 10 for city
130
        let nominatimUrlZoom10 =
131
            nominatimUrl + '&zoom=10&addressdetails=1&namedetails=0';
132
133
        // We do a request with zoom factor 18 for others data
134
        let nominatimUrlZoom18 =
135
            nominatimUrl + '&zoom=18&addressdetails=1&namedetails=1';
136
137
        // language
138
        const nominatimLanguage = theConfig.nominatim.language;
139
        if ( nominatimLanguage && '*' !== nominatimLanguage ) {
140
            nominatimUrlZoom10 += '&accept-language=' + nominatimLanguage;
141
            nominatimUrlZoom18 += '&accept-language=' + nominatimLanguage;
142
        }
143
144
        const nominatimHeaders = new Headers ( );
145
        if ( nominatimLanguage && '*' === nominatimLanguage ) {
146
            nominatimHeaders.append ( 'accept-language', '' );
147
        }
148
149
        // call to Nominatim
150
        const nominatimResponseZoom10 = await fetch ( nominatimUrlZoom10, { headers : nominatimHeaders } );
151
        const nominatimResponseZoom18 = await fetch ( nominatimUrlZoom18, { headers : nominatimHeaders } );
152
        if (
153
            HTTP_STATUS_OK === nominatimResponseZoom10.status
154
            &&
155
            nominatimResponseZoom10.ok
156
            &&
157
            HTTP_STATUS_OK === nominatimResponseZoom18.status
158
            &&
159
            nominatimResponseZoom18.ok
160
        ) {
161
            this.#parseNominatimData (
162
                await nominatimResponseZoom10.json ( ),
163
                await nominatimResponseZoom18.json ( )
164
            );
165
        }
166
        else {
167
            this.#nominatimStatusOk = false;
168
        }
169
    }
170
171
    /**
172
    The name found by Nominatim. Null when an error occurs when calling Nomnatim
173
    @type {String}
174
    */
175
176
    get name ( ) { return this.#nominatimStatusOk ? this.#name : null; }
177
178
    /**
179
    The street found by Nominatim. Null when an error occurs when calling Nomnatim
180
    @type {String}
181
    */
182
183
    get street ( ) { return this.#nominatimStatusOk ? this.#street : null; }
184
185
    /**
186
    The city found by Nominatim. Null when an error occurs when calling Nomnatim
187
    @type {String}
188
    */
189
190
    get city ( ) { return this.#nominatimStatusOk ? this.#city : null; }
191
192
    /**
193
    The country found by Nominatim. Null when an error occurs when calling Nomnatim
194
    @type {String}
195
    */
196
197
    get country ( ) { return this.#nominatimStatusOk ? this.#country : null; }
198
199
}
200
201
export default NominatimDataLoader;
202
203
/* --- End of file --------------------------------------------------------------------------------------------------------- */
204