Skip to content

Commit

Permalink
Merge pull request #616 from djhoese/bugfix-geotiff-writer-import
Browse files Browse the repository at this point in the history
Fix geotiff writer being unimportable if gdal isn't installed
  • Loading branch information
djhoese authored Feb 14, 2019
2 parents f55d08a + baf5f42 commit 85758ba
Showing 1 changed file with 26 additions and 15 deletions.
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

0 comments on commit 85758ba

Please sign in to comment.