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)
def category_attr_counts(dirpath): """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() # Dictionaries for cumulating attribute counts d_nodes = {} d_areas = {} d_lines = {} # 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)) d_nodes = dlg.merge_attrs(d_nodes, dlg.attrs_counts(dlgf.nodes)) d_areas = dlg.merge_attrs(d_areas, dlg.attrs_counts(dlgf.areas)) d_lines = dlg.merge_attrs(d_lines, dlg.attrs_counts(dlgf.lines)) return d_nodes, d_areas, d_lines
def show_headers(name, state): path = os.path.join(dlg_base_dir, name[0].upper()) for half in 'ew': mapname = f'{name}-{half}_{state}' map_path = os.path.join(path, mapname) if not os.path.isdir(map_path): print(f"Can't find '{mapname}'") else: layer_path = os.path.join(map_path, 'transportation') for f in os.listdir(layer_path): if f.endswith('.opt.gz'): filepath = os.path.join(layer_path, f) d = dlg.load_data(filepath) print(d.show_headers())
def open(self, filepath): """Open a single file. This corresponds to a certain mapname, section, and category. """ # mapname / category / filename filename = os.path.basename(filepath) m = re.match('[0-9]+.([A-Z]{2}).opt.gz', filename) if not m: return category = m.group(1) dir = os.path.dirname(os.path.dirname(filepath)) mapname = os.path.basename(dir) # Get the actual data from the DLG-3 file try: dlg_instance = load_data(filepath) except ValueError: return obj = Dlg3LocalObject(dlg_instance, filepath, mapname, category, filename) section = obj.dlg_instance.section # The set of open files is organized as a dictionary of categories, # each category has a dictionary of mapnames, and each mapname has a # dictionary of (section, file) couples. if category not in self.files: d = {} d[mapname] = {} d[mapname][section] = obj self.files[category] = d else: d = self.files[category] if mapname not in d: d[mapname] = {} d[mapname][section] = obj else: # FIXME are we replacing with the same thing ? d[mapname][section] = obj print(self.get_local_mapnames()) # Let the client know what we've loaded return mapname, category, filename
def test02_second_file(self): # Open another instance of the same file (only in the context of this # method, not in the shared model) for comparison purposes d = load_data(self.filepath) hdr_2nd = d.show_headers() # Test the 'get_first_file' method # FIXME the DlgFile class does not implement __eq__, so we can't test # dlg instances for equality. In the mean time, we settle for comparing # the dlg file headers. md = self.model.get_first_file() hdr_md = md.show_headers() self.assertEqual(hdr_2nd, hdr_md) # Test the 'get_files_by_category' method x = list(self.model.get_files_by_category(self.category)) self.assertEqual(1, len(x)) md = x[0] hdr_md = md.show_headers() self.assertEqual(hdr_2nd, hdr_md)
def open_files_category(self, tgt_category): """Request to have this category in all the currently open files. The tgt_category is given as the two-letter code. If there are mapnames/files open that do not have this category, then we add it by opening the relevant files. Note that not every mapname has every category, we may well find that the tgt_category is absent. """ print(tgt_category) if tgt_category in self.files: # Invariant: when a category exists in the data structure, all open # mapnames have the files for this category. return self.files[tgt_category] = {} for mapname in self.get_local_mapnames(): print(mapname) # print(f'open_files_category: mapname={mapname}') self.files[tgt_category][mapname] = {} # Now we need to know what files are open from this mapname. It's # not in terms of files, it's in terms of sections: the same # section, F03 for example, can have files open in many category. for src_section in self.get_sections(mapname): # Each of these sections has a file open in some category # If tgt_category is 'roads and trails' (code RD), then every # current section is normal. Every section we find will be F0x, # but in getting the files, we might need to get 4 files # instead of just 1 if the target is RD and dense (this is # handled by storage.py). # If tgt_category is not a transportation one (RR, MT, or RD): # # - if no section is currently open on transportation, then # it's a one-for-one mapping. # # - if there is some transportation section open, and it is # dense, then the target section is bigger than the current # section, and we will find 4 source sections pointing to # the same target section. # Request the file(s) from storage module filepaths = get_matching_filepaths(mapname, tgt_category, src_section) if filepaths is None: s = f'tgt_category=={tgt_category}, mapname={mapname}' s += f', no filepaths found for section {src_section}' print(s) continue # Get the actual data from the DLG-3 file for f in filepaths: try: dlg_instance = load_data(f) except ValueError: continue filename = os.path.basename(f) obj = Dlg3LocalObject(dlg_instance, f, mapname, tgt_category, filename) self.files[tgt_category][mapname][src_section] = obj