hdx.location.adminlevel
AdminLevel Objects
class AdminLevel()
AdminLevel class which takes in p-codes and then maps names to those p-codes with fuzzy matching if necessary.
The dictionary admin_config, which defaults to an empty dictionary, can have the following optional keys: countries_fuzzy_try are countries (iso3 codes) for which to try fuzzy matching. Default is all countries. admin_name_mappings is a dictionary of mappings from name to p-code (for where fuzzy matching fails) admin_name_replacements is a dictionary of textual replacements to try when fuzzy matching admin_fuzzy_dont is a list of names for which fuzzy matching should not be tried
Arguments:
admin_config
Dict - Configuration dictionary. Defaults to {}.admin_level
int - Admin level. Defaults to 1.admin_level_overrides
Dict - Countries at other admin levels.
looks_like_pcode
@classmethod
def looks_like_pcode(cls, string: str) -> bool
Check if a string looks like a p-code using regex matching of format. Checks for 2 or 3 letter country iso code at start and then numbers.
Arguments:
string
str - String to check
Returns:
bool
- Whether string looks like a p-code
set_default_admin_url
@classmethod
def set_default_admin_url(cls, admin_url: Optional[str] = None) -> None
Set default admin URL from which to retrieve admin data
Arguments:
admin_url
Optional[str] - Admin URL from which to retrieve admin data. Defaults to internal value.
Returns:
None
get_libhxl_dataset
@classmethod
def get_libhxl_dataset(cls, admin_url: str = _admin_url) -> hxl.Dataset
Get libhxl Dataset object given a URL which defaults to global p-codes dataset on HDX.
Arguments:
admin_url
str - URL from which to load data. Defaults to global p-codes dataset.
Returns:
None
setup_row
def setup_row(countryiso3: str, pcode: str, adm_name: str,
parent: Optional[str])
Setup a single p-code
Arguments:
countryiso3
str - Countrypcode
str - P-codeadm_name
str - Administrative nameparent
Optional[str] - Parent p-code
Returns:
None
setup_from_admin_info
def setup_from_admin_info(
admin_info: ListTuple[Dict],
countryiso3s: Optional[ListTuple[str]] = None) -> None
Setup p-codes from admin_info which is a list with values of the form below with parent optional: :: {"iso3": "AFG", "pcode": "AF0101", "name": "Kabul", parent: "AF01"}
Arguments:
admin_info
ListTuple[Dict] - p-code dictionarycountryiso3s
Optional[ListTuple[str]] - Countries to read. Defaults to None (all).
Returns:
None
setup_from_libhxl_dataset
def setup_from_libhxl_dataset(
libhxl_dataset: hxl.Dataset,
countryiso3s: Optional[ListTuple[str]] = None) -> None
Setup p-codes from a libhxl Dataset object.
Arguments:
libhxl_dataset
hxl.Dataset - Dataset object from libhxl librarycountryiso3s
Optional[ListTuple[str]] - Countries to read. Defaults to None (all).
Returns:
None
setup_from_url
def setup_from_url(admin_url: str = _admin_url,
countryiso3s: Optional[ListTuple[str]] = None) -> None
Setup p-codes from a URL. Defaults to global p-codes dataset on HDX.
Arguments:
admin_url
str - URL from which to load data. Defaults to global p-codes dataset.countryiso3s
Optional[ListTuple[str]] - Countries to read. Defaults to None (all).
Returns:
None
load_pcode_formats
def load_pcode_formats(formats_url: str = _formats_url) -> None
Load p-code formats from a URL. Defaults to global p-codes dataset on HDX.
Arguments:
formats_url
str - URL from which to load data. Defaults to global p-codes dataset.
Returns:
None
set_parent_admins
def set_parent_admins(parent_admins: List[List]) -> None
Set parent admins
Arguments:
parent_admins
List[List] - List of P-codes per parent admin
Returns:
None
set_parent_admins_from_adminlevels
def set_parent_admins_from_adminlevels(
adminlevels: List["AdminLevel"]) -> None
Set parent admins from AdminLevel objects
Arguments:
parent_admins
List[AdminLevel] - List of parent AdminLevel objects
Returns:
None
get_pcode_list
def get_pcode_list() -> List[str]
Get list of all pcodes
Returns:
List[str]
- List of pcodes
get_admin_level
def get_admin_level(countryiso3: str) -> int
Get admin level for country
Arguments:
countryiso3
str - Iso3 country code
Returns:
int
- Admin level
get_pcode_length
def get_pcode_length(countryiso3: str) -> Optional[int]
Get pcode length for country
Arguments:
countryiso3
str - Iso3 country code
Returns:
Optional[int]
- Country's pcode length or None
init_matches_errors
def init_matches_errors() -> None
Initialise storage of fuzzy matches, ignored and errors for logging purposes
Returns:
None
convert_admin_pcode_length
def convert_admin_pcode_length(countryiso3: str, pcode: str,
**kwargs: Any) -> Optional[str]
Standardise pcode length by country and match to an internal pcode. Requires that p-code formats be loaded (eg. using load_pcode_formats)
Arguments:
countryiso3
str - ISO3 country codepcode
str - P code to match **kwargs:parent
Optional[str] - Parent admin codelogname
str - Log using this identifying name. Defaults to not logging.
Returns:
Optional[str]
- Matched P code or None if no match
convert_admin1_pcode_length
def convert_admin1_pcode_length(
countryiso3: str,
pcode: str,
logname: Optional[str] = None) -> Optional[str]
Standardise pcode length by country and match to an internal pcode. Only works for admin1 pcodes.
Arguments:
countryiso3
str - ISO3 country codepcode
str - P code for admin one to matchlogname
Optional[str] - Identifying name to use when logging. Defaults to None (don't log).
Returns:
Optional[str]
- Matched P code or None if no match
fuzzy_pcode
def fuzzy_pcode(countryiso3: str, name: str, **kwargs: Any) -> Optional[str]
Fuzzy match name to pcode
Arguments:
countryiso3
str - Iso3 country codename
str - Name to match **kwargs:parent
Optional[str] - Parent admin codelogname
str - Log using this identifying name. Defaults to not logging.
Returns:
Optional[str]
- Matched P code or None if no match
get_pcode
def get_pcode(countryiso3: str,
name: str,
fuzzy_match: bool = True,
**kwargs: Any) -> Tuple[Optional[str], bool]
Get pcode for a given name
Arguments:
countryiso3
str - Iso3 country codename
str - Name to matchfuzzy_match
bool - Whether to try fuzzy matching. Defaults to True. **kwargs:parent
Optional[str] - Parent admin codelogname
str - Log using this identifying name. Defaults to not logging.
Returns:
Tuple[Optional[str], bool]: (Matched P code or None if no match, True if exact match or False if not)
output_matches
def output_matches() -> List[str]
Output log of matches
Returns:
List[str]
- List of matches
output_ignored
def output_ignored() -> List[str]
Output log of ignored
Returns:
List[str]
- List of ignored
output_errors
def output_errors() -> List[str]
Output log of errors
Returns:
List[str]
- List of errors