-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate-geo-data.py
132 lines (116 loc) · 3.17 KB
/
generate-geo-data.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import django
from django.core.management import call_command
import os
from utils import (
JURISDICTION_NAMES,
ROOTDIR,
bulk_upload,
convert_to_geojson,
create_tiles,
download_from_tiger,
download_boundary_file,
find_jurisdiction,
load_settings,
setup_source,
upload_tiles,
)
def _django_cmds() -> None:
"""
Execute Django commands to ensure database is configured correctly
"""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djapp.settings")
django.setup()
call_command("migrate")
call_command("load_divisions")
call_command("clean_divisions")
def generate_geo_data(
SETTINGS,
jurisdictions,
) -> None:
db_url = os.environ.get("DATABASE_URL")
if not db_url:
os.environ.setdefault(
"DATABASE_URL",
"postgis://openstates:openstates@localhost:5405/openstatesorg",
)
"""
Download shp files from TIGER
"""
for jur in jurisdictions:
if jur not in JURISDICTION_NAMES:
print(f"Invalid jurisdiction {jur}. Skipping.")
continue
if jur not in SETTINGS["jurisdictions"]:
print(f"Skipping {jur}. No configuration present.")
continue
print(f"Fetching shapefiles for {jur}")
jurisdiction = find_jurisdiction(jur)
download_from_tiger(jurisdiction, "RD18", SETTINGS)
download_boundary_file(SETTINGS["BOUNDARY_YEAR"])
"""
Convert downloaded shp files to geojson
"""
convert_to_geojson(SETTINGS)
if SETTINGS["create_tiles"]:
create_tiles(SETTINGS)
if SETTINGS["run_migrations"]:
_django_cmds()
if SETTINGS["upload_data"]:
bulk_upload(SETTINGS)
upload_tiles()
def main():
parser = ArgumentParser(
description="Download and process shapefiles for defined jurisdictions",
formatter_class=ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--clean-source",
action="store_true",
default=False,
help="Remove any cached download/processed data",
)
parser.add_argument(
"--config",
"-c",
type=str,
default=f"{ROOTDIR}/configs",
help="Config directory for downloading geo data",
)
parser.add_argument(
"--run-migrations",
"-m",
action="store_true",
default=False,
help="Run database migrations and uploads",
)
parser.add_argument(
"--upload-data",
"-u",
action="store_true",
default=False,
help="Actually upload data to S3/Mapbox",
)
parser.add_argument(
"--skip-tile-creation",
"-s",
action="store_true",
default=False,
help="Don't generate Mapbox tilesets",
)
args = parser.parse_args()
SETTINGS = load_settings(
args.config,
args.run_migrations,
args.upload_data,
args.skip_tile_creation,
args.clean_source,
)
setup_source(SETTINGS)
generate_geo_data(
SETTINGS,
JURISDICTION_NAMES,
)
if __name__ == "__main__":
main()