首页 > 解决方案 > 从长纬度坐标和高程数据创建 tif 文件

问题描述

我想从坐标数据和高程数据列表中创建一个 tif 文件。它们是我自己为我正在尝试处理的玩具问题创建的数据。数据如下:

-78.5000 32.5000 -78 1 1
-78.5000 32.5100 -78 1 2
-78.5000 32.5200 -78 1 3
-78.5000 32.5300 -78 1 4
-78.5000 32.5400 -78 1 5
-78.5000 32.5500 -78 1 6
-78.5000 32.5600 -78 1 7
-78.5000 32.5700 -78 1 8
-78.5000 32.5800 -78 1 9
-78.5000 32.5900 -78 1 10
-78.5000 32.6000 -78 1 11
-78.5000 32.6100 -78 1 12
-78.5000 32.6200 -78 1 13
-78.5000 32.6300 -78 1 14
-78.5000 32.6400 -78 1 15
-78.5000 32.6500 -78 1 16
-78.5000 32.6600 -78 1 17
-78.5000 32.6700 -78 1 18
-78.5000 32.6800 -78 1 19
-78.5000 32.6900 -78 1 20
-78.5000 32.7000 -78 1 21
-78.5000 32.7100 -78 1 22
-78.5000 32.7200 -78 1 23
-78.5000 32.7300 -78 1 24
-78.5000 32.7400 -78 1 25
-78.5000 32.7500 -78 1 26
...

第一列是长,第二列是纬度,第三列是英尺深度。第四个和第五个是我的每个网格单元的特定问题 (i,j) 值的另一种类型的坐标数据。

如何创建这些数据的 tif 文件?有没有办法在python中做到这一点?

标签: python

解决方案


我在这里使用答案几乎成功地回答了我自己的问题:

from osgeo import gdal
from osgeo import osr
import numpy as np

image_size = (201,201)
lon = np.zeros((image_size), dtype=np.float64)
lat = np.zeros((image_size), dtype=np.float64)
red = np.zeros((image_size), dtype=np.int8)
for x in range(0,image_size[0]):
    for y in range(0,image_size[1]):
        lon[y,x] =  -1*(78.5+0.01*-x)
        lat[y,x] =  32.5+0.01*y
        red[y,x] =  lon[y,x]

# set geotransform
nx = red.shape[0]
ny = red.shape[0]
xmin, ymin, xmax, ymax = [lon.min(), lat.min(), lon.max(), lat.max()]
xres = (xmax - xmin) / float(nx)
yres = (ymax - ymin) / float(ny)
geotransform = (xmin, xres, 0, ymax, 0, -yres)

# create the 1-band raster file
dst_ds = gdal.GetDriverByName('GTiff').Create('myGeoTIFF.tif', ny, nx, 1, gdal.GDT_Byte)
dst_ds.SetGeoTransform(geotransform)    # specify coords
srs = osr.SpatialReference()            # establish encoding
srs.ImportFromEPSG(3857)                # WGS84 lat/long
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(red)   # write r-band to the raster
dst_ds.FlushCache()                     # write to disk
dst_ds = None                           # save, close

但是,它将红色值写为正值而不是负值。红色数组是 -78..-76 但它写成 180..178 我不知道为什么。


推荐阅读