[geonames] Adding ability to lookup GeoNames alternate names (may obtain IDs from Quattroshapes). Not great for local-language primary names (OSM remains the best) but decent for extracting foreign toponyms
This commit is contained in:
27
scripts/geodata/geonames/db.py
Normal file
27
scripts/geodata/geonames/db.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import sqlite3
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
class GeoNamesDB(object):
|
||||
names_query = '''
|
||||
select iso_language, alternate_name,
|
||||
is_preferred_name, is_short_name
|
||||
from alternate_names
|
||||
where geonames_id = ?
|
||||
and is_historic != '1'
|
||||
and is_colloquial != '1'
|
||||
and iso_language != 'post'
|
||||
order by iso_language, cast(is_preferred_name as integer) desc, cast(is_short_name as integer)
|
||||
'''
|
||||
|
||||
def __init__(self, filename):
|
||||
self.db = sqlite3.connect(filename)
|
||||
|
||||
def get_alternate_names(self, geonames_id):
|
||||
cursor = self.db.execute(self.names_query, [geonames_id])
|
||||
language_names = defaultdict(list)
|
||||
for language, name, is_preferred, is_short in cursor:
|
||||
language_names[language].append((name,
|
||||
int(is_preferred or 0),
|
||||
int(is_short or 0)))
|
||||
return dict(language_names)
|
||||
Reference in New Issue
Block a user