Cartometric Blog

A scrapbook of GIS tricks, with emphasis on FOSS4G.

Convert Google Maps Polygon (API V3) to Well Known Text (WKT) Geometry Expression

with 7 comments

There’s dozens of reasons why you might want the Well Known Text (WKT) geometry expression for a Google Maps Polygon object.

Assuming you’re using the Google Maps API V3, and you’ve got a variable referencing your Polygon, I’ll suggest two approaches you can take to iterate over the paths and vertices in your Google Maps polygon and return the geometry expression as a Well Known Text string.

Add a Simple Utility Method to Your Project

Easy enough. Just add the following method to your project. Look below the method for an example of how you’d call it.

function GMapPolygonToWKT(poly)
{
 // Start the Polygon Well Known Text (WKT) expression
 var wkt = "POLYGON(";

 var paths = poly.getPaths();
 for(var i=0; i<paths.getLength(); i++)
 {
  var path = paths.getAt(i);
  
  // Open a ring grouping in the Polygon Well Known Text
  wkt += "(";
  for(var j=0; j<path.getLength(); j++)
  {
   // add each vertice and anticipate another vertice (trailing comma)
   wkt += path.getAt(j).lng().toString() +" "+ path.getAt(j).lat().toString() +",";
  }
  
  // Google's approach assumes the closing point is the same as the opening
  // point for any given ring, so we have to refer back to the initial point
  // and append it to the end of our polygon wkt, properly closing it.
  //
  // Also close the ring grouping and anticipate another ring (trailing comma)
  wkt += path.getAt(0).lng().toString() + " " + path.getAt(0).lat().toString() + "),";
 }

 // resolve the last trailing "," and close the Polygon
 wkt = wkt.substring(0, wkt.length - 1) + ")";

 return wkt;
}

Here’s how you’d access the Well Known Text expression using the utility method:

// Assuming you've already instantiated "myPolygon" somewhere.
var wkt = GMapPolygonToWKT(myPolygon);


Extend Google’s Polygon Object Prototype with a ToWKT() Method

There’s nothing wrong with the first approach, but you might find it handy to extend Google’s Polygon object prototype, itself, to include a ToWKT() member function, which makes it even easier to get its Well Known Text. To do that, add the following JavaScript somewhere near the top of your code (caveat—this will need to be called after the Google Maps library has been loaded):

if (typeof google.maps.Polygon.prototype.ToWKT !== 'function')
{
 google.maps.Polygon.prototype.ToWKT = function()
 {
  var poly = this;
  
  // Start the Polygon Well Known Text (WKT) expression
  var wkt = "POLYGON(";
  
  var paths = poly.getPaths();
  for(var i=0; i<paths.getLength(); i++)
  {
   var path = paths.getAt(i);
   
   // Open a ring grouping in the Polygon Well Known Text
   wkt += "(";
   for(var j=0; j<path.getLength(); j++)
   {
   // add each vertice, automatically anticipating another vertice (trailing comma)
   wkt += path.getAt(j).lng().toString() + " " + path.getAt(j).lat().toString() + ",";
   }
   
   // Google's approach assumes the closing point is the same as the opening
   // point for any given ring, so we have to refer back to the initial point
   // and append it to the end of our polygon wkt, properly closing it.
   //
   // Additionally, close the ring grouping and anticipate another ring (trailing comma)
   wkt += path.getAt(0).lng().toString() + " " + path.getAt(0).lat().toString() + "),";
  }
  
  // resolve the last trailing "," and close the Polygon
  wkt = wkt.substring(0, wkt.length - 1) + ")";
  
  return wkt;
 };
}


If you prefer the second approach, you can get the Well Known Text expression like this:

// Assuming you've already instantiated "myPolygon" somewhere.
var wkt = myPolygon.ToWKT();

Written by elrobis

June 6th, 2014 at 10:44 am

7 Responses to 'Convert Google Maps Polygon (API V3) to Well Known Text (WKT) Geometry Expression'

Subscribe to comments with RSS

  1. Thanks a lot!!!

    Sam

    25 Feb 15 at 11:35 am

  2. Definitely worth thanking you for this function. Very concise code. Now I just need to find code to do the reverse operation! :-)

    Gary

    27 May 15 at 12:50 pm

  3. Thank you for your code, that saves my time :)

    I got this wkt from your code:
    “POLYGON((16.319847106933594+48.21506527580137,16.37340545654297+48.213692646648035,16.383705139160156+48.19424311372133,16.279335021972656+48.184858933932304,16.319847106933594+48.21506527580137))”

    Why did I get plus(+) sign between lat and long insteaqd of space !?

    Milad

    18 Aug 15 at 10:04 am

  4. Finally worked
    Thank you so much
    you saved me …. :) :)

    Milad

    18 Aug 15 at 10:35 am

  5. Hi Milad, sorry for the slow reply. I’m not sure why exactly you’re getting pluses (+) for spaces, but I know that spaces are submitted as pluses for url encoded form parameters. If you create a WKT string variable and dump it to the console.log() function, then check it in Firebug or Developer Tools, you should see a properly formatted WKT expression.

    elrobis

    19 Oct 15 at 1:29 pm

  6. thank you so much
    you saved me !!

    sarah

    22 Mar 17 at 3:46 am

  7. I think your website needs some fresh content.
    Writing manually takes a lot of time, but there is tool for this
    time consuming task, search for; Boorfe’s tips unlimited content

    Sallie04

    3 Nov 17 at 8:11 pm

Leave a Reply