Skip to content

mvum.py

MVUM

MVUM(dataspec=None, yamlspec='utilities/mvum.yaml')

Bases: object

Parameters:

Name Type Description Default
dataspec str

The input data to convert

None
yamlspec str

The YAML config file for converting data

'utilities/mvum.yaml'

Returns:

Type Description
MVUM

An instance of this class

Source code in osm_merge/utilities/mvum.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def __init__(self,
             dataspec: str = None,
             yamlspec: str = "utilities/mvum.yaml",
             ):
    """
    This class processes the MVUM dataset.

    Args:
        dataspec (str): The input data to convert
        yamlspec (str): The YAML config file for converting data

    Returns:
        (MVUM): An instance of this class
    """
    self.file = None
    if dataspec is not None:
        self.file = open(dataspec, "r")

    filespec = f"{rootdir}/{yamlspec}"
    if not os.path.exists(filespec):
        log.error(f"{yamlspec} does not exist!")
        quit()

    yaml = YamlFile(filespec)
    # yaml.dump()
    self.config = yaml.getEntries()

count_lines

count_lines(filespec)

Count the records in the data file.

Parameters:

Name Type Description Default
filespec str

The input data file name

required

Returns:

Type Description
int

The number of lines in the file

Source code in osm_merge/utilities/mvum.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def count_lines(self, filespec: str) -> int:
    """
    Count the records in the data file.

    Args:
        filespec (str): The input data file name

    Returns:
        (int): The number of lines in the file
    """
    if not os.path.exists(filespec):
        logging.error(f"{filespec} doesn't exist!")
        return -1

    # data = fiona.open(filespec, "r")
    with fiona.open(filespec, 'r') as fp:
        for count, line in enumerate(fp):
            pass
        fp.close()
        return count

process_data

process_data(filespec, overwrite, usemem=True, tmpdir='/tmp')

Read a large file in chunks so python stops core dumping.

Parameters:

Name Type Description Default
filespec str

The input data file name

required

Returns:

Type Description
list

The lines of data from the file

Source code in osm_merge/utilities/mvum.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
def process_data(self,
                 filespec: str,
                 overwrite: bool,
                 usemem: bool = True,
                 tmpdir: str = "/tmp") -> list():
    """
    Read a large file in chunks so python stops core dumping.

    Args:
        filespec (str): The input data file name

    Returns:
        (list): The lines of data from the file
    """

    path = Path(filespec)
    lines = self.count_lines(path)
    if lines < 0:
        # logging.error(f"")
        return list()

    # Split the files into smaller chunks, one for each core.
    # FIXME: I hate tmp files, but disk space is better till
    # python stops core dumping on large files as the geojson
    # module loads the entire file into memory.
    size = round(lines / cores)

    data = fiona.open(filespec, "r")
    meta = data.meta
    files = list()
    single = True           # FIXME: debug only
    if not single:
        # with concurrent.futures.ProcessPoolExecutor(max_workers=cores) as executor:
        #     for block in range(0, size, chunk):
        #         data = list()
        #         future = executor.submit(processDataThread, self.conf,            )
        #         futures.append(future)
        #     #for thread in concurrent.futures.wait(futures, return_when='ALL_COMPLETED'):
        #     for future in concurrent.futures.as_completed(futures):
        #         res = future.result()
        #         # log.debug(f"Waiting for thread to complete..,")
        #         data.extend(res[0])
        #         # newdata.extend(res[1])
        #     alldata = [data, newdata]
        # return
        pass

    # single threaded for debugging
    if single:
        osmdata = processDataThread(self.config, list(data))

    # osmdata = list()
    # for i in range(0, lines, size):
    #     # Ignore the first output record, it's empty
    #     if i == 0:
    #         continue
    #     chunk = list()
    #     # dataout = f"{tmpdir}/{path.stem}_{i}.geojson"
    #     # files.append(dataout)
    #     # if os.path.exists(dataout):
    #     #    continue
    #     for record in iter(data):
    #         chunk.append(record)
    #         if len(chunk) == i:
    #             # for feature in chunk:
    #             #    alldata.append(feature)
    #             if not usemem:
    #                 dataout = f"{tmpdir}/{path.stem}_{i}.geojson"
    #                 out = fiona.open(dataout, 'w', **meta)
    #                 out.writerecords(chunk)
    #                 out.close()
    #                 logging.debug(f"Wrote tmp file {dataout}...")
    #             else:
    #                 if single:
    #                     osmdata.apppend(processDataThread(self.config, chunk))

    return osmdata

dump

dump()

Dump internal variables for debugging.

Source code in osm_merge/utilities/mvum.py
374
375
376
377
378
379
def dump(self):
    """
    Dump internal variables for debugging.
    """
    for key, values in self.config:
        print(f"\t{k} = {values}")

parse_opening_hours

parse_opening_hours(datesopen)

Parse the string from the external dataset to OSM format.

Parameters:

Name Type Description Default
datesopen str

The string from the external dataset

required

Returns:

Type Description
str

The formatted string for OSM

Source code in osm_merge/utilities/mvum.py
 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
def parse_opening_hours(datesopen: str) -> str:
    """
    Parse the string from the external dataset to OSM format.

    Args:
        datesopen (str): The string from the external dataset

    Returns:
        (str): The formatted string for OSM
    """
    months = {1: "Jan",
              2: "Feb",
              3: "Mar",
              4: "Apr",
              5: "May",
              6: "Jun",
              7: "Jul",
              8: "Aug",
              9: "Sep",
              10: "Oct",
              11: "Nov",
              12: "Dec",
              }
    opening_hours = str()
    # it's seasonal=no
    if not datesopen or datesopen == "01/01-12/31" or len(datesopen) < 11:
        return str()

    # Date ranges start with the first of the month, and end with
    # the last day of the month.
    dates = datesopen.split('-')
    start = dates[0].split('/')[0]
    try:
        end = dates[1].split('/')[0]
    except:
        breakpoint()
    try:
        opening_hours = f"{months[int(start)]}-{months[int(end)]}"
    except:
        breakpoint()
    return opening_hours

processDataThread

processDataThread(config, data, fixref=True)

Convert the Feature from the USFS schema to OSM syntax.

Source code in osm_merge/utilities/mvum.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def processDataThread(config: dict,
                      # filespec: str,
                      data: list,
                      fixref: bool = True,
                      ) -> FeatureCollection:
    """
    Convert the Feature from the USFS schema to OSM syntax.

    """
    spin = Bar('Processing input data...', max=len(data))
    highways = list()

    for entry in data:
        spin.next()
        geom = entry["geometry"]
        props = dict()
        if geom is None:
            continue
        exists = ["Drive", "Road", "Lane", "Circle"]
        # unclassified, as we don't know what it is.
        #        if "highway" not in props:
        props["highway"] = "unclassified"

        for key, value in entry["properties"].items():
            # print(f"FIXME: \'{key}\' = {value}")
            if key in config["tags"]["vehicle"]:
                props[key.lower()] = "designated"

            if key[-9:] == "DATESOPEN":
                if "opening_hours" not in props:
                    hours = parse_opening_hours(value)
                    # print(f"FIXME: {value} : {len(hours)}")
                    if len(hours) > 0:
                        props["opening_hours"] = hours
                        props["seasonal"] = "yes"
                    elif "opening_hours" in props:
                        props["seasonal"] = "no"
            if key not in config["tags"]:
                continue
            if config["tags"][key] == "name":
                if value:
                    # print(f"FIXME: \'{props}\' = {value.title()}")
                    newname = re.sub(' +', ' ', value.title()).strip()
                    # Some names are just spaces
                    if len(value.strip()) == 0 or newname == "Un-Named":
                        continue
                    words = newname.split(' ')
                    for word in words:
                        if word in config["abbreviations"]:
                            newname = newname.replace(word, config["abbreviations"][word])
                    # the ref is often duplicated in the name.
                    if "refs" in props and "name" in props:
                        if props["ref"] == props["name"]:
                            del props["name"]
                    # most of the names lack "Road" OSM prefers
                    props["name"] = newname.title()
            if config["tags"][key] == "smoothness":
                if value is None:
                    continue
                if value[:1].isnumeric():
                    # breakpoint()
                    if "smoothness" not in props:
                        for k, v in config["tags"]["smoothness"].items():
                            if k == value[:1]:
                                props["smoothness"] = v

                if len(value.strip()) == 0:
                    continue
                if value[:2] == "NA":
                    continue
                index = int(value[:1])
                if index >= 3:
                    props["4wd_only"] = "yes"
                for i in config["tags"]["smoothness"]:
                    if int(i) == index:
                        props["smoothness"] = config["tags"]["smoothness"][i]
            if config["tags"][key] == "seasonal":
                if value in config["tags"]["seasonal"]:
                    if bool(value):
                        props["seasonal"] = "no"
                    else:
                        props["seasonal"] = "yes"
            elif config["tags"][key] == "ref":
                if value is None or len(value.strip()) == 0:
                    continue
                if value.isnumeric() and fixref and len(value) == 5:
                    # FIXME: this fixes multiple forests in Utah and Colorado,
                    # don't know if any other states use a 5 digit reference
                    # numbers
                    # props["ref:orig"] = f"FR {value}"
                    props["ref"] = f"FR {value}"
                    # props["ref"] = f"FR {value[1:]}"
                    # props["note"] = f"Validate this changed ref!"
                    # logging.debug(f"Converted {value} to {props["ref"]}")
                elif value.isalnum() and fixref:
                    pat = re.compile("[0-9]+")
                    result = re.match(pat, value)
                    # There are other patterns, like M21 for example
                    if not result:
                        props["ref"] = f"FR {value}"
                        continue

                    num = result.group()
                    if len(num) == 5:
                        # FIXME: Same here, but need to validate if 5 digit
                        # reference numbers are used by some forests.
                        num = num[1:]
                    minor = value.find('.') > 0
                    if minor:
                        result = value.split('.')
                        newref = f"${num}.${result[1]}"
                    else:
                        alpha = value[len(num):]
                        newref = f"{num}{alpha}"

                    # props["ref:orig"] = f"FR {value}"
                    props["ref"] = f"FR {newref}"
                    # props["note"] = "Validate this changed ref!"
                else:
                    props["ref"] = f"FR {value}"

        if "name" in props and props["name"].find("Road") <= 0:
            props["name"] += " Road"
        if geom is not None:
            props["highway"] = "unclassified"
            simple = shapely.simplify(shape(geom), tolerance=0.0001) # preserve_topology=True
            # Short roads may get simplified down to a single node.
            # In that case, use the original geometry.
            if shapely.count_coordinates(simple) <= 1:
                highways.append(Feature(geometry=geom, properties=props))
            else:
                highways.append(Feature(geometry=simple, properties=props))
        # print(props)

    return FeatureCollection(highways)

main

main()

This main function lets this class be run standalone by a bash script

Source code in osm_merge/utilities/mvum.py
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def main():
    """This main function lets this class be run standalone by a bash script"""
    parser = argparse.ArgumentParser(
        prog="mvum",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="This program converts MVUM highway data into OSM tagging",
        epilog="""
This program processes the MVUM data. It will convert the MVUM dataset
to using OSM tagging schema so it can be conflated. Abbreviations are
discouraged in OSM, so they are expanded. Most entries in the MVUM
dataset are ignored. For fixing the TIGER mess, all that is relevant
are the name and the USFS reference number. The surface and smoothness
tags are also converted, but should never overide what is in OSM, as the
OSM values for these may be more recent. And the values change over time,
so what is in the MVUM dataset may not be accurate. These tags are converted
primarily as an aid to navigation when ground-truthing, since it's usually
good to avoid any highway with a smoothness of "very bad" or worse.

    For Example: 
        mvum.py -v -c -i WY_RoadsMVUM.geojson
        """,
    )
    parser.add_argument("-v", "--verbose", action="store_true", help="verbose output")
    parser.add_argument("-i", "--infile", required=True, help="Output file from the conflation")
    parser.add_argument("-c", "--convert", default=True, action="store_true", help="Convert MVUM feature to OSM feature")
    parser.add_argument("-o", "--outfile", default="out.geojson", help="Output file")

    args = parser.parse_args()

    # if verbose, dump to the terminal.
    log_level = os.getenv("LOG_LEVEL", default="INFO")
    if args.verbose is not None:
        log_level = logging.DEBUG

    logging.basicConfig(
        level=log_level,
        format=("%(asctime)s.%(msecs)03d [%(levelname)s] " "%(name)s | %(funcName)s:%(lineno)d | %(message)s"),
        datefmt="%y-%m-%d %H:%M:%S",
        stream=sys.stdout,
    )

    mvum = MVUM()

    if args.convert and args.convert:
        data = mvum.process_data(args.infile, False)
        # data = mvum.convert(args.infile)
        path = Path(args.outfile)
        if path.suffix == ".geojson":
            file = open(args.outfile, "w", encoding='utf-8',)
            # FIXME: should use fiona
            import geojson
            geojson.dump(data, file, indent=4)
        elif path.suffix == ".osm":
            osm = OsmFile()
            osm.header()
            osm.writeOSM(data, args.outfile)
            # osm.footer()
        log.info(f"Wrote {args.outfile}")

options: show_source: false heading_level: 3