Skip to content

Commit

Permalink
generalize to all features, filter waypoints, related to #11
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Dec 3, 2015
1 parent 728dfce commit c428db1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
36 changes: 21 additions & 15 deletions mapboxcli/scripts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@ class MapboxCLIException(click.ClickException):


def normalize_waypoints(waypoints):
point_features = []
if len(waypoints) == 0:
waypoints = ('-',)
for wp in waypoints:
features = normalize_features(waypoints)
# skip non-points
return [f for f in features if f['geometry']['type'] == 'Point']


def normalize_features(features_like):
features = []
if len(features_like) == 0:
features_like = ('-',)
for fl in features_like:
try:
# It's a file/stream with GeoJSON
stdin = click.open_file(wp, 'r')
waypt = json.loads(stdin.read())
stdin = click.open_file(fl, 'r')
geojson_mapping = json.loads(stdin.read())
except IOError:
# It's a coordinate string
coords = list(coords_from_query(wp))
waypt = {
coords = list(coords_from_query(fl))
geojson_mapping = {
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Point',
'coordinates': coords}}

if waypt['type'] == 'Feature' and waypt['geometry']['type'] == "Point":
point_features.append(waypt)
elif waypt['type'] == 'FeatureCollection':
for feature in waypt['features']:
point_features.append(feature)
if geojson_mapping['type'] == 'Feature':
features.append(geojson_mapping)
elif geojson_mapping['type'] == 'FeatureCollection':
for feat in geojson_mapping['features']:
features.append(feat)
else:
pass # TODO, handle non-feature or non-point feature
raise ValueError("Not a valid coordinate, Feature or FeatureCollection")

return point_features
return features


def iter_query(query):
Expand Down
4 changes: 2 additions & 2 deletions mapboxcli/scripts/static.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

import mapbox
from .helpers import MapboxCLIException, normalize_waypoints
from .helpers import MapboxCLIException, normalize_features


@click.command(short_help="Static map images.")
Expand All @@ -26,7 +26,7 @@ def staticmap(ctx, mapid, output, features, lat, lon, zoom, size):
"""
access_token = (ctx.obj and ctx.obj.get('access_token')) or None
if features:
featiter = normalize_waypoints(features) # TODO normalize_features
featiter = normalize_features(features)
else:
featiter = None

Expand Down

0 comments on commit c428db1

Please sign in to comment.