I have taken a direction(route) from google map. enter image description here Can I save this resultant direction/path/route to my postgresql/postgis database? How?

  • 6
    Whether possible or not, it will most certainly violate googles terms of service. See google.com/intl/en_en/help/terms_maps.html , number 2.
    – til_b
    Commented Apr 14, 2014 at 6:45
  • Then Can buffer that route and save the buffer in pgsql/postgis db?
    – mapsir
    Commented Apr 14, 2014 at 7:01
  • 1
    That would still be a derivative work. Please read the limitations in Section 2 of the terms that til_b linked to. Perhaps you'd like to consider OpenStreetMap instead.
    – BradHards
    Commented Apr 14, 2014 at 7:09
  • 1
    If you have a defined project area and are dependent on address data, maybe you could try to add the address data yourself? The street data is all there, see openstreetmap.org/#map=15/-33.7550/150.9809 . But beware that you may not copy the address data from google maps. You'll have to get it on the ground, by looking at house number signs.
    – til_b
    Commented Apr 14, 2014 at 9:51
  • 1
    The image is not from the actual area. The actual area does not have enough street data for routing.
    – mapsir
    Commented Apr 14, 2014 at 11:29

2 Answers 2


If you don't consider the legal aspect of this here is the solution for your need:

Use the Google Directions API to get the route between your locations. A sample answer for such a directions api request can be seen here:

Google Directions API sample response

The interesting part for you here is the 'overview_polyline' object and its attribute called 'points'. This attribute contains the polyline itself as an encoded string, that you have to decode first in order to proceed. (there are tools and code freely available on the web for doing this) Once you have your polyline decoded, you will probably have an array of coordinate pairs that make up the polyline itself. What you have to do here, is to iterate through this array and form a WKT string from these coordinates. An example for such a string is:

LINESTRING(0 0, 1 1, 2 1, 2 2)

Where the first member of a pair is the longitude and the second is the latitude value fetched from each point of the polyline. Provided you are done with generating your WKT string, you can use ST_GeomFromText somehow like the below:

ST_GeomFromText('your-wkt-here', 4326);

Where 4326 is your EPSG number.

And there you go. If you did everything correctly you finally have the polyline in your database.

  • is it legally forbidden to store routes from google directions API?
    – simo
    Commented Jan 21, 2016 at 9:05

I was just looking for information about it and I found this, I think you can save the polyline like string and convert it in pairs of numbers (coordinates) or convert it and then store the coordinates(point fields)




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.