I have dug up some old code I wrote 6 years ago that was doing the conversion from Lat Lon coordinates to UTM.
I have tried to run it today and the code returns NaN values. I suspect there has been some significant changes with GDAL. I am using GDAL 3.0 on Arch Linux.
Function latlon2utm
takes X1, Y1 lat/lon coordinates and returns UTM coordinates X2, Y2
#include "ogr_api.h"
#include "ogr_srs_api.h"
void LatLon2UTM(double *X1, double *Y1, double *X2, double *Y2)
{
int zone;
int err;
printf("%f %f\n", *X1, *Y1);
OGRGeometryH pt1;
OGRSpatialReferenceH SREF;
OGRSpatialReferenceH UTM;
SREF = OSRNewSpatialReference(NULL);
OSRImportFromEPSG(SREF, 4326);
zone = (int)(((*X1 + 180.0) / 6.0) + 1.0);
UTM = OSRNewSpatialReference(NULL);
if(*Y1 < 0) OSRSetUTM(UTM,zone,FALSE);
if(*Y1 > 0) OSRSetUTM(UTM,zone,TRUE);
pt1 = OGR_G_CreateGeometry(wkbPoint);
//printf("%f %f", *X1, *X2);
OGR_G_SetPoint_2D(pt1, 0, *X1, *Y1);
OGR_G_AssignSpatialReference(pt1, SREF);
// Convert to UTM
err = OGR_G_TransformTo(pt1,UTM);
printf("%d", err);
*X2 = OGR_G_GetX(pt1, 0);
*Y2 = OGR_G_GetY(pt1, 0);
printf("%f %f\n", *X2, *Y2);
}
Is There anyone that could give me the proper way to do that?
#define USE_DEPRECATED_SRS_WKT_WGS84 before including ogr_srs_api.h
that I found from the migration guide github.com/OSGeo/gdal/blob/release/3.0/gdal/MIGRATION_GUIDE.TXT.