Stay foolish, stay hungry.

#RUTN# 2019.1.1 Work Log

Posted on By 吴杰

  • Download the whole map (osm format) of New York City from geofabrik.
  • Extract useful nodes into a file(csv or xml &c.).
  • Transform the nodes to a json file (in the dictionary form).
  • Put it into my github.io.

Download original osm file

As we cannot process pbf file directly, we chose new-york-latest.osm.bz2.

Process original xml file

It is recommended to use Python to do this office. tutorialspoint has provided a detailed tutorial on how to process xml documents with Python.

import xml.sax
import csv
import json

nodeset = set()
nodemap = {}
count = 1

# The nodes in example_traveltime.csv are the basic nodes that we're going to use in our project. 
# So we firstly need to extract them distinctly.
with open('example_traveltime.csv','r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        nodeset.add(row[0])
        nodeset.add(row[1])

class NodesHandler(xml.sax.ContentHandler):
    def startElement(self,name,attrs):
        global count
        global nodemap
        global nodeset
        if name == "node":
            id = attrs["id"]
            if id in nodeset:
                lat = attrs["lat"]
                lon = attrs["lon"]
                nodeset.remove(id)
                nodemap[id] = [lat,lon]
                print(count)
                count += 1

parser = xml.sax.make_parser()
parser.setContentHandler(NodesHandler())
parser.parse(open("new-york-latest.osm","r",encoding='utf-8'))

# Write the result
jsonstr = json.dumps(nodemap)
with open('nodes.js','w') as file:
    file.write('var nodes = '+jsonstr+';')