# Problem with Converting XY Coordinates to LatLon

Here is the function that I use to convert from pixel x,y to lat,lon coordinate:

``````    public static Coordinate imageToWorld(Dataset gds, int x, int y)
{
double[] p = new double[3];

OSGeo.OSR.SpatialReference src = new OSGeo.OSR.SpatialReference("");
string s = gds.GetProjectionRef();
src.ImportFromWkt(ref s);
//src.SetUTM(41, 1);
OSGeo.OSR.SpatialReference wgs84 = new OSGeo.OSR.SpatialReference("");
wgs84.SetWellKnownGeogCS("WGS84");
OSGeo.OSR.CoordinateTransformation ct = new OSGeo.OSR.CoordinateTransformation(src, wgs84);
ct.TransformPoint(p);
return new Coordinate(p[0], p[1]);
}
``````

but this function doesn't work for utm projection and gives incorrect longitude. for example for an image with the following WKT it gives 35.25,3.65 but it should be 35.29,61.17 @(x=0,y=0). if I uncomment line 12 it gives correct result only for this image.

PROJCS["WGS 84 / UTM zone 41N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",0],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32641"]]

You don't say, but I'm assuming the PROJCS string in your question is the string being read from your dataset. There are several errors in the string. Here's what I found on SpatialReference.org:

``````PROJCS["WGS 84 / UTM zone 41N",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.01745329251994328,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",63],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
AUTHORITY["EPSG","32641"],
AXIS["Easting",EAST],
AXIS["Northing",NORTH]]
``````

Your central meridian is wrong, as is the false easting.

• thanks for your response. when I open this image with "global mapper", it shows correct lat/long. how that sofware can generate desired results if the string have errors? – user3768377 Aug 31 '14 at 20:47
• about all I can guess is that GlobalMapper is reading just the EPSG value, or even the UTM 41 zone 41N, and ignoring the actual projection parameters in the string – Llaves Aug 31 '14 at 21:49