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

Skip to content

Commit a2da838

Browse files
committed
PIL images
1 parent 66fe9d7 commit a2da838

File tree

4 files changed

+88
-48
lines changed

4 files changed

+88
-48
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ See: [http://www2.jpl.nasa.gov/srtm/](http://www2.jpl.nasa.gov/srtm/).
1010
>>> elevation_data = srtm.get_data()
1111
>>> print 'CGN Airport elevation (meters):', elevation_data.get_elevation(50.8682, 7.1377)
1212

13+
You can create elevation images with:
14+
15+
import srtm
16+
geo_elevation_data = srtm.get_data()
17+
image = geo_elevation_data.get_image((500, 500), (45, 46), (13, 14), 300)
18+
# the image s a standard PIL object, you can save or show it:
19+
image.show()
20+
21+
image.show()
22+
1323
## gpxelevations
1424

1525
**TODO**

srtm/data.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import zipfile as mod_zipfile
2929
import cStringIO as mod_cstringio
3030

31-
32-
import retriever as mod_retriever
31+
from . import utils as mod_utils
32+
from . import retriever as mod_retriever
3333

3434
class GeoElevationData:
3535
"""
@@ -129,7 +129,6 @@ def retrieve_or_load_file_data(self, file_name):
129129

130130
def get_file_name(self, latitude, longitude):
131131
# Decide the file name:
132-
133132
if latitude >= 0:
134133
north_south = 'N'
135134
else:
@@ -149,6 +148,52 @@ def get_file_name(self, latitude, longitude):
149148

150149
return file_name
151150

151+
def get_image(self, size, latitude_interval, longitude_interval, max_elevation,
152+
unknown_color = (255, 255, 255, 255), zero_color = (0, 0, 255, 255),
153+
min_color = (0, 0, 0, 255), max_color = (0, 255, 0, 255)):
154+
"""
155+
Returns a PIL image.
156+
"""
157+
import Image as mod_image
158+
import ImageDraw as mod_imagedraw
159+
160+
if not size or len(size) != 2:
161+
raise Exception('Invalid size %s' % size)
162+
if not latitude_interval or len(latitude_interval) != 2:
163+
raise Exception('Invalid latitude interval %s' % latitude_interval)
164+
if not longitude_interval or len(longitude_interval) != 2:
165+
raise Exception('Invalid longitude interval %s' % longitude_interval)
166+
167+
width, height = size
168+
width, height = int(width), int(height)
169+
170+
latitude_from, latitude_to = latitude_interval
171+
longitude_from, longitude_to = longitude_interval
172+
173+
image = mod_image.new('RGBA', (width, height),
174+
(255, 255, 255, 255))
175+
draw = mod_imagedraw.Draw(image)
176+
177+
for row in range(height):
178+
for column in range(width):
179+
latitude = latitude_from + float(row) / height * (latitude_to - latitude_from)
180+
longitude = longitude_from + float(column) / height * (longitude_to - longitude_from)
181+
elevation = self.get_elevation(latitude, longitude)
182+
183+
if elevation == None:
184+
color = unknown_color
185+
else:
186+
elevation_coef = elevation / float(max_elevation)
187+
if elevation_coef < 0: elevation_coef = 0
188+
if elevation_coef > 1: elevation_coef = 1
189+
color = mod_utils.get_color_between(min_color, max_color, elevation_coef)
190+
if elevation <= 0:
191+
color = zero_color
192+
193+
draw.point((column, height - row), color)
194+
195+
return image
196+
152197
class GeoElevationFile:
153198
""" Contains data from a single Shuttle elevation file. """
154199

srtm/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2013 Tomo Krajina
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
def get_color_between(color1, color2, i):
18+
""" i is a number between 0 and 1, if 0 then color1, if 1 color2, ... """
19+
if i <= 0:
20+
return color1
21+
if i >= 1:
22+
return color2
23+
return (int(color1[0] + (color2[0] - color1[0]) * i),
24+
int(color1[1] + (color2[1] - color1[1]) * i),
25+
int(color1[2] + (color2[2] - color1[2]) * i))

test_srtm_file_to_image.py

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,16 @@
11
# -*- coding: utf-8 -*-
22

3+
import logging as mod_logging
34
import Image as mod_image
45
import ImageDraw as mod_imagedraw
5-
import srtm as mod_srtm
6-
7-
def get_color_between(color1, color2, i):
8-
""" i is a number between 0 and 1, if 0 then color1, if 1 color2, ... """
9-
if i <= 0:
10-
return color1
11-
if i >= 1:
12-
return color2
13-
return (int(color1[0] + (color2[0] - color1[0]) * i),
14-
int(color1[1] + (color2[1] - color1[1]) * i),
15-
int(color1[2] + (color2[2] - color1[2]) * i))
166

17-
UNKNOWN_COLOR = (255, 255, 255, 255)
18-
ZERO_COLOR = (0 , 0 , 255, 255)
19-
MIN_COLOR = (0 , 0 , 0 , 255)
20-
MAX_COLOR = (0 , 255, 0 , 255)
21-
22-
width, height = 300, 300
23-
max_elevation = 300
7+
import srtm as mod_srtm
248

25-
start_latitude, start_longitude = 45.0, 13.0
26-
# Zašto ovo pukne?
27-
#start_latitude, start_longitude = 43.0, 14.0
9+
mod_logging.basicConfig(level=mod_logging.DEBUG,
10+
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
2811

2912
geo_elevation_data = mod_srtm.get_data()
30-
geo_file = geo_elevation_data.get_file(start_latitude, start_longitude)
31-
32-
side = geo_file.square_side
33-
34-
image = mod_image.new('RGBA', (side, side),
35-
(255, 255, 255, 255))
36-
draw = mod_imagedraw.Draw(image)
37-
38-
for row in range(side):
39-
if row % 100 == 0:
40-
print 'row=%s' % row
41-
for column in range(side):
42-
elevation = geo_file.get_elevation_from_row_and_column(row, column)
43-
44-
if elevation == None:
45-
color = UNKNOWN_COLOR
46-
else:
47-
elevation_coef = elevation / float(max_elevation)
48-
if elevation_coef < 0: elevation_coef = 0
49-
if elevation_coef > 1: elevation_coef = 1
50-
color = get_color_between(MIN_COLOR, MAX_COLOR, elevation_coef)
51-
if elevation <= 0:
52-
color = ZERO_COLOR
5313

54-
draw.point((column, row), color)
14+
image = geo_elevation_data.get_image((500, 500), (20, 30), (-80, -70), 300)
5515

5616
image.show()

0 commit comments

Comments
 (0)