Position the bus stops with markers on the Stavanger region map

v2


In [1]:
from IPython.display import SVG, display
import os, csv, time

def c2p (lat,lon):
    x1 = 5.492680
    y1 = 59.038151
    x2 = 5.791032
    y2 = 58.794293 # Map range
    qvx = 1193/(x2-x1)
    qvy = 1881/(y1-y2) # Pixels/decimal degree
    dx = lon-x1
    dy = y1-lat
    rx = int(dx*qvx)
    ry = int(dy*qvy)
    return (rx,ry) # Take decimal degrees and give coordinates in pixels - 0,0 is upper left

f=open("stops.csv")
o=open("StavangerRegionMap.svg","r")
n=open("StavangerRegionBS.svg","w")
nth_byte = os.path.getsize("StavangerRegionMap.svg")-7
n.write(o.read(nth_byte))

for row in csv.reader(f):
    x,y=c2p(float(row[0]),float(row[1]))
    if x >= 6 and y >=6 and x <= 1193-6 and y <= 1881-6: # are x,y inside the defined map?
        ids = row[2]+","+row[3] # Generate id attribute
        markerstring = """<circle id="%s" cx="%s" cy="%s" fill="rgb(100, 0, 250)" r="6" opacity="0.9"/><circle cx="%s" cy="%s" fill="rgb(255, 255, 255)" r="3" opacity="0.8"/>"""%(ids,x,y,x,y)
        # "id" is free to define and contains both bus stop number and description, comma delimited
        # "type" and "name" are not standard SVG <circle> attributes and can explode some browsers rendering engine
        n.write(markerstring)
    else:
        pass
    
n.write(o.read())
n.close()
time.sleep(4) # Allow the new created file to settle a little while
print ("READY >")


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-035b68f89eba> in <module>()
     22 
     23 for row in csv.reader(f):
---> 24     x,y=c2p(float(row[0]),float(row[1]))
     25     if x >= 6 and y >=6 and x <= 1193-6 and y <= 1881-6: # are x,y inside the defined map?
     26         ids = row[2]+","+row[3] # Generate id attribute

ValueError: could not convert string to float: StopPointRef,StopPointName,Longitude,Latitude

In [2]:
SVG(filename="StavangerRegionBS.svg")


---------------------------------------------------------------------------
ExpatError                                Traceback (most recent call last)
<ipython-input-2-63bd2a96a4d1> in <module>()
----> 1 SVG(filename="StavangerRegionBS.svg")

/usr/local/lib/python3.5/site-packages/IPython/core/display.py in __init__(self, data, url, filename)
    384         self.filename = None if filename is None else unicode_type(filename)
    385 
--> 386         self.reload()
    387         self._check_data()
    388 

/usr/local/lib/python3.5/site-packages/IPython/core/display.py in reload(self)
    403         if self.filename is not None:
    404             with open(self.filename, self._read_flags) as f:
--> 405                 self.data = f.read()
    406         elif self.url is not None:
    407             try:

/usr/local/lib/python3.5/site-packages/IPython/core/display.py in data(self, svg)
    488         from xml.dom import minidom
    489         svg = cast_bytes_py2(svg)
--> 490         x = minidom.parseString(svg)
    491         # get svg tag (should be 1)
    492         found_svg = x.getElementsByTagName('svg')

/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/xml/dom/minidom.py in parseString(string, parser)
   1966     if parser is None:
   1967         from xml.dom import expatbuilder
-> 1968         return expatbuilder.parseString(string)
   1969     else:
   1970         from xml.dom import pulldom

/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/xml/dom/expatbuilder.py in parseString(string, namespaces)
    923     else:
    924         builder = ExpatBuilder()
--> 925     return builder.parseString(string)
    926 
    927 

/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/xml/dom/expatbuilder.py in parseString(self, string)
    221         parser = self.getParser()
    222         try:
--> 223             parser.Parse(string, True)
    224             self._setup_subset(string)
    225         except ParseEscape:

ExpatError: no element found: line 19930, column 0

In [ ]:


In [ ]:


In [ ]: