## Archive for the ‘WKT’ tag

## 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.

## Decode Google Map encoded points as Well Known Text (WKT) with Python

I had close encounter of the 5th kind yesterday.. here’s the gist..

It started when someone “gave” me a GIS dataset (..of polygons, kind of..) that a colleague of theirs, way back in ancient history, chose to pre-cook as ASCII-encoded point pairs. Their intention was almost certainly to use the pre-cooked data in Google Maps. Anyway, being arguably sane, I wanted to return this data to a more GIS-normal format so I could put it in a database like MySQL or Post and use it for other stuff.

I considered a few different approaches to this problem, including creating a Google Map that could load-in all of the polygons from their encodings, then iterate over the polygons, interrogate the polygon point pairs, and finally concatenate WKT features from the points and save the geofeatures into a MySQL table. This approach offered the advantage of using Google’s existing Maps API to do the decoding for me. But let’s face it, that’s lame, uninspired, and not inventive.. it wasn’t even interesting.

Besides.. I wanted to use Python.

I expected to find a Python recipe for this looming in the misty www, but I didn’t. However, I did find a JavaScript recipe by trolling around in Mark McClure’s website. Specifically, he provides a Polyline Decoder utility, and when I viewed the page source, I found the JavaScript code that actually does the decoding (opening in FireFox will show you the code, or IE should prompt you to download the file).

Long story short, the following Python method is an adaptation of Mark McClure’s JavaScript method (twisted a little to return WKT features rather than the pure point array). If you’re somewhat comfortable with Python, you should be able to copy/paste the method right into your Python file and start calling it; just pass-in the encoded point string and let the method do the rest.

Best / Elijah

———

def decodeGMapPolylineEncoding(asciiEncodedString): print "\nExtrapolating WKT For:" print asciiEncodedString strLen = len(asciiEncodedString) index = 0 lat = 0 lng = 0 coordPairString = "" # Make it easy to close PolyWKT with the first pair. countOfLatLonPairs = 0 firstLatLonPair = "" gotFirstPair = False while index < strLen: shift = 0 result = 0 stayInLoop = True while stayInLoop: # GET THE LATITUDE b = ord(asciiEncodedString[index]) - 63 result |= (b & 0x1f) << shift shift += 5 index += 1 if not b >= 0x20: stayInLoop = False # Python ternary instruction.. dlat = ~(result >> 1) if (result & 1) else (result >> 1) lat += dlat shift = 0 result = 0 stayInLoop = True while stayInLoop: # GET THE LONGITUDE b = ord(asciiEncodedString[index]) - 63 result |= (b & 0x1f) << shift shift += 5 index += 1 if not b >= 0x20: stayInLoop = False # Python ternary instruction.. dlng = ~(result >> 1) if (result & 1) else (result >> 1) lng += dlng lonNum = lng * 1e-5 latNum = lat * 1e-5 coordPairString += str(lonNum) + " " + str(latNum) if gotFirstPair == False: gotFirstPair = True firstLatLonPair = str(lonNum) + " " + str(latNum) countOfLatLonPairs += 1 if countOfLatLonPairs > 1: coordPairString += "," # The data I was converting was rather dirty.. # At first I expected 100% polygons, but sometimes the encodings returned only one point. # Clearly one point cannot represent a polygon. Nor can two points represent a polygon. # This was an issue because I wanted to return proper WKT for every encoding, so I chose # To handle the matter by screening for 1, 2, and >=3 points, and returning WKT for # Points, Lines, and Polygons, respectively, and returning proper WKT. # # It's arguable that any encodings resulting in only one or two points should be rejected. wkt = "" if countOfLatLonPairs == 1: wkt = "POINT(" + coordPairString + ")" elif countOfLatLonPairs == 2: wkt = "POLYLINE(" + coordPairString + ")" elif countOfLatLonPairs >= 3: wkt = "POLYGON((" + coordPairString + "," + firstLatLonPair + "))" return wkt