This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracts.py
61 lines (48 loc) · 1.86 KB
/
tracts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pathlib
import zipfile
import logging
from urllib.request import urlretrieve
logger = logging.getLogger(__name__)
class BaseTractDownloader(object):
THIS_DIR = pathlib.Path(__file__).parent
def __init__(self):
self.data_dir = self.THIS_DIR.joinpath("data", "tracts")
if not self.data_dir.exists():
self.data_dir.mkdir()
def run(self):
self.download()
self.unzip()
def download(self):
"""
Downloads the TIGER SHP file of Census block for the provided state and county.
Returns the path to the ZIP file.
"""
# Check if the zip file already exists
zip_path = self.data_dir.joinpath(self.zip_name)
if zip_path.exists():
logger.debug(f"ZIP file already exists at {zip_path}")
return zip_path
# If it doesn't, download it from the Census FTP
logger.debug(f"Downloading {self.url} to {zip_path}")
urlretrieve(self.url, zip_path)
# Return the path
return zip_path
def unzip(self):
"""
Unzip the provided ZIP file.
"""
shp_path = pathlib.Path(self.zip_name.replace(".zip", ".shp"))
if shp_path.exists():
logger.debug(f"SHP already unzipped at {shp_path}")
return shp_path
zip_path = self.data_dir.joinpath(self.zip_name)
logger.debug(f"Unzipping {zip_path} to {self.data_dir}")
with zipfile.ZipFile(zip_path, "r") as z:
z.extractall(self.data_dir)
return shp_path
class TractDownloader2010(BaseTractDownloader):
zip_name = "tl_2010_06_tract10.zip"
url = f"https://www2.census.gov/geo/tiger/TIGER2010/TRACT/2010/{zip_name}"
class TractDownloader2000(BaseTractDownloader):
zip_name = "tl_2009_06_tract00.zip"
url = f"https://www2.census.gov/geo/tiger/TIGER2009/06_CALIFORNIA/{zip_name}"