I'm using snappy to try to find out what are the x,y coordinates in order to crop an image.
I've done a few tests using snappy functions but i've noticed that there is something wrong with them. I've converted a X,Y from the image to latitude and longitude and then,using those coordinates, I tried to convert them again into X,Y but i didn't get the same result.
The idea or final purpose is to get the LatLong coordinates from a geojson, read them, and using those coordinates get the X,Y in the image.
Notice that the path refers to a tiff file.
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
Is there any other way to get the X,Y pixel from an image using lat long?
For those of you who are wondering about the same problem I find out this solution:
Change the path to refers not to a .tiff file but the folder .SAFE
Then, I write again my functions and now it looks like this:
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)
This way, I get almost the same values (13000 ~ 13006)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.