Commit c5eb4d3c authored by Daniel Scheffler's avatar Daniel Scheffler

Replaced 'importlib.util.find_spec' with 'pkgutil.find_loader' to ensure...

Replaced 'importlib.util.find_spec' with 'pkgutil.find_loader' to ensure Python 2.7 compatibility. Added some type hints.
parent 29260096
Pipeline #3329 passed with stages
in 9 minutes and 2 seconds
......@@ -2,9 +2,10 @@
import os
import warnings
from importlib import util
from pkgutil import find_loader
from collections import OrderedDict
from copy import deepcopy
from typing import Union # noqa F401
import numpy as np
from matplotlib import pyplot as plt
......@@ -53,7 +54,7 @@ __author__ = 'Daniel Scheffler'
class GeoArray(object):
def __init__(self, path_or_array, geotransform=None, projection=None, bandnames=None, nodata=None, progress=True,
q=False):
# type: (any, tuple, str, list, float, bool, bool) -> None
# type: (Union[str, np.ndarray], tuple, str, list, float, bool, bool) -> None
"""This class creates a fast Python interface for geodata - either on disk or in memory. It can be instanced with
a file path or with a numpy array and the corresponding geoinformation. Instances can always be indexed like
normal numpy arrays, no matter if GeoArray has been instanced from file or from an in-memory array. GeoArray
......@@ -254,6 +255,7 @@ class GeoArray(object):
@geotransform.setter
def geotransform(self, gt):
# type: (Union[list, tuple]) -> None
assert isinstance(gt, (list, tuple)) and len(gt) == 6,\
'geotransform must be a list with 6 numbers. Got %s.' % str(gt)
......@@ -303,6 +305,7 @@ class GeoArray(object):
@projection.setter
def projection(self, prj):
# type: (str) -> None
if self.filePath:
assert self.projection is None or prj_equal(self.projection, prj), \
"Cannot set %s.projection to the given value because it does not match the projection from the file " \
......@@ -320,6 +323,7 @@ class GeoArray(object):
@epsg.setter
def epsg(self, epsg_code):
# type: (int) -> None
self.projection = EPSG2WKT(epsg_code)
@property
......@@ -354,6 +358,7 @@ class GeoArray(object):
@nodata.setter
def nodata(self, value):
# type: (Union[int, None]) -> None
self._nodata = value
@property
......@@ -1110,7 +1115,7 @@ class GeoArray(object):
palette.set_under('0')
# check availability of holoviews
if not util.find_spec('holoviews'):
if not find_loader('holoviews'):
warnings.warn("Interactive mode requires holoviews. Install it by running, e.g., "
"'conda install --yes -c ioam bokeh holoviews'. Using non-interactive mode.")
interactive = False
......@@ -1177,13 +1182,13 @@ class GeoArray(object):
:return:
"""
if not util.find_spec('mpl_toolkits.basemap'):
if not find_loader('mpl_toolkits.basemap'):
raise ImportError('This function requires Basemap. You need to install basemap manually (see www./'
'matplotlib.org/basemap) if you want to plot maps. It is not automatically installed.')
from mpl_toolkits.basemap import Basemap
mpld3_avl = util.find_spec('mpld3')
mpld3_avl = find_loader('mpld3')
if not mpld3_avl:
warnings.warn('mpld3 is not available. Zooming disabled.')
zoomable = False
......@@ -1265,7 +1270,7 @@ class GeoArray(object):
def show_map_utm(self, xlim=None, ylim=None, band=0, figsize=None, interpolation='none', cmap=None,
nodataVal=None, vmin=None, vmax=None, res_factor=None, return_map=False):
if not util.find_spec('mpl_toolkits.basemap'):
if not find_loader('mpl_toolkits.basemap'):
raise ImportError('This function requires Basemap. You need to install basemap manually (see www./'
'matplotlib.org/basemap) if you want to plot maps. It is not automatically installed.')
......@@ -1334,7 +1339,7 @@ class GeoArray(object):
footprint of GeoArray.
"""
if not util.find_spec('folium') or not util.find_spec('geojson'):
if not find_loader('folium') or not find_loader('geojson'):
raise ImportError(
"This method requires the libraries 'folium' and 'geojson'. They can be installed with "
"the shell command 'pip install folium geojson'.")
......
......@@ -3,7 +3,8 @@
from setuptools import setup, find_packages
import warnings
from importlib import util
from pkgutil import find_loader
with open('README.rst') as readme_file:
readme = readme_file.read()
......@@ -61,11 +62,11 @@ setup(
# check for holoviews
if not util.find_spec('holoviews'):
if not find_loader('holoviews'):
warnings.warn('You need to install holoviews manually (see www.holoviews.org) if you want to use interactive '
'plotting. It is not automatically installed.')
# check for basemap
if not util.find_spec('mpl_toolkits.basemap'):
if not find_loader('mpl_toolkits.basemap'):
warnings.warn('You need to install basemap manually (see www./matplotlib.org/basemap) if you want to plot maps. '
'It is not automatically installed.')
......@@ -34,7 +34,7 @@ import unittest
from unittest import TestLoader
import matplotlib
from typing import Iterable
from importlib import util
from pkgutil import find_loader
import tempfile
# Imports regarding the created python module.
......@@ -625,7 +625,7 @@ class Test_GeoarrayFunctions(unittest.TestCase):
self.testtiff.show()
# self.testtiff.show(interactive=True) # only works if test is started with ipython.
if util.find_spec('mpl_toolkits.basemap'):
if find_loader('mpl_toolkits.basemap'):
self.testtiff.show_map()
self.testtiff.show_map_utm() # Function still under construction.
self.testtiff.show_histogram()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment