77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
|
|
from country_iso2ify import get_resolver
|
|
import pycountry
|
|
from country_bounding_boxes import country_subunits_by_iso_code
|
|
|
|
|
|
|
|
class Topology:
|
|
def __init__(self,
|
|
country=None,
|
|
iso_country=None,
|
|
subregion=None,
|
|
linemap_size=(800,800),
|
|
linemap_rows=30,
|
|
linemap_cols=30,
|
|
linemap_width=1.0,
|
|
linemap_roundcap=True,
|
|
linemap_transparent=False
|
|
):
|
|
self.country = country
|
|
self.iso_country = iso_country
|
|
self.subregion = subregion
|
|
self.linemap_size = linemap_size
|
|
self.linemap_rows = linemap_rows
|
|
self.linemap_cols = linemap_cols
|
|
self.linemap_width = linemap_width
|
|
self.linemap_roundcap = linemap_roundcap
|
|
self.linemap_transparent = linemap_transparent
|
|
if self.subregion is None:
|
|
self.subregion = self.country
|
|
if self.country is None and self.iso_country is None:
|
|
self.country = "Switzerland"
|
|
if self.country is None:
|
|
self.country = pycountry.countries.get(alpha_2=self.iso_country).name
|
|
self.subregion = self.country
|
|
return
|
|
if self.iso_country is None:
|
|
self.iso_country = get_resolver().resolve(self.country)
|
|
return
|
|
|
|
def list_subunits(self):
|
|
countries = country_subunits_by_iso_code(self.iso_country)
|
|
print("Country is [{}] iso code is [{}]".format(self.country, self.iso_country))
|
|
item = None
|
|
if countries:
|
|
for item in countries:
|
|
print("Subunit of [{}]: [{}]".format(self.country, item.subunit))
|
|
|
|
def get_mainland_bbox(self):
|
|
countries = country_subunits_by_iso_code(self.iso_country)
|
|
print("bbox--Country is [{}] iso code is [{}]".format(self.country, self.iso_country))
|
|
# The library is designed to return the main body of the country
|
|
# in the first result or by filtering for largest area.
|
|
item = None
|
|
countries = country_subunits_by_iso_code(self.iso_country)
|
|
if countries and self.subregion is None:
|
|
for item in countries:
|
|
print(self.subregion)
|
|
if item.subunit == self.subregion:
|
|
return item.bbox
|
|
else:
|
|
if countries:
|
|
for item in countries:
|
|
if item.subunit == self.subregion:
|
|
return item.bbox
|
|
return None
|
|
|
|
if __name__ == "__main__":
|
|
topology = Topology(country="France")
|
|
topology.list_subunits()
|
|
print(topology.get_mainland_bbox())
|
|
t2=Topology(country="United Kingdom",subregion="Wales")
|
|
t2.list_subunits()
|
|
print(t2.get_mainland_bbox())
|
|
t3=Topology(iso_country="US")
|
|
t3.list_subunits()
|
|
print(t3.get_mainland_bbox()) |