I am trying to re-projected a .tiff file to EPSG:32638 with the following code.
Versions I have installed:
Rasterio version 1.1.5
Numpy version 1.18.1
This is the code I am using:https://rasterio.readthedocs.io/en/latest/topics/reproject.html
(Reprojecting a GeoTIFF dataset)
Error:
—————————————————————————
CPLE_BaseError Traceback (most recent call last)
rasterio/_crs.pyx in rasterio._crs._CRS.from_user_input()
rasterio/_err.pyx in rasterio._err.exc_wrap_ogrerr()
CPLE_BaseError: OGR Error code 6
During handling of the above exception, another exception occurred:
CRSError Traceback (most recent call last)
7 with rasterio.open(‘/home/DATA/Test.tif’) as src:
8 transform, width, height = calculate_default_transform(
—-> 9 src.crs, dst_crs, src.width, src.height, *src.bounds)
10 kwargs = src.meta.copy()
11 kwargs.update({
~/anaconda3/envs/elsa/lib/python3.7/site-packages/rasterio/env.py in wrapper(*args, **kwds)
380 def wrapper(*args, **kwds):
381 if local._env:
–> 382 return f(*args, **kwds)
383 else:
384 with Env.from_defaults():
~/anaconda3/envs/elsa/lib/python3.7/site-packages/rasterio/warp.py in calculate_default_transform(src_crs, dst_crs, width, height, left, bottom, right, top, gcps, resolution, dst_width, dst_height)
468
469 dst_affine, dst_width, dst_height = _calculate_default_transform(
–> 470 src_crs, dst_crs, width, height, left, bottom, right, top, gcps
471 )
472
rasterio/_warp.pyx in rasterio._warp._calculate_default_transform()
rasterio/_base.pyx in rasterio._base._osr_from_crs()
~/anaconda3/envs/elsa/lib/python3.7/site-packages/rasterio/crs.py in from_user_input(cls, value, morph_from_esri_dialect)
457 elif isinstance(value, string_types):
458 obj = cls()
–> 459 obj._crs = _CRS.from_user_input(value, morph_from_esri_dialect=morph_from_esri_dialect)
460 return obj
461 else:
rasterio/_crs.pyx in rasterio._crs._CRS.from_user_input()
CRSError: The WKT could not be parsed. OGR Error code 6
Okay, I\’ve analyzed the error and the code you\’re using. The core issue is that Rasterio is having trouble interpreting the CRS (Coordinate Reference System) you\’re providing, specifically the destination CRS (EPSG:32638). The error \”OGR Error code 6\” and \”The WKT could not be parsed\” indicates a problem with how the CRS is being defined or accessed within the Rasterio environment.\n\nHere\’s a breakdown of the problem and a comprehensive solution:\n\n**Understanding the Problem**\n\n* **CRS Definition:** The `dst_crs` variable in your code likely holds a string representation of the target CRS (EPSG:32638). Rasterio uses the OGR library (part of GDAL) to parse this string and understand the coordinate system. The error suggests that the string is either invalid, malformed, or not recognized by OGR.\n* **GDAL/PROJ Database:** Rasterio relies on GDAL and its underlying PROJ library to handle CRS transformations. If the EPSG code (32638) is missing from the PROJ database on your system or if the database is corrupted, Rasterio won\’t be able to resolve the CRS definition.\n* **String Format:** While EPSG codes are commonly used, sometimes the exact string representation matters. Ambiguity in how the CRS is defined (e.g., \”+init=epsg:32638\” vs \”EPSG:32638\”) can cause parsing failures.\n* **Rasterio and GDAL version compatibility**: There can sometimes be issues between the versions of Rasterio and GDAL.\n\n**Solutions**\n\nHere\’s a prioritized list of solutions, from most likely to fix the problem to less common causes:\n\n1. **Explicitly Define CRS with `rasterio.crs.CRS.from_epsg()`:** This is the recommended and most robust way to define a CRS in Rasterio. It directly uses the EPSG code to create the CRS object, avoiding potential string parsing issues.\n\n “`python\n import rasterio\n from rasterio.warp import calculate_default_transform, reproject, Resampling\n from rasterio.crs import CRS # Import CRS\n\n # Input and output file paths\n src_filename = \’/home/DATA/Test.tif\’\n dst_filename = \’/home/DATA/Test_reprojected.tif\’\n\n # Define the destination CRS using rasterio.crs.CRS.from_epsg()\n dst_crs = CRS.from_epsg(32638)\n\n with rasterio.open(src_filename) as src:\n transform, width, height = calculate_default_transform(\n src.crs, dst_crs, src.width, src.height, *src.bounds)\n\n kwargs = src.meta.copy()\n kwargs.update({\n \’crs\’: dst_crs,\n \’transform\’: transform,\n \’width\’: width,\n \’height\’: height\n })\n\n with rasterio.open(dst_filename, \’w\’, **kwargs) as dst:\n for i in range(1, src.count + 1):\n reproject(\n source=rasterio.band(src, i),\n destination=rasterio.band(dst, i),\n src_transform=src.transform,\n src_crs=src.crs,\n dst_transform=transform,\n dst_crs=dst_crs,\n resampling=Resampling.nearest)\n\n print(f\”Reprojected image saved to: {dst_filename}\”)\n “`\n\n2. **Verify GDAL and PROJ Installation and EPSG Database:** Ensure that GDAL and PROJ are correctly installed and configured on your system. Specifically, check that the PROJ database (which contains the EPSG definitions) is accessible. The location of this database is often defined by the `PROJ_LIB` environment variable.\n\n * **Check GDAL Version:** In your terminal, run `gdal-config –version`. This will show you the GDAL version. If it\’s very old (e.g., older than GDAL 2.x), consider upgrading.\n\n * **Check PROJ_LIB:** In your terminal:\n * Linux/macOS: `echo $PROJ_LIB`\n * Windows: `echo %PROJ_LIB%`\n\n If the output is empty, you need to set the `PROJ_LIB` environment variable to the directory containing the `proj.db` file. The location of this file depends on how GDAL/PROJ was installed. A common location is `/usr/share/proj`.\n\n **Setting PROJ_LIB (Example – adapt to your system):**\n “`bash\n export PROJ_LIB=/usr/share/proj # Example – adjust the path as needed\n “`\n You might need to add this line to your `.bashrc` or `.zshrc` file to make it permanent. On Windows, you\’ll need to set the environment variable through the System Properties.\n\n3. **Upgrade Rasterio and GDAL:** You are using an older version of Rasterio (1.1.5). Consider upgrading to the latest version, as well as upgrading GDAL. Newer versions often include bug fixes and improved CRS handling.\n\n “`bash\n pip install -U rasterio\n conda update -c conda-forge gdal #If you are using conda\n “`\n\n After upgrading, restart your Python environment (e.g., close and reopen your Jupyter Notebook or terminal) to ensure the new versions are loaded.\n\n4. **String CRS Definition (Less Recommended, but sometimes necessary):** If the above solutions don\’t work, try explicitly defining the CRS as a string, but be very careful with the format. Try these variations:\n\n * `dst_crs = \”EPSG:32638\”`\n * `dst_crs = \”+init=epsg:32638\”` (This format is sometimes needed, but less preferred)\n\n Use this *only* if the `CRS.from_epsg()` method fails.\n\n5. **Check Input GeoTIFF:** Verify that the input GeoTIFF file (`/home/DATA/Test.tif`) has a valid and defined CRS. You can use `gdalinfo` to check this:\n\n “`bash\n gdalinfo /home/DATA/Test.tif\n “`\n\n Look for the `CRS` or `SRS` section in the output. If the input file doesn\’t have a CRS defined, you\’ll need to assign one before reprojecting.\n\n6. **Simplify the Reprojection (Debugging):** As a test, try a very simple reprojection:\n\n “`python\n import rasterio\n from rasterio.warp import reproject, Resampling\n from rasterio.crs import CRS\n\n src_filename = \’/home/DATA/Test.tif\’\n dst_filename = \’/home/DATA/Test_reprojected.tif\’\n dst_crs = CRS.from_epsg(32638)\n\n with rasterio.open(src_filename) as src:\n kwargs = src.meta.copy()\n kwargs.update({\n \’crs\’: dst_crs\n })\n\n with rasterio.open(dst_filename, \’w\’, **kwargs) as dst:\n for i in range(1, src.count + 1):\n reproject(\n source=rasterio.band(src, i),\n destination=rasterio.band(dst, i),\n src_crs=src.crs,\n dst_crs=dst_crs,\n resampling=Resampling.nearest)\n\n print(f\”Reprojected image saved to: {dst_filename}\”)\n “`\n\n This simplified version removes the `calculate_default_transform` function. If this works, the problem is likely with how you are calculating the transform. If it still fails, the issue is definitely with the CRS definition or GDAL/PROJ setup.\n\n**Troubleshooting Steps**\n\n1. **Start with Solution #1 (using `CRS.from_epsg()` ).** This is the most likely fix.\n2. If that doesn\’t work, move on to checking your GDAL/PROJ installation and environment variables.\n3. Upgrade Rasterio and GDAL *after* you\’ve confirmed your environment is set up correctly. Upgrading is a good practice, but it won\’t solve the problem if the underlying issue is a misconfigured GDAL/PROJ setup.\n4. If all else fails, examine your input GeoTIFF file with `gdalinfo` to ensure it has a valid CRS defined.\n\nBy systematically working through these solutions, you should be able to resolve the \”OGR Error code 6\” and successfully reproject your GeoTIFF file. Remember to restart your Python environment after making changes to environment variables or installing new packages.\n