Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Created January 30, 2020 21:46
Show Gist options
  • Save jgcmarins/4b3f0edc1d75911162c1e02229e9512c to your computer and use it in GitHub Desktop.
Save jgcmarins/4b3f0edc1d75911162c1e02229e9512c to your computer and use it in GitHub Desktop.
Mapbox helpers
import idx from 'idx';
import { ForwardFeature } from './mapboxTypes';
export interface AddressResult {
id?: string | null;
fullAddress?: string | null;
shortAddress?: string | null;
zipcode?: string | null;
street?: string | null;
number?: string | null;
neighborhood?: string | null;
city?: string | null;
state?: string | null;
complement?: string | null;
country?: string | null;
lat?: number | null;
lng?: number | null;
}
export default function mapboxToAddress(input: ForwardFeature): AddressResult {
const address: AddressResult = {};
if (input.id) {
address.id = input.id;
}
if (input.place_name) {
address.fullAddress = input.place_name;
}
if (Array.isArray(input.place_type)) {
if (input.place_type.includes('poi')) {
address.shortAddress = input.text;
if (input.properties && input.properties.address) {
const addressString = input.properties.address;
const lastComma = addressString.lastIndexOf(', ');
if (lastComma > 0) {
address.street = addressString.substring(0, lastComma);
address.number = addressString.substring(lastComma + 2);
} else {
address.street = addressString;
}
}
}
if (input.place_type.includes('address')) {
address.street = input.text;
if (input.address) {
address.number = input.address;
}
}
}
const coordinates = idx(input, _ => _.geometry.coordinates);
if (Array.isArray(coordinates) && coordinates.length == 2 && coordinates.every(Number.isFinite)) {
address.lng = coordinates[0];
address.lat = coordinates[1];
}
for (const context of idx(input, _ => _.context) || []) {
if (context.id.startsWith('neighborhood.') && !address.neighborhood && context.text) {
address.neighborhood = context.text;
}
if (context.id.startsWith('country.') && !address.country && context.short_code) {
address.country = context.short_code.trim().toUpperCase();
}
if (context.id.startsWith('region.') && !address.state && context.text) {
address.state = context.text;
}
if (context.id.startsWith('place.') && !address.city && context.text) {
address.city = context.text;
}
if (context.id.startsWith('postcode.') && !address.zipcode && context.text) {
address.zipcode = context.text;
}
}
return address as any;
}
export interface Coordinates {
lat: number;
lng: number;
}
export interface ReverseOptions {
language?: string;
limit?: number;
types?: string[];
}
export interface ForwardOptions {
signal?: AbortSignal;
autocomplete?: boolean;
proximity?: Coordinates | null;
language?: string;
}
export interface ForwardProperties {
accuracy: string;
landmark?: boolean;
address: string;
category: string;
}
export interface ForwardGeometry {
type: string;
coordinates: number[];
interpolated?: boolean;
omitted?: boolean;
}
export interface ForwardContext {
id: string;
text: string;
wikidata: string;
short_code: string;
}
export interface ForwardFeature {
id: string;
type: string;
place_type: string[];
relevance: number;
properties: ForwardProperties;
text: string;
place_name: string;
center: number[];
geometry: ForwardGeometry;
context: ForwardContext[];
address: string;
}
export interface ForwardResponse {
type: string;
query: string[];
features: ForwardFeature[];
attribution: string;
}
export interface ReverseProperties {
wikidata: string;
short_code: string;
}
export interface ReverseGeometry {
type: string;
coordinates: number[];
}
export interface ReverseContext {
id: string;
short_code: string;
wikidata: string;
text: string;
}
export interface ReverseFeature {
id: string;
type: string;
place_type: string[];
relevance: number;
properties: ReverseProperties;
text: string;
place_name: string;
bbox: number[];
center: number[];
geometry: ReverseGeometry;
context: ReverseContext[];
}
export interface ReverseResponse {
type: string;
query: number[];
features: ReverseFeature[];
attribution: string;
}
export interface SearchTzResponse {
type: string;
features: Feature[];
}
export interface Feature {
type: string;
id: number;
geometry: Geometry;
properties: Properties;
}
export interface Geometry {
type: string;
coordinates: number[];
}
export interface Properties {
TZID: string;
tilequery: Tilequery;
}
export interface Tilequery {
distance: number;
geometry: string;
layer: string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment