Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ninuxorg/python-geojson-elevation
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Nov 28, 2016
2 parents d10ed2d + ffccd62 commit 845f428
Show file tree
Hide file tree
Showing 8 changed files with 4,767 additions and 3 deletions.
9 changes: 6 additions & 3 deletions geojson_elevation/backends/google.py
Expand Up @@ -22,9 +22,11 @@ def elevation(path, api_key=None, sampling=50):
if len(points) > 1:
# length of the path in meters
length = LineString(points).length * 100000
# get 1 point every x meters, where x is defined in ELEVATION_DEFAULT_SAMPLING
# get 1 point every x meters, where x is defined in
# ELEVATION_DEFAULT_SAMPLING
samples = int(round(length / sampling))
# use the automatically calculated value as long as it is compatibile with the API usage limits
# use the automatically calculated value as long as it is compatibile
# with the API usage limits
if samples > 512:
samples = 512
# at least 2 samples
Expand Down Expand Up @@ -61,4 +63,5 @@ def elevation(path, api_key=None, sampling=50):
))
# else return original response
else:
raise ElevationApiError("Google Elevation API error:\n\n{0}".format(response.content))
raise ElevationApiError(
"Google Elevation API error:\n\n{0}".format(response.content))
2 changes: 2 additions & 0 deletions geojson_elevation/exceptions.py
@@ -1,9 +1,11 @@
class GeojsonElevationException(Exception):

""" root geojson_elevation exception """
pass


class ElevationApiError(GeojsonElevationException):

"""
Raised if the elevation web service response is not ok
"""
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Expand Up @@ -2,3 +2,4 @@ nose
coverage
coveralls
shapely
responses==0.4.0
42 changes: 42 additions & 0 deletions tests/google_tests.py
@@ -1,32 +1,74 @@
import os
import responses
import unittest

from geojson_elevation.backends.google import elevation
from geojson_elevation.exceptions import ElevationApiError


class TestGoogleBackend(unittest.TestCase):

def load_fixture(self, file):
return open(os.path.abspath(file)).read()

""" Google Elevation API tests """
@responses.activate
def test_1_point(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json?locations=41.889040454306752%2C12.525333445447737",
body=self.load_fixture("tests/static/test_1_point.json"),
match_querystring=True,
content_type='application/json',
)

result = elevation('41.889040454306752,12.525333445447737')
self.assertIn('Point', result['geometry']['type'])
self.assertEqual(len(result['geometry']['coordinates']), 1)
self.assertEqual(len(result['geometry']['coordinates'][0]), 3)

@responses.activate
def test_2_points(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json?path=41.889040454306752%2C12.525333445447737%7C41.889050454306752%2C12.525335445447737&samples=2",
body=self.load_fixture("tests/static/test_2_points.json"),
match_querystring=True,
content_type='application/json',
)

result = elevation('41.889040454306752,12.525333445447737|41.889050454306752,12.525335445447737')
self.assertIn('LineString', result['geometry']['type'])
self.assertEqual(len(result['geometry']['coordinates']), 2)
self.assertEqual(len(result['geometry']['coordinates'][0]), 3)
self.assertEqual(len(result['geometry']['coordinates'][1]), 3)

@responses.activate
def test_maximum_sampling(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json?path=41.889040454306752%2C12.525333445447737%7C41.872041927699982%2C12.582239191900001&samples=512",
body=self.load_fixture("tests/static/test_maximum_sampling.json"),
match_querystring=True,
content_type='application/json',
)

result = elevation('41.889040454306752,12.525333445447737|41.872041927699982,12.582239191900001', sampling=4)
self.assertIn('LineString', result['geometry']['type'])
self.assertEqual(len(result['geometry']['coordinates']), 512)
self.assertEqual(len(result['geometry']['coordinates'][0]), 3)
self.assertEqual(len(result['geometry']['coordinates'][-1]), 3)

@responses.activate
def test_automatic_sampling(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json?path=41.8890404543067518%2C12.5253334454477372%7C41.8972185849048984%2C12.4902286938660296&samples=72",
body=self.load_fixture("tests/static/test_automatic_sampling.json"),
match_querystring=True,
content_type='application/json',
)

result = elevation('41.8890404543067518,12.5253334454477372|41.8972185849048984,12.4902286938660296')
self.assertIn('LineString', result['geometry']['type'])
self.assertEqual(len(result['geometry']['coordinates']), 72)
Expand Down
13 changes: 13 additions & 0 deletions tests/static/test_1_point.json
@@ -0,0 +1,13 @@
{
"results": [
{
"elevation": 42.70062255859375,
"location": {
"lat": 41.88904045430675,
"lng": 12.52533344544774
},
"resolution": 4.771975994110107
}
],
"status": "OK"
}
21 changes: 21 additions & 0 deletions tests/static/test_2_points.json
@@ -0,0 +1,21 @@
{
"results": [
{
"elevation": 42.70062255859375,
"location": {
"lat": 41.88904045430675,
"lng": 12.52533344544774
},
"resolution": 4.771975994110107
},
{
"elevation": 42.74621200561523,
"location": {
"lat": 41.88905045430676,
"lng": 12.52533544544774
},
"resolution": 4.771975994110107
}
],
"status": "OK"
}

0 comments on commit 845f428

Please sign in to comment.