Skip to content

hdx.location.adminlevel

AdminLevel Objects

class AdminLevel()

[view_source]

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

[view_source]

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

[view_source]

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

[view_source]

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])

[view_source]

Setup a single p-code

Arguments:

  • countryiso3 str - Country
  • pcode str - P-code
  • adm_name str - Administrative name
  • parent 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

[view_source]

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 dictionary
  • countryiso3s 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

[view_source]

Setup p-codes from a libhxl Dataset object.

Arguments:

  • libhxl_dataset hxl.Dataset - Dataset object from libhxl library
  • countryiso3s 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

[view_source]

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

[view_source]

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

[view_source]

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

[view_source]

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]

[view_source]

Get list of all pcodes

Returns:

  • List[str] - List of pcodes

get_admin_level

def get_admin_level(countryiso3: str) -> int

[view_source]

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]

[view_source]

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

[view_source]

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]

[view_source]

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 code
  • pcode str - P code to match **kwargs:
  • parent Optional[str] - Parent admin code
  • logname 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]

[view_source]

Standardise pcode length by country and match to an internal pcode. Only works for admin1 pcodes.

Arguments:

  • countryiso3 str - ISO3 country code
  • pcode str - P code for admin one to match
  • logname 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]

[view_source]

Fuzzy match name to pcode

Arguments:

  • countryiso3 str - Iso3 country code
  • name str - Name to match **kwargs:
  • parent Optional[str] - Parent admin code
  • logname 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]

[view_source]

Get pcode for a given name

Arguments:

  • countryiso3 str - Iso3 country code
  • name str - Name to match
  • fuzzy_match bool - Whether to try fuzzy matching. Defaults to True. **kwargs:
  • parent Optional[str] - Parent admin code
  • logname 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]

[view_source]

Output log of matches

Returns:

  • List[str] - List of matches

output_ignored

def output_ignored() -> List[str]

[view_source]

Output log of ignored

Returns:

  • List[str] - List of ignored

output_errors

def output_errors() -> List[str]

[view_source]

Output log of errors

Returns:

  • List[str] - List of errors