latitude/longitude/mapping Python script

Will Ware wware at world.std.com
Thu Mar 29 23:41:28 EST 2001


import string, urllib, re, os

JUST_THE_US = 0

def xerox_parc_url(marklist):
    avg_lat, avg_lon = max_lat, max_lon = marklist[0]
    marks = "%f,%f" % marklist[0]
    for lat, lon in marklist[1:]:
        marks = marks + ";%f,%f" % (lat, lon)
        avg_lat = avg_lat + lat
        avg_lon = avg_lon + lon
        if lat > max_lat: max_lat = lat
        if lon > max_lon: max_lon = lon
    avg_lat = avg_lat / len(marklist)
    avg_lon = avg_lon / len(marklist)
    if len(marklist) == 1:
        max_lat, max_lon = avg_lat + 1, avg_lon + 1
    diff = max(max_lat - avg_lat, max_lon - avg_lon)
    D = {'height': 4 * diff,
         'width': 4 * diff,
         'lat': avg_lat, 'lon': avg_lon,
         'marks': marks}
    if JUST_THE_US:
        url = ("http://pubweb.parc.xerox.com/map/db=usa/ht=%(height)f" +
               "/wd=%(width)f/color=1/mark=%(marks)s/lat=%(lat)f/" +
               "lon=%(lon)f/") % D
    else:
        url = ("http://pubweb.parc.xerox.com/map/color=1/ht=%(height)f" +
               "/wd=%(width)f/color=1/mark=%(marks)s/lat=%(lat)f/" +
               "lon=%(lon)f/") % D
    return url

def findcity(city, state):
    Please_click = re.compile("Please click")
    url = ("""http://www.astro.ch/cgi-bin/atlw3/aq.cgi?expr=%s&lang=e"""
           % (string.replace(city, " ", "+") + "%2C+" + state))
    lst = [ ]
    inf = urllib.URLopener().open(url)
    for x in inf.readlines():
        quitsoon = 0
        if Please_click.search(x) != None:
            lst = [ ]
            quitsoon = 1
        L = [ ]
        for y in string.split(x[:-1], '<'):
            L = L + string.split(y, '>')
        L = filter(lambda x: len(x) > 0, L)
        lst.append(L)
        if quitsoon and len(lst) == 20:
            break
    inf.close()
    a_href = re.compile("a href")
    okay = 0
    for x in lst:
        for y in x:
            if a_href.search(y) != None:
                okay = 1
                break
        if okay:
            break
    assert okay
    lat, lon = x[6], x[10]
    def getdegrees(x, dividers):
        if string.count(x, dividers[0]):
            x = map(eval, string.split(x, dividers[0]))
            return x[0] + (x[1] / 60.)
        elif string.count(x, dividers[1]):
            x = map(eval, string.split(x, dividers[1]))
            return -(x[0] + (x[1] / 60.))
        else:
            raise "Bogus result", x
    return getdegrees(lat, "ns"), getdegrees(lon, "ew")

def showcities(citylist):
    marklist = [ ]
    for city, state in citylist:
        lat, lon = findcity(city, state)
        print ("%s, %s:" % (city, state)), lat, lon
        marklist.append((lat, lon))
    url = xerox_parc_url(marklist)
    os.system('netscape "%s"' % url)

citylist = (("Natick", "MA"),
            ("Rhinebeck", "NY"),
            ("New Haven", "CT"),
            ("King of Prussia", "PA"))

citylist = (("Cancun", "Mexico"),
            ("Merida", "Mexico"),
            ("Chichen Itza", "Mexico"),
            ("Tulum", "Mexico"))

showcities(citylist)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: wware.vcf
Type: text/x-vcard
Size: 386 bytes
Desc: Card for Will Ware
URL: <http://mail.python.org/pipermail/python-list/attachments/20010329/97eb397e/attachment.vcf>


More information about the Python-list mailing list