{"id":203,"date":"2014-06-06T10:44:09","date_gmt":"2014-06-06T15:44:09","guid":{"rendered":"http:\/\/cartometric.com\/blog\/?p=203"},"modified":"2014-09-25T13:10:24","modified_gmt":"2014-09-25T18:10:24","slug":"convert-google-maps-polygon-api-v3-to-well-known-text-wkt-geometry-expression","status":"publish","type":"post","link":"https:\/\/elrobis.com\/blog\/2014\/06\/06\/convert-google-maps-polygon-api-v3-to-well-known-text-wkt-geometry-expression\/","title":{"rendered":"Convert Google Maps Polygon (API V3) to Well Known Text (WKT) Geometry Expression"},"content":{"rendered":"<p>There&#8217;s dozens of reasons why you might want the Well Known Text (WKT) geometry expression for a Google Maps Polygon object.<\/p>\n<p>Assuming you&#8217;re using the<a href=\"https:\/\/developers.google.com\/maps\/documentation\/javascript\/reference\"> Google Maps API V3<\/a>, and you&#8217;ve got a variable referencing your <a href=\"https:\/\/developers.google.com\/maps\/documentation\/javascript\/reference#Polygon\">Polygon<\/a>, I&#8217;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.<\/p>\n<p><strong>Add a Simple Utility Method to Your Project<\/strong><\/p>\n<p>Easy enough. Just add the following method to your project. Look below the method for an example of how you&#8217;d call it.<\/p>\n<p><code>function GMapPolygonToWKT(poly)<br \/>\n{<br \/>\n&emsp;\/\/ Start the Polygon Well Known Text (WKT) expression<br \/>\n&emsp;var wkt = \"POLYGON(\";<\/p>\n<p>&emsp;var paths = poly.getPaths();<br \/>\n&emsp;for(var i=0; i&lt;paths.getLength(); i++)<br \/>\n&emsp;{<br \/>\n&emsp;&emsp;var path = paths.getAt(i);<br \/>\n&emsp;&emsp;<br \/>\n&emsp;&emsp;\/\/ Open a ring grouping in the Polygon Well Known Text<br \/>\n&emsp;&emsp;wkt += \"(\";<br \/>\n&emsp;&emsp;for(var j=0; j&lt;path.getLength(); j++)<br \/>\n&emsp;&emsp;{<br \/>\n&emsp;&emsp;&emsp;\/\/ add each vertice and anticipate another vertice (trailing comma)<br \/>\n&emsp;&emsp;&emsp;wkt += path.getAt(j).lng().toString() +\" \"+ path.getAt(j).lat().toString() +\",\";<br \/>\n&emsp;&emsp;}<br \/>\n&emsp;&emsp;<br \/>\n&emsp;&emsp;\/\/ Google's approach assumes the closing point is the same as the opening<br \/>\n&emsp;&emsp;\/\/ point for any given ring, so we have to refer back to the initial point<br \/>\n&emsp;&emsp;\/\/ and append it to the end of our polygon wkt, properly closing it.<br \/>\n&emsp;&emsp;\/\/<br \/>\n&emsp;&emsp;\/\/ Also close the ring grouping and anticipate another ring (trailing comma)<br \/>\n&emsp;&emsp;wkt += path.getAt(0).lng().toString() + \" \" + path.getAt(0).lat().toString() + \"),\";<br \/>\n&emsp;}<br \/>\n&emsp;<br \/>\n&emsp;\/\/ resolve the last trailing \",\" and close the Polygon<br \/>\n&emsp;wkt = wkt.substring(0, wkt.length - 1) + \")\";<br \/>\n&emsp;<br \/>\n&emsp;return wkt;<br \/>\n}<\/code><\/p>\n<p>Here&#8217;s how you&#8217;d access the Well Known Text expression using the utility method:<\/p>\n<p><code>\/\/ Assuming you've already instantiated \"myPolygon\" somewhere.<br \/>\nvar wkt = GMapPolygonToWKT(myPolygon);<\/code><\/p>\n<p><\/br><\/p>\n<p><strong>Extend Google&#8217;s Polygon Object Prototype with a ToWKT() Method<\/strong><\/p>\n<p>There&#8217;s nothing wrong with the first approach, but you might find it handy to extend Google&#8217;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&mdash;this will need to be called after the Google Maps library has been loaded):<\/p>\n<pre>if (typeof google.maps.Polygon.prototype.ToWKT !== 'function')\r\n{\r\n&emsp;google.maps.Polygon.prototype.ToWKT = function()\r\n&emsp;{\r\n&emsp;&emsp;var poly = this;\r\n&emsp;&emsp;\r\n&emsp;&emsp;\/\/ Start the Polygon Well Known Text (WKT) expression\r\n&emsp;&emsp;var wkt = \"POLYGON(\";\r\n&emsp;&emsp;\r\n&emsp;&emsp;var paths = poly.getPaths();\r\n&emsp;&emsp;for(var i=0; i&lt;paths.getLength(); i++)\r\n&emsp;&emsp;{\r\n&emsp;&emsp;&emsp;var path = paths.getAt(i);\r\n&emsp;&emsp;&emsp;\r\n&emsp;&emsp;&emsp;\/\/ Open a ring grouping in the Polygon Well Known Text\r\n&emsp;&emsp;&emsp;wkt += \"(\";\r\n&emsp;&emsp;&emsp;for(var j=0; j&lt;path.getLength(); j++)\r\n&emsp;&emsp;&emsp;{\r\n&emsp;&emsp;&emsp;\/\/ add each vertice, automatically anticipating another vertice (trailing comma)\r\n&emsp;&emsp;&emsp;wkt += path.getAt(j).lng().toString() + \" \" + path.getAt(j).lat().toString() + \",\";\r\n&emsp;&emsp;&emsp;}\r\n&emsp;&emsp;&emsp;\r\n&emsp;&emsp;&emsp;\/\/ Google's approach assumes the closing point is the same as the opening\r\n&emsp;&emsp;&emsp;\/\/ point for any given ring, so we have to refer back to the initial point\r\n&emsp;&emsp;&emsp;\/\/ and append it to the end of our polygon wkt, properly closing it.\r\n&emsp;&emsp;&emsp;\/\/\r\n&emsp;&emsp;&emsp;\/\/ Additionally, close the ring grouping and anticipate another ring (trailing comma)\r\n&emsp;&emsp;&emsp;wkt += path.getAt(0).lng().toString() + \" \" + path.getAt(0).lat().toString() + \"),\";\r\n&emsp;&emsp;}\r\n&emsp;&emsp;\r\n&emsp;&emsp;\/\/ resolve the last trailing \",\" and close the Polygon\r\n&emsp;&emsp;wkt = wkt.substring(0, wkt.length - 1) + \")\";\r\n&emsp;&emsp;\r\n&emsp;&emsp;return wkt;\r\n&emsp;};\r\n}<\/pre>\n<p><\/br><\/p>\n<p>If you prefer the second approach, you can get the Well Known Text expression like this:<\/p>\n<p><code>\/\/ Assuming you've already instantiated \"myPolygon\" somewhere.<br \/>\nvar wkt = myPolygon.ToWKT();<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s dozens of reasons why you might want the Well Known Text (WKT) geometry expression for a Google Maps Polygon object. Assuming you&#8217;re using the Google Maps API V3, and you&#8217;ve got a variable referencing your Polygon, I&#8217;ll suggest two approaches you can take to iterate over the paths and vertices in your Google Maps [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,26,32,24,28,44],"tags":[18,5,51,48,21,20],"_links":{"self":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts\/203"}],"collection":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":23,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions\/255"}],"wp:attachment":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}