Repository: rasterio/rasterio

Issue:
Can't unset crs metadata values
Using the cli:
```bash
# crs is defined
$ rio info tests/data/alpha.tif | jq '.crs'
"EPSG:3857"

# attempt to unset crs
$ rio edit-info tests/data/alpha.tif --crs ""

# but
$ rio info tests/data/alpha.tif | jq '.crs'
"EPSG:3857"
```

Per @perrygeo - the python version of this is simple:
```python
import rasterio
from rasterio.crs import CRS

raster = 'test/prep_test/de_hamburg_custom_srs/fixtures/dop20c_32587_5922.tif'

with rasterio.open(raster, 'r+') as src:
    src.crs = CRS()
```

Reminds me of https://github.com/mapbox/rasterio/issues/328.


Discussion:

Resolving patch:
diff --git a/rasterio/rio/edit_info.py b/rasterio/rio/edit_info.py
--- a/rasterio/rio/edit_info.py
+++ b/rasterio/rio/edit_info.py
@@ -2,12 +2,12 @@
 
 
 import json
+import warnings
 
 import click
 
 import rasterio
 import rasterio.crs
-from rasterio.compat import string_types
 from rasterio.crs import CRS
 from rasterio.errors import CRSError
 from rasterio.rio import options
@@ -82,6 +82,8 @@ def transform_handler(ctx, param, value):
               help="Unset the dataset's nodata value.")
 @click.option('--crs', callback=crs_handler, default=None,
               help="New coordinate reference system")
+@click.option('--unset-crs', default=False, is_flag=True,
+              help="Unset the dataset's CRS value.")
 @click.option('--transform', callback=transform_handler,
               help="New affine transform matrix")
 @click.option('--units', help="Edit units of a band (requires --bidx)")
@@ -94,8 +96,8 @@ def transform_handler(ctx, param, value):
               help="Copy all metadata items from the template file.")
 @options.like_opt
 @click.pass_context
-def edit(ctx, input, bidx, nodata, unset_nodata, crs, transform, units,
-         description, tags, allmd, like):
+def edit(ctx, input, bidx, nodata, unset_nodata, crs, unset_crs, transform,
+         units, description, tags, allmd, like):
     """Edit a dataset's metadata: coordinate reference system, affine
     transformation matrix, nodata value, and tags.
 
@@ -146,6 +148,10 @@ def in_dtype_range(value, dtype):
             raise click.BadParameter(
                 "--unset-nodata and --nodata cannot be used together.")
 
+        if unset_crs and crs:
+            raise click.BadParameter(
+                "--unset-crs and --crs cannot be used together.")
+
         if unset_nodata:
             # Setting nodata to None will raise NotImplementedError
             # if GDALDeleteRasterNoDataValue() isn't present in the
@@ -164,7 +170,9 @@ def in_dtype_range(value, dtype):
                     param=nodata, param_hint='nodata')
             dst.nodata = nodata
 
-        if crs:
+        if unset_crs:
+            dst.crs = CRS()
+        elif crs:
             dst.crs = crs
 
         if transform:
@@ -178,3 +186,11 @@ def in_dtype_range(value, dtype):
 
         if description:
             dst.set_description(bidx, description)
+
+    # Post check - ensure that crs was unset properly
+    if unset_crs:
+        with ctx.obj['env'], rasterio.open(input, 'r') as src:
+            if dict(src.crs) != {}:
+                warnings.warn(
+                    'CRS was not unset. Availability of his functionality '
+                    'differs depending on GDAL version and driver')


Test patch:
diff --git a/tests/test_rio_info.py b/tests/test_rio_info.py
--- a/tests/test_rio_info.py
+++ b/tests/test_rio_info.py
@@ -25,6 +25,45 @@ def test_delete_nodata_exclusive_opts(data):
     assert result.exit_code == 2
 
 
+def test_delete_crs_exclusive_opts(data):
+    """--unset-crs and --crs can't be used together"""
+    runner = CliRunner()
+    inputfile = str(data.join('RGB.byte.tif'))
+    result = runner.invoke(
+        main_group, ['edit-info', inputfile, '--unset-crs', '--crs', 'epsg:4326'])
+    assert result.exit_code == 2
+
+
+@pytest.mark.xfail(
+    Version(rasterio.__gdal_version__) < Version('1.10'),
+    reason='GDAL version >= 1.10 required')
+def test_unset_crs(data):
+    runner = CliRunner()
+    inputfile = str(data.join('RGB.byte.tif'))
+    result = runner.invoke(main_group,
+                           ['edit-info', inputfile, '--unset-crs'])
+    assert result.exit_code == 0
+    with rasterio.open(inputfile) as src:
+        assert dict(src.crs) == {}
+
+@pytest.mark.skip(
+    Version(rasterio.__gdal_version__) >= Version('1.10'),
+    reason='Test applies to GDAL version < 1.10')
+def test_unset_crs_gdal19(data):
+    """unsetting crs doesn't work for geotiff and gdal 1.9
+    and should emit an warning"""
+    runner = CliRunner()
+    inputfile = str(data.join('RGB.byte.tif'))
+    with rasterio.open(inputfile) as src:
+        orig_crs = src.crs
+    with pytest.warns(UserWarning):
+        result = runner.invoke(main_group,
+                            ['edit-info', inputfile, '--unset-crs'])
+    assert result.exit_code == 0
+    with rasterio.open(inputfile) as src:
+        assert src.crs == orig_crs  # nochange
+
+
 def test_edit_nodata_err(data):
     runner = CliRunner()
     inputfile = str(data.join('RGB.byte.tif'))