如何在Python中从原始数据创建COG文件?

发布于 2025-01-12 17:06:54 字数 100 浏览 0 评论 0原文

我想知道是否有任何 python 库或工具可以帮助将原始数据转换为云优化的 GeoTIFF。我有很多 dat 文件,其中包含纬度、经度和一些有关我希望能够在地图上渲染的事件的其他元数据。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2025-01-19 17:06:54

可以使用GDAL。我发现这两个链接很有帮助:

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

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

最后我的代码大致如下所示。我确信您需要调整、自定义和添加更多内容(我在仍在搜索如何添加日期时间时遇到了您的问题),但这只是一个开始。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文