Skip to content

Commit c425423

Browse files
committed
local files stored in custom directory
1 parent 75b1a39 commit c425423

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

srtm/__init__.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,51 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import pdb
18+
1719
import cPickle as mod_pickle
1820
import os as mod_os
1921
import os.path as mod_path
2022

2123
from . import data as mod_data
2224
from . import retriever as mod_retriever
2325

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/'
2628

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')
2934

30-
FILES_LOCATION = '{0}/list'.format(LOCAL_FILES_DIRECTORY)
35+
result = '{0}/.srtm'.format(mod_os.environ['HOME'])
3136

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)
3439

35-
def get_data():
40+
return result
41+
42+
def get_data(local_srtm_dir=None):
3643
"""
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.
3952
"""
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)
4060
try:
41-
f = open(FILES_LOCATION, 'r')
61+
f = open(files_list_file_name, 'r')
4262
contents = f.read()
4363
f.close()
4464

@@ -50,11 +70,11 @@ def get_data():
5070
srtm1_files = mod_retriever.retrieve_all_files_urls(SRTM1_URL)
5171
srtm3_files = mod_retriever.retrieve_all_files_urls(SRTM3_URL)
5272

53-
f = open(FILES_LOCATION, 'w')
73+
f = open(files_list_file_name, 'w')
5474
f.write(mod_pickle.dumps({'srtm1': srtm1_files, 'srtm3': srtm3_files}))
5575
f.close()
5676

5777
assert srtm1_files
5878
assert srtm3_files
5979

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

Comments
 (0)