3
3
"""
4
4
5
5
import os
6
+ import sys
6
7
import logging
7
8
import pickle
8
9
import gzip
9
10
import shutil
10
11
import warnings
11
12
import copy
12
13
import datetime
14
+ import pickletools
15
+
13
16
import attr
14
17
import dateutil .parser
15
18
import numpy as np
20
23
from .constants import FeatureType , FileFormat , OverwritePermission
21
24
from .utilities import deep_eq , FeatureParser
22
25
26
+ # pylint: disable=too-many-lines
23
27
LOGGER = logging .getLogger (__name__ )
24
28
25
29
MAX_DATA_REPR_LEN = 100
26
30
27
31
32
+ if sentinelhub .__version__ >= '2.5.0' :
33
+ sys .modules ['sentinelhub.common' ] = sentinelhub .geometry
34
+
35
+
28
36
@attr .s (repr = False , cmp = False , kw_only = True )
29
37
class EOPatch :
30
38
"""The basic data object for multi-temporal remotely sensed data, such as satellite imagery and its derivatives.
@@ -875,9 +883,38 @@ def get_file_path(self):
875
883
"""
876
884
return os .path .join (self .patch_path , self .filename )
877
885
886
+ @staticmethod
887
+ def _correctly_load_bbox (bbox , path , is_zipped = False ):
888
+ """ Helper method for loading old version of pickled BBox object
889
+
890
+ :param bbox: BBox object which was incorrectly loaded with pickle
891
+ :type bbox: sentinelhub.BBox
892
+ :param path: Path to file where BBox object is stored
893
+ :type path: str
894
+ :param is_zipped: `True` if file is zipped and `False` otherwise
895
+ :type is_zipped: bool
896
+ :return: Correctly loaded BBox object
897
+ :rtype: sentinelhub.BBox
898
+ """
899
+ warnings .warn ("Bounding box of your EOPatch is saved in old format which in the future won't be supported "
900
+ "anymore. Please save bounding box again, you can overwrite the existing one" , DeprecationWarning ,
901
+ stacklevel = 4 )
902
+
903
+ with open (gzip .open (path ) if is_zipped else path , 'rb' ) as pickle_file :
904
+ crs_cnt = - 1
905
+ for _ , arg , _ in pickletools .genops (pickle_file ):
906
+ if arg == 'sentinelhub.constants CRS' :
907
+ crs_cnt = 2
908
+ if crs_cnt == 0 :
909
+ return sentinelhub .BBox (tuple (bbox ), sentinelhub .CRS (arg ))
910
+ crs_cnt -= 1
911
+
912
+ raise ValueError ('Failed to correctly load BBox object, try downgrading sentinelhub package to <=2.4.7' )
913
+
878
914
def load (self ):
879
915
""" Method which loads data from the file
880
916
"""
917
+ # pylint: disable=too-many-return-statements
881
918
if not os .path .isdir (self .patch_path ):
882
919
raise OSError ('EOPatch does not exist in path {} anymore' .format (self .patch_path ))
883
920
@@ -889,7 +926,11 @@ def load(self):
889
926
890
927
if not file_formats or file_formats [- 1 ] is FileFormat .PICKLE :
891
928
with open (path , "rb" ) as infile :
892
- return pickle .load (infile )
929
+ data = pickle .load (infile )
930
+
931
+ if isinstance (data , sentinelhub .BBox ) and not hasattr (data , 'crs' ):
932
+ return self ._correctly_load_bbox (data , path )
933
+ return data
893
934
894
935
if file_formats [- 1 ] is FileFormat .NPY :
895
936
if self .mmap :
@@ -901,7 +942,11 @@ def load(self):
901
942
return np .load (gzip .open (path ))
902
943
903
944
if len (file_formats ) == 1 or file_formats [- 2 ] is FileFormat .PICKLE :
904
- return pickle .load (gzip .open (path ))
945
+ data = pickle .load (gzip .open (path ))
946
+
947
+ if isinstance (data , sentinelhub .BBox ) and not hasattr (data , 'crs' ):
948
+ return self ._correctly_load_bbox (data , path , is_zipped = True )
949
+ return data
905
950
906
951
raise ValueError ('Could not load data from unsupported file format {}' .format (file_formats [- 1 ]))
907
952
0 commit comments