从Python中的Geojson文件计算光栅边界

时间:2019-05-17 13:36:36

标签: python geojson raster sentinel rasterio

我正在尝试使用rasterio.merge.merge工具并使用geojson文件的边界合并4个哨兵2栅格图像来裁剪图像。我无法找到如何将geojson文件正确转换为正确的边界。

将边界设置为“无”时,我可以正常合并4个光栅图像。我尝试从geojson文件[bounds =(left,bottom,right,top)]中手动输入界限。但这给了我一个没有数据的输出栅格。 如果打印正常的合并文件并查看范围和其他数据,我可以看到范围是大数字,其值以米为单位,并且与某些UTM时区有关。

import rasterio
from rasterio import merge
import glob
import os

SAVE_Location = 'E:/Products/2016-1-1 - 2017-4-1_galway/20160101 - 
20170204/True_Colour_Image/'
search_criteria = "T*.tiff"

q = os.path.join(SAVE_Location, search_criteria)
print(q)

Files_for_Mosaic = glob.glob(q) #search for .tiffs in folder
print(Files_for_Mosaic)

src_files_to_mosaic = [] 
for fp in Files_for_Mosaic: #open tiff files and add there values to list. 
    src = rasterio.open(fp)
    src_files_to_mosaic.append(src)

top = 53.352190769802725 #bounds from .geojson file
left = -9.3878173828125
bottom = 53.097322593577
right = -8.8714599609375

bounds = (left, bottom, right, top)

mosaic, out_trans = rasterio.merge.merge(src_files_to_mosaic, bounds = 
None) #Merge rasters, bounds = None or bounds 
print(mosaic)
print(out_trans)

out_meta = src.meta.copy() #Copy the metadata

out_meta.update({"driver": "GTiff", # Update the metadata
                 "height": mosaic.shape[1],
                 "width": mosaic.shape[2],
                 "transform": out_trans,
                 #"crs": "+proj=utm +zone=35 +ellps=GRS80 +units=m 
               +no_defs "
             }
            )

 saveLocationName = SAVE_Location + 'Mosaic.tiff'


with rasterio.open(saveLocationName, "w", **out_meta) as dest: #Write the 
mosaic raster to disk
    dest.write(mosaic)

image = rasterio.open('E:/Products//2016-1-1 - 2017-4-1_galway//20160101 - 
20170204/True_Colour_Image/Mosaic.tiff', 'r') #open created mosaic to view 
properties
print('bounds = ', image.bounds)
print('transform = ', image.transform)
print('crs =', image.crs)
print('meta =', image.meta)

我需要某种方法将geojson边界转换为rasterio.merge所不能提供的东西,并希望仅通过读取.geojson文件来完成。我相信这与输入tiff文件的转换和crs有关,但是我对如何使它起作用的想法已经用尽。

我从上面的代码中获得以下输出:

bounds =  BoundingBox(left=399960.0, bottom=5790240.0, right=609780.0, 
top=6000000.0)
transform =  | 10.00, 0.00, 399960.00|
             | 0.00,-10.00, 6000000.00|
             | 0.00, 0.00, 1.00|
crs = EPSG:32629
meta = {'driver': 'GTiff', 'dtype': 'uint16', 'nodata': None, 'width': 
20982, 'height': 20976, 'count': 3, 'crs': 
CRS.from_dict(init='epsg:32629'), 'transform': Affine(10.0, 0.0, 399960.0,
   0.0, -10.0, 6000000.0)}

非常感谢任何帮助。

0 个答案:

没有答案