From dbed1fa677be1fa1ea04b705eae4fbf73e3f83ba Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 24 Sep 2017 20:48:22 +0200 Subject: [PATCH] Make worker.closest JSON compatible --- classes/__init__.py | 29 ++++++++++++++++++++++++----- workers/closest.py | 16 +++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/classes/__init__.py b/classes/__init__.py index 37a0792..9d3151b 100644 --- a/classes/__init__.py +++ b/classes/__init__.py @@ -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 + "\n" + out += (" " * indent + " %s\n" % self.name) if name else "" out += (" " * indent + " %s\n" % self.useId()) if extid else "" + out += (" " * indent + " %i\n" % int(self.distance)) if distance else "" out += (" " * indent + " %s\n" % self.sttype) if sttype else "" if coords: @@ -35,7 +54,7 @@ class Station: out += (" " * indent + " %s\n" % self.prodclass) if prodclass else "" - out += " " * indent + "\n" + out += " " * indent + "" return out diff --git a/workers/closest.py b/workers/closest.py index 5dc83ae..3c9e0a8 100644 --- a/workers/closest.py +++ b/workers/closest.py @@ -60,17 +60,19 @@ def findStations(lat, lon, distance = 1000, validate = True): return stations def worker(lat, lon, distance = 1000, json = False): - outtext = """ + outtext = """{ + "stations": [ +""" if json else """ """ for station in findStations(lat, lon, distance): - try: - idistance = int(station.distance) - except: - idistance = int(cDistance(station.lat(), station.lon(), lat, lon)) - outtext += "%s%s%i\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 += "" + outtext += """ + ] +}""" if json else "" return outtext