-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_attr.py
64 lines (53 loc) · 1.93 KB
/
find_attr.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
# find_attr.py
"""Run this module with the pathname of a mapname-e_STATE/layer directory, and
a (major, minor) attribute pair, to get the occurrences of the attribute pair
in the 4 or 16 files of data for this layer.
"""
import os
import sys
import dlg
categories = [
'boundaries',
'hydrography',
'hypsography',
'public_lands',
'transportation',
]
#-------------------------------------------------------------------------------
# find_attributes -
#-------------------------------------------------------------------------------
def find_attributes(dirpath, major, minor):
"""Read all the files under this category (half a quad)"""
if dirpath[-1] == '/':
# Remove trailing slash, otherwise basename will be empty
dirpath = dirpath[:-1]
base = os.path.basename(dirpath)
if base not in categories:
print(f'Unknown category "{base}", exiting.')
exit()
# Iterate over files in this category (sections of the quad)
for f in os.listdir(dirpath):
if not f.endswith('.opt.gz'):
continue
print(f'Processing {f}')
dlgf = dlg.load_data(os.path.join(dirpath, f))
finds = dlgf.has_attribute(major, minor)
s = ''
for elem_type in ['nodes', 'areas', 'lines']:
if elem_type not in finds:
continue
# print(f'len={len(finds[elem_type])}')
s += f'{elem_type}\n'
for elem_id in finds[elem_type]:
s += f"{' '*4}{elem_id}"
print(s)
#===============================================================================
# main
#===============================================================================
if __name__ == '__main__':
# Check cmd line args
if len(sys.argv) != 4:
print(f'usage: {sys.argv[0]} <dirpath> <major> <minor>')
exit(-1)
dirpath = sys.argv[1]
print(find_attributes(dirpath, int(sys.argv[2]), int(sys.argv[3])))