-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
62 lines (46 loc) · 1.8 KB
/
util.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
"""
Utilities.
"""
import unittest
# ******************
# Shapefile utilities
# ******************
# Note there are files of differing geographic granuality: census tracts, block groups, blocks.
# They are named accordingly: shapefiles/[state]/[level]_[regions]
# level: (county|tract)
# region: (e.g. middlesex_norfolk_suffolk)
# e.g. /shapefiles/ma/blockgroup_middlesex_norfolk_suffolk.shp
DEFAULT_SHAPEFILE_DIR = "./shapefiles/"
def get_shapefile_filename(level, regions=None):
""" Constructs filename for shapefile for given level and regions
Args:
(str) level of census area
[(str)] optional list of regions [reg1, reg2, ...]
Returns filename in format 'level[_reg1_reg2]' for each region in regions list,
where the output regions are sorted.
If no regions included, returns 'level'
"""
filename = level
if (regions and len(regions)):
regions = [r.lower() for r in regions]
regions = sorted(regions)
filename = filename + "_" + "_".join(regions)
return filename + '.shp'
def get_shapefile_filepath(state, level, regions=None, shapefile_dir=DEFAULT_SHAPEFILE_DIR):
return shapefile_dir + state + '/' + get_shapefile_filename(level, regions)
# ***********
# Tests
# ***********
class ShapefileTest(unittest.TestCase):
def test_get_shapefile_filename(self):
self.assertEqual(
get_shapefile_filename("county", ["norfolk", "Middlesex", "suffolk"]),
"county_middlesex_norfolk_suffolk.shp")
self.assertEqual(get_shapefile_filename("blockgroup"),
"blockgroup.shp")
def test_get_shapefile_filepath(self):
self.assertEqual(
get_shapefile_filepath("ma", "tract"),
DEFAULT_SHAPEFILE_DIR + "ma/tract.shp")
if __name__ == '__main__':
unittest.main()