Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge f1a415b into 0ecd094
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Oct 26, 2015
2 parents 0ecd094 + f1a415b commit 52ba05d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions mapbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .services.base import Service
from .services.geocoding import Geocoder, InvalidPlaceTypeError
from .services.upload import Uploader
from .services.surface import Surface
39 changes: 39 additions & 0 deletions mapbox/services/surface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from uritemplate import URITemplate
from mapbox.encoding import encode_waypoints, encode_polyline
from .base import Service


class Surface(Service):

def __init__(self, access_token=None):
self.baseuri = 'https://api.mapbox.com/v4/surface'
self.session = self.get_session(access_token)

def surface(self,
features,
mapid="mapbox.mapbox-terrain-v1",
layer="contour",
fields=["ele"],
geojson=True):

waypoints = encode_waypoints(features, precision=6,
min_limit=2, max_limit=300)

# TODO other params
params = {
'layer': layer,
'fields': ','.join(fields),
'geojson': 'true' if geojson else 'false',
'points': waypoints}

# TODO encoded polyline

uri = URITemplate('%s/{mapid}.json' % self.baseuri).expand(mapid=mapid)
res = self.session.get(uri, params=params)
res.raise_for_status()

def geojson():
return res.json()['results']
res.geojson = geojson

return res
52 changes: 52 additions & 0 deletions tests/test_surface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import responses
import mapbox


points = [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-112.084004, 36.05322]}}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-112.083914, 36.053573]}}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-112.083965, 36.053845]}}]


@responses.activate
def test_surface():
body = """{"results":[{"id":0,"latlng":{"lat":36.05322,"lng":-112.084004},"ele":2186.361304424316},{"id":1,"latlng":{"lat":36.053573,"lng":-112.083914},"ele":2187.6233827411997},{"id":2,"latlng":{"lat":36.053845,"lng":-112.083965},"ele":2163.921475128245}],"attribution":"<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox</a>"}"""

responses.add(
responses.GET,
'https://api.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?access_token=pk.test&points=-112.084004%2C36.053220%3B-112.083914%2C36.053573%3B-112.083965%2C36.053845&geojson=true&fields=ele&layer=contour',
match_querystring=True,
body=body, status=200,
content_type='application/json')

res = mapbox.Surface(access_token='pk.test').surface(points)
assert res.status_code == 200


@responses.activate
def test_surface_geojson():
body = """{"results":{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-112.084004,36.05322]},"properties":{"id":0,"ele":2186.361304424316}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-112.083914,36.053573]},"properties":{"id":1,"ele":2187.6233827411997}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-112.083965,36.053845]},"properties":{"id":2,"ele":2163.921475128245}}]},"attribution":"<a href='https://www.mapbox.com/about/maps/' target='_blank'>&copy; Mapbox</a>"}"""

responses.add(
responses.GET,
'https://api.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?access_token=pk.test&fields=ele&layer=contour&geojson=true&points=-112.084004%2C36.053220%3B-112.083914%2C36.053573%3B-112.083965%2C36.053845',
match_querystring=True,
body=body, status=200,
content_type='application/json')

res = mapbox.Surface(access_token='pk.test').surface(points)
fc = res.geojson()
assert fc['type'] == 'FeatureCollection'
assert len(fc['features']) == 3

0 comments on commit 52ba05d

Please sign in to comment.