     # Create the mongoose model.
     mongoose.model 'Camera', Camera
     CameraModel = mongoose.model 'Camera'
-
-    # Connect to mongodb.
-    mongoose.connect 'mongodb://localhost/cams'
 
     # Remove all previously saved documents.
     aCamera = new CameraModel()

    Camera = new mongoose.Schema
        name: String
        url: String # TODO: Make this a URL object sub-document
        geocode: String
        loc:
            x: Number
            y: Number


parser = new xml2js.Parser()

cameraToRoadMap = []

# Connect to mongodb.
mongoose.connect 'mongodb://localhost/cams'

# Read in the mapping of geocoded highway names.
fs.readFile 'data/roads.json', 'utf-8', (err, data) ->
    roads = JSON.parse data

    # Define the `Road` schema.
    Road = new mongoose.Schema
        name: String
        roadId: String
        cameraCount: Number

    Road.index 'road'
    mongoose.model 'Road', Road
    RoadModel = mongoose.model 'Road'
    aRoad = new RoadModel()
    aRoad.collection.drop()

    _(roads).each (road, i) ->
        if i + 1 is roads.length then isLast = true else isLast = false

        aRoad = new RoadModel
            name: road.road.name
            roadId: road.road.id
            cameraCount: road.cameras.length

        aRoad.save (err) ->
            if err then console.log "Could not save road: #{aRoad}: #{err}"
            if isLast
                # Massage the data structure into a simple array for easy lookup of
                # road info. when the camera documents are created.
                for road in roads
                    for camera in road.cameras
                        cameraToRoadMap[camera] = road.road.name

                # Read and parse camera KML.
                fs.readFile 'data/raw/trafficcameraswithfeed.kml', (err, data) ->
                    if err then console.log err else parser.parseString data

# Parse camera KML into MongoDB.
parser.addListener 'end', (result) ->
