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