Skip to content

trails.py

Trails

Trails(dataspec=None, yamlspec='utilities/trails.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/trails.yaml'

Returns:

Type Description
Trails

An instance of this class

Source code in osm_merge/utilities/trails.py
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
def __init__(self,
             dataspec: str = None,
             yamlspec: str = "utilities/trails.yaml",
             ):
    """
    This class processes the NPS and USFS Trails datasets.

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

    Returns:
        (Trails): 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()

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/trails.py
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 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 = count_lines(filespec)
    #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
    # single threaded for debugging
    if single:
        osmdata = processDataThread(self.config, list(data))
    else:
        logging.error(f"FIXME: multithreaded reader isn't implemented yet!")
    return osmdata

processDataThread

processDataThread(config, data, fixref=True)

Convert the Feature from the NPS or USFS schema to OSM syntax.

Source code in osm_merge/utilities/trails.py
 64
 65
 66
 67
 68
 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
110
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
def processDataThread(config: dict,
                      # filespec: str,
                      data: list,
                      fixref: bool = True,
                      ) -> FeatureCollection:
    """
    Convert the Feature from the NPS or 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()
        # OBJECTID is only in the NPS dataset
        if "OBJECTID" in entry["properties"]:
            props["operator"] = "National Park Service"
        else:
            props["operator"] = "US Forest Service"
        props["highway"] = "path"
        if geom is None:
            # This is unfortunately common in the USFS dataset
            # logging.error(f"The entry has no geometry!")
            continue
        for key, value in entry["properties"].items():
            # In USFS Trail data, these are the types of backcountry
            # access types. Each one has four values, but we only need
            # one, since in OSM the value will only be "yes".
            if value == "N/A" or value is None or value == "Unknown":
                continue
            pat = re.compile(".*ACCPT_DISC")
            if pat.search(key):
                hours = parse_opening_hours(value)
                if len(hours) > 0:
                    props["seasonal"] = "yes"
                    props["opening_hours"] = hours
                for k2, v2 in config["tags"]["type"].items():
                    pat = re.compile(f".*{k2}.*")
                    if pat.search(key.lower()):
                        if v2.find('=') > 0:
                            tmp = v2.split('=')
                            props[tmp[0]] = tmp[1]
                        else:
                            if "OBJECTID" in entry["properties"]:
                                props[v2] = "yes"
                            else:
                                props[v2] = "designated"
            if key not in config["tags"]:
                continue
            # print(f"FIXME: \'{key}\' = {value}")
            if config["tags"][key] == "name":
                # Seems obvious if there is no name, so drop it. The ref will
                # be enough to identify the trail.
                if value == "Un-Named" or len(value.strip()) == 0 or value is None:
                    continue
                words = value.split(' ')
                newname = value
                for word in words:
                    if word in config["abbreviations"]:
                        newname = newname.replace(word, config["abbreviations"][word])
                if newname.title().find(" Trail") > 0:
                    props["name"] = f"{newname.title()}"
                else:
                    props["name"] = f"{newname.title()} Trail"
            elif config["tags"][key] == "alt_name":
                if len(value.strip()) > 0:
                    # this is a bogus alternate name
                    if value == "Developed Area Trail":
                        continue
                    props["alt_name"] = value.title()
            elif config["tags"][key] == "ref":
                if "OBJECTID" in entry["properties"]:
                    props["ref"] = f"NPS {value}"
                else:
                    props["ref"] = f"FR {value}"
            # elif config["tags"][key] == "operator":
            #     if value == "RG-NPS":
            #         props["operator"] = "National Park Service"
            #     else:
            #         props["operator"] = value
            elif config["tags"][key] == "surface":
                pass
            elif config["tags"][key] == "seasonal":
                if value == "Yes":
                    props["seasonal"] = "yes"
                elif value == "No":
                    # props["seasonal"] = "no"
                    # This is the default, avoid tag bloat
                    pass
            elif config["tags"][key] == "access":
                # print(f"FIXME: {value}")
                for access in config["tags"]["access"]:
                    [[k2, v2]] = access.items()
                    pat = re.compile(f".*{k2}.*")
                    if pat.search(value.lower()):
                        if v2.find('=') > 0:
                            tmp = v2.split('=')
                            props[tmp[0]] = tmp[1]
                        else:
                            props[v2] = "yes"
                        # According to the OSM Wiki, these access types
                        # use highway=track instead of highway=path
                        if k2 in config["tags"]["highway"]:
                            props["highway"] = "track"

        simple = shapely.simplify(shape(geom), tolerance=0.0001) # preserve_topology=True
        # Short trails 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))

    return FeatureCollection(highways)

main

main()

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

Source code in osm_merge/utilities/trails.py
247
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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 NPS and USFS trails datasets. It will convert
the data an OSM tagging schema so it can be conflated. Abbreviations are
discouraged in OSM, so they are expanded. Most entries in the two
dataset are ignored as they are blank. The schema is similar to the
MVUM schema, but not exactly.

    For Example: 
        trails.py -v -i NPS_-_Trails_-_Geographic_Coordinate_System.geojson -o NPS_Trails.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.
    if args.verbose:
        log.setLevel(logging.DEBUG)
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.DEBUG)
        formatter = logging.Formatter(
            "%(threadName)10s - %(name)s - %(levelname)s - %(message)s"
        )
        ch.setFormatter(formatter)
        log.addHandler(ch)

    trails = Trails()
    data = trails.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