## Archive for the ‘WKT’ Category

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

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();

## PostGIS: query all multipolygon parcels with at least one hole

I was writing some code to iterate over Well Known Text expressions for polygon features, and I decided I needed to test the most complex edge-case I could think of–multipolygon geometries where at least one of the bound polygons has a hole (i.e. an interior ring).

I ended up with the following query. This seems like the kind of thing I’ll want to reuse later, so I’m noting it here. For good measure, I also use a rudimentary technique to sort the output with the most complicated geometries in the table at the top of the list. Basically, the more “text” it takes to describe the geometry using Well Known Text, the larger and more complex I figure it must be!

SELECT SomePrimaryId, /* your primary key, i.e. ogc_fid, etc. */ SomeUniqueId, /* your descriptive id, i.e. a parcel number */ ST_NumGeometries(wkb_geometry) AS num_geoms, ST_NRings(wkb_geometry) AS num_rings, ST_AsText(ST_Centroid(wkb_geometry)) AS center, Char_Length(ST_AsText(wkb_geometry)) AS len, ST_AsText(wkb_geometry) AS wkt FROM SomePolygonTable WHERE ST_NumGeometries(wkb_geometry) > 1 AND ST_NRings(wkb_geometry) > ST_NumGeometries(wkb_geometry) ORDER BY Char_Length(ST_AsText(wkb_geometry)) ASC ;

Just for the sake of promoting caution, I’m not certain this is a definitive approach for identifying the largest geometry in a table, as the length of the binary representation and the length of the readable text representation do not correspond one-to-one. Moreover, a feature could have more vertices that required less precision to express (fewer decimal position), than a geometry with fewer vertices that needed more precision, and then you have to ask, which is bigger, fewer vertices and more text, or more vertices that coincidentally did not require as much text? My conclusion is, the “most complicated geometry” is probably relative to the one asking the question. However for my purposes, this was close enough to put the most complicated stuff at the top of the list.