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

Skip to content

Commit 20457c8

Browse files
committed
Testiranje
1 parent 5f63552 commit 20457c8

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

srtm/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_default_srtm_dir():
3939

4040
return result
4141

42-
def get_data(local_srtm_dir=None, reduce_big_files=False):
42+
def get_data(local_srtm_dir=None, reduce_big_files=False, leave_zipped=False):
4343
"""
4444
Get the utility object for querying elevation data.
4545
@@ -54,6 +54,11 @@ def get_data(local_srtm_dir=None, reduce_big_files=False):
5454
same size as the rest of the world. It may be used to save disk space
5555
(because otherwise the tipical USA file is 25 megabaytes comparet to 2-3
5656
megabytes for the rest).
57+
58+
If leave_zipped is True then files will be stored locally as compressed
59+
zip files. That means less disk space but more computing space for every
60+
file loaded. Note that if you leave this to True and don't reduce big
61+
files -- unzipping them on runtime will be slow.
5762
"""
5863
if not local_srtm_dir:
5964
local_srtm_dir = get_default_srtm_dir()
@@ -84,4 +89,5 @@ def get_data(local_srtm_dir=None, reduce_big_files=False):
8489
assert srtm3_files
8590

8691
return mod_data.GeoElevationData(srtm1_files, srtm3_files, local_srtm_dir,
87-
reduce_big_files=reduce_big_files)
92+
reduce_big_files=reduce_big_files,
93+
leave_zipped=leave_zipped)

srtm/data.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import re as mod_re
2626
import urllib as mod_urllib
2727
import os.path as mod_path
28-
import zipfile as mod_zipfile
2928
import cStringIO as mod_cstringio
3029

3130
from . import utils as mod_utils
@@ -45,13 +44,14 @@ class GeoElevationData:
4544
files = None
4645

4746
def __init__(self, srtm1_files, srtm3_files, files_directory,
48-
reduce_big_files=False):
47+
reduce_big_files=False, leave_zipped=False):
4948
self.srtm1_files = srtm1_files
5049
self.srtm3_files = srtm3_files
5150
# Place where local files are stored:
5251
self.files_directory = files_directory
5352

5453
self.reduce_big_files = reduce_big_files
54+
self.leave_zipped = leave_zipped
5555

5656
self.files = {}
5757

@@ -116,23 +116,24 @@ def retrieve_or_load_file_data(self, file_name):
116116
mod_logging.info('Retrieved {0}'.format(url))
117117
f.close()
118118

119+
if not data:
120+
return None
121+
119122
# data is zipped:
120123
mod_logging.info('Unzipping {0}'.format(url))
121-
zip_file = mod_zipfile.ZipFile(mod_cstringio.StringIO(data))
122-
zip_info_list = zip_file.infolist()
123-
zip_info = zip_info_list[0]
124-
data = zip_file.open(zip_info).read()
125124
mod_logging.info('Unzipped {0}'.format(url))
126125

127-
if not data:
128-
return None
129-
130126
if self.reduce_big_files:
127+
data = mod_utils.unzip(data)
131128
data = self._reduce_file(data, file_name)
132-
133-
f = open(data_file_name, 'w')
134-
f.write(data)
135-
f.close()
129+
data = mod_utils.zip(data)
130+
131+
with open(data_file_name, 'w') as f:
132+
if self.leave_zipped:
133+
f = open(data_file_name + '.zip', 'w')
134+
else:
135+
data = mod_utils.unzip(data)
136+
f = open(data_file_name, 'w')
136137

137138
return data
138139

srtm/utils.py

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

17+
import pdb
18+
1719
import math as mod_math
20+
import zipfile as mod_zipfile
21+
import cStringIO as mod_cstringio
1822

1923
ONE_DEGREE = 1000. * 10000.8 / 90.
2024

@@ -38,3 +42,17 @@ def get_color_between(color1, color2, i):
3842
return (int(color1[0] + (color2[0] - color1[0]) * i),
3943
int(color1[1] + (color2[1] - color1[1]) * i),
4044
int(color1[2] + (color2[2] - color1[2]) * i))
45+
46+
def zip(contents):
47+
pdb.set_trace()
48+
result = mod_cstringio.StringIO()
49+
zip_file = mod_zipfile.ZipFile(result, 'w', mod_zipfile.ZIP_DEFLATED, False)
50+
zip_file.writestr('archive.zip', contents)
51+
result.seek(0)
52+
return result.read()
53+
54+
def unzip(contents):
55+
zip_file = mod_zipfile.ZipFile(mod_cstringio.StringIO(contents))
56+
zip_info_list = zip_file.infolist()
57+
zip_info = zip_info_list[0]
58+
return zip_file.open(zip_info).read()

0 commit comments

Comments
 (0)