Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix geotiff writer being unimportable if gdal isn't installed #616

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions satpy/writers/geotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,23 @@

import dask
import numpy as np
from osgeo import gdal, osr

from satpy.utils import ensure_dir
from satpy.writers import ImageWriter

LOG = logging.getLogger(__name__)

try:
import rasterio
gdal = osr = None
except ImportError as r_exc:
try:
# fallback to legacy gdal writer
from osgeo import gdal, osr
rasterio = None
except ImportError:
# raise the original rasterio exception
raise r_exc

# Map numpy data types to GDAL data types
NP2GDAL = {
np.float32: gdal.GDT_Float32,
np.float64: gdal.GDT_Float64,
np.uint8: gdal.GDT_Byte,
np.uint16: gdal.GDT_UInt16,
np.uint32: gdal.GDT_UInt32,
np.int16: gdal.GDT_Int16,
np.int32: gdal.GDT_Int32,
np.complex64: gdal.GDT_CFloat32,
np.complex128: gdal.GDT_CFloat64,
}
LOG = logging.getLogger(__name__)


class GeoTIFFWriter(ImageWriter):
Expand Down Expand Up @@ -256,6 +253,20 @@ def save_image(self, img, filename=None, dtype=None, fill_value=None,
"This will be deprecated in the future. Install "
"'rasterio' for faster saving and future "
"compatibility.", PendingDeprecationWarning)

# Map numpy data types to GDAL data types
NP2GDAL = {
np.float32: gdal.GDT_Float32,
np.float64: gdal.GDT_Float64,
np.uint8: gdal.GDT_Byte,
np.uint16: gdal.GDT_UInt16,
np.uint32: gdal.GDT_UInt32,
np.int16: gdal.GDT_Int16,
np.int32: gdal.GDT_Int32,
np.complex64: gdal.GDT_CFloat32,
np.complex128: gdal.GDT_CFloat64,
}

# force to numpy dtype object
dtype = np.dtype(dtype)
gformat = NP2GDAL[dtype.type]
Expand Down