Skip to content

Instantly share code, notes, and snippets.

@pedro-stanaka
Last active June 18, 2018 07:57
Show Gist options
  • Save pedro-stanaka/6294625 to your computer and use it in GitHub Desktop.
Save pedro-stanaka/6294625 to your computer and use it in GitHub Desktop.
A fast way to transform a PostGIS polygon in WKT format to Javascript (Google Maps API)
/**
* Get an polygon postgis object casted to a string and transform to an instance of
* google.maps.polygon
* @author Pedro Tanaka <pedro.stanaka@gmail.com>
* @param {String} polygonStr String from the PostGis function TS_geometryToText(...)
* @returns {Array} An array with the LatLng's to be put in the polygon
*/
function postgisPolygonToJsonArray(polygonStr){
polygonStr = polygonStr.replace('POLYGON((', '');
polygonStr = polygonStr.replace('))', '');
var strPoints = polygonStr.split(',');
var gPoints = new Array();
for (var index = strPoints.length-1; index >= 0; index--) {
gPoints.push(new google.maps.LatLng( parseFloat(strPoints[index].split(' ')[0]), parseFloat(strPoints[index].split(' ')[1]) ) );
}
return gPoints;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment