-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateStreetIndex.py
73 lines (56 loc) · 2.75 KB
/
CreateStreetIndex.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
import os
import sys
from collections import defaultdict
from collections import OrderedDict
import arcpy
def main(argv=None):
if len(argv) != 5:
arcpy.AddMessage("Usage: CreateStreetIndex <centerline_feature_class> <centerline_street_name> <index_feature_class> <index_name> <output_file>")
return(1)
(road_centerlines, street_name , index_map, index_name, output_file) = tuple(argv)
arcpy.env.overwriteOutput = True
scratch_workspace = arcpy.env.scratchGDB
# Select all features from map index that don't have NULL names
index_map_layer = arcpy.management.MakeFeatureLayer(index_map, 'indexmap',
"""{0} IS NOT NULL""".format(index_name))
# Make sure we have some features to work with
cnt = arcpy.management.GetCount(index_map_layer)
if not int(cnt.getOutput(0)) > 0:
arcpy.AddError('{0} field contains all NULL values and cannot proceed.'.format(index_name))
return
# Intersect centerlines with the map index features.
arcpy.AddMessage('--Intersecting centerlines with index layer...')
intersect_result = arcpy.analysis.Intersect([road_centerlines, index_map_layer], os.path.join(scratch_workspace, 'centerlines_index'), output_type='LINE')
# Make a view of the intersect result where street name is not null.
result_view = arcpy.management.MakeTableView(intersect_result, 'result_view',
"""{0} IS NOT NULL AND {0} <> ''""".format(street_name ))
# Create a defaultdict for the streets so they can be sorted by key
streets = defaultdict(list)
# Fields to output
fields = [street_name , index_name]
# Loop through the view and get a list of index values for each street
with arcpy.da.SearchCursor(result_view, fields) as rows:
for row in rows:
s = row[0] # street name
p = row[1] # page number
streets[s].append(p)
# Now that we have a full list, sort by street name
sortedstreets = OrderedDict(sorted(streets.items()))
# Loop through the sorted streets and create output
arcpy.AddMessage(f'--Writing index to {output_file}...')
firstletter = ''
f = open(output_file, 'w')
for street, indexes in sortedstreets.items():
if street[0] != firstletter:
firstletter = street[0]
f.write(firstletter + '\n')
output_string = street + "\t" + ', '.join(sorted(set(indexes))) + '\n'
f.write(output_string)
f.close()
# # Delete intermediate data.
arcpy.AddMessage('--Cleaning up intermediate data...')
arcpy.management.Delete(intersect_result)
arcpy.AddMessage('--Finished!')
## End main function
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))