气象学家公众号 发表于 2024-3-15 23:30:48

利用gdal等提取TIF文件的经纬度信息

前言介绍
之前处理北美五大湖的数据,湖表温度为卫星观测数据,官方有提供.asc文本格式的阵列像素数据,但是缺少地理坐标信息(这也是比较麻烦的地方,需要自己根据已有的信息进行坐标转换),官网并没有相关的地理数据文件,只有一些描述性的文件。Great Lakes Surface Environmental Analysis(GLSEA,https://coastwatch.glerl.noaa.gov/glsea/),不得已就采用这些零碎的信息,最终也获得了像素文件相匹配的经纬度信息,但是方法比较繁琐,采用ArcGIS来转换的(此处,感谢南大环境学院博士YJ鼎力相助!),先转换为shapefile相关文件,再导出latlon为.csv文件。好在把图用NCL画出来匹配度还可以,就基本用起来了。手头没有Windows电脑,用ArcGIS显得很笨重又奢侈。后来就发邮件给数据负责人,管理员很热心,就把包含地理信息的.tif文件发给我了。对于我完全没有什么相关坐标转换的经验,但也不能浪费这个tif文件,总觉得应该有更加优雅的解决方案。于是另辟蹊径,算是把这个问题再一次解决了。

文末,单独给出地理坐标信息完整的geotif文件转换方式,相对简单,基本一键式操作。 测试数据>>>链接:https://pan.baidu.com/s/1UElL2Gv9PHb0-Qm0Yoy4mw

官网的地理描述信息

问题:优雅转换.tif文件到经纬度.csv文件操作流程???

需要的库/软件
1.cs2cs;2.R语言;3.gdal;4.NCL(操作环境为macOS或者Linux)

查看文件
查看tif文件信息,确定地理坐标。进到R语言的终端环境:

图片以上可以确定这个tif文件是附带了地理坐标信息的。

转换过程
1$   gdal_translate -of XYZ 2018_252_glsea.tif2018_252_glsea.csv
参考:
1.https://gis.stackexchange.com/questions/277780/transform-data-stored-in-a-tiff-file-to-a-csv-with-latitude-longitude-and-data

2.https://mygeodata.cloud/cs2cs/

3.https://codeday.me/bug/20181220/457615.html

经纬度显示


和实际北美五大湖的范围是匹配的。

绘图检验

上图,Great Lakes Surface Environmental Analysis(GLSEA)官网提供的样图。


上图,原始高分辨率的湖表温度图


上图,为0.1°分辨率经纬度格点数据绘制的湖表温度图,可见匹配关系很不错

综上,就是处理数据的部分流程,重点是格式转换,比较初步的工作,如果有需要将这一套卫星观测数据处理为经纬度格点的netCDF文件存储,可以和我私下交流(邮件/微信)。上面的几步基本涵盖了数据处理的基础性工作了。地理信息完整的.tif文件转换(此处,感谢WHZ,提供了新思路!)

安装gdal
Ubuntu/Debian 下:
1    sudo apt-get install gdal-binCentOS/Fedora下:
1   sudo yum install gdalWindows 下,到 http://www.gisinternals.com/ 下载安装包,安装后命令位于 C:\Program Files (x86)\GDAL\gdal_translate.exe。如果偶尔用一次,直接用 命令的完整路径即可;如果经常使用,可以将 C:\Program Files (x86)\GDAL\ 添加到环境变量 PATH中
macOS High Sierra下:
1    brew install gdalOK!
OK!
OK!
假若你已经安装完毕了!需要测试数据的可以私信我!我百度云盘分享给你链接!
1    ➜Downloads gdal_translate -of netCDF -co "FOMRAT=NC4"mosic1.tifmosic1.nc
2    Input file size is 21601, 21601
3    Warning 6: driver netCDF does not support creation option FOMRAT
4    0...10...20...30...40...50...60...70...80...90...100 - done.
5    ➜Downloads rm mosic1.nc
6    ➜Downloads gdal_translate -of GMT mosic1.tif mosic1.nc
7    Input file size is 21601, 21601
8    ➜Downloads




➜Downloads ncdump -h mosic1.nc
netcdf mosic1 {
dimensions:
      side = 2 ;
      xysize = 466603201 ;
variables:
      double x_range(side) ;
                x_range:units = "meters" ;
      double y_range(side) ;
                y_range:units = "meters" ;
      double z_range(side) ;
                z_range:units = "meters" ;
      double spacing(side) ;
      int dimension(side) ;
      short z(xysize) ;
                z:scale_factor = 1. ;
                z:add_offset = 0. ;
                z:node_offset = 1 ;

// global attributes:
                :title = "" ;
                :source = "" ;
}
➜Downloads

gdal_translate 用法
1Usage: gdal_translate [--help-general] [--long-usage]
2       [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/
3             CInt16/CInt32/CFloat32/CFloat64}] [-strict]
4       [-of format] [-b band] [-mask band] [-expand {gray|rgb|rgba}]
5       [-outsize xsize[%] ysize[%]]
6       [-unscale] [-scale ]]* [-exponent exp_val]*
7       [-srcwin xoff yoff xsize ysize] [-projwin ulx uly lrx lry] [-epo] [-eco]
8       [-a_srs srs_def] [-a_ullr ulx uly lrx lry] [-a_nodata value]
9       [-gcp pixel line easting northing ]*
10       [-mo"META-TAG=VALUE"]* [-q] [-sds]
11       [-co"NAME=VALUE"]* [-stats] [-norat]
12       src_dataset dst_dataset-of 后接要转换的数据格式(可能需要新一点的gdal版本才能支持更加丰富的格式转换!),使用 gdal_translate --long-usage 可以看到,支持的格式如下:
1VRT: Virtual Raster
2GTiff: GeoTIFF
3NITF: National Imagery Transmission Format
4HFA: Erdas Imagine Images (.img)
5ELAS: ELAS
6AAIGrid: Arc/Info ASCII Grid
7DTED: DTED Elevation Raster
8PNG: Portable Network Graphics
9JPEG: JPEG JFIF
10MEM: In Memory Raster
11GIF: Graphics Interchange Format (.gif)
12FITS: Flexible Image Transport System
13XPM: X11 PixMap Format
14BMP: MS Windows Device Independent Bitmap
15PCIDSK: PCIDSK Database File
16PCRaster: PCRaster Raster File
17ILWIS: ILWIS Raster Map
18SGI: SGI Image File Format 1.0
19SRTMHGT: SRTMHGT File Format
20Leveller: Leveller heightfield
21Terragen: Terragen heightfield
22GMT: GMT NetCDF Grid Format
23netCDF: Network Common Data Format
24HDF4Image: HDF4 Dataset
25ERS: ERMapper .ers Labelled
26JPEG2000: JPEG-2000 part 1 (ISO/IEC 15444-1)
27FIT: FIT Image
28RMF: Raster Matrix Format
29RST: Idrisi Raster A.1
30INGR: Intergraph Raster
31GSAG: Golden Software ASCII Grid (.grd)
32GSBG: Golden Software Binary Grid (.grd)
33R: R Object Data Store
34PNM: Portable Pixmap Format (netpbm)
35ENVI: ENVI .hdr Labelled
36EHdr: ESRI .hdr Labelled
37PAux: PCI .aux Labelled
38MFF: Vexcel MFF Raster
39MFF2: Vexcel MFF2 (HKV) Raster
40BT: VTP .bt (Binary Terrain) 1.3 Format
41IDA: Image Data and Analysis
42USGSDEM: USGS Optional ASCII DEM (and CDED)
43ADRG: ARC Digitized Raster Graphics
44BLX: Magellan topo (.blx)
45Rasterlite: Rasterlite
46SAGA: SAGA GIS Binary Grid (.sdat)参考:
1.https://blog.seisman.info/trash/convert-geotiff-to-gmt/

也可以用Python,这样conda install gdal 一键式安装,适用于多种平台
1import glob
2import pandas as pd
3import xarray as xr
4
5def time_index_from_filenames(filenames):
6    '''helper function to create a pandas DatetimeIndex
7   Filename example: 20150520_0164.tif'''
8    return pd.DatetimeIndex() for f in filenames])
9
10filenames = glob.glob('*.tif')
11time = xr.Variable('time', time_index_from_filenames(filenames))
12chunks = {'x': 5490, 'y': 5490, 'band': 1}
13da = xr.concat(, dim=time)
14da.to_netcdf('foo.nc')当然有时候需要将netcdf的.nc文件转换为带地理坐标信息的.tif文件,也是如法炮制!
1library(raster)
2
3# read the netcdf file as raster
4nc_raster = raster("input_file.nc")
5
6# if raster crs is NA, set the correct projection using proj4
7projection(nc_raster) = CRS(+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0)
8
9# save output
10writeRaster(nc_raster, "nc_raster.tif", "GTiff")或者
1    gdal_translate -of GTiff file.nc test.tiffand using the -b option will allow you to specify which band you want to convert.
1    gdal_translate -of GTiff -b 10 file.nc test.tiff# to get 10th band参考:
1.https://stackoverflow.com/questions/52046282/convert-netcdf-nc-to-geotiff

TIF, TIFF, GeoTIFF区别
Adobe Tagged Image File Format (TIFF or TIF, same thing) is so universally used as a format for imagery that few people know it is a Adobe format.    TIF files can contain tags, additional items of information, that can be used to extend the standard for different purposes.Software that does not understand how to use extra tags simply ignores them.   Manifold can read and write TIF files.   TIF is a very popular export format for interchanging images.   Manifold also reads BigTIFF, an extension of TIFF format to handle TIF images that are larger than 4 GB.

A GeoTIFF is a TIF file that ends in a three letter .tif extension just like other TIF files, but a GeoTIFF contains additional tags that provide projection information for that image as specified by the GeoTIFF standard.   When Manifold reads a TIF that contains GeoTIFF tags, the system will automatically capture that projection information to assign to the image.   When Manifold exports an image to TIF format, Manifold always writes GeoTIFF tags into that file to specify the projection that image used within the Manifold project.    TIFs created by Manifold therefore are always GeoTIFF compliant TIF files.    When Manifold exports a TIFF bigger than 4 GB, it is automatically exported as a BigTIFF.

参考:
1.http://www.manifold.net/doc/mfd9/tif,_tiff,_geotiff.htm


文章来源于微信公众号:气象学家


页: [1]
查看完整版本: 利用gdal等提取TIF文件的经纬度信息