Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add support for ways when importing an OSM XML file #275

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions osm_fieldwork/osmfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,53 +353,61 @@ def loadFile(
"""
size = os.path.getsize(osmfile)
with open(osmfile, "r") as file:
xml = file.read(size) # Instances are small, read the whole file
xml = file.read(size)
doc = xmltodict.parse(xml)
if "osm" not in doc:
logging.warning("No data in this instance")
return False
field = doc["osm"]

if "node" not in field:
data = doc["osm"]
if "node" not in data:
logging.warning("No nodes in this instance")
return False

if type(field["node"]) == dict:
attrs = dict()
for node in data["node"]:
attrs = {
"id": int(node["@id"]),
"lat": node["@lat"][:10],
"lon": node["@lon"][:10],
}
if "@timestamp" in node:
attrs["timestamp"] = node["@timestamp"]

tags = dict()
for k, v in field["node"].items():
if k[0] == "@":
attrs[k[1:]] = v
else:
if type(field["node"]["tag"]) == dict:
tags[field["node"]["tag"]["@k"]] = field["node"]["tag"]["@v"].strip()
if "tag" in node:
for tag in node["tag"]:
if type(tag) == dict:
tags[tag["@k"]] = tag["@v"].strip()
# continue
else:
for pair in field["node"]["tag"]:
tags[pair["@k"]] = pair["@v"]

tags[node["tag"]["@k"]] = node["tag"]["@v"].strip()
# continue
node = {"attrs": attrs, "tags": tags}
self.data[node["attrs"]["id"]] = node
else:
for node in field["node"]:
attrs = {
"id": int(node["@id"]),
"lat": node["@lat"][:10],
"lon": node["@lon"][:10],
}
if "@timestamp" in node:
attrs["timestamp"] = node["@timestamp"]

tags = dict()
if "tag" in node:
for tag in node["tag"]:
if type(tag) == dict:
tags[tag["@k"]] = tag["@v"].strip()
# continue
else:
tags[node["tag"]["@k"]] = node["tag"]["@v"].strip()
# continue
node = {"attrs": attrs, "tags": tags}
self.data.append(node)
self.data.append(node)

for way in data["way"]:
attrs = {
"id": int(way["@id"]),
}
refs = list()
if len(way["nd"]) > 0:
for ref in way["nd"]:
refs.append(int(ref["@ref"]))

if "@timestamp" in node:
attrs["timestamp"] = node["@timestamp"]

tags = dict()
if "tag" in way:
for tag in way["tag"]:
if type(tag) == dict:
tags[tag["@k"]] = tag["@v"].strip()
# continue
else:
tags[node["tag"]["@k"]] = node["tag"]["@v"].strip()
# continue
way = {"attrs": attrs, "refs": refs, "tags": tags}
self.data.append(way)

return self.data

def dump(self):
Expand Down
Loading