Convert WKT X,Y coordinates to Latitude/Longitude

Balasaheb Molawade 136 Reputation points
2024-03-20T09:47:23.8266667+00:00

Hi,

We have a requirement from the customer. They have a point in WKT format as follows: (POINT(998546 172742)).

To fulfill their requirement, we want to convert X and Y coordinates to latitude and longitude using C#.

Is there any code or method available in Bing Maps to achieve this? Does anyone have sample code for the same.

Thanks!

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
592 questions
0 comments No comments
{count} votes

Accepted answer
  1. rbrundritt 15,311 Reputation points Microsoft Employee
    2024-03-20T17:05:12.97+00:00

    This isn't something you would use Bing Maps for as there are a lot of open-source libraries that are well established for this (some are 30+ years old) and used by most map platforms behind the scenes (use directly and avoid costs).

    The first thing you will want to do is figure out what spatial reference system the data you have is in. "POINT(998546 172742)" does not contain latitude/longitude data and is most likely northing/easting data (distance in meters from specific point). Once you know the spatial reference system (SRID) of that data, then a conversion can be done. If you know the name or SRID, you can look it up here: https://epsg.io/

    If you are working in C#, I would recommend using NetTopologySuite with the ProjNet4GeoAPI extension. Aside from doing what you need, this will provide a ton of spatial calculation tools.

    The first step would be to parse the WKT into a NetTopologySuite geometry. Here is an example:

    const string wkt = "POINT(998546 172742)";
    var wktReader = new NetTopologySuite.IO.WKTReader();
    
    var pointGeom = wktReader.Read(wkt);
    

    The pointGeom value will be in the original spatial reference system, so the next step is to then reproject it to WGS84 decimal degrees spatial reference system (SRID: 4326). I've assumed the input coordinates are in the WebMercator spatial reference system.

    //Convert the coordinate system to WGS84.
    var transform = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory().CreateFromCoordinateSystems(
              ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator,
           ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
    
    var wgs84Point = transform.MathTransform.Transform(new double[] { pointGeom.Coordinate.X, pointGeom.Coordinate.Y });
    
    var lat = wgs84Point[1];
    var lon = wgs84Point[0];
    

    This code would produce the following information Lat: 1.5515781157321518, Lon: 8.970091336964115. Now these numbers are likely incorrect since I just guessed what the original data's spatial reference system is. If you have any more information you can share on the input data, I can help modify the code to work accordingly.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful