Make worker.closest JSON compatible
This commit is contained in:
parent
cd3cc716b1
commit
dbed1fa677
2 changed files with 33 additions and 12 deletions
|
@ -16,15 +16,34 @@ class Station:
|
||||||
def lon(self):
|
def lon(self):
|
||||||
return self.xcoord
|
return self.xcoord
|
||||||
|
|
||||||
def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False):
|
def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False):
|
||||||
out = ""
|
out = " " * indent + "{\n"
|
||||||
|
|
||||||
return
|
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
|
||||||
|
out += (" " * indent + " \"id\": \"%s\",\n" % self.useId()) if extid else ""
|
||||||
|
out += (" " * indent + " \"distance\": %i,\n" % int(self.distance)) if distance else ""
|
||||||
|
out += (" " * indent + " \"type\": \"%s\",\n" % self.sttype) if sttype else ""
|
||||||
|
|
||||||
def xml(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False):
|
if coords:
|
||||||
|
out += " " * indent + " \"coords\": {\n"
|
||||||
|
out += " " * indent + " \"lon\": %f,\n" % self.xcoord
|
||||||
|
out += " " * indent + " \"lat\": %f\n" % self.ycoord
|
||||||
|
out += " " * indent + " },\n"
|
||||||
|
|
||||||
|
out += (" " * indent + " \"prodclass\": \"%s\",\n" % self.prodclass) if prodclass else ""
|
||||||
|
|
||||||
|
out = "".join(out.rsplit(",", 1))
|
||||||
|
|
||||||
|
out += " " * indent + "}"
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
def xml(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False):
|
||||||
out = " " * indent + "<station>\n"
|
out = " " * indent + "<station>\n"
|
||||||
|
|
||||||
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
|
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
|
||||||
out += (" " * indent + " <id>%s</id>\n" % self.useId()) if extid else ""
|
out += (" " * indent + " <id>%s</id>\n" % self.useId()) if extid else ""
|
||||||
|
out += (" " * indent + " <distance>%i</distance>\n" % int(self.distance)) if distance else ""
|
||||||
out += (" " * indent + " <type>%s</type>\n" % self.sttype) if sttype else ""
|
out += (" " * indent + " <type>%s</type>\n" % self.sttype) if sttype else ""
|
||||||
|
|
||||||
if coords:
|
if coords:
|
||||||
|
@ -35,7 +54,7 @@ class Station:
|
||||||
|
|
||||||
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
|
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
|
||||||
|
|
||||||
out += " " * indent + "</station>\n"
|
out += " " * indent + "</station>"
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
|
@ -60,17 +60,19 @@ def findStations(lat, lon, distance = 1000, validate = True):
|
||||||
return stations
|
return stations
|
||||||
|
|
||||||
def worker(lat, lon, distance = 1000, json = False):
|
def worker(lat, lon, distance = 1000, json = False):
|
||||||
outtext = """<?xml version="1.0" encoding="UTF-8"?>
|
outtext = """{
|
||||||
|
"stations": [
|
||||||
|
""" if json else """<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<stations>
|
<stations>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for station in findStations(lat, lon, distance):
|
for station in findStations(lat, lon, distance):
|
||||||
try:
|
outtext += ",\n" if (json and not outtext.strip()[-1] == "[") else ""
|
||||||
idistance = int(station.distance)
|
station.distance = station.distance or int(cDistance(station.lat(), station.lon(), lat, lon))
|
||||||
except:
|
outtext += station.json(2, distance = True) if json else station.xml(1, distance = True)
|
||||||
idistance = int(cDistance(station.lat(), station.lon(), lat, lon))
|
|
||||||
outtext += "<station><name>%s</name><id>%s</id><distance>%i</distance></station>\n" % (station.name, station.useId(), idistance)
|
|
||||||
|
|
||||||
outtext += "</stations>"
|
outtext += """
|
||||||
|
]
|
||||||
|
}""" if json else "</stations>"
|
||||||
|
|
||||||
return outtext
|
return outtext
|
||||||
|
|
Loading…
Reference in a new issue