如果您有一个GeoTiff文件,使用GeoTransform能否将经纬度点转换为X、Y坐标?

5

我正在使用GDAL库。目前,我可以输入左上角点和右上角点,并从原始图像中裁剪出一张图片。现在,我想输入两个WKT点并将其转换为X,Y坐标以执行相同的操作。我想知道是否有可能这样做,如果我知道GeoTransform和它所使用的坐标系统(WGS84)。


更多信息:我目前可以使用GeoTransform将X、Y转换为经纬度。 - avtoader
3个回答

6
我之前也遇到过这个问题,以下是一种不错的坐标转换方法。
来自GDAL文档的注意事项:
GDALDataset :: GetProjectionRef()返回的坐标系统描述了由GDALDataset :: GetGeoTransform()返回的仿射地理参考变换所暗示的地理参考坐标。
我们可以使用OGRCoordinateTransformation 来进行转换。
基本上代码会像这样:
// Load up some dataset.
dataset = (GDALDataset *) GDALOpen( mapfile, GA_ReadOnly );

// Define Geographic coordinate system - set it to WGS84.
OGRSpatialReference *poSRS_Geog = new OGRSpatialReference();
poSRS_Geog->importFromEPSG( 4326 ); // WGS84

// Define Projected coordinate system - set to the GeoTransform.
const char *sProj = dataset->GetProjectionRef();
OGRSpatialReference *poSRS_Proj = new OGRSpatialReference( sProj );

// Set up the coordinate transform (geographic-to-projected).
OGRCoordinateTransformation *poCT_Geog2Proj;
poCT_Geog2Proj = OGRCreateCoordinateTransformation( poSRS_Geog, poSRS_Proj );

// Now everything is set up and we set transforming coordinates!
// Pass Lon/Lat coordinates to the Transform function:
double x = lon;
double y = lat;
poCT_Geog2Proj->Transform( 1, &x, &y );

// Now x and y variables will contain the X/Y pixel coordinates.

这就是如何在经纬度和像素坐标之间进行转换。请注意,您可以使用带有Transform()的数组,并一起转换多个坐标。第一个参数是要转换的坐标对数,第二个和第三个参数是指向x和y的指针。我只在这里转换了一对。
请注意,设置反向转换同样很容易:
// Set up the coordinate transform (projected-to-geographic).
OGRCoordinateTransformation *poCT_Proj2Geog;
poCT_Proj2Geog = OGRCreateCoordinateTransformation( poSRS_Proj, poSRS_Geog );

0
我使用这种方法:
void transformCoordinatesEPSG(OGRGeometry &geometry,int from, int to) {
    OGRSpatialReference srcSpatialReference;
    OGRErr error = srcSpatialReference.importFromEPSG(from);

    #ifdef __OGRTRANSFORMDEBUG
        qDebug() << "Import EPSG  " << from << "return " << error;
    #endif

    OGRSpatialReference dstSpatialReference;
    error = error | dstSpatialReference.importFromEPSG(to);

    #ifdef __OGRTRANSFORMDEBUG
    qDebug() << "Import EPSG  " << to << "return " << error;
    #endif

    OGRCoordinateTransformation* coordTrans = OGRCreateCoordinateTransformation(&srcSpatialReference, &dstSpatialReference);
    geometry.transform(coordTrans);
}

经纬度必须是4326。


0

我使用仿射变换来计算一些样本纬度/经度的图像。唯一的问题是,如果图像朝北,当计算纬度/经度时,geoTransform[2]和geTransform[4]需要被清零。

x = (int)Math.Abs(Math.Round((Latitude - geotransform[0]) / geotransform[1]));
y = (int)Math.Abs(Math.Round((Longitude - geotransform[3]) / geotransform[5]));

如果您想要强制执行它,可以执行以下操作(我这样做了,它起作用,但这只是伪代码):

//获取长度和宽度的像素,此部分适用于整个图像
pixelXSize = AbsoluteValue((latitudeAt(Zero)-(latitudeAt(Length)-1))/imageLength);
pixelYSize = AbsoluteValue((longitudeAt(Zero)-(LongitudeAt(Width)-1))/imageWidth);

//计算要计算的点的x、y转换
x = AbsoluteValue((latitudeToConvert-latitudeAt(Zero))/pixelXSize);
y = AbsoluteValue((longitudeToConvert-longitudteAt(Zero))/pixelYSize);

这个答案可能会偏移一到两个像素。如果您使用的是地理转换,再次北向变量可能会搞砸返回的答案。到目前为止,我只对朝北的图像进行了测试。


1
我想我的问题在于如何处理非朝北的图像。 - avtoader

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接