Tuesday, 10 July 2018

javascript - Unproject Radius with Openlayers with CRS:84 Projection


I have made a Openlayers integrated map that lets you draw a circle, On completion of the circle I have told the map to give me , Bounds, Center Long and Lats, and lastly the radius. My problem is that I can't understand in what decimal it gives the radius to me in. Below is a image that shows you I have drawn a very large circle and the alert for the radius. How many kilometers is this? (I made a google map with the same functionality and the radius works in meters with EPSG Projection.


enter image description here


Below is my code. I am using CRS:84 for my map projection.


Here is the initial bit of code I want working in Meters rather than the decimal structure of the above picture.


            vectors.events.on({
featuremodified: onFeatureModified
});

function onFeatureModified(event) {

var bounds = event.feature.geometry.getBounds();
var answer = "bottom: " + bounds.bottom + "\n";
answer += "left: " + bounds.left + "\n";
answer += "right: " + bounds.right + "\n";
answer += "top: " + bounds.top + "\n";
alert(answer);
var area = event.feature.geometry.getArea();
var radius = 0.565352 * Math.sqrt(area);
alert(radius);
var lonlat = event.feature.geometry.getBounds().getCenterLonLat()

alert(lonlat);

Below this is my complete html document:


    





















  • onclick="toggleControl(this);" checked="checked" />









  • onclick="update();" />




    • name="rotate" onChange="update()" checked="checked"/>



    • name="resize" onChange="update()" checked="checked"/>
      name="keepAspectRatio" onChange="update()" checked="checked" />



    • name="drag" onChange="update()" checked="checked"/>









Answer



The result is in meters. To get the kilometers, multiply the radius by .001.


I came to this conclusion by testing your code against google earth's measuring tool. And it makes sense because your map units are in meters. However, i'm confused as to what code you used to get the screenshot above because when i test your code i get something very different.



This is what more or less 8 meter radius looks like when i tested your code. It certainly did not cover a few countries in west Africa.


enter image description here



Based on your comments and your new code example all you need to do is use getGeodesicArea() to get the square meters. And your resulting radius will be in meters.


Change this line:


var area = event.features[0].geometry.getArea();

For this line:


var area = event.features[0].geometry.getGeodesicArea();



Returns {float} The approximate geodesic area of the polygon in square meters.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...