Position the bus stops with markers on the Stavanger region map

v2


In [ ]:
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 >")

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


Out[3]:

In [ ]:


In [ ]:


In [ ]: