Skip to content

Commit

Permalink
feat: add support for geotrace (#254)
Browse files Browse the repository at this point in the history
* fix: Actually write the CSV file for CSVDump.py

* fix: Add support for geotrace

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Rob Savoye <rob@senecass.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed May 19, 2024
1 parent 139c768 commit 50ad36b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
35 changes: 28 additions & 7 deletions osm_fieldwork/CSVDump.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3

# Copyright (c) 2020, 2021, 2022, 2023 Humanitarian OpenStreetMap Team
# Copyright (c) 2020, 2021, 2022, 2023, 2024 Humanitarian OpenStreetMap Team
#
# This file is part of OSM-Fieldwork.
#
Expand All @@ -24,6 +24,7 @@
import os
import re
import sys
from datetime import datetime

import pandas as pd
from geojson import Feature, FeatureCollection, Point, dump
Expand Down Expand Up @@ -125,7 +126,8 @@ def writeOSM(

def finishOSM(self):
"""Write the OSM XML file footer and close it."""
self.osm.footer()
# This is now handled by a destructor in the OsmFile class
# self.osm.footer()

def createGeoJson(
self,
Expand All @@ -149,6 +151,9 @@ def finishGeoJson(self):
"""Write the GeoJson FeatureCollection to the output file and close it."""
features = list()
for item in self.features:
if len(item["attrs"]["lon"]) == 0 or len(item["attrs"]["lat"]) == 0:
log.warning("Bad location data in entry! %r", item["attrs"])
continue
poi = Point((float(item["attrs"]["lon"]), float(item["attrs"]["lat"])))
if "private" in item:
props = {**item["tags"], **item["private"]}
Expand Down Expand Up @@ -293,8 +298,9 @@ def createEntry(

if value is not None and value != "no" and value != "unknown":
if key == "track" or key == "geoline":
refs.append(tag)
log.debug("Adding reference %s" % tag)
# refs.append(tags)
# log.debug("Adding reference %s" % tags)
refs = value.split(";")
elif len(value) > 0:
if self.privateData(key):
priv[key] = value
Expand All @@ -303,7 +309,7 @@ def createEntry(
if len(tags) > 0:
feature["attrs"] = attrs
feature["tags"] = tags
if len(refs) > 0:
if len(refs) > 1:
feature["refs"] = refs
if len(priv) > 0:
feature["private"] = priv
Expand Down Expand Up @@ -344,12 +350,27 @@ def main():
log.debug("Parsing csv files %r" % args.infile)
data = csvin.parse(args.infile)
# This OSM XML file only has OSM appropriate tags and values
nodeid = -1000
for entry in data:
feature = csvin.createEntry(entry)
# Sometimes bad entries, usually from debugging XForm design, sneak in
if len(feature) == 0:
continue
if len(feature) > 0:
if "refs" in feature:
refs = list()
for ref in feature["refs"]:
now = datetime.now().strftime("%Y-%m-%dT%TZ")
if len(ref) == 0:
continue
coords = ref.split(" ")
print(coords)
node = {"attrs": {"id": nodeid, "version": 1, "timestamp": now, "lat": coords[0], "lon": coords[1]}, "tags": dict()}
csvin.writeOSM(node)
refs.append(nodeid)
nodeid -= 1
feature["refs"] = refs
csvin.writeOSM(feature)
else:
# Sometimes bad entries, usually from debugging XForm design, sneak in
if "lat" not in feature["attrs"]:
log.warning("Bad record! %r" % feature)
continue
Expand Down
37 changes: 23 additions & 14 deletions osm_fieldwork/odk2osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

import argparse
import csv
import logging
import os
import re
Expand Down Expand Up @@ -102,14 +103,21 @@ def main():
continue
if jj is None:
continue
# print(f"tag: {i} == {j}")
print(f"tag2: {i} == {j}")
if type(jj) == OrderedDict or type(jj) == dict:
for iii, jjj in jj.items():
if jjj is not None:
tags[iii] = jjj
# print(iii, jjj)
pat = re.compile("[0-9.]* [0-9.-]* [0-9.]* [0-9.]*")
if pat.match(str(jjj)):
gps = jjj.split(" ")
tags["lat"] = gps[0]
tags["lon"] = gps[1]
continue
else:
tags[iii] = jjj
# print(f"FOO {iii}, {jjj}")
else:
# print(ii, jj)
# print(f"WHERE {ii}, {jj}")
fields.append(ii)
tags[ii] = jj
else:
Expand All @@ -121,18 +129,19 @@ def main():
tmp = xml.replace(" ", "").split("_")
now = datetime.now()
timestamp = f"_{now.year}_{now.hour}_{now.minute}"

outfile = tmp[0] + timestamp + ".csv"

# with open(outfile, "w", newline="") as csvfile:
# fields = list()
# for row in rows:
# for key in row.keys():
# if key not in fields:
# fields.append(key)
# csv = csv.DictWriter(csvfile, dialect="excel", fieldnames=fields)
# csv.writeheader()
# for row in rows:
# csv.writerow(row)
with open(outfile, "w", newline="") as csvfile:
fields = list()
for row in rows:
for key in row.keys():
if key not in fields:
fields.append(key)
out = csv.DictWriter(csvfile, dialect="excel", fieldnames=fields)
out.writeheader()
for row in rows:
out.writerow(row)

print("Wrote: %s" % outfile)

Expand Down

0 comments on commit 50ad36b

Please sign in to comment.