1

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?

4
  • 1
    Perhaps you should try #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.
    – user30184
    Commented Sep 9, 2019 at 6:28
  • You could try setting UTM using the EPSG code, it's 32700 + zone for southern hemisphere and 32600 + zone for northern hemisphere instead of using OSRSetUTM. Commented Sep 9, 2019 at 7:06
  • Thanks! I am going to try that
    – rbeucher
    Commented Sep 11, 2019 at 1:48
  • 1
    No luck. Anyone with a simple C-API example to do that?
    – rbeucher
    Commented Sep 11, 2019 at 11:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.