我正在尝试使用 geojson 中的形状来掩盖图像。
我要做的第一件事是获取图像中代表我的经纬度坐标的像素。
当我获得该信息时,我使用该坐标构建了一个“多边形”,然后尝试裁剪图像,但出现此错误:
“numpy.ndarray”对象不可调用
请注意,绝对路径是指.jp2文件。
geoms = [{'type':'Polygon', 'coordinates': [[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]]}]
with rasterio.open(absolutePath) as src:
out_image,out_transform = mask(src,geoms,crop=True)
out_meta = src.meta.copy()
我也尝试使用第一个坐标作为第五个坐标来“密封”多边形,但没有任何改变。
我得到的唯一错误信息是:
TypeErrorTraceback (most recent call last)
<ipython-input-13-796462428f9b> in <module>()
2
3 with rasterio.open(absolutePath) as src:
----> 4 out_image,out_transform = mask(src,geoms,crop=True)
5 out_meta = src.meta.copy()
TypeError: 'numpy.ndarray' object is not callable
这是我所有的全部代码:
from snappy import ProductIO
from snappy import PixelPos, GeoPos
import numpy as np
import geojson
import cv2
import os
import rasterio
from rasterio.mask import mask
path = '/home/.../x.SAFE'
product = ProductIO.readProduct(path)
sg = product.getSceneGeoCoding()
pathGeoJson = '/home/.../x.geojson'
with open(pathGeoJson) as f:
gj = geojson.load(f)
features = gj['features'][0]
latlon1 = features.geometry.coordinates[0][0]
latlon2 = features.geometry.coordinates[0][1]
latlon3 = features.geometry.coordinates[0][2]
latlon4 = features.geometry.coordinates[0][3]
geoms = gj['features'][0].geometry
geoms
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
x1,y1 = XY_from_LatLon(sg,latlon1[0], latlon1[1])
x2,y2 = XY_from_LatLon(sg,latlon2[0], latlon2[1])
x3,y3 = XY_from_LatLon(sg,latlon3[0], latlon3[1])
x4,y4 = XY_from_LatLon(sg,latlon4[0], latlon4[1])
print(x1,y1)
print(x2,y2)
print(x3,y3)
print(x4,y4)
pathToJP2 = '/home/.../IMG_DATA/'
arr = os.listdir(pathToJP2)
absolutePath = pathToJP2+arr[1]
arr[1]
print(absolutePath)
geoms = [{'type':'Polygon', 'coordinates': [[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]]}]
with rasterio.open(absolutePath) as src:
out_image,out_transform = mask(src,geoms,crop=True)
out_meta = src.meta.copy()