简体   繁体   中英

How to create COG files from raw data in Python?

I would like to know if there are any python libraries or tools to help convert raw data into a cloud optimized GeoTIFF. I have a lot of dat files with latitude, longitude, and a few other bits of metadata about an event that I would like to be able to render on a map.

GDAL can be used. I found these two links helped a lot:

https://geoexamples.com/other/2019/02/08/cog-tutorial.html

https://gdal.org/tutorials/raster_api_tut.html

In the end my code looked roughly like this. I'm sure you will need to adjust, customize, and add more (I came across your question while still searching for how to add a DateTime), but it's a start.

from osgeo import gdal
from osgeo import osr

# tutorial said we need to start with MEM driver
driver = gdal.GetDriverByName('MEM')

# create framework for the data
data_set = driver.Create('', x_size, y_size, num_bands, gdal.GDT_Float32)

# coordinates and spatial reference system
data_set.SetGeoTransform([UL_X, Xspace, 0, UL_Y, 0, -Yspace])
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
data_set.SetProjection(srs.ExportToWkt())

# my data had only a single band to write
data_set.GetRasterBand(1).WriteArray(data_frame)

# I think this is for tiling in the COG
data_set.BuildOverviews("NEAREST", [2, 4, 8, 16, 32, 64])

# now we create the COG
driver = gdal.GetDriverByName('GTiff')
data_set2 = driver.CreateCopy(out_cog_filename, data_set, options=["COPY_SRC_OVERVIEWS=YES", "TILED=YES", "COMPRESS=LZW"])

# close the datasets
data_set = None
data_set2 = None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM