1

I have some features I've created via some Kml data.

Now, to display these features on a Google Map, I need to project them from 'EPSG:4326' to 'EPSG:3857'. This works great.

Now, i need to convert this to WKT 4326. When I do that, i re-transform the features but the google map doesn't display them any more because the features are in the wrong projection.

Can anyone suggest a way to clean up my code so I can keep the features in 4326 format, but when I need to display these features on my Layer, it's smart enough to transform it into 3857.

Here's my full javascript code ... but here's the important bits in there...

// Converts some Kml string data into an array of Features.
function convertKmlToFeatures(kml) {
    var kmlFormat = new OpenLayers.Format.KML();
    var features = kmlFormat.read(kml);
    for (var i = 0, l = features.length; i < l; i++) {
        features[i].geometry.transform('EPSG:4326', 'EPSG:3857');
    }
    return features;
}

.....
// Display features. 
// NOTE: (This will be as 3857 .. but i wish i can leave this as 4326 and
//        just transform on the fly, here).
map.layers[1].addFeatures(features);


.....
// Finally Output the features as WKT. This needs to be as 4326.
// Right now, it's still as 3857.
var wktFormat = new OpenLayers.Format.WKT();
return wktFormat.write(features);
4

I think you should extend your Layer with custom protocol option.

var options_proj = {
    'internalProjection': new OpenLayers.Projection("EPSG:3857"),
    'externalProjection': new OpenLayers.Projection("EPSG:4326")
   };

new OpenLayers.Format.WKT({
    protocol: new OpenLayers.Protocol.HTTP(
        { url: "kml path", 
          format: new OpenLayers.Format.WKT(options_proj)})
    //... other options
});  

Therefore,when layer is loaded,it converts kml objects directly to features between projections you specified without writing extra conversion code.

|improve this answer|||||
  • Worked! Instead of having to grab the data via HTTP, I just did this: var wktFormat = new OpenLayers.Format.WKT(options); and it did it :) :) :) – Pure.Krome May 27 '13 at 7:25

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.