簡體   English   中英

Snappy-從像素到經度轉換錯誤

[英]Snappy - Wrong conversion from pixel to lat long

我正在使用snappy嘗試找出x,y坐標是什么,以便裁剪圖像。

我已經使用快照函數完成了一些測試,但是我注意到它們存在問題。 我已經將圖像的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

還有其他方法可以使用經緯度從圖像中獲取X,Y像素嗎?

對於那些想知道同一問題的人,我可以找到以下解決方案:

將路徑更改為不指向.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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM