21
21
import os .path as mod_path
22
22
23
23
from . import data as mod_data
24
+ from . import utils as mod_utils
24
25
from . import retriever as mod_retriever
25
26
26
27
SRTM1_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM1/'
27
28
SRTM3_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/'
28
29
29
- def get_default_srtm_dir ():
30
- """ The default path to store files. """
31
- # Local cache path:
32
- if not mod_os .environ .has_key ('HOME' ):
33
- raise Error ('No default HOME directory found, please specify a path where to store files' )
34
-
35
- result = '{0}/.srtm' .format (mod_os .environ ['HOME' ])
36
-
37
- if not mod_path .exists (result ):
38
- mod_os .makedirs (result )
39
-
40
- return result
41
-
42
- def get_data (local_srtm_dir = None , reduce_big_files = False , leave_zipped = False ):
30
+ def get_data (reduce_big_files = False , leave_zipped = False , file_handler = None ):
43
31
"""
44
32
Get the utility object for querying elevation data.
45
33
46
- All data files will be stored in local_srtm_dir (note that it may be
34
+ All data files will be stored in localy (note that it may be
47
35
gigabytes of data so clean it from time to time).
48
36
49
- On first run -- all files url will be stored and for every next elevation
50
- query if the SRTM file is not found in local_srtm_dir it will be retrieved
51
- and saved.
37
+ On first run -- all files needed url will be stored and for every next
38
+ elevation query if the SRTM file is not found it will be retrieved and
39
+ saved.
40
+
41
+ If you need to change the way the files are saved locally (for example if
42
+ you need to save them locally) -- change the file_handler. See
43
+ srtm.utils.FileHandler.
52
44
53
45
If reduce_big_files is True files for north America will be reduced to the
54
46
same size as the rest of the world. It may be used to save disk space
@@ -60,17 +52,12 @@ def get_data(local_srtm_dir=None, reduce_big_files=False, leave_zipped=False):
60
52
file loaded. Note that if you leave this to True and don't reduce big
61
53
files -- unzipping them on runtime will be slow.
62
54
"""
63
- if not local_srtm_dir :
64
- local_srtm_dir = get_default_srtm_dir ()
65
-
66
- if not local_srtm_dir :
67
- raise Error ('Please specify a path where to store files' )
55
+ if not file_handler :
56
+ file_handler = FileHandler ()
68
57
69
- files_list_file_name = '{0}/ list.json' . format ( local_srtm_dir )
58
+ files_list_file_name = 'list.json'
70
59
try :
71
- f = open (files_list_file_name , 'r' )
72
- contents = f .read ()
73
- f .close ()
60
+ contents = file_handler .read (files_list_file_name )
74
61
75
62
urls = mod_json .loads (contents )
76
63
@@ -80,14 +67,42 @@ def get_data(local_srtm_dir=None, reduce_big_files=False, leave_zipped=False):
80
67
srtm1_files = mod_retriever .retrieve_all_files_urls (SRTM1_URL )
81
68
srtm3_files = mod_retriever .retrieve_all_files_urls (SRTM3_URL )
82
69
83
- f = open (files_list_file_name , 'w' )
84
- f .write (mod_json .dumps ({'srtm1' : srtm1_files , 'srtm3' : srtm3_files },
85
- sort_keys = True , indent = 4 ))
86
- f .close ()
70
+ file_handler .write (files_list_file_name ,
71
+ mod_json .dumps ({'srtm1' : srtm1_files , 'srtm3' : srtm3_files }, sort_keys = True , indent = 4 ))
87
72
88
73
assert srtm1_files
89
74
assert srtm3_files
90
75
91
- return mod_data .GeoElevationData (srtm1_files , srtm3_files , local_srtm_dir ,
76
+ return mod_data .GeoElevationData (srtm1_files , srtm3_files , file_handler = file_handler ,
92
77
reduce_big_files = reduce_big_files ,
93
78
leave_zipped = leave_zipped )
79
+
80
+ class FileHandler :
81
+ """
82
+ The default file handler. It can be changed if you need to save/read SRTM
83
+ files in a database or Amazon S3.
84
+ """
85
+
86
+ def get_srtm_dir (self ):
87
+ """ The default path to store files. """
88
+ # Local cache path:
89
+ if not mod_os .environ .has_key ('HOME' ):
90
+ raise Error ('No default HOME directory found, please specify a path where to store files' )
91
+
92
+ result = '{0}/.srtm' .format (mod_os .environ ['HOME' ])
93
+
94
+ if not mod_path .exists (result ):
95
+ mod_os .makedirs (result )
96
+
97
+ return result
98
+
99
+ def exists (self , file_name ):
100
+ return mod_path .exists ('%s/%s' % (self .get_srtm_dir (), file_name ))
101
+
102
+ def write (self , file_name , contents ):
103
+ with open ('%s/%s' % (self .get_srtm_dir (), file_name ), 'w' ) as f :
104
+ f .write (contents )
105
+
106
+ def read (self , file_name ):
107
+ with open ('%s/%s' % (self .get_srtm_dir (), file_name ), 'r' ) as f :
108
+ return f .read ()
0 commit comments