0

In my Openlayers2 based project I give the possibility for users to upload their own features in KML file or with a WKT string.

I only want to accept features with ESPG:4326 coordinates. Otherwise a error message should be displayed. "Please use only 4326 coordinates bla bla bla"

The issue is that I can't find any way to identify the input coordinates system to make the necessary condition.

Is there a way to identify the EPSG only with the coordinates?

1
  • if x>abs(180) || y>abs(90) is the best I can think of. Dec 9 '14 at 11:58
1

KML coordinates are in EPSG:4326 by definition. WKT lacks any concept of CRS whatsoever, encoding just the geometry type and its coordinates.

So there is nothing within the format's themselves that would allow you to force coordinates into EPSG:4326. However, as virtually all other terrestrial coordinate systems will use meters (or incredible, as it may seem in the 21st century, feet, if in the world's most advanced nation, technologically speaking, the US), it should be pretty easy to write a test that rejects anything greater than 180 in the x and 90 in the y, eg,

function is4325(geom){
  for (var i=0; i < geom.coords.length; i++){
    if (Math.abs(geom.coords[i].x)>180 || Math.abs(geom.coords[i].y)>90){
       return false;
    }
    return true;
  }
}

Obviously, it is possible to have projected coordinates in meters or feet in this range, but they will be in a tiny area in the sea off the coast of West Africa.

2
  • I saw your first answer 5 hours ago and ended up writing a function very similar to the one you posted now. For WKT already tested it and it works great! I was convinced that in programs like QGIS it was possible to export features in KML and force others coordinate systems in to it. But now I see I was mistaken. Many thanks, simple and effective approach. Cheers Dec 9 '14 at 17:47
  • You are welcome. According to this answer, the Postgis function, ST_AsKML even does an automatic conversion to 4326, so it really is set in stone Dec 9 '14 at 18:11
0

My approach using regular expressions directly on the WKT string

function validateWKT(wkt){

    var match = wkt.match(/\d+\.\d+/g);

    for(var i=0; i < match.length; i=i+2){

        if((match[i] > 180) || (match[i+1] > 90)) {
            return false;

        }   
    }
    return true;
}

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.