Skip to content

odk2osm.py

main

main()

This is a program that reads in the ODK Instance file, which is in XML, and converts it to an OSM XML file so it can be viewed in an editor.

Source code in osm_merge/fieldwork/odk2osm.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def main():
    """This is a program that reads in the ODK Instance file, which is in XML,
    and converts it to an OSM XML file so it can be viewed in an editor.
    """
    parser = argparse.ArgumentParser(description="Convert ODK XML instance file to OSM XML format")
    parser.add_argument("-v", "--verbose", nargs="?", const="0", help="verbose output")
    parser.add_argument("-y", "--yaml", help="Alternate YAML file")
    parser.add_argument("-x", "--xlsfile", help="Source XLSFile")
    parser.add_argument("-i", "--infile", required=True, help="The input file")
    parser.add_argument("-o","--outfile", default='out.osm',
                        help='The output file for JOSM')
    args = parser.parse_args()

    # if verbose, dump to the terminal
    if args.verbose is not None:
        logging.basicConfig(
            level=logging.DEBUG,
            format=("%(threadName)10s - %(name)s - %(levelname)s - %(message)s"),
            datefmt="%y-%m-%d %H:%M:%S",
            stream=sys.stdout,
        )

    toplevel = Path(args.infile)
    odk = ODKParsers(args.yaml)
    odk.parseXLS(args.xlsfile)
    xmlfiles = list()
    data = list()
    # It's a wildcard, used for XML instance files
    if args.infile.find("*") >= 0:
        log.debug(f"Parsing multiple ODK XML files {args.infile}")
        toplevel = Path(args.infile[:-1])
        for dirs in glob.glob(args.infile):
            xml = os.listdir(dirs)
            full = os.path.join(dirs, xml[0])
            xmlfiles.append(full)
        for infile in xmlfiles:
            entry = odk.XMLparser(infile)
            # entry = odk.createEntry(tmp[0])
            data.append(entry)
    elif toplevel.suffix == ".xml":
        # It's an instance file from ODK Collect
        log.debug(f"Parsing ODK XML files {args.infile}")
        # There is always only one XML file per infile
        full = os.path.join(toplevel, os.path.basename(toplevel))
        xmlfiles.append(full + ".xml")
        tmp = odk.XMLparser(args.infile)
        # entry = odk.createEntry(tmp)
        data.append(entry)
    elif toplevel.suffix == ".csv":
        log.debug(f"Parsing csv files {args.infile}")
        for entry in odk.CSVparser(args.infile):
            data.append(entry)
    elif toplevel.suffix == ".json":
        log.debug(f"Parsing json files {args.infile}")
        for entry in odk.JSONparser(args.infile):
            data.append(entry)

    # Write the data
    osm = OsmFile()
    osm.writeOSM(data, args.outfile)
    log.info(f"Wrote {args.outfile}")

options: show_source: false heading_level: 3