使用下面的自定义函数,我们可以轻松地将图像中的任何(纬度,经度)转换为它的(x,y)位置,只要经纬度在我们的产品范围内.
from snappy import GeoPos
def XY_from_LatLon(ProductSceneGeoCoding, latitude, longitude):
#From Latitude, Longitude satellite image (SAR), get the x, y position in image
pixelPos = ProductSceneGeoCoding.getPixelPos(GeoPos(latitude, longitude), None)
x = pixelPos.getX()
y = pixelPos.getY()
if str(x)=='nan':
raise ValueError('Latitude or Longitude out of this product')
else:
return x, y
更新:
以下更新的功能应该适用于更多快照版本
import jpy
import snappy
def XY_from_LatLon(ProductSceneGeoCoding, latitude, longitude):
geoPosType = jpy.get_type('org.esa.snap.core.datamodel.PixelPos')
geocoding = ProductSceneGeoCoding.getSceneGeoCoding()
pixel_pos = geocoding.getPixelPos(snappy.GeoPos(latitude, longitude), geoPosType())
if str(pixel_pos.x)=='nan':
raise ValueError('Latitude or Longitude out of this product')
else:
return int(np.round(pixel_pos.x)), int(np.round(pixel_pos.y))
例如对于下面给定的产品(正如我们在scihub 中看到的,它是希腊南部的产品),我们可以得到雅典坐标(纬度=37.9838,经度=23.7275)图像中的(x,y)位置为
产品名称:S1A_IW_GRDH_1SDV_20170821T162310_20170821T162335_018024_01E414_C88B
path='path to S1A_IW_GRDH_1SDV_20170821T162310_20170821T162335_018024_01E414_C88B.SAFE'
product = ProductIO.readProduct(path)
sg = product.getSceneGeoCoding()
x, y = XY_from_LatLon(sg, 37.9838, 23.7275)
x, y
# (13705.242822312131, 14957.933651457932)