latitude/longitude/mapping Python script

Dinu Gherman dinu at reportlab.com
Fri Mar 30 02:54:30 EST 2001


On Thu, 29 Mar 2001 23:41:28 -0500, Will Ware <wware at world.std.com>
wrote:

>This is a multi-part message in MIME format.
>--------------1FF6E68DDA96A9621DE8BB6C
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>
>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 = (("Munich", "Germany"),)  # changed from original posting
>
>showcities(citylist)

% python map.py
Traceback (most recent call last):
  File "map.py", line 85, in ?
    showcities(citylist)
  File "map.py", line 77, in showcities
    lat, lon = findcity(city, state)
  File "map.py", line 72, in findcity
    return getdegrees(lat, "ns"), getdegrees(lon, "ew")
  File "map.py", line 65, in getdegrees
    x = map(eval, string.split(x, dividers[0]))
  File "<string>", line 1
    08
    ^
SyntaxError: unexpected EOF while parsing

Dinu

-- 
Dinu C. Gherman
dinu at reportlab dot com
http://www.reportlab.com
................................................................
"The only possible values [for quality] are 'excellent' and 'in-
sanely excellent', depending on whether lives are at stake or 
not. Otherwise you don't enjoy your work, you don't work well, 
and the project goes down the drain." 
                    (Kent Beck, "Extreme Programming Explained")



More information about the Python-list mailing list