def export(mapfile, node_ids=False):
	fits = {}
	while True:
		try:
			fit = cPickle.load(sys.stdin)
			fits[fit[0]] = fit
		except EOFError:
			break
	
	raw_nodes, edges, tags = osm2graph.get_graph(mapfile)
	node_coords = {}
	for key, coords in raw_nodes.iteritems():
		cart = coord_proj(*coords)
		node_coords[key] = cart

	hdr = "shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence"
	if node_ids:
		hdr += ",node_id"
	sys.stdout.write(hdr)
	sys.stdout.write("\n")

	for (shape_id, coords, fit, states) in fits.itervalues():
		fit = get_fit_map_coords(states, node_coords)
		lonlat = zip(*coord_proj(*zip(*fit), inverse=True))
		nodes = get_fit_map_path(states)
		# Skip the first and last nodes as they don't
		# hit "exactly" on the node
		nodes[0] = ""
		nodes[-1] = ""
		
		for i, (lon, lat) in enumerate(lonlat):
			sys.stdout.write(",".join(map(str, (shape_id, lat, lon, i+1))))
			if node_ids:
				sys.stdout.write(","+str(nodes[i]))
			sys.stdout.write("\n")
def export(mapfile, node_ids=False):
    fits = {}
    while True:
        try:
            fit = cPickle.load(sys.stdin)
            fits[fit[0]] = fit
        except EOFError:
            break

    raw_nodes, edges, tags = osm2graph.get_graph(mapfile)
    node_coords = {}
    for key, coords in raw_nodes.iteritems():
        cart = coord_proj(*coords)
        node_coords[key] = cart

    hdr = "shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence"
    if node_ids:
        hdr += ",node_id"
    sys.stdout.write(hdr)
    sys.stdout.write("\n")

    for (shape_id, coords, fit, states) in fits.itervalues():
        fit = get_fit_map_coords(states, node_coords)
        lonlat = zip(*coord_proj(*zip(*fit), inverse=True))
        nodes = get_fit_map_path(states)
        # Skip the first and last nodes as they don't
        # hit "exactly" on the node
        nodes[0] = ""
        nodes[-1] = ""

        for i, (lon, lat) in enumerate(lonlat):
            sys.stdout.write(",".join(map(str, (shape_id, lat, lon, i + 1))))
            if node_ids:
                sys.stdout.write("," + str(nodes[i]))
            sys.stdout.write("\n")
def view(mapfile, whitelist=""):
    import matplotlib.pyplot as plt

    if whitelist != "":
        whitelist = whitelist.split(',')
        do_include = lambda x: x in whitelist
    else:
        do_include = lambda x: True

    fits = {}
    while True:
        try:
            fit = cPickle.load(sys.stdin)
            if not do_include(fit[0]): continue
            fits[fit[0]] = fit
        except EOFError:
            break

    raw_nodes, edges, tags = osm2graph.get_graph(mapfile)
    nodes = {}
    for key, coords in raw_nodes.iteritems():
        cart = coord_proj(*coords)
        nodes[key] = cart

    stats = []
    for fitstuff in fits.itervalues():
        (shape_id, coords, fit, states) = fitstuff
        cart = np.array(coord_proj(*zip(*coords)[::-1])).T

        assert len(states) == len(coords)

        points = []
        diffs = []
        for i in range(1, len(states)):
            mapcoords = [states[i - 1].point]
            mapcoords.extend([nodes[n] for n in states[i].path])
            mapcoords.append(states[i].point)
            points.extend(mapcoords)
            diffs.extend([
                slowmapmatch.lineseg_point_projection(p, cart[i - 1],
                                                      cart[i])[1]
                for p in mapcoords
            ])
        stats.append((np.max(diffs), diffs, points, fitstuff))

    stats.sort(key=lambda s: -s[0])
    for (maxdiff, diffs, points, fitstuff) in stats:
        (shape_id, coords, fit, states) = fitstuff
        cart = np.array(coord_proj(*zip(*coords)[::-1])).T
        print "%s,%f" % (shape_id, maxdiff)
        plt.title("%s max diff %fm" % (shape_id, maxdiff))
        osm2graph.plot_graph(nodes, edges, color='black', alpha=0.5)
        plt.plot(*cart.T, linewidth=2, alpha=0.5)
        plt.plot(*np.array(fit).T, linewidth=2, alpha=0.5)
        x, y = zip(*points)
        plt.scatter(x, y, c=diffs, lw=0)
        plt.show()
def get_matcher(mapfile, **kwargs):
    raw_nodes, edges, tags = osm2graph.get_graph(mapfile)
    nodes = {}
    for key, coords in raw_nodes.iteritems():
        cart = coord_proj(*coords)
        nodes[key] = cart

    matcher = MapMatcher(edges, nodes, **kwargs)
    return matcher
def get_matcher(mapfile, **kwargs):
	raw_nodes, edges, tags = osm2graph.get_graph(mapfile)
	nodes = {}
	for key, coords in raw_nodes.iteritems():
		cart = coord_proj(*coords)
		nodes[key] = cart
	
	
	matcher = MapMatcher(edges, nodes, **kwargs)
	return matcher
def view(mapfile, whitelist=""):
	import matplotlib.pyplot as plt
	
	if whitelist != "":
		whitelist = whitelist.split(',')
		do_include = lambda x: x in whitelist
	else:
		do_include = lambda x: True
	
	fits = {}
	while True:
		try:
			fit = cPickle.load(sys.stdin)
			if not do_include(fit[0]): continue
			fits[fit[0]] = fit
		except EOFError:
			break
	
	raw_nodes, edges, tags = osm2graph.get_graph(mapfile)
	nodes = {}
	for key, coords in raw_nodes.iteritems():
		cart = coord_proj(*coords)
		nodes[key] = cart

	
	stats = []
	for fitstuff in fits.itervalues():
		(shape_id, coords, fit, states) = fitstuff
		cart = np.array(coord_proj(*zip(*coords)[::-1])).T
		
		assert len(states) == len(coords)
		
		points = []
		diffs = []
		for i in range(1, len(states)):
			mapcoords = [states[i-1].point]
			mapcoords.extend([nodes[n] for n in states[i].path])
			mapcoords.append(states[i].point)
			points.extend(mapcoords)
			diffs.extend([slowmapmatch.lineseg_point_projection(p, cart[i-1], cart[i])[1] for p in mapcoords])
		stats.append((np.max(diffs), diffs, points, fitstuff))
	
	stats.sort(key=lambda s: -s[0])
	for (maxdiff, diffs, points, fitstuff) in stats:
		(shape_id, coords, fit, states) = fitstuff
		cart = np.array(coord_proj(*zip(*coords)[::-1])).T
		print "%s,%f"%(shape_id,maxdiff)
		plt.title("%s max diff %fm"%(shape_id, maxdiff))
		osm2graph.plot_graph(nodes, edges, color='black', alpha=0.5)
		plt.plot(*cart.T, linewidth=2, alpha=0.5)
		plt.plot(*np.array(fit).T, linewidth=2, alpha=0.5)
		x, y = zip(*points)
		plt.scatter(x, y, c=diffs, lw=0)
		plt.show()