Convert lat lon to x and y

Hi,

Any one knows who to convert lat lon to x and y in c#.

I am trying to use flow api

Many thanks,
Omar

Hi Omar!

Can you write more details about your use case and what you are trying to achieve?
What kind of app you are creating (WinForms, WPF, Web)?

Hopefully, having more details we’ll be able to help you better.

Thank you in advance,
Mateusz

Hi,

Many thanks for the reply.

I am trying to use the traffic API (Traffic flow - Raster Flow Tiles).

I have the lat and lon for the location, but the API is requesting the X and Y, so I need to transform the lat and lon to X and Y.

How do I do that, is there specific way that works well.

Regards,

Omar

Hi,

Any hint please on how to get X and Y

Regards,
Omar

You’ll need an X and Y points only if you want to display a tile (map or traffic).
The Raster Flow Tiles API or any other tile-based API will give you nothing more than a simple image.

For all ‘data’ related calls it is possible to use lat-long coordinates. Like for example in the Flow Segment Data API

If you are sure that you still want to use tile-based API, you can use the following code snippet which should help you with generating the X and Y points. Please note that it will work only for the Spherical Mercator projection, has been tested only on a few sample coordinates using .NET Core 3.1 and requires a lot of improvements and tests before any “production” usage.

public static String getTileForLatLng(double lat, double lng, int zoom) {
	//max zoom = 22 
	int x = (int) Math.Floor((lng+180)/360*Math.Pow(2,zoom));
	int y = (int) Math.Floor((1-Math.Log(Math.Tan(lat*Math.PI/180) + 1/Math.Cos(lat*Math.PI/180))/Math.PI)/2 * Math.Pow(2,zoom));
	return $"https://api.tomtom.com/traffic/map/4/tile/flow/absolute/{zoom}/{x}/{y}.png?tileSize=256&key=YOUR_API_KEY";
}

Regards,
Mateusz

Hi,

Many thanks for you answer.
What I would like to do is present the traffic status for certain area and not for one section of the road at a time. Is it possible to do that? Show road traffic status for multi roads at single request?
Also, is there any history data for the traffic flow that can be accessed, such for 21/12/2019 at 10:00AM, something like that?

Your help is really appreciated.

Best Regards,
Omar

You can get a traffic incidents details for a certain area by using our Incident Details API. It allows you to check the traffic for certain area for multiple roads. All of that with a single request.
For more advanced analysis you might want to check our private API Traffic Stats but it requires a custom contract.

Hi,
Sorry, I mean traffic speed and so one see attached image

Now I have to send Lat lon and it will get me the traffic speed for that single part of the road. Is it possible to get bunch of roads traffic speed at once?

Best regard,
Omar

Hi,

Unfortunately, the Flow Segment Data API works only with road segment per one call.

Regards,
Mateusz

Hi,
Is there another service that you provide that can be do what I am after?

Regards,
Omar

Hi,

Yes. The Traffic Stats API have such functionality.

Regards,
Mateusz

hi, i have a problem about lat-lon and x-y conversion.

i want to use taffic incident api, whose input and output are all expressed in projection.

i need to know a high accurate algorithm which i can converse from lat-lon -> x-y and reverse.

it would be great if a lib programmed in C or APIs are available.

Hi, If you want to convert lat, lon (EPSG:4326) into a mercator projection (EPSG:3857), this post can help: https://gis.stackexchange.com/questions/142866/converting-latitude-longitude-epsg4326-into-epsg3857

1 Like