Mississippi River Valley

A while back I wrote a program to calculate which cities are within 300 miles of the Mississppi River. I used a file called place2k.txt
from http://www.census.gov/geo/www/gazetteer/places2k.html . I only took 85 data points for the river, so the distances might have as much as 15 miles in error. I included all points within 315 miles of the river, just to be safe.

Here are my data points for the Mississppi River.

Here is the output.

from __future__ import with_statement

#infile is the output of usefuldata
def doit(infile, outfile, v=False):
    river = dmsfile("mississippi river.txt")
    n = file(outfile,'w')
    with file(infile) as f:
        header = True
        for line in f:
            line = line.rsplit("t")
            if header:
                header = False
                string = ""
                for x in line:
                    x = x.strip()
                    string += '%st' % x
                string += '%st' % "distance"
                if v:
                    print string
            lat = float(line[9])
            lon = float(line[10])
            distance, point = dist2river(lon, lat, river)
            if distance < 315:
                d = (point[0] < lon) and "w" or "e" #east or west
                string = ""
                for x in line:
                    x = x.strip()
                    string += '%st' % x
                string += '%st' % str(distance)
                if v:
                    print string

def dist2river(lon,lat,river):
    'returns the distance and closest point on the river to the point'
    smallest = distance(river[0][0],river[0][1],lon,lat) #check against initial point
    for point in river:
        d = distance(point[0],point[1],lon,lat)
        if d <= smallest: #see if this point is closer
    return smallest, thepoint

def count(filename):
    'counts the number of lines in a file'
    with file(filename) as f:
        for line in f:
    print i

def dms(d,m,s):
    return d+m/60.+s/3600.

def dmsfile(filen):
    Imports a coordinates from tsv file and returns it as a list
    with file(filen) as f:
        for line in f:
    return a

def distance(Lon1,Lat1,Lon2,Lat2):
    Calculates the distance between two coordinates
    Based on calcDistance in http://nmviewogc.cr.usgs.gov/javascript/aimsMap.js
    import math as Math
    Lon1 = Lon1 * Math.pi / 180;
    Lon2 = Lon2 * Math.pi / 180;
    Lat1 = Lat1 * Math.pi / 180;
    Lat2 = Lat2 * Math.pi / 180;
    LonDist = Lon1-Lon2;
    LatDist = Lat1-Lat2;
    A = Math.pow(Math.sin(LatDist / 2),2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
    C = 2 * Math.asin(Math.sqrt(A));
    D = (3963.1676 - 13 * Math.sin((Lat1 + Lat2) / 2)) * C;
    return D

if __name__ == '__main__':