Made sure that the XML parser gets valid XML as otherwise the check lead to crashes in the code that generates the orthos

This commit is contained in:
Marcus Str. 2024-12-01 15:55:34 +01:00
parent 8774e3b6ac
commit 7b7aff4acf

View File

@ -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