diff --git a/lib/xmet/city.py b/lib/xmet/city.py new file mode 100644 index 0000000..fad38b2 --- /dev/null +++ b/lib/xmet/city.py @@ -0,0 +1,41 @@ +import csv +import shapely + +""" +Implements a parser and wrapper class for the top 150 populous US cities +available on Wikipedia: + + https://en.wikipedia.org/wiki/List_of_United_States_cities_by_population + +The input TSV file is created by copying and pasting the tabular data +from a web browser into a text file. +""" + +class City(): + name: str + state: str + pop_estimate: int + pop_census: int + location: shapely.Point + + @staticmethod + def from_tsv_row(row: list): + city = City() + city.name = row[0] + city.state = row[1] + city.pop_estimate = int(row[2]) + city.pop_census = int(row[3]) + city.location = shapely.Point(float(row[10]), float(row[9])) + + return city + + @staticmethod + def each_from_tsv(file: str): + with open(file) as fh: + reader = csv.reader(fh, delimiter='\t') + + for row in reader: + for i in range(0, len(row)): + row[i] = row[i].rstrip() + + yield City.from_tsv_row(row)