14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
+ import pdb
18
+
17
19
import cPickle as mod_pickle
18
20
import os as mod_os
19
21
import os .path as mod_path
20
22
21
23
from . import data as mod_data
22
24
from . import retriever as mod_retriever
23
25
24
- # Local cache path:
25
- LOCAL_FILES_DIRECTORY = '{0}/.geo_elevation_files' . format ( mod_os . environ [ 'HOME' ])
26
+ SRTM1_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM1/'
27
+ SRTM3_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/'
26
28
27
- if not mod_path .exists (LOCAL_FILES_DIRECTORY ):
28
- mod_os .makedirs (LOCAL_FILES_DIRECTORY )
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' )
29
34
30
- FILES_LOCATION = '{0}/list ' .format (LOCAL_FILES_DIRECTORY )
35
+ result = '{0}/.srtm ' .format (mod_os . environ [ 'HOME' ] )
31
36
32
- SRTM1_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM1/'
33
- SRTM3_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/'
37
+ if not mod_path . exists ( result ):
38
+ mod_os . makedirs ( result )
34
39
35
- def get_data ():
40
+ return result
41
+
42
+ def get_data (local_srtm_dir = None ):
36
43
"""
37
- Load the urls of all geo-elevation files. If it can't be found -- it wil
38
- retrieve it and save to LOCAL_FILES_DIRECTORY.
44
+ Get the utility object for querying elevation data.
45
+
46
+ All data files will be stored in local_srtm_dir (note that it may be
47
+ gigabytes of data so clean it from time to time).
48
+
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.
39
52
"""
53
+ if not local_srtm_dir :
54
+ local_srtm_dir = get_default_srtm_dir ()
55
+
56
+ if not local_srtm_dir :
57
+ raise Error ('Please specify a path where to store files' )
58
+
59
+ files_list_file_name = '{0}/list' .format (local_srtm_dir )
40
60
try :
41
- f = open (FILES_LOCATION , 'r' )
61
+ f = open (files_list_file_name , 'r' )
42
62
contents = f .read ()
43
63
f .close ()
44
64
@@ -50,11 +70,11 @@ def get_data():
50
70
srtm1_files = mod_retriever .retrieve_all_files_urls (SRTM1_URL )
51
71
srtm3_files = mod_retriever .retrieve_all_files_urls (SRTM3_URL )
52
72
53
- f = open (FILES_LOCATION , 'w' )
73
+ f = open (files_list_file_name , 'w' )
54
74
f .write (mod_pickle .dumps ({'srtm1' : srtm1_files , 'srtm3' : srtm3_files }))
55
75
f .close ()
56
76
57
77
assert srtm1_files
58
78
assert srtm3_files
59
79
60
- return mod_data .GeoElevationData (srtm1_files , srtm3_files , LOCAL_FILES_DIRECTORY )
80
+ return mod_data .GeoElevationData (srtm1_files , srtm3_files , local_srtm_dir )
0 commit comments