From 7b7aff4acfe4d1743672bc998d4945c5aa378d43 Mon Sep 17 00:00:00 2001 From: "Marcus Str." Date: Sun, 1 Dec 2024 15:55:34 +0100 Subject: [PATCH] Made sure that the XML parser gets valid XML as otherwise the check lead to crashes in the code that generates the orthos --- osmxml.py | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/osmxml.py b/osmxml.py index 05e376e..f8ae5a5 100644 --- a/osmxml.py +++ b/osmxml.py @@ -13,16 +13,23 @@ import xml.dom.minidom +from pyexpat import ExpatError + import requests import os from defines import * from log import * +import time class mstr_osmxml: def __init__(self): #self._xmlfn = mstr_datafolder + "_cache/tile_" + str(lat) + "-" + str(v) + "_" + str(lng) + "-" + str(h) + ".xml" self._xmldata = None self._xmlcontent = "" + self._lat = 0 + self._lng = 0 + self._curB_lat = 0 + self._curB_lng = 0 # Adjust bbox for when this class should persost, but acquire data for a different bbox @@ -58,7 +65,8 @@ class mstr_osmxml: mstr_msg("osmxml", "Acquiring OSM data for " + str(self._lat)+","+str(self._lng)+" - "+str(self._curB_lat)+","+str(self._curB_lng)) # We will use our self-hosted API for this. - while self._xmlcontent == "": + parse = False + while parse == False: data = { "bbox": { "lat": str(self._lat), @@ -73,21 +81,24 @@ class mstr_osmxml: } r = requests.post(mstr_osm_endpoint, json=data) - self._xmlcontent = r.content + try: + # Attempt to parse the XML string + dom = xml.dom.minidom.parseString(r.content) - #if os.path.isfile(self._xmlfn): - # os.remove(self._xmlfn) - #with open(self._xmlfn, 'wb') as textfile: - # textfile.write(r.content) + # Check if the DOM object has a document element + if dom.documentElement: + # Store the content in memory + self._xmlcontent = r.content + self._xmldata = xml.dom.minidom.parseString(self._xmlcontent) + self._xmlcontent = "" # Clear + parse = True - # 1 second delay in case the request fails - if self._xmlcontent == "": - #if os.path.isfile(self._xmlfn) == False: - sleep(1) - - # Store the content in memory - self._xmldata = xml.dom.minidom.parseString(self._xmlcontent) - self._xmlcontent = "" # Clear + except ExpatError as e: + parse = False + time.sleep(1) + except Exception as e: + parse = False + time.sleep(1) # Get all nodes from the specified OSM file -- 2.30.2