Topics

Copying Rasterio crs from input to output dataset

Luke Pinner
 

Hi all.  Congrats on the 1.0.0 release!  

Using rasterio 1.0.0 (installed from conda-forge in a fresh conda env). I'm having trouble copying the CRS from on dataset to another.  The output dataset CRS is None.

Simplified example demonstrating:

import rasterio as rio

inpath = r"test.tif"
outpath = r"testout.tif"

with rio.open(inpath) as inds:
    print(repr(inds.crs), inds.meta['crs'])
    with rio.open(outpath, 'w', **inds.meta) as outds:
        outds.write(inds.read())

with rio.open(outpath) as outds:
    print(repr(outds.crs), outds.meta['crs'])

The output of the above is:

CRS({'init': 'epsg:4326'}) +init=epsg:4326
None None

Dataset info:

rio info test.tif
{"blockxsize": 128, "blockysize": 128, "bounds": [143.22468138068513, -34.72000041269073, 146.20968138068514, -31.952500412690732], "colorinterp": ["grey", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"], "count": 11, "crs": "EPSG:4326", "descriptions": [null, null, null, null, null, null, null, null, null, null, null], "driver": "GTiff", "dtype": "float32", "height": 1107, "indexes": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], "interleave": "pixel", "lnglat": [144.71718138068513, -33.33625041269073], "mask_flags": [["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"]], "nodata": -9999.0, "res": [0.0025000000000000113, 0.0024999999999999983], "shape": [1107, 1194], "tiled": true, "transform": [0.0025000000000000113, 0.0, 143.22468138068513, 0.0, -0.0024999999999999983, -31.952500412690732, 0.0, 0.0, 1.0], "units": [null, null, null, null, null, null, null, null, null, null, null], "width": 1194}

Is this user error (do I need to pass a crs string in?) or perhaps a bug?

Regards
Luke



Sean Gillies
 

Hi Luke,

Unfortunately, I cannot reproduce your results on my computer.

$ python
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import rasterio
>>> with rasterio.open('tests/data/RGB.byte.tif') as src:
...     with rasterio.open('/tmp/foo.tif', 'w', **src.meta) as dst:
...         dst.write(src.read())
...
>>> with rasterio.open('/tmp/foo.tif') as src:
...     print(src.meta)
...     print(src.profile)
...
{'driver': 'GTiff', 'dtype': 'uint8', 'nodata': 0.0, 'width': 791, 'height': 718, 'count': 3, 'crs': CRS({'init': 'epsg:32618'}), 'transform': Affine(300.0379266750948, 0.0, 101985.0,
       0.0, -300.041782729805, 2826915.0)}
{'driver': 'GTiff', 'dtype': 'uint8', 'nodata': 0.0, 'width': 791, 'height': 718, 'count': 3, 'crs': CRS({'init': 'epsg:32618'}), 'transform': Affine(300.0379266750948, 0.0, 101985.0,
       0.0, -300.041782729805, 2826915.0), 'tiled': False, 'interleave': 'pixel'}

I wonder if you may have a problem with unresolved Proj data. To debug this, turn on Python's logging facility and set the level to DEBUG at the top of your code like so:

import logging
logging.basicConfig(level=logging.DEBUG)

On Thu, Jul 19, 2018 at 11:24 PM, Luke Pinner <lukepinnerau@...> wrote:
Hi all.  Congrats on the 1.0.0 release!  

Using rasterio 1.0.0 (installed from conda-forge in a fresh conda env). I'm having trouble copying the CRS from on dataset to another.  The output dataset CRS is None.

Simplified example demonstrating:

import rasterio as rio

inpath = r"test.tif"
outpath = r"testout.tif"

with rio.open(inpath) as inds:
    print(repr(inds.crs), inds.meta['crs'])
    with rio.open(outpath, 'w', **inds.meta) as outds:
        outds.write(inds.read())

with rio.open(outpath) as outds:
    print(repr(outds.crs), outds.meta['crs'])

The output of the above is:

CRS({'init': 'epsg:4326'}) +init=epsg:4326
None None

Dataset info:

rio info test.tif
{"blockxsize": 128, "blockysize": 128, "bounds": [143.22468138068513, -34.72000041269073, 146.20968138068514, -31.952500412690732], "colorinterp": ["grey", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"], "count": 11, "crs": "EPSG:4326", "descriptions": [null, null, null, null, null, null, null, null, null, null, null], "driver": "GTiff", "dtype": "float32", "height": 1107, "indexes": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], "interleave": "pixel", "lnglat": [144.71718138068513, -33.33625041269073], "mask_flags": [["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"], ["nodata"]], "nodata": -9999.0, "res": [0.0025000000000000113, 0.0024999999999999983], "shape": [1107, 1194], "tiled": true, "transform": [0.0025000000000000113, 0.0, 143.22468138068513, 0.0, -0.0024999999999999983, -31.952500412690732, 0.0, 0.0, 1.0], "units": [null, null, null, null, null, null, null, null, null, null, null], "width": 1194}

Is this user error (do I need to pass a crs string in?) or perhaps a bug?

Regards
Luke






--
Sean Gillies

Luke Pinner
 

My GDAL_DATA env var wasn't set.

Explanation:
I was running the code within the PyCharm IDE that supposedly supports conda envs but doesn't actually *activate* them properly.  There are init scripts in CONDA_PREFIX/etc/conda/activate.d  that get run when you activate/source activate a conda env that do some additional environment setup and there's a gdal one that should've been run but wasn't.

Apologies for the noise. 

Luke