Skip to content

OdkForm

ODKInstance

ODKInstance(filespec=None, data=None)

Bases: object

Parameters:

Name Type Description Default
filespec str

The filespec to the ODK XML Instance file

None
data str

The XML data

None

Returns:

Type Description
ODKInstance

An instance of this object

Source code in osm_fieldwork/ODKInstance.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(
    self,
    filespec: str = None,
    data: str = None,
):
    """This class imports a ODK Instance file, which is in XML into a
    data structure.

    Args:
        filespec (str): The filespec to the ODK XML Instance file
        data (str): The XML data

    Returns:
        (ODKInstance): An instance of this object
    """
    self.data = data
    self.filespec = filespec
    self.ignore = ["today", "start", "deviceid", "nodel", "instanceID"]
    if filespec:
        self.data = self.parse(filespec=filespec)
    elif data:
        self.data = self.parse(data)

parse

parse(filespec, data=None)

Import an ODK XML Instance file ito a data structure. The input is either a filespec to the Instance file copied off your phone, or the XML that has been read in elsewhere.

Parameters:

Name Type Description Default
filespec str

The filespec to the ODK XML Instance file

required
data str

The XML data

None

Returns:

Type Description
dict

All the entries in the OSM XML Instance file

Source code in osm_fieldwork/ODKInstance.py
 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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def parse(
    self,
    filespec: str,
    data: str = None,
) -> dict:
    """Import an ODK XML Instance file ito a data structure. The input is
    either a filespec to the Instance file copied off your phone, or
    the XML that has been read in elsewhere.

    Args:
        filespec (str): The filespec to the ODK XML Instance file
        data (str): The XML data

    Returns:
        (dict): All the entries in the OSM XML Instance file
    """
    row = dict()
    if filespec:
        logging.info("Processing instance file: %s" % filespec)
        file = open(filespec, "rb")
        # Instances are small, read the whole file
        xml = file.read(os.path.getsize(filespec))
    elif data:
        xml = data
    doc = xmltodict.parse(xml)

    json.dumps(doc)
    tags = dict()
    data = doc["data"]
    flattened = flatdict.FlatDict(data)
    rows = list()
    pat = re.compile("[0-9.]* [0-9.-]* [0-9.]* [0-9.]*")
    for key, value in flattened.items():
        if key[0] == "@" or value is None:
            continue
        if re.search(pat, value):
            gps = value.split(" ")
            row["lat"] = gps[0]
            row["lon"] = gps[1]
            continue

        # print(key, value)
        tmp = key.split(":")
        if tmp[len(tmp) - 1] in self.ignore:
            continue
        row[tmp[len(tmp) - 1]] = value

    return row