GDAL python 바인딩을 사용하여 다시 프로젝트 / 다시 샘플링하려고하지만 명령 줄 유틸리티의 결과와 약간 다른 결과가 나타납니다 gdalwarp
.
더 짧은 예는 아래 업데이트를 참조하십시오.
이 스크립트는 Python 접근 방식을 보여줍니다.
from osgeo import osr, gdal
import numpy
def reproject_point(point, srs, target_srs):
'''
Reproject a pair of coordinates from one spatial reference system to
another.
'''
transform = osr.CoordinateTransformation(srs, target_srs)
(x, y, z) = transform.TransformPoint(*point)
return (x, y)
def reproject_bbox(top_left, bottom_right, srs, dest_srs):
x_min, y_max = top_left
x_max, y_min = bottom_right
corners = [
(x_min, y_max),
(x_max, y_max),
(x_max, y_min),
(x_min, y_min)]
projected_corners = [reproject_point(crnr, srs, dest_srs)
for crnr in corners]
dest_top_left = (min([crnr[0] for crnr in projected_corners]),
max([crnr[1] for crnr in projected_corners]))
dest_bottom_right = (max([crnr[0] for crnr in projected_corners]),
min([crnr[1] for crnr in projected_corners]))
return dest_top_left, dest_bottom_right
################################################################################
# Create synthetic data
gtiff_drv = gdal.GetDriverByName('GTiff')
w, h = 512, 512
raster = numpy.zeros((w, h), dtype=numpy.uint8)
raster[::w / 10, :] = 255
raster[:, ::h / 10] = 255
top_left = (-109764, 215677)
pixel_size = 45
src_srs = osr.SpatialReference()
src_srs.ImportFromEPSG(3413)
src_geotran = [top_left[0], pixel_size, 0,
top_left[1], 0, -pixel_size]
rows, cols = raster.shape
src_ds = gtiff_drv.Create(
'test_epsg3413.tif',
cols, rows, 1,
gdal.GDT_Byte)
src_ds.SetGeoTransform(src_geotran)
src_ds.SetProjection(src_srs.ExportToWkt())
src_ds.GetRasterBand(1).WriteArray(raster)
################################################################################
# Reproject to EPSG: 3573 and upsample to 7m
dest_pixel_size = 7
dest_srs = osr.SpatialReference()
dest_srs.ImportFromEPSG(3573)
# Calculate new bounds by re-projecting old corners
x_min, y_max = top_left
bottom_right = (x_min + cols * pixel_size,
y_max - rows * pixel_size)
dest_top_left, dest_bottom_right = reproject_bbox(
top_left, bottom_right,
src_srs, dest_srs)
# Make dest dataset
x_min, y_max = dest_top_left
x_max, y_min = dest_bottom_right
new_rows = int((x_max - x_min) / float(dest_pixel_size))
new_cols = int((y_max - y_min) / float(dest_pixel_size))
dest_ds = gtiff_drv.Create(
'test_epsg3573.tif',
new_rows, new_cols, 1,
gdal.GDT_Byte)
dest_geotran = (dest_top_left[0], dest_pixel_size, 0,
dest_top_left[1], 0, -dest_pixel_size)
dest_ds.SetGeoTransform(dest_geotran)
dest_ds.SetProjection(dest_srs.ExportToWkt())
# Perform the projection/resampling
gdal.ReprojectImage(
src_ds, dest_ds,
src_srs.ExportToWkt(), dest_srs.ExportToWkt(),
gdal.GRA_NearestNeighbour)
dest_data = dest_ds.GetRasterBand(1).ReadAsArray()
# Close datasets
src_ds = None
dest_ds = None
다음의 출력과 비교하십시오.
gdalwarp -s_srs EPSG:3413 -t_srs EPSG:3573 -tr 7 7 -r near -of GTiff test_epsg3413.tif test_epsg3573_gdalwarp.tif
크기가 다르며 (2 행 1 열) 가장자리 근처의 픽셀 값도 다릅니다.
아래 test_epsg3573.tif 및 test_epsg3573_gdalwarp.tif의 투명 오버레이를 참조하십시오. 이미지가 동일하면 흑백 픽셀 만 있고 회색은 없습니다.
Python 2.7.8, GDAL 1.11.1, Numpy 1.9.1로 테스트
업데이트 :
여기 훨씬 짧은 예가 있습니다. 다음은 또한 다음과 일치하지 않는 결과를 생성하므로 업 샘플링으로 인한 것이 아닌 것 같습니다gdalwarp
from osgeo import osr, gdal
import numpy
# Create synthetic data
gtiff_drv = gdal.GetDriverByName('GTiff')
w, h = 512, 512
raster = numpy.zeros((w, h), dtype=numpy.uint8)
raster[::w / 10, :] = 255
raster[:, ::h / 10] = 255
top_left = (-109764, 215677)
pixel_size = 45
src_srs = osr.SpatialReference()
src_srs.ImportFromEPSG(3413)
src_geotran = [top_left[0], pixel_size, 0,
top_left[1], 0, -pixel_size]
rows, cols = raster.shape
src_ds = gtiff_drv.Create(
'test_epsg3413.tif',
cols, rows, 1,
gdal.GDT_Byte)
src_ds.SetGeoTransform(src_geotran)
src_ds.SetProjection(src_srs.ExportToWkt())
src_ds.GetRasterBand(1).WriteArray(raster)
# Reproject to EPSG: 3573
dest_srs = osr.SpatialReference()
dest_srs.ImportFromEPSG(3573)
int_ds = gdal.AutoCreateWarpedVRT(src_ds, src_srs.ExportToWkt(), dest_srs.ExportToWkt())
# Make dest dataset
dest_ds = gtiff_drv.Create(
'test_epsg3573_avrt.tif',
int_ds.RasterXSize, int_ds.RasterYSize, 1,
gdal.GDT_Byte)
dest_ds.SetGeoTransform(int_ds.GetGeoTransform())
dest_ds.SetProjection(int_ds.GetProjection())
dest_ds.GetRasterBand(1).WriteArray(int_ds.GetRasterBand(1).ReadAsArray())
# Close datasets
src_ds = None
dest_ds = None
그리고 이것은 내가 같을 것으로 예상되는 gdalwarp 호출입니다.
gdalwarp -s_srs EPSG:3413 -t_srs EPSG:3573 -of GTiff test_epsg3413.tif test_epsg3573_gdalwarp.tif
아래 이미지는 50 % 투명도로 오버레이 된 각 이진 이미지를 보여줍니다. 밝은 회색 픽셀은 두 결과간에 불일치입니다.
gdal.AutoCreateWarpedVRT(source_file, source_srs_wkt, dest_srs_wkt)
?