LTA Datamall provides MRT station data in .shp format. To extract this data in a python environment, I used pyshp. The .shp file contains the geo information, and the .dbf file contains more info about these points.
# import pyshp
import shapefile
# read the file
myshp = open('data/TrainStations.shp', "rb")
mydbf = open('data/TrainStations.dbf', "rb")
sf = shapefile.Reader(shp=myshp, dbf=mydbf)
records = sf.shapeRecords()
# check what we have
print 'There are', len(records), 'shape objects in this file'
There are several possible types of shape. To find out what we have, we need to inspect the shape type. From pyshp's repo, we see the value 1 means these are of the type "POINT".
Using sf.fields()
, we can see what fields are available in the .dbf file
print 'Type', sf.shapes()[0].shapeType
print sf.fields
Iterate over a few records to inspect the data
for record in records[:10]:
print record.record[0], record.shape.points[0]
The coordinates look weird because Singapore has a special coordinate system called SVY21. Thankfully cgcai wrote an open source tool to convert these coordinates to lat/lng!
from utils.svy21 import SVY21
svy = SVY21()
for record in records[:10]:
print svy.computeLatLon(record.shape.points[0][1], record.shape.points[0][0])
Finally time to put this on a map!
import folium
sg_map = folium.Map(location=[1.38, 103.8], zoom_start=12)
for record in records:
lat, lng = svy.computeLatLon(record.shape.points[0][1], record.shape.points[0][0])
folium.RegularPolygonMarker(
[lat, lng],
popup=record.record[0].title(),
fill_color='#0f0f0f',
number_of_sides=4,
radius=5
).add_to(sg_map)
sg_map