I have a polygon in WKT in EPSG:4326:

polygonWKT = 'POLYGON((13.215179443359373 52.588032137196755,13.42803955078125 52.592620721000365,13.454132080078125 52.46897854656702,13.347015380859375 52.45893824522763,13.215179443359373 52.588032137196755))';

How can I reproject this to EPSG:3857 with Leaflet or any other Javascript Library?

  • You could use terraformer.io for this – Devdatta Tengshe Jun 24 '14 at 12:34
  • Could you please a little example on how exactly I would use it. I can't find a re-project function in terraformer. – ustroetz Jun 24 '14 at 12:41
  • @ustroz: What do you want to do with this data? Show it on the map? or project this polygon to WebMercator and then get the WKT of that? – Devdatta Tengshe Jun 24 '14 at 13:08
  • I want to use this polygon in a CQL Filter in WFS request. – ustroetz Jun 24 '14 at 13:26

Terraformer can parse WKT, and it can project from Geographic Latlong to WebMercator, but it can't write a WKT.

However we can build the WKT string from projected polygon.

First refer to the terraformer-core and teraformer wkt parser libraries in your code, like this:

<script src="terraformer-1.0.3.min.js"></script>
<script src="terraformer-wkt-parser.min.js"></script>

Then you can use the following code to project.

    function project(geoPolygonWKT) {
    var geoPoly = Terraformer.WKT.parse(geoPolygonWKT); //Create Polygon from WKT
    if (geoPoly.type != "Polygon") {
        throw "Not a Polygon";
        return "";
    geoPoly.toMercator(); //project Polygon to Mercator
    var coord = geoPoly.coordinates;

    var projectedWKT = 'POLYGON(';

    var ringCount = coord.length;
    for (var i = 0; i < ringCount; i++) {
        var ring = coord[i];
        //ring starts; add opening bracket
        projectedWKT = projectedWKT + "(";
        var ptCount = ring.length;
        var coordList = "";
        for (var j = 0; j < ptCount; j++) {
            var pt = ring[j];
            //write the coordinates
            coordList = coordList + String(pt[0]) + " " + String(pt[1]) + ", ";
        //remove the last comma (indicating end of coordinate)
        coordList = coordList.substring(0, coordList.lastIndexOf(','));
        //add to the WKT String
        projectedWKT = projectedWKT + coordList + "), ";
    //remove the last comma (indicating end of ring)
    projectedWKT = projectedWKT.substring(0, projectedWKT.lastIndexOf(','));

    //closing bracket
    projectedWKT = projectedWKT + ")";
    console.log(projectedWKT); //<-- this will be your projected WKT
    return projectedWKT;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.