oebb_py/classes/station.py

81 lines
2.8 KiB
Python

class Station:
def __init__(self, name, sttype, extid = None, xcoord = None, ycoord = None, prodclass = None):
self.name = name
self.sttype = sttype
self.extid = extid
self.xcoord = float(xcoord)/1000000 if xcoord else None
self.ycoord = float(ycoord)/1000000 if ycoord else None
self.prodclass = prodclass
self.services = []
def addService(self, svc):
self.services += [svc]
def useId(self):
return self.extid or self.name
def lat(self):
return self.ycoord
def lon(self):
return self.xcoord
def json(self, indent = 0, name = True, extid = True, sttype = False, coords = False, prodclass = False, distance = False, services = False, servicekwargs = {}):
out = " " * indent + "{\n"
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 ""
if coords and self.xcoord:
out += " " * indent + " \"coords\": {\n"
out += " " * indent + " \"lon\": %f,\n" % self.xcoord
out += " " * indent + " \"lat\": %f\n" % self.ycoord
out += " " * indent + " },\n"
if services and self.services:
out += " " * indent + " \"services\": [\n"
for i in range(len(self.services)):
out += self.services[i].json(indent + 2, i, **servicekwargs) + (",\n" if not i == len(self.services) - 1 else "\n")
out += " " * indent + " ],"
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, services = False, servicekwargs = {}):
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 and self.xcoord:
out += " " * indent + " <coords>\n"
out += " " * indent + " <lon>%f</lon>\n" % self.xcoord
out += " " * indent + " <lat>%f</lat>\n" % self.ycoord
out += " " * indent + " </coords>\n"
if services and self.services:
out += " " * indent + " <services>\n"
for i in range(len(self.services)):
out += self.services[i].xml(indent + 2, i, **servicekwargs) + "\n"
out += " " * indent + " </services>\n"
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
out += " " * indent + "</station>"
return out