Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geometry sequence as features #14

Merged
merged 7 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 37 additions & 3 deletions cligj/features.py
Expand Up @@ -65,15 +65,21 @@ def iter_features(geojsonfile, func=None):
for line in geojsonfile:
if line.startswith(u'\x1e'):
if text_buffer:
newfeat = func(json.loads(text_buffer))
obj = json.loads(text_buffer)
if 'coordinates' in obj:
obj = to_feature(obj)
newfeat = func(obj)
if newfeat:
yield newfeat
text_buffer = line.strip(u'\x1e')
else:
text_buffer += line
# complete our parsing with a for-else clause.
else:
newfeat = func(json.loads(text_buffer))
obj = json.loads(text_buffer)
if 'coordinates' in obj:
obj = to_feature(obj)
newfeat = func(obj)
if newfeat:
yield newfeat

Expand All @@ -97,9 +103,17 @@ def iter_features(geojsonfile, func=None):
newfeat = func(feat)
if newfeat:
yield newfeat
elif 'coordinates' in obj:
newfeat = func(to_feature(obj))
if newfeat:
yield newfeat
for line in geojsonfile:
newfeat = func(to_feature(json.loads(line)))
if newfeat:
yield newfeat

# Indented or pretty-printed GeoJSON features or feature
# collections will fail out of the try clause above since
# collections will fail out of the try clause above since
# they'll have no complete JSON object on their first line.
# To handle these, we slurp in the entire file and parse its
# text.
Expand All @@ -115,6 +129,26 @@ def iter_features(geojsonfile, func=None):
newfeat = func(feat)
if newfeat:
yield newfeat
elif 'coordinates' in obj:
newfeat = func(to_feature(obj))
if newfeat:
yield newfeat


def to_feature(obj):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@perrygeo I've got no immediate use for what I'm about to suggest: but what about adding a properties keyword argument to this function, the value of which would be copied to the result's properties member?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we punt on that and cover that use case with a more general tool like the proposed fio calc?

"""Takes a feature or a geometry
returns feature verbatim or
wraps geom in a feature with empty properties
"""
if obj['type'] == 'Feature':
return obj
elif 'coordinates' in obj:
return {
'type': 'Feature',
'properties': {},
'geometry': obj}
else:
raise ValueError("Object is not a feature or geometry")


def iter_query(query):
Expand Down
4 changes: 4 additions & 0 deletions tests/point_pretty_geom.txt
@@ -0,0 +1,4 @@
{
"coordinates": [-122.7282, 45.5801],
"type": "Point"
}
23 changes: 22 additions & 1 deletion tests/test_features.py
Expand Up @@ -4,7 +4,7 @@
import pytest

from cligj.features import \
coords_from_query, iter_query, \
coords_from_query, iter_query, to_feature, \
normalize_feature_inputs, normalize_feature_objects


Expand Down Expand Up @@ -118,6 +118,20 @@ def test_coordpairs_space(expected_features):
assert _geoms(features) == _geoms(expected_features)


def test_geometrysequence(expected_features):
features = normalize_feature_inputs(None, 'features', ["tests/twopoints_geom_seq.txt"])
assert _geoms(features) == _geoms(expected_features)


def test_geometrysequencers(expected_features):
features = normalize_feature_inputs(None, 'features', ["tests/twopoints_geom_seqrs.txt"])
assert _geoms(features) == _geoms(expected_features)


def test_geometrypretty(expected_features):
features = normalize_feature_inputs(None, 'features', ["tests/point_pretty_geom.txt"])
assert _geoms(features)[0] == _geoms(expected_features)[0]

class MockGeo(object):
def __init__(self, feature):
self.__geo_interface__ = feature
Expand All @@ -134,3 +148,10 @@ def test_normalize_feature_objects_bad(expected_features):
objs.append(MockGeo(dict()))
with pytest.raises(ValueError):
list(normalize_feature_objects(objs))

def test_to_feature(expected_features):
geom = expected_features[0]['geometry']
feat = {'type': 'Feature', 'properties': {}, 'geometry': geom}
assert to_feature(feat) == to_feature(geom)
with pytest.raises(ValueError):
assert to_feature({'type': 'foo'})
2 changes: 2 additions & 0 deletions tests/twopoints_geom_seq.txt
@@ -0,0 +1,2 @@
{"coordinates": [-122.7282, 45.5801], "type": "Point"}
{"coordinates": [-121.3153, 44.0582], "type": "Point"}
8 changes: 8 additions & 0 deletions tests/twopoints_geom_seqrs.txt
@@ -0,0 +1,8 @@
{
"coordinates": [-122.7282, 45.5801],
"type": "Point"
}
{
"coordinates": [-121.3153, 44.0582],
"type": "Point"
}