import sys, urllib2, suds if __name__ == '__main__': url = 'http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl' client = suds.client.Client(url) client.set_options(port='BasicHttpBinding_IGeocodeService') request = client.factory.create('GeocodeRequest') credentials = client.factory.create('ns0:Credentials') credentials.ApplicationId = 'YOUR-APPLICATION-KEY' request.Credentials = credentials #Address address = client.factory.create('ns0:Address') address.AddressLine = "535 Shirley St. NE" address.AdminDistrict = "Michigan" address.Locality = "Grand Rapids" address.CountryRegion = "United States" request.Address = address try: response = client.service.Geocode(request) except suds.client.WebFault, e: print "ERROR!" print(e) sys.exit(1) locations = response['Results']['GeocodeResult'][0]['Locations']['GeocodeLocation'] for location in locations: print(location)
If you need to make the request via an HTTP proxy server expand the line client = suds.client.Client(url) to:
proxy = urllib2.ProxyHandler({'http': 'http://YOUR-PROXY-SERVER:3128'})\ transport = suds.transport.http.HttpTransport() transport.urlopener = urllib2.build_opener(proxy) client = suds.client.Client(url, transport=transport)
The results will be Bing API GeocodeLocation objects that have an Longitude and Latitude properties. Note that you may receive multiple coordinates for an address as there are multiple mechanism for locating an address; the method corresponding to the coordinates is a string in the CalculationMethod property of the GeocodeLocation objects.
There's also geopy, which let's you pick from half a dozen different geocoding services (including Google and Yahoo), and is even easier to use.
ReplyDeletehttp://code.google.com/p/geopy/wiki/GettingStarted