google distance matrix with python

2.5k Views Asked by At

My problem is i want to get the distance for around 4000 lat longs . I have used Google Direction matrix service for this. To certain extend it worked fine for me. After that i am getting the error of Invalid request .Its because of GET method. I want a solution how can i use Distance matrix service with POST using Python. Appreciate your help on this. Thanks in Advance.

I am writing a google distance matrix with python using appengine, i got struck with urllib post method

code: url = 'http://maps.googleapis.com/maps/api/distancematrix/json'

    conn = getConnection()
    cursor = conn.cursor()
    origins=[]
    try:
        cursor.execute('select username,lat,lng,cabNo,orderno from tripsheet order by username;')
        origins= cursor.fetchall()
    except:
        self.response.out.write("Some thing bad happened")    
    conn.close()
    responseArray= []
    for o in origins :
        origin= {}
        key= "blah"
        origin = {"name":o[0],"key":key, "latitude":o[1],"longitude":o[2],"cabNo":o[3],"order":o[4]}
        responseArray.append(origin)
        url=url+o[1]+','+o[2]+'|'
    values = {
      'sensor' : 'false',
      'mode' : 'driving',
      'avoid' : 'tolls',
      'destinations': '%s,%s' % (destination["lat"] ,destination["lon"])
    }
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    self.response.out.write(the_page)

i am trying passing around 4000 origin and single destination.

getting below error message because it is taking as GET . I wanted to convert into POST using urllib2:

Traceback (most recent call last):
  File "/home/xxx/Projects/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 714, in __call__
    handler.get(*groups)
  File "/home/xxx/4.2WorkSpace/RouteOptimization/src/main.py", line 41, in get
    self.calculate_indv_distance(destination)
  File "/home/xxx/4.2WorkSpace/RouteOptimization/src/main.py", line 109, in calculate_indv_distance
    response = urllib2.urlopen(req)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1182, in do_open
    r = h.getresponse()
  File "/home/xxx/Projects/google_appengine/google/appengine/dist/httplib.py", line 222, in getresponse
    deadline=self.timeout)
  File "/home/xxx/Projects/google_appengine/google/appengine/api/urlfetch.py", line 266, in fetch
    return rpc.get_result()
  File "/home/xxx/Projects/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 604, in get_result
    return self.__get_result_hook(self)
  File "/home/xxx/Projects/google_appengine/google/appengine/api/urlfetch.py", line 370, in _get_fetch_result
    'Invalid request URL: ' + url + error_detail)
InvalidURLError: Invalid request URL: http://maps.googleapis.com/maps/api/distancematrix/

Any help really appreciate.

0

There are 0 best solutions below