Top

geodata.GeoDB module

geoname database support routines. Add locations to geoname DB, create geoname tables and indices.
Provides a number of methods to lookup locations by name, feature, admin ID, etc.

Module variables

var FUZZY_LOOKUP

Classes

class GeoDB

geoname database routines. Add items to geoname DB, look up items, create tables, indices

Ancestors (in MRO)

Static methods

def __init__(

self, db_path, show_message, exit_on_error, set_speed_pragmas, db_limit)

geoname data database init. Open database if present otherwise raise error

Args:

db_path: full path to database file
show_message: If True, show messagebox to user on error
exit_on_error: If True, exit if significant error occurs
set_speed_pragmas: If True, set DB pragmas for maximum performance. 
db_limit: SQL LIMIT parameter

Raises:

ValueError('Cannot open database'), ValueError('Database empty or corrupt')

def add_query_by_feature(

self, query_list, lookup_target, iso)

def assign_scores(

self, place, target_feature, fast, quiet)

Assign match score to each result in list

Args: place: target_feature: fast: quiet: if True, set logging to INFO

Returns:

def clear_geoname_data(

self)

Delete geodata table and admin table from database

def close(

self)

Close database. Set optimize pragma

def copy_georow_to_place(

self, row, place, fast)

Copy data from DB row into place instance
Country, admin1_id, admin2_id, city, lat/lon, feature, geoid are updated if available

Args:

row: georow from geoname database   
place: Loc instance

Returns:

None.  Place instance is updated with data from georow

def create_admin1_wildcard(

pattern)

Create wildcard pattern. Convert * to %. Add % on end. Currently this
is the same at create_wildcard

Args:

pattern:

Returns: wildcard pattern

def create_county_wildcard(

pattern)

create wildcard for county lookup

Args:

pattern:

Returns:

def create_wildcard(

pattern)

Create wildcard pattern. Convert * to %. Add % on end

Args:

pattern:

Returns: wildcard pattern

def debug(

self, text)

Feature search - lookup by name, ISO Country and Feature class
e.g. place.target='d*'
place.country_iso='gb'
place.feature='CSTL'

Args:

place: Uses place.target as lookup target, place.feature as feature target,   
    place.country_iso as country code target.

Returns:

None.  place.georow_list has list of matching georows

def get_admin1_alt_name(

self, place)

Get Admin1 name from alternate name table

Args:

place:   place instance.  place.admin1_id is used for lookup

Returns:

None.  place.admin2_id is updated with best match

def get_admin1_name(

self, place)

Get admin1 name using place.admin1_id

Args:

place:   place instance.  Uses  place.admin1_id for lookup

Returns:

Admin1 name.  Place instance admin1_name is updated with DB result

def get_admin1_name_direct(

self, admin1_id, iso)

Search for Admin1 name using admin1_id (rather than place instance)

Args:

admin1_id: Admin1 ID   
iso: country ISO

Returns:

None.  place.admin1_id and place.country_iso are updated with best match

def get_admin2_name(

self, place)

Get admin2 name using place.admin1_id and place.admin2_id

Args:

place:   place instance.  Uses  place.admin1_id and place.admin2_id for lookup

Returns:

Admin2 name.  Place instance admin2_name is updated with DB result

def get_admin2_name_direct(

self, admin1_id, admin2_id, iso)

Search for Admin2 name using admin2_id and admin1_id (rather than place instance)

Args:

admin1_id: Admin1 ID   
admin2_id: Admin2 ID   
iso: country ISO

Returns:

None.  place.admin1_id and place.country_iso are updated with best match

def get_alt_name(

self, geoid)

Retrieve alternate names for specified GEOID

Args:

geoid: Geoid to get alternate names for

Returns:

row_list from DB matches

def get_country_iso(

self, place)

return country ISO code for place.country_name

Args:

place:   place instance.  looks up by place.country_name

Returns:

Country ISO or ''.  If found, update place.country_name with DB country name

def get_country_name(

self, iso)

return country name for specified ISO code

Args:

iso:   Country ISO code

Returns:

Country name or ''

def get_db_version(

self)

Get schema version of database

Returns:

schema version of database

def get_geoid(

self, place)

Search for location using Geoid in place.target

Args:

place:   place instance.  Uses  Geoid in place.target for lookup

Returns:

None.   Place instance is updated with DB results

def get_row_count(

self)

Get row count of main.geodata :return:

def insert(

self, geo_row, feat_code)

Insert a geo_row into geonames database

Args:

geo_row: row to insert   
feat_code: Geonames feature code of item

Returns:

row_id for inserted row

def insert_alternate_name(

self, alternate_name, geoid, lang)

Add alternate name to altname table

Args:

alternate_name: alternate name to add for this geoid   
geoid: geonames.org geoid   
lang: ISO lang code for this entry

Returns: None

def insert_version(

self, db_version)

Insert DB version into Database. This is used to track when DB schema changes

Args:

db_version: Version of this DB schema

Returns: None

def lookup_main_dbid(

self, place)

Search for DB ID in main table

def lookup_place(

self, place)

Lookup a place in geoname.org db
Lookup is based on place.place_type as follows:
Loc.PlaceType.ADMIN1: does self.wide_search_admin1(place)
Loc.PlaceType.ADMIN2: does self.wide_search_admin2(place)
Loc.PlaceType.COUNTRY: does self.wide_search_country(place)
Loc.PlaceType.ADVANCED_SEARCH: does self.feature_search(place)
Otherwise: do self.wide_search_city(place)

Args:

place: Loc instance.  Call Loc.parse_place() before calling lookup_place()

Returns:

None.  
Place.georow_list contains a list of matching entries.  
Each entry has: Lat, Long, districtID (County or State or Province ID), and a match quality score

def make_georow(

name, iso, adm1, adm2, lat, lon, feat, geoid, sdx)

Create a georow based on arguments

Args:

name:   
iso:   
adm1: admin1 id   
adm2: admin2_id   
lat:   
lon: 
feat:     
geoid:   
sdx:

Returns:

georow

def process_query_list(

self, result_list, select_string, from_tbl, query_list)

Args: result_list: select_string: from_tbl: query_list:

Returns: Result type. Result list contains matches

def set_display_names(

self, temp_place)

See if there is an alternate name entry for this place

Args:

temp_place: place instance

Returns: None

def wide_search_admin1(

self, place)

Search for Admin1 using place.admin1_name

Args:

place:

Returns:

None.  place.georow_list is updated with list of matches

def wide_search_admin1_id(

self, place)

Search for Admin1 ID using place.admin1_name

Args:

place:

Returns:

None.  place.admin1_id and place.country_iso are updated with best match

def wide_search_admin2(

self, place)

Search for Admin2 using place.admin2_name

Args:

place:

Returns:

None.  place.georow_list is updated with list of matches

def wide_search_admin2_id(

self, place)

Search for Admin2 ID using place.admin2_name

Args:

place:

Returns:

None.  place.admin2_id is updated with best match

def wide_search_city(

self, place)

Search for city using place.target

Args:

place: Loc instance

Returns:

None.  place.georow_list is updated with list of matches

def wide_search_country(

self, place)

Search for Country using country_iso

Args:

place:

Returns:

None.  place.georow_list is updated with list of matches

def word_match(

self, select_string, where, from_tbl, args)

Perform a wildcard match on each word in args[0], and then
merges the results into a single result. During the merge, we note if
a duplicate occurs, and mark that as a higher priority result. We
also note if an individual word has too many results, as we will drop
those results from the final list after doing the priority checks.
This should kill off common words from the results, while still
preserving combinations.

For example, searching for "Village of Bay", will find all three words
to have very many results, but the combinations of "Village" and "Bay"
or "Village" and "of" or "Bay" and "of" will show up in the results.

The order of the words will also not matter, so results should contain
"City of Bay Village", "Bay Village" etc.

Args:

select_string:    
where:  
from_tbl:  
args: args[0] has the words to search for

Returns: List of matches

Instance variables

var db

var db_limit

var db_path

var detailed_debug

var geoid_admin_dict

var geoid_main_dict

var logger

var match

var max_query_results

var place_type

var select_str

var start

var total_lookups

var total_time

var use_wildcards