scene_indexer.py 에서 gdalinfo 확인
def scan_file_with_gdalinfo(self, filename) -> None:
print('filename', filename)
result = subprocess.Popen(f"gdalinfo -json -proj4 {filename}", shell=True,
stdout=subprocess.PIPE).stdout.read()
try:
result = result.decode('utf-8')
except Exception as ex:
logging.error(f'GDALInfo returned non-UTF included text: {filename}')
result = str(result)
result = result[2:-1]
result = result.replace('\\\\\\\\', '\\\\')
result = result.replace('\\\\n', '\\n')
return result
계산 코드
import gdal
import os
import subprocess
import json
file_path = '/nas/Dataset/R-USR21C/K3A/K3A_20201207054938_31483_00049626_L1G/K3A_20201207_054938_RGB_PS.tif'
x_num = 0
y_num = 0
x_total = 0
y_total = 0
result = subprocess.Popen(f"gdalinfo -json -proj4 {file_path}", shell=True,
stdout=subprocess.PIPE).stdout.read()
result = result.decode('utf-8')
result = json.loads(result)
# pixel size
x_pixel = abs(result['geoTransform'][1])
y_pixel = abs(result['geoTransform'][5])
if x_pixel < 0.1:
x_pixel = abs(result['geoTransform'][1])*111000
y_pixel = abs(result['geoTransform'][5])*111000
# WV3 경우 tif가 row, column으로 분리된 경우
# if 'R' in file_name.split('_')[-3] and 'C' in file_name.split('_')[-3]:
# if 'R1' in file_name:
# x_num = result['size'][0]
# x_total += x_num * x_pixel
# elif 'C1' in file_name:
# y_num = result['size'][1]
# y_total += y_num * y_pixel
# else:
# pass
# 통합 tif 경우
else:
x_num = result['size'][0]
y_num = result['size'][1]
x_total += x_num * x_pixel
y_total += y_num * y_pixel
total = x_total * y_total / 1000000
print(int(total),'km^2')