Last active
June 18, 2018 07:57
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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