Commit 6d8f59b5 authored by JOE XMG's avatar JOE XMG
Browse files

update

parent 22fcb4d1
Pipeline #6293 passed with stage
in 6 seconds
import * as L from 'leaflet';
import { getJSON } from '../util';
import {
IGeocoder,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult,
reverseParams
} from './api';
export interface NeutrinoOptions extends GeocoderOptions {
userId: string;
}
/**
* Implementation of the [Neutrino API](https://www.neutrinoapi.com/api/geocode-address/)
*/
export class Neutrino implements IGeocoder {
options: NeutrinoOptions = {
userId: undefined,
apiKey: undefined,
serviceUrl: 'https://neutrinoapi.com/'
};
constructor(options?: Partial<NeutrinoOptions>) {
L.Util.setOptions(this, options);
}
// https://www.neutrinoapi.com/api/geocode-address/
geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = geocodingParams(this.options, {
apiKey: this.options.apiKey,
userId: this.options.userId,
//get three words and make a dot based string
address: query.split(/\s+/).join('.')
});
getJSON(this.options.serviceUrl + 'geocode-address', params, data => {
const results: GeocodingResult[] = [];
if (data.locations) {
data.geometry = data.locations[0];
const center = L.latLng(data.geometry['latitude'], data.geometry['longitude']);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.geometry.address,
bbox: bbox,
center: center
};
}
cb.call(context, results);
});
}
suggest(query: string, cb: GeocodingCallback, context?: any): void {
return this.geocode(query, cb, context);
}
// https://www.neutrinoapi.com/api/geocode-reverse/
reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void {
const params = reverseParams(this.options, {
apiKey: this.options.apiKey,
userId: this.options.userId,
latitude: location.lat,
longitude: location.lng
});
getJSON(this.options.serviceUrl + 'geocode-reverse', params, data => {
const results: GeocodingResult[] = [];
if (data.status.status == 200 && data.found) {
const center = L.latLng(location.lat, location.lng);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.address,
bbox: bbox,
center: center
};
}
cb.call(context, results);
});
}
}
/**
* [Class factory method](https://leafletjs.com/reference.html#class-class-factories) for {@link Neutrino}
* @param options the options
*/
export function neutrino(options?: Partial<NeutrinoOptions>) {
return new Neutrino(options);
}
import * as L from 'leaflet';
import { template, getJSON } from '../util';
import {
IGeocoder,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult,
reverseParams
} from './api';
export interface NominatimResult {
place_id: number;
licence: string;
osm_type: string;
osm_id: number;
boundingbox: string[];
lat: string;
lon: string;
display_name: string;
class: string;
type: string;
importance: number;
icon?: string;
address: NominatimAddress;
}
export interface NominatimAddress {
building?: string;
city_district?: string;
city?: string;
country_code?: string;
country?: string;
county?: string;
hamlet?: string;
house_number?: string;
neighbourhood?: string;
postcode?: string;
road?: string;
state_district?: string;
state?: string;
suburb?: string;
village?: string;
}
export interface NominatimOptions extends GeocoderOptions {
/**
* Additional URL parameters (strings) that will be added to geocoding requests; can be used to restrict results to a specific country for example, by providing the [`countrycodes`](https://wiki.openstreetmap.org/wiki/Nominatim#Parameters) parameter to Nominatim
*/
geocodingQueryParams?: Record<string, unknown>;
/**
* A function that takes an GeocodingResult as argument and returns an HTML formatted string that represents the result. Default function breaks up address in parts from most to least specific, in attempt to increase readability compared to Nominatim's naming
*/
htmlTemplate: (r: NominatimResult) => string;
}
/**
* Implementation of the [Nominatim](https://wiki.openstreetmap.org/wiki/Nominatim) geocoder.
*
* This is the default geocoding service used by the control, unless otherwise specified in the options.
*
* Unless using your own Nominatim installation, please refer to the [Nominatim usage policy](https://operations.osmfoundation.org/policies/nominatim/).
*/
export class Nominatim implements IGeocoder {
options: NominatimOptions = {
serviceUrl: 'https://nominatim.openstreetmap.org/',
htmlTemplate: function(r: NominatimResult) {
const address = r.address;
let className: string;
const parts = [];
if (address.road || address.building) {
parts.push('{building} {road} {house_number}');
}
if (address.city || (address as any).town || address.village || address.hamlet) {
className = parts.length > 0 ? 'leaflet-control-geocoder-address-detail' : '';
parts.push(
'<span class="' + className + '">{postcode} {city} {town} {village} {hamlet}</span>'
);
}
if (address.state || address.country) {
className = parts.length > 0 ? 'leaflet-control-geocoder-address-context' : '';
parts.push('<span class="' + className + '">{state} {country}</span>');
}
return template(parts.join('<br/>'), address);
}
};
constructor(options?: Partial<NominatimOptions>) {
L.Util.setOptions(this, options || {});
}
geocode(query: string, cb: GeocodingCallback, context?: any) {
const params = geocodingParams(this.options, {
q: query,
limit: 5,
format: 'json',
addressdetails: 1
});
getJSON(this.options.serviceUrl + 'search', params, data => {
const results: GeocodingResult[] = [];
for (let i = data.length - 1; i >= 0; i--) {
const bbox = data[i].boundingbox;
for (let j = 0; j < 4; j++) bbox[j] = +bbox[j];
results[i] = {
icon: data[i].icon,
name: data[i].display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data[i]) : undefined,
bbox: L.latLngBounds([bbox[0], bbox[2]], [bbox[1], bbox[3]]),
center: L.latLng(data[i].lat, data[i].lon),
properties: data[i]
};
}
cb.call(context, results);
});
}
reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any) {
const params = reverseParams(this.options, {
lat: location.lat,
lon: location.lng,
zoom: Math.round(Math.log(scale / 256) / Math.log(2)),
addressdetails: 1,
format: 'json'
});
getJSON(this.options.serviceUrl + 'reverse', params, data => {
const result: GeocodingResult[] = [];
if (data && data.lat && data.lon) {
const center = L.latLng(data.lat, data.lon);
const bbox = L.latLngBounds(center, center);
result.push({
name: data.display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data) : undefined,
center: center,
bbox: bbox,
properties: data
});
}
cb.call(context, result);
});
}
}
/**
* [Class factory method](https://leafletjs.com/reference.html#class-class-factories) for {@link Nominatim}
* @param options the options
*/
export function nominatim(options?: Partial<NominatimOptions>) {
return new Nominatim(options);
}
import * as L from 'leaflet';
import { IGeocoder, GeocodingCallback, GeocodingResult } from './api';
export interface OpenLocationCodeOptions {
OpenLocationCode: OpenLocationCodeApi;
codeLength?: number;
}
export interface OpenLocationCodeApi {
encode(latitude: number, longitude: number, codeLength?: number): string;
decode(code: string): CodeArea;
}
export interface CodeArea {
latitudeLo: number;
longitudeLo: number;
latitudeHi: number;
longitudeHi: number;
latitudeCenter: number;
longitudeCenter: number;
codeLength: number;
}
/**
* Implementation of the [Plus codes](https://plus.codes/) (formerly OpenLocationCode) (requires [open-location-code](https://www.npmjs.com/package/open-location-code))
*/
export class OpenLocationCode implements IGeocoder {
options: OpenLocationCodeOptions;
constructor(options?: Partial<OpenLocationCodeOptions>) {
L.Util.setOptions(this, options);
}
geocode(query: string, cb: GeocodingCallback, context?: any) {
try {
const decoded = this.options.OpenLocationCode.decode(query);
const result: GeocodingResult = {
name: query,
center: L.latLng(decoded.latitudeCenter, decoded.longitudeCenter),
bbox: L.latLngBounds(
L.latLng(decoded.latitudeLo, decoded.longitudeLo),
L.latLng(decoded.latitudeHi, decoded.longitudeHi)
)
};
cb.call(context, [result]);
} catch (e) {
console.warn(e); // eslint-disable-line no-console
cb.call(context, []);
}
}
reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any) {
try {
const code = this.options.OpenLocationCode.encode(
location.lat,
location.lng,
this.options.codeLength
);
const result = {
name: code,
center: L.latLng(location.lat, location.lng),
bbox: L.latLngBounds(
L.latLng(location.lat, location.lng),
L.latLng(location.lat, location.lng)
)
};
cb.call(context, [result]);
} catch (e) {
console.warn(e); // eslint-disable-line no-console
cb.call(context, []);
}
}
}
/**
* [Class factory method](https://leafletjs.com/reference.html#class-class-factories) for {@link OpenLocationCode}
* @param options the options
*/
export function openLocationCode(options?: Partial<OpenLocationCodeOptions>) {
return new OpenLocationCode(options);
}
import * as L from 'leaflet';
import { getJSON } from '../util';
import {
IGeocoder,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult,
reverseParams
} from './api';
export interface OpenCageOptions extends GeocoderOptions {}
/**
* Implementation of the [OpenCage Data API](https://opencagedata.com/)
*/
export class OpenCage implements IGeocoder {
options: OpenCageOptions = {
serviceUrl: 'https://api.opencagedata.com/geocode/v1/json'
};
constructor(options?: Partial<OpenCageOptions>) {
L.Util.setOptions(this, options);
}
geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = geocodingParams(this.options, {
key: this.options.apiKey,
q: query
});
getJSON(this.options.serviceUrl, params, data => {
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
for (let i = 0; i < data.results.length; i++) {
const loc = data.results[i];
const center = L.latLng(loc.geometry);
let bbox: L.LatLngBounds;
if (loc.annotations && loc.annotations.bounds) {
bbox = L.latLngBounds(
L.latLng(loc.annotations.bounds.northeast),
L.latLng(loc.annotations.bounds.southwest)
);
} else {
bbox = L.latLngBounds(center, center);
}
results.push({
name: loc.formatted,
bbox: bbox,
center: center
});
}
}
cb.call(context, results);
});
}
suggest(query: string, cb: GeocodingCallback, context?: any): void {
return this.geocode(query, cb, context);
}
reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void {
const params = reverseParams(this.options, {
key: this.options.apiKey,
q: [location.lat, location.lng].join(',')
});
getJSON(this.options.serviceUrl, params, data => {
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
for (let i = 0; i < data.results.length; i++) {
const loc = data.results[i];
const center = L.latLng(loc.geometry);
let bbox: L.LatLngBounds;
if (loc.annotations && loc.annotations.bounds) {
bbox = L.latLngBounds(
L.latLng(loc.annotations.bounds.northeast),
L.latLng(loc.annotations.bounds.southwest)
);
} else {
bbox = L.latLngBounds(center, center);
}
results.push({
name: loc.formatted,
bbox: bbox,
center: center
});
}
}
cb.call(context, results);
});
}
}
export function opencage(options?: Partial<OpenCageOptions>) {
return new OpenCage(options);
}
This diff is collapsed.
This diff is collapsed.
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"esModuleInterop": true,
"strict": false
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment