Make worker.closest JSON compatible
This commit is contained in:
parent
cd3cc716b1
commit
dbed1fa677
|
@ -16,15 +16,34 @@ class Station:
|
|||
def lon(self):
|
||||
return self.xcoord
|
||||
|
||||
def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False):
|
||||
out = ""
|
||||
def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False):
|
||||
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 + " <name>%s</name>\n" % self.name) if name 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 ""
|
||||
|
||||
if coords:
|
||||
|
@ -35,7 +54,7 @@ class Station:
|
|||
|
||||
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
|
||||
|
||||
out += " " * indent + "</station>\n"
|
||||
out += " " * indent + "</station>"
|
||||
|
||||
return out
|
||||
|
||||
|
|
|
@ -60,17 +60,19 @@ def findStations(lat, lon, distance = 1000, validate = True):
|
|||
return stations
|
||||
|
||||
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>
|
||||
"""
|
||||
|
||||
for station in findStations(lat, lon, distance):
|
||||
try:
|
||||
idistance = int(station.distance)
|
||||
except:
|
||||
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 += ",\n" if (json and not outtext.strip()[-1] == "[") else ""
|
||||
station.distance = station.distance or int(cDistance(station.lat(), station.lon(), lat, lon))
|
||||
outtext += station.json(2, distance = True) if json else station.xml(1, distance = True)
|
||||
|
||||
outtext += "</stations>"
|
||||
outtext += """
|
||||
]
|
||||
}""" if json else "</stations>"
|
||||
|
||||
return outtext
|
||||
|
|
Loading…
Reference in a new issue