\r
\r
import xml.dom.minidom\r
+from pyexpat import ExpatError\r
+\r
import requests\r
import os\r
from defines import *\r
from log import *\r
+import time\r
\r
class mstr_osmxml:\r
def __init__(self):\r
#self._xmlfn = mstr_datafolder + "_cache/tile_" + str(lat) + "-" + str(v) + "_" + str(lng) + "-" + str(h) + ".xml"\r
self._xmldata = None\r
self._xmlcontent = ""\r
+ self._lat = 0\r
+ self._lng = 0\r
+ self._curB_lat = 0\r
+ self._curB_lng = 0\r
\r
\r
# Adjust bbox for when this class should persost, but acquire data for a different bbox\r
mstr_msg("osmxml", "Acquiring OSM data for " + str(self._lat)+","+str(self._lng)+" - "+str(self._curB_lat)+","+str(self._curB_lng))\r
\r
# We will use our self-hosted API for this.\r
- while self._xmlcontent == "":\r
+ parse = False\r
+ while parse == False:\r
data = { \r
"bbox": { \r
"lat": str(self._lat),\r
}\r
r = requests.post(mstr_osm_endpoint, json=data)\r
\r
- self._xmlcontent = r.content\r
+ try:\r
+ # Attempt to parse the XML string\r
+ dom = xml.dom.minidom.parseString(r.content)\r
\r
- #if os.path.isfile(self._xmlfn):\r
- # os.remove(self._xmlfn)\r
- #with open(self._xmlfn, 'wb') as textfile:\r
- # textfile.write(r.content)\r
+ # Check if the DOM object has a document element\r
+ if dom.documentElement:\r
+ # Store the content in memory\r
+ self._xmlcontent = r.content\r
+ self._xmldata = xml.dom.minidom.parseString(self._xmlcontent)\r
+ self._xmlcontent = "" # Clear\r
+ parse = True\r
\r
- # 1 second delay in case the request fails\r
- if self._xmlcontent == "":\r
- #if os.path.isfile(self._xmlfn) == False:\r
- sleep(1)\r
- \r
- # Store the content in memory\r
- self._xmldata = xml.dom.minidom.parseString(self._xmlcontent)\r
- self._xmlcontent = "" # Clear\r
+ except ExpatError as e:\r
+ parse = False\r
+ time.sleep(1)\r
+ except Exception as e:\r
+ parse = False\r
+ time.sleep(1)\r
\r
\r
# Get all nodes from the specified OSM file\r