Map Addresses
Last Update: 2/2/10
This is a script I’d first written about back in July 2009 and then revised in October 2009. It utilizes Google Map’s API to search your address book for all contacts within a specified distance of some other address.
The main improvements to the below are that I put in a dialog box to request the search location. This is because with Snow Leopard it is very easy to turn Python scripts into Services in the Services menu. So I typically run this when I get an email asking for nearby stores that stock our chocolate. As a service it’s very easy to use.
I also limited the searching to a specified group. (In my case “Clients”) I put in a line to search all of Address Book commented out. See the function scan_addresses(). So it’s easy to modify this behavior.
I finally changed it from simply printing on standard out to instead maintain a list and then copy that list to either a new TextMate window or a new TextEdit window. See the function finish_send(). Once again just comment out the lines you don’t want.
Probably I should create an option that instead creates a temporary group. However 95% of the time I’m going to email the generated list to someone. So having it as nicely formatted text is what I prefer.
Note that to run this you have to have a Google Map API key. See my blog posts on using Python and Google Maps for more information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | #!/usr/bin/python ############################### # mapaddress.py ############################### # # Brings up a dialog box prompting for a city and state. It then queries # the address book for all addresses within 75 miles. (This value can be # changed in the global constant "distance") # # The data is then opened up in a new document within TextMate or alternatively # within TextEdit. (The TextEdit code is present but commented out) import urllib from math import * from appscript import * import subprocess, sys import osax from time import sleep from operator import itemgetter key = "" # Put your own Google Map API key here distance = 75 output_text = [] debug_script = False def send_output(t): global output_text output_text.append(t) def finish_send(): global output_text # To send to TextMate proc = subprocess.Popen(['mate'], shell=True, stdin=subprocess.PIPE) proc.communicate('\n'.join(output_text)) # To send to TextEdit # app(u'TextEdit').make(new=k.document, with_properties={k.text: '\n'.join(output_text)}) def sigdigits(x, n): if n < 1: raise ValueError("number of significant digits must be >= 1") # Use %e format to get the n most significant digits, as a string. format = "%." + str(n-1) + "e" r = format % x return float(r) def deg2rad( pos ): return [ float( pos[0] ) * 2 * pi / 360 , float( pos[1] ) * 2 * pi / 360 ] def find_distance(co1, co2): """Given two coordinates (latitute, longitude) in degrees returns the distance beteween them.""" try: co1 = deg2rad( co1 ) co2 = deg2rad( co2 ) earth = 3956.6 # radius of earth in miles d = acos( cos( co1[0] ) * cos( co1[1] ) * cos( co2[0] ) * cos( co2[1] ) + cos( co1[0] ) * sin( co1[1] ) * cos( co2[0] ) * sin( co2[1]) + sin( co1[0] ) * sin( co2[0] ) ) * earth return sigdigits(d,2) except: return None def get_location( query, key=key ): """Get the ( latitute, longitude ) coordinates in radians corresponding to the query. If something goes wrong return None.""" params = { } params[ 'key' ] = key params[ 'output' ] = 'csv' params[ 'q' ] = query params = urllib.urlencode( params ) try: f = urllib.urlopen( "http://maps.google.com/maps/geo?%s" % params ) except IOError: # maybe some local problem at google? let's try again sleep( 5 ) try: f = urllib.urlopen( "http://maps.google.com/maps/geo?%s" % params ) except IOError: # okay we give up return None response = f.read( ).split(',') f.close( ) try: status = response[0] accuracy = response[1] latitude = response[2] longitude = response[3] except: return None if status != '200': return None else: return latitude, longitude def scan_addresses( center, range ): AB = app('/Applications/Address Book.app') # uncomment below to search all people in Address Book # people = AB.people.get() # uncomment below to search for all people in specified group people = AB.groups[u'Clients'].people.get() address_list = [] for person in people: if len(person.addresses()) > 0: try: address = (person.addresses())[0].street() + " " address = address + (person.addresses())[0].city() + " " address = address + (person.addresses())[0].state() + " " address = address + (person.addresses())[0].zip() loc = get_location( address ) if loc == None: continue distance = find_distance( center, loc ) if distance < range: if debug_script: send_output("") send_output(person.name()) send_output(address) send_output( "Distance:" + str(distance)) store = {} store['name'] = person.name() store['address'] = address if len(person.phones()) >0: store["phone"] = person.phones()[0].value() else: store["phone"] = "" store['distance'] = distance address_list.append(store) except: #print "error" continue return sorted(address_list, key=itemgetter('name')) def fillnumbers( table, row, data): table.columns[1].cells[row].value.set( row ) table.columns[2].cells[row].value.set( data["name"] ) table.columns[3].cells[row].value.set( data["address"] ) table.columns[4].cells[row].value.set( data["phone"] ) table.columns[5].cells[row].value.set( str(data["distance"] )) def addresses_to_numbers(center="Provo, UT", range=50): list = scan_addresses( center, range ) # Hack to create new document in numbers app(u'Numbers').activate() app(u'System Events').processes[its.title == u'Numbers'].processes[1].menu_bars[1].menu_bar_items[3].menus[1].menu_items[1].click() table = app(u'Numbers').documents[1].sheets[1].tables[1] # do a header fillnumbers( table, 1, {'name':'Name', 'address':'Address', 'phone':'Phone', 'distance':'Distance'}) row = 2 for l in list: fillnumbers( table, row, l) row = row + 1 def list_addresses(center="Provo, UT", range=50): list = scan_addresses( center, range ) send_output( "Lists") send_output( "------") for l in list: send_output(l['name']) send_output(l['address']) send_output(l['phone']) send_output("Distance: " + str(l['distance'])) send_output('\n') def get_center(): AB = app('/Applications/Address Book.app') a = AB.selection.get()[0] send_output("Searching around " + a.name()) address = a.addresses()[0].street() + " " address = address + a.addresses()[0].city() + " " address = address + a.addresses()[0].state() + " " address = address + a.addresses()[0].zip() send_output( address ) loc = get_location( address ) return loc def make_center(address): send_output("Search around " + address) loc = get_location( address ) return loc def main(): global distance if len( sys.argv ) > 2: q = sys.argv[1] center = get_location( q ) distance = sys.argv[2] else: #center = get_center() sa = osax.OSAX() results = sa.display_dialog("Search Around: ", with_title="Area Search", default_answer="los angeles, ca") if results[k.button_returned] == u"OK": center = make_center(results[k.text_returned]) if center == None: send_output("Address is bad") # addresses_to_numbers( center, distance ) list_addresses(center, distance) finish_send() if __name__ == '__main__': main() |
- No comments yet.
.jpg)
Recent Comments