Skip to content

Commit

Permalink
Merge branch 'multi' of github.com:hotosm/osm-fieldwork into multi
Browse files Browse the repository at this point in the history
  • Loading branch information
rsavoye committed Jun 15, 2024
2 parents 0e42198 + 9953555 commit 5e93da4
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 129 deletions.
16 changes: 8 additions & 8 deletions osm_fieldwork/ODKInstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
#

import argparse
import json
import logging
import os
import re
import sys

import flatdict
import json
from collections import OrderedDict
import xmltodict

# Instantiate logger
log = logging.getLogger(__name__)


class ODKInstance(object):
def __init__(
self,
filespec: str = None,
data: str = None,
):
"""
This class imports a ODK Instance file, which is in XML into a
"""This class imports a ODK Instance file, which is in XML into a
data structure.
Args:
Expand All @@ -60,8 +60,7 @@ def parse(
filespec: str,
data: str = None,
) -> dict:
"""
Import an ODK XML Instance file ito a data structure. The input is
"""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.
Expand Down Expand Up @@ -89,7 +88,7 @@ def parse(
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:
if key[0] == "@" or value is None:
continue
if re.search(pat, value):
gps = value.split(" ")
Expand All @@ -98,13 +97,14 @@ def parse(
continue

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

return row


if __name__ == "__main__":
"""This is just a hook so this file can be run standlone during development."""
parser = argparse.ArgumentParser()
Expand Down
67 changes: 26 additions & 41 deletions osm_fieldwork/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import logging
import re
import sys
import pandas as pd
import re

import pandas as pd

Expand All @@ -33,9 +31,9 @@
# Instantiate logger
log = logging.getLogger(__name__)


def escape(value: str) -> str:
"""
Escape characters like embedded quotes in text fields.
"""Escape characters like embedded quotes in text fields.
Args:
value (str):The string to modify
Expand All @@ -47,9 +45,9 @@ def escape(value: str) -> str:
tmp = value.replace("&", " and ")
return tmp.replace("'", "'")


class Convert(YamlFile):
"""
A class to apply a YAML config file and convert ODK to OSM.
"""A class to apply a YAML config file and convert ODK to OSM.
Returns:
(Convert): An instance of this object
Expand Down Expand Up @@ -102,8 +100,7 @@ def privateData(
self,
keyword: str,
) -> bool:
"""
Search the private data category for a keyword.
"""Search the private data category for a keyword.
Args:
keyword (str): The keyword to search for
Expand All @@ -117,8 +114,7 @@ def convertData(
self,
keyword: str,
) -> bool:
"""
Search the convert data category for a keyword.
"""Search the convert data category for a keyword.
Args:
keyword (str): The keyword to search for
Expand All @@ -132,8 +128,7 @@ def ignoreData(
self,
keyword: str,
) -> bool:
"""
Search the convert data category for a ketyword.
"""Search the convert data category for a ketyword.
Args:
keyword (str): The keyword to search for
Expand All @@ -147,8 +142,7 @@ def getKeyword(
self,
value: str,
) -> str:
"""
Get the keyword for a value from the yaml file.
"""Get the keyword for a value from the yaml file.
Args:
value (str): The value to find the keyword for
Expand All @@ -167,8 +161,7 @@ def getValues(
self,
keyword: str = None,
) -> str:
"""
Get the values for a primary key.
"""Get the values for a primary key.
Args:
keyword (str): The keyword to get the value of
Expand All @@ -187,8 +180,7 @@ def convertEntry(
tag: str,
value: str,
) -> list:
"""
Convert a tag and value from the ODK represention to an OSM one.
"""Convert a tag and value from the ODK represention to an OSM one.
Args:
tag (str): The tag from the ODK XML file
Expand All @@ -215,7 +207,7 @@ def convertEntry(
# If the tag is in the config file, convert it.
if self.convertData(newtag):
newtag = self.convertTag(newtag)
#if newtag != tag:
# if newtag != tag:
# logging.debug(f"Converted Tag for entry {tag} to {newtag}")

# Truncate the elevation, as it's really long
Expand All @@ -240,8 +232,7 @@ def convertValue(
tag: str,
value: str,
) -> list:
"""
Convert a single tag value.
"""Convert a single tag value.
Args:
tag (str): The tag from the ODK XML file
Expand Down Expand Up @@ -284,8 +275,7 @@ def convertTag(
self,
tag: str,
) -> str:
"""
Convert a single tag.
"""Convert a single tag.
Args:
tag (str): The tag from the ODK XML file
Expand Down Expand Up @@ -317,8 +307,7 @@ def convertMultiple(
self,
value: str,
) -> list:
"""
Convert a multiple tags from a select_multiple question..
"""Convert a multiple tags from a select_multiple question..
Args:
value (str): The tags from the ODK XML file
Expand All @@ -327,12 +316,12 @@ def convertMultiple(
(list): The new tags
"""
tags = dict()
for tag in value.split(' '):
for tag in value.split(" "):
low = tag.lower()
if self.convertData(low):
newtag = self.convert[low]
if newtag.find('=') > 0:
tmp = newtag.split('=')
if newtag.find("=") > 0:
tmp = newtag.split("=")
if tmp[0] in tags:
tags[tmp[0]] = f"{tags[tmp[0]]};{tmp[1]}"
else:
Expand All @@ -354,10 +343,10 @@ def parseXLS(
defaults = self.entries["default"]
i = 0
while i < len(self.entries):
if type(self.entries['type'][i]) == float:
self.types[self.entries['name'][i]] = None
if type(self.entries["type"][i]) == float:
self.types[self.entries["name"][i]] = None
else:
self.types[self.entries['name'][i]] = self.entries['type'][i].split(' ')[0]
self.types[self.entries["name"][i]] = self.entries["type"][i].split(" ")[0]
i += 1
total = len(names)
i = 0
Expand All @@ -377,8 +366,7 @@ def createEntry(
self,
entry: dict,
) -> dict:
"""
Create the feature data structure.
"""Create the feature data structure.
Args:
entry (dict): The feature data
Expand Down Expand Up @@ -429,8 +417,8 @@ def createEntry(
# log.debug("Adding attribute %s with value %s" % (key, value))
continue
if value is not None and value != "no" and value != "unknown":
if key == 'username':
tags['user'] = value
if key == "username":
tags["user"] = value
continue
items = self.convertEntry(key, value)
if key in self.types:
Expand Down Expand Up @@ -466,9 +454,7 @@ def createEntry(
return feature

def dump(self):
"""
Dump internal data structures, for debugging purposes only.
"""
"""Dump internal data structures, for debugging purposes only."""
print("YAML file: %s" % self.filespec)
print("Convert section")
for key, val in self.convert.items():
Expand All @@ -489,9 +475,7 @@ def dump(self):
# this way than using pytest,
#
def main():
"""
This main function lets this class be run standalone by a bash script.
"""
"""This main function lets this class be run standalone by a bash script."""
parser = argparse.ArgumentParser(description="Read and parse a YAML file")

parser.add_argument("-v", "--verbose", action="store_true", help="verbose output")
Expand Down Expand Up @@ -553,6 +537,7 @@ def main():
for i in entry:
print("XX: %r" % i)


if __name__ == "__main__":
"""This is just a hook so this file can be run standlone during development."""
main()
17 changes: 6 additions & 11 deletions osm_fieldwork/csvdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,20 @@
import csv
import logging
import os
import re
import sys
from datetime import datetime

import pandas as pd
from geojson import Feature, FeatureCollection, Point, dump

from osm_fieldwork.convert import Convert
from osm_fieldwork.osmfile import OsmFile
from osm_fieldwork.xlsforms import xlsforms_path
from osm_fieldwork.support import basename
from osm_fieldwork.xlsforms import xlsforms_path

# Instantiate logger
log = logging.getLogger(__name__)


class CSVDump(Convert):
"""
A class to parse the CSV files from ODK Central.
"""
"""A class to parse the CSV files from ODK Central."""

def __init__(
self,
yaml: str = None,
Expand All @@ -68,8 +62,7 @@ def parse(
filespec: str,
data: str = None,
) -> list:
"""
Parse the CSV file from ODK Central and convert it to a data structure.
"""Parse the CSV file from ODK Central and convert it to a data structure.
Args:
filespec (str): The file to parse.
Expand Down Expand Up @@ -139,6 +132,7 @@ def parse(
all_tags.append(tags)
return all_tags


def main():
"""Run conversion directly from the terminal."""
parser = argparse.ArgumentParser(description="convert CSV from ODK Central to OSM XML")
Expand Down Expand Up @@ -208,6 +202,7 @@ def main():
log.info("Wrote OSM XML file: %r" % osmoutfile)
log.info("Wrote GeoJson file: %r" % jsonoutfile)


if __name__ == "__main__":
"""This is just a hook so this file can be run standlone during development."""
main()
12 changes: 4 additions & 8 deletions osm_fieldwork/jsondump.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@
import logging

# import pandas as pd
import re
import sys
from pathlib import Path

import flatdict
import geojson
import shapely
from geojson import Feature, FeatureCollection, Point, dump

from osm_fieldwork.convert import Convert
from osm_fieldwork.osmfile import OsmFile

log = logging.getLogger(__name__)

Expand All @@ -45,8 +41,7 @@ def __init__(
self,
yaml: str = None,
):
"""
A class to convert the JSON file from ODK Central, or the GeoJson
"""A class to convert the JSON file from ODK Central, or the GeoJson
file created by the odk2geojson utility.
Args:
Expand All @@ -68,8 +63,7 @@ def parse(
filespec: str = None,
data: str = None,
) -> list:
"""
Parse the JSON file from ODK Central and convert it to a data structure.
"""Parse the JSON file from ODK Central and convert it to a data structure.
The input is either a filespec to open, or the data itself.
Args:
Expand Down Expand Up @@ -181,6 +175,7 @@ def parse(
# log.debug(f"Finished parsing JSON file {filespec}")
return total


def main():
"""Run conversion directly from the terminal."""
parser = argparse.ArgumentParser(description="convert JSON from ODK Central to OSM XML")
Expand Down Expand Up @@ -252,6 +247,7 @@ def main():
log.info("Wrote OSM XML file: %r" % osmoutfile)
log.info("Wrote GeoJson file: %r" % jsonoutfile)


if __name__ == "__main__":
"""This is just a hook so this file can be run standlone during development."""
main()
Loading

0 comments on commit 5e93da4

Please sign in to comment.