Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cebfee

Browse files
committed
Added FileHandler
1 parent c4ffba7 commit 9cebfee

File tree

3 files changed

+64
-53
lines changed

3 files changed

+64
-53
lines changed

srtm/__init__.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,26 @@
2121
import os.path as mod_path
2222

2323
from . import data as mod_data
24+
from . import utils as mod_utils
2425
from . import retriever as mod_retriever
2526

2627
SRTM1_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM1/'
2728
SRTM3_URL = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/'
2829

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):
4331
"""
4432
Get the utility object for querying elevation data.
4533
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
4735
gigabytes of data so clean it from time to time).
4836
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.
5244
5345
If reduce_big_files is True files for north America will be reduced to the
5446
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):
6052
file loaded. Note that if you leave this to True and don't reduce big
6153
files -- unzipping them on runtime will be slow.
6254
"""
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()
6857

69-
files_list_file_name = '{0}/list.json'.format(local_srtm_dir)
58+
files_list_file_name = 'list.json'
7059
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)
7461

7562
urls = mod_json.loads(contents)
7663

@@ -80,14 +67,42 @@ def get_data(local_srtm_dir=None, reduce_big_files=False, leave_zipped=False):
8067
srtm1_files = mod_retriever.retrieve_all_files_urls(SRTM1_URL)
8168
srtm3_files = mod_retriever.retrieve_all_files_urls(SRTM3_URL)
8269

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

8873
assert srtm1_files
8974
assert srtm3_files
9075

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,
9277
reduce_big_files=reduce_big_files,
9378
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()

srtm/data.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ class GeoElevationData:
4343
# Lazy loaded files used in current app:
4444
files = None
4545

46-
def __init__(self, srtm1_files, srtm3_files, files_directory,
47-
reduce_big_files=False, leave_zipped=False):
46+
def __init__(self, srtm1_files, srtm3_files, reduce_big_files=False,
47+
leave_zipped=False, file_handler=None):
4848
self.srtm1_files = srtm1_files
4949
self.srtm3_files = srtm3_files
50-
# Place where local files are stored:
51-
self.files_directory = files_directory
5250

5351
self.reduce_big_files = reduce_big_files
5452
self.leave_zipped = leave_zipped
5553

54+
self.file_handler = file_handler if file_handler else mod_utils.FileHandler()
55+
5656
self.files = {}
5757

5858
def get_elevation(self, latitude, longitude, approximate=None):
@@ -90,16 +90,14 @@ def get_file(self, latitude, longitude):
9090
return result
9191

9292
def retrieve_or_load_file_data(self, file_name):
93-
data_file_name = '{0}/{1}'.format(self.files_directory, file_name)
94-
zip_data_file_name = '{0}/{1}.zip'.format(self.files_directory, file_name)
93+
data_file_name = file_name
94+
zip_data_file_name = '{0}.zip'.format(file_name)
9595

9696
if mod_path.exists(data_file_name):
97-
with open(data_file_name) as f:
98-
return f.read()
97+
return self.file_handler.read(data_file_name)
9998
elif mod_path.exists(zip_data_file_name):
100-
with open(zip_data_file_name) as f:
101-
data = f.read()
102-
return mod_utils.unzip(data)
99+
data = self.file_handler.read(zip_data_file_name)
100+
return mod_utils.unzip(data)
103101

104102
url = None
105103

@@ -129,13 +127,11 @@ def retrieve_or_load_file_data(self, file_name):
129127
data = mod_utils.zip(data, file_name)
130128

131129
if self.leave_zipped:
132-
with open(data_file_name + '.zip', 'w') as f:
133-
f.write(data)
134-
data = mod_utils.unzip(data)
130+
self.file_handler.write(data_file_name + '.zip', data)
131+
data = mod_utils.unzip(data)
135132
else:
136-
with open(data_file_name, 'w') as f:
137-
data = mod_utils.unzip(data)
138-
f.write(data)
133+
data = mod_utils.unzip(data)
134+
self.file_handler.write(data_file_name, data)
139135

140136
return data
141137

srtm/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
import pdb
1818

19-
import logging as mod_logging
20-
import math as mod_math
21-
import zipfile as mod_zipfile
22-
import cStringIO as mod_cstringio
19+
import logging as mod_logging
20+
import math as mod_math
21+
import zipfile as mod_zipfile
22+
import cStringIO as mod_cstringio
2323

2424
ONE_DEGREE = 1000. * 10000.8 / 90.
2525

0 commit comments

Comments
 (0)