Skip to content

Instantly share code, notes, and snippets.

@shlomoweb1
Created June 24, 2023 20:47
Show Gist options
  • Select an option

  • Save shlomoweb1/5ffdca36175ab16fbf2234fea3118cdf to your computer and use it in GitHub Desktop.

Select an option

Save shlomoweb1/5ffdca36175ab16fbf2234fea3118cdf to your computer and use it in GitHub Desktop.
interface NominatimResponse {
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: {
town: string;
state_district: string;
state: string;
"ISO3166-2-lvl4"?: string;
country: string;
country_code: string;
};
namedetails: {
name: string
"name:ar": string
"name:en": string
"name:fr": string
"name:he": string
"name:pl"?: string
"alt_name:ar": string
"name:de"?: string
"name:es"?: string
"name:ru": string
"name:ur"?: string
}
}
// Function to convert Hebrew town name to English using Nominatim
// Example usage
// Filter by ISO, and possiable keys for city
// CLI purpose, Hebrew show in reverse
import axios from 'axios';
// Function to convert Hebrew town name to English using Nominatim
export async function convertHebrewToEnglishWithNominatim(query: string) {
try {
const response = await axios.get<NominatimResponse[]>('https://nominatim.openstreetmap.org/search', {
params: {
q: query,
debug: 0,
format: 'json',
addressdetails: 1,
namedetails: 1,
countrycodes: 'IL,PS', // Limit results to Israel (IL) and Palestine (PS)
},
});
if (response.data.length > 0) {
return response.data;
} else {
throw new Error('Failed to convert town name.');
}
} catch (error: any) {
console.error('Error converting town name:', error.message);
}
}
export const posiableTypes =
[
"village",
"city",
"town",
// "suburb",
"amenity",
"hamlet",
"minicipality",
"municipality",
"administrative", // other key
]
// only call file directly execute sample
if (require.main === module) {
// Example usage
const townName = process.argv[2] || 'תל אביב'; // Provide the Hebrew town name here
convertHebrewToEnglishWithNominatim(townName)
.then((data) => {
if (!data) throw new Error('nothing found')
console.log(
data
.filter(a => {
// Filter by ISO, and possiable keys for city
return a.address['ISO3166-2-lvl4'] === 'IL-Z' && posiableTypes.indexOf(a.type) !== -1
})
.map(a => ({
name: a.namedetails,
address: a.address,
display: a.display_name.split("").reverse().join(""), // cli purpose, hebrew show in reverse
type: {
osm: a.osm_type,
location: a.type
}
}))
);
// console.log('English town name (using Nominatim):', englishTownName);
})
.catch((error) => {
console.error(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment