Implement city.py

This commit is contained in:
XANTRONIX 2025-03-30 18:10:38 -04:00
parent 0f30130f40
commit af29de2754

41
lib/xmet/city.py Normal file
View file

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