question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to convert geotiffs programmatically to cogs

See original GitHub issue

After I have verified the successful run of the command line rio cogeo example.tif example_cog_raw.tif --cog-profile raw then I’m trying to use the function cog_translate programmatically.

This snippet is failing:

cog_img = "cog" + "_" + name # name is for instance example.tif
cog_profile = cog_profiles.get('raw')
cog_profile.update(dict(BIGTIFF=os.environ.get("BIGTIFF", "IF_SAFER")))
block_size = 512
config = dict(
            NUM_THREADS=8,
            GDAL_TIFF_INTERNAL_MASK=os.environ.get("GDAL_TIFF_INTERNAL_MASK", True),
            GDAL_TIFF_OVR_BLOCKSIZE=os.environ.get("GDAL_TIFF_OVR_BLOCKSIZE", block_size),
)
cog_translate(
            img,
            cog_img,
            cog_profile,
            None,
            None,
            None,
            6,
            config
)

where img the binary image file which is properly opened previously by rasterio and checked for being or not a COG. The error stack trace is:

File "/Users/geobart/Development/CallForCode/cog-k8s/app/api_views.py", line 87, in post
    block_size = 512
  File "/Users/geobart/pyenv/cog-k8s-2pcfZKQH/lib/python3.6/site-packages/rio_cogeo/cogeo.py", line 52, in cog_translate
    with rasterio.open(src_path) as src:
  File "/Users/geobart/.pyenv/versions/3.6.2/lib/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "/Users/geobart/pyenv/cog-k8s-2pcfZKQH/lib/python3.6/site-packages/rasterio/__init__.py", line 178, in fp_reader
    dataset = memfile.open()
  File "/Users/geobart/pyenv/cog-k8s-2pcfZKQH/lib/python3.6/site-packages/rasterio/env.py", line 360, in wrapper
    return f(*args, **kwds)
  File "/Users/geobart/pyenv/cog-k8s-2pcfZKQH/lib/python3.6/site-packages/rasterio/io.py", line 132, in open
    writer = get_writer_for_driver(driver)
  File "/Users/geobart/pyenv/cog-k8s-2pcfZKQH/lib/python3.6/site-packages/rasterio/io.py", line 179, in get_writer_for_driver
    if driver_can_create(driver):
  File "rasterio/_base.pyx", line 112, in rasterio._base.driver_can_create
  File "rasterio/_base.pyx", line 95, in rasterio._base.driver_supports_mode
AttributeError: 'NoneType' object has no attribute 'encode'

Do you have any hints on the cause of the error and if I’m using cogeo in a wrong way?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
francbartolicommented, Aug 22, 2018

Thanks @vincentsarago, I think doing that server side is a little bit different. I found out the way to get it done through a MemoryFile instance and the trick to use the first item in the list files. The working snippet is below for those interested later:

with rasterio.open(img) as dataset:
    inpt_profile = dataset.profile
    block_size = 512
    config = dict(
        NUM_THREADS=8,
        GDAL_TIFF_INTERNAL_MASK=os.environ.get("GDAL_TIFF_INTERNAL_MASK", True),
        GDAL_TIFF_OVR_BLOCKSIZE=os.environ.get("GDAL_TIFF_OVR_BLOCKSIZE", block_size),
    )
    cog_profile = cog_profiles.get('ycbcr')
    cog_profile.update(dict(BIGTIFF=os.environ.get("BIGTIFF", "IF_SAFER")))
    with MemoryFile() as dst:
        with dst.open(**inpt_profile) as cog_img:
            cog_translate(
                            dataset.files[0],
                            cog_img.files[0],
                            cog_profile,
                            indexes=None,
                            nodata=None,
                            alpha=None,
                            overview_level=6,
                            config=config
                        )
1reaction
francbartolicommented, Nov 26, 2019

Nice @vincentsarago, many thanks indeed. Btw I wanted to come and say hello in Bucharest but the time was very tight. I’ll do definitively next time 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

GeoExamples - Cloud Optimized GeoTIFF tutorial
The easiest way to create a COG is using the GDAL command line interface. If the GeoTIFF data is created from a script...
Read more >
An Introduction to Cloud Optimized GeoTIFFS (COGs) Part 2
An Introduction to Cloud Optimized GeoTIFFS (COGs) Part 2: Converting Regular GeoTIFFs into COGs. last updated: November 01, 2022.
Read more >
Working with Geospatial data — Part:4, Cloud Optimized ...
CoG converter — This will convert your standard GeoTIFF file to Cloud optimized GeoTIFF. Repo link; Tiling — This repo will help you...
Read more >
Programmatic Access to OpenTopography's Cloud Optimized ...
Programmatic Access to OpenTopography's Cloud Optimized GeoTIFF (COG) Global ... gpd.read_file(river) #Convert the river centerline to the same coordinate ...
Read more >
Cloud Optimized GeoTIFF
A Cloud Optimized GeoTIFF (COG) is a regular GeoTIFF file, aimed at being hosted on a HTTP file server, with an internal organization...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found