快捷-像素转换为经纬度的错误转换

2024-06-19 19:40:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用snapy来找出x,y坐标,以便裁剪图像。在

我用snapy函数做了一些测试,但我注意到它们有问题。我已经把图像中的X,Y转换成经纬度,然后,用这些坐标,我试图再次把它们转换成X,Y,但是没有得到相同的结果。在

这个想法或最终目的是从geojson获取LatLong坐标,读取它们,然后使用这些坐标获得图像中的X,Y。在

请注意,路径引用的是tiff文件。在

from snappy import ProductIO
from snappy import PixelPos, GeoPos
import numpy as np

path='/home/.../x.tiff'
###############################################################################
product = ProductIO.readProduct(path)
sg = product.getSceneGeoCoding()

def LatLon_from_XY(ProductSceneGeoCoding, x, y):
    #From x,y position in satellite image (SAR), get the Latitude and Longitude
    geopos = ProductSceneGeoCoding.getGeoPos(PixelPos(x, y), None)
    latitude = geopos.getLat()
    longitude = geopos.getLon()
    return latitude, longitude

latitude, longitude = LatLon_from_XY(sg, 11048, 1365)

print('LatLong from PixelPosition')
print(latitude)
print(longitude)
### 38.3976151718
### -5.47978868123

###############################################################################

def getPixelPosFromLatLong(source, lat,lon):
    if sg.canGetPixelPos() is not True:
        raise Exception('Cant''t get Pixel Position from this source')
    else:
        pos = GeoPos(lat,lon)
        pixpos = sg.getPixelPos(pos,None)
        X = np.round(pixpos.getX())
        Y = np.round(pixpos.getY())
    return [X,Y]

[X,Y] = getPixelPosFromLatLong(path, 38.3976151718, -5.47978868123)

print('Pixel Position from LatLong')
print(X)
print(Y)
### 10715.0
### 1143.0

有没有其他方法可以使用lat long从图像中获取X,Y像素?在


Tags: pathfrom图像importnpsgprintlat
1条回答
网友
1楼 · 发布于 2024-06-19 19:40:14

对于那些想知道同样问题的人,我找到了这个解决方案:

将路径更改为引用的不是.tiff文件而是文件夹.SAFE

然后,我再写一次函数,现在看起来是这样的:

from snappy import ProductIO
from snappy import PixelPos, GeoPos
import numpy as np

def LatLon_from_XY(ProductSceneGeoCoding, x, y):
    geoPos = ProductSceneGeoCoding.getGeoPos(PixelPos(x,y),None)
    lat = geoPos.getLat()
    lon = geoPos.getLon()
    return lat,lon

def XY_from_LatLon(ProductSceneGeoCoding, latitude, longitude):
    pixelPos = ProductSceneGeoCoding.getPixelPos(GeoPos(latitude, longitude),None)
    x = np.round(pixelPos.getX())
    y = np.round(pixelPos.getY())
    return x,y

###############################################################################

#Notice that the path does not refer to any file but to the folder .SAFE
path = '/.../X.SAFE'
product = ProductIO.readProduct(path)
sg = product.getSceneGeoCoding()
originalX = 13000
originalY = 13000

print('Original X,Y: ', originalX,originalY)

lat,lon = LatLon_from_XY(sg, originalX, originalY)
print(lat,lon)

x,y = XY_from_LatLon(sg,lat,lon)
print(x,y)

originalLat = 37.36475504265766
originalLon = -5.972416873450527

print('Original Lat,Lon: ', originalLat, originalLon)

x,y = XY_from_LatLon(sg, originalLat, originalLon)
print(x,y)

lat,lon = LatLon_from_XY(sg, x, y)
print(lat,lon)

这样,我得到的值几乎相同(13000~13006)

相关问题 更多 >