1

I'm using the GDAL/OGR API (C++) to reproject XY points into Lat/Long coordinates from a given Tiff.

I'm able to achieve it right now, but, the coordenates that i am getting do not correspond to the actual coordinates i am trying to get.

I realized that the method is not reading the input dataset, so, there is not an "starting point".

In fact, using the gdaltranslate tool from the command line, gives the right corrdinates, because i am sending the Tiff as a parameter. Unfortunately, i cannot afford the use of this command.

Below is my code (so far) and the example of gdaltranslate, as the outputs:

C++ Method:

void reproject_coords(string map_biomass) {
        GDALDataset *dataset;
        GDALAllRegister();
        string ds = map_biomass;
        dataset = (GDALDataset *) GDALOpen(ds.c_str(), GA_ReadOnly);

        OGRSpatialReference source(dataset->GetMetadata()), target;
        OGRCoordinateTransformation *poCT;

        target.importFromEPSG(4326);
        //source.AutoIdentifyEPSG();
        //source.SetWellKnownGeogCS("32643");
        double x = 1794, y = 6644;

        //source.dumpReadable();
        poCT = OGRCreateCoordinateTransformation(&source, &target);

        if( poCT == NULL || !poCT->Transform( 1, &x, &y ) )
            printf( "Transformation failed.\n" );
        else
            printf( "(%f,%f)\n",
                    x, y );
}

Input: x = 1794 y = 6644
Output: (70.527326,0.059926)

Command Line:

gdaltransform Karnataka_biomass.tif -t_srs EPSG:4326 -s_srs EPSG:32643

Input: x = 1794 y = 6644 Output: 75.7461474090405 12.4826172522718

The question is if is there any way to "read" the Tiff in the C++ Method in order to get the real Lat/Long coords. I need?

1 Answers1

3

You creating the source spatial reference the wrong way. You should use the method GetProjectionRef() not GetMetaData():

OGRSpatialReference source(dataset->GetProjectionRef()), target;

This should initialize the source spatial reference with the same SRS in your GeoTIFF.

If you just need to transform points, there is no need to use the GeoTIFF though. You can just initialize two OGRSpatialReference objects and transform an OGRPoint.

Update #1

Here is an example:

GDALAllRegister();

OGRSpatialReference source, target;

source.importFromEPSG(32643);
target.importFromEPSG(4326);

OGRPoint p;
p.setX(1794);
p.setY(6644);
p.assignSpatialReference(&source);

p.transformTo(&target);

// transformed coordinates
cout << p.getX() << " | " << p.getY();

However, please note that your results are correct, and you will get the same with the code above. The reason you see different values when you look at the transformed GeoTIFF is because gdaltransform will probably need to rotate / rescale it, and since an image is always rectangular no matter what, it may choose a different upper left corner to accomplish this.

Update #2

The OP clarified he doesn't want to project a single point but rather the whole image. This task can be accomplished using the GDALWarp API (a tutorial here).

As an alternative, it is still possible to use the gdaltransform utility, invoking it via a system() call.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Sergio Monteleone
  • 2,776
  • 9
  • 21
  • I replaced `GetMetaData()` with `GetProjectionRef()` but i'm getting the same coordinates. And i'm trying to use the **OGRPoint** approach, but i'm having troubles.Do you have an example of it? Thanks in advance. – René Orozco Apr 18 '18 at 18:32
  • I see, so, what could i do in this case? Find a different approach? – René Orozco Apr 18 '18 at 20:05
  • It depends on what is your goal. If you want to transform that point, the correct approach is the one I showed you. Otherwise, can you please clarify what you want to obtain exactly? – Sergio Monteleone Apr 18 '18 at 20:09
  • Of course, i'm creating multiple points in base of a Tiff. Then, i create PNGs which are nothing but a search route from the _actual point_. In order to show the image in Google Maps, i am trying to get the exact Lat/Long coords. This can be achieved using `gdaltransform`, but, i need to be _automatically_, in other words, no user-interaction. – René Orozco Apr 18 '18 at 20:16
  • I see, then you have two options: use the **GDALWarp** API (there's a tutorial [here](http://www.gdal.org/warptut.html)) or invoke the gdaltransform command using **QProcess**. – Sergio Monteleone Apr 18 '18 at 20:24
  • I'll try with the GDALWarp API first. In case it cannot solve the problem, then i will go with the **invoke**, which i have tried it before, but i was invoking it with `system()` in C++. Thank you very much for your time and explanation. – René Orozco Apr 18 '18 at 20:28
  • You're more than welcome. When you have time, please edit your question to specify you want to transform a picture and not a single point, and I will too update my answer, so people having the same issue can easily find the answer without having to dig the comments. – Sergio Monteleone Apr 18 '18 at 20:32