ThinkGeo.com    |     Documentation    |     Premium Support

External Projection - GetDistanceTo gives incorrect results

Hi,

I am using Proj4Projection

InternalProjection 4326
ExternalProjection 3857

I have a polygon created from WKT in 4326/WGS84 from which I get a bounding box.

When I use GetDistanceTo to measure the distance from top left to top right of the bounding box I get a value of 365 metres (This is the correct value according to measuring using Goog maps)

When I create an external projection of this bounding box and again measure the top left to top right distance I get a value of 666 metres.

The external projection seems to be correct as when I add the bounding box to the feature it displays as expected on the map (Bing)

This seems like a trival piece of work so I suspect there is something basic I am missing. Why are the measurements different?

Many thanks

//Measurements
int measurementInternal = (int)Math.Round(internalBBox.UpperLeftPoint.GetDistanceTo(internalBBox.UpperRightPoint, GeographyUnit.DecimalDegree, DistanceUnit.Meter), 0, MidpointRounding.AwayFromZero);

int measurementExternal = (int)Math.Round(externalBBox.UpperLeftPoint.GetDistanceTo(externalBBox.UpperRightPoint, GeographyUnit.Meter, DistanceUnit.Meter), 0, MidpointRounding.AwayFromZero);

Hi Scott,

For calculate GetDistanceTo, you have to convert the points from anyone projection back to 4326, then you can get the correct result.

And for render, you should want to use the projection just like the background layer, because Bing and Google is using the 3857, so please render your shapes under 3857 and calculate the distance under 4326.

Regards,

Don

Thanks for your reply Don,

I am still unclear as to why the distances are different. Surely re-projection from 4326 to 3857 should not cause the distances to change? What am I missing here?

Thanks

Doug

Hi Doug,

It looks this API is designed like this, it will calculate the coordinates distance but not the geodesic distance. So if you want to get the geodesic distance, you need convert shape back to 4326 first.

But I think this API maybe get enhancement that provide a parameter to select, so user can decide to calculate which type distance, I will let our developers know it, if they think it’s a good idea maybe it will be added in release future.

But for now, please convert the projection before your calculate the distance.

Regards,

Don

Ok Don, thanks,

So, if I want to draw a grid over my aforementioned polygon where each grid cell is 50m X 50m how would I go about doing that?

My original plan was to convert the polygon to metres projection, measure it, and create a grid in the 3857 projection in order to overlay the grid. I cant do that now because if I use the 4326 measurement the grid will be displayed too small and if I use the 3857 measurement the grid will have too many grid cells (666/50m).

From what you’ve said I need to create the grid in the 4326 projection and then render the finished result in the 3857 projection.

Because 3857 uses decimal degrees how to I create a metre based grid without doing the reprojection first?

Thanks

Hi Doug,

I think you can render your shapes on map under 3857, then when you need do any calculate, just convert related coordinate back to 4326 and get the result on the fly.

That means the 4326 value only be used for calculate but won’t be displayed on map or added into map, that means the 4326 value is live in the memory, you don’t need save it.

Regards,

Don