Skip to content

Commit

Permalink
Clip dataframes to the given bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
invisiblefunnel committed Sep 20, 2018
1 parent 07e0cdf commit bfc10b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 12 additions & 4 deletions sharedstreets/dataframe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import collections
import geopandas
import mercantile
from shapely.geometry import box
from .. import tile

# Container for dataframes of SharedStreets geometries and intersections.
Expand All @@ -19,7 +20,7 @@ def __init__(self, properties, type, coordinates):
'geometry': {'type': type, 'coordinates': coordinates},
}

def _make_frames(intersections, geometries):
def _make_frames(intersections, geometries, bounds=None):
''' Return a Frames instance for lists of SharedStreets entities.
'''
ifeatures = [
Expand All @@ -42,18 +43,25 @@ def _make_frames(intersections, geometries):
for item in geometries
]

def clip_bbox(gdf):
if bounds is None:
return gdf
index = list(gdf.sindex.intersection(bounds))
return gdf.iloc[index]

def make_frame(features):
gdf = geopandas.GeoDataFrame.from_features(features)
return gdf.set_index('id', drop=False, verify_integrity=True)

intersectionsdf = make_frame(ifeatures)
geometriesdf = make_frame(gfeatures)
intersectionsdf = clip_bbox(make_frame(ifeatures))
geometriesdf = clip_bbox(make_frame(gfeatures))

return Frames(intersectionsdf, geometriesdf)

def get_bbox(minlon, minlat, maxlon, maxlat, data_url_template=None):
''' Get a single Frames instance of SharedStreets entities in an area.
'''
bounds = (minlon, minlat, maxlon, maxlat)
ul = mercantile.tile(minlon, maxlat, tile.DATA_ZOOM)
lr = mercantile.tile(maxlon, minlat, tile.DATA_ZOOM)

Expand All @@ -65,7 +73,7 @@ def get_bbox(minlon, minlat, maxlon, maxlat, data_url_template=None):
all_geometries = functools.reduce(lambda d, t: dict(d, **t.geometries), tiles, {})
all_intersections = functools.reduce(lambda d, t: dict(d, **t.intersections), tiles, {})

return _make_frames(all_intersections.values(), all_geometries.values())
return _make_frames(all_intersections.values(), all_geometries.values(), bounds)

def get_tile(*args, **kwargs):
''' Get a single Frames instance for a tile of SharedStreets entities.
Expand Down
16 changes: 10 additions & 6 deletions sharedstreets/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
mock_tile.intersections = {
'NNNN': Intersection('NNNN', 1, ['IN'], ['NI'], -0.000113, 0.000038),
'dddd': Intersection('dddd', 4, ['NI'], ['IN'], 0.000231, -0.000032),
'OOOO': Intersection('OOOO', 5, ['LO'], ['OL'], -122.2589, 37.8116),
'land': Intersection('land', 8, ['OL'], ['LO'], -122.2341, 37.8068),
}

mock_tile.geometries = {
'NlId': Geometry('NlId', 6, 'NNNN', 'dddd', 'NI', 'IN',
[-0.000113, 0.000038, 0.000027, 0.000032, 0.000038, -0.000027, 0.000231, -0.000032]),
'Okld': Geometry('Okld', 6, 'OOOO', 'land', 'OL', 'LO',
[-122.2589, 37.8116, -122.2472, 37.8119, -122.2468, 37.8068, -122.2341, 37.8068]),
}

class TestDataframe (unittest.TestCase):
Expand All @@ -28,12 +32,12 @@ def test_get_tile(self):
get_tile.return_value = mock_tile
frames = dataframe.get_tile(12, 2048, 2048)

self.assertEqual(set(frames.intersections.id), {'NNNN', 'dddd'})
self.assertEqual(set(frames.intersections.nodeId), {1, 4})
self.assertEqual(set(frames.geometries.id), {'NlId'})
self.assertEqual(set(frames.geometries.fromIntersectionId), {'NNNN'})
self.assertEqual(set(frames.geometries.toIntersectionId), {'dddd'})
self.assertEqual(set(frames.intersections.id), {'NNNN', 'dddd', 'OOOO', 'land'})
self.assertEqual(set(frames.intersections.nodeId), {1, 4, 5, 8})
self.assertEqual(set(frames.geometries.id), {'NlId', 'Okld'})
self.assertEqual(set(frames.geometries.fromIntersectionId), {'NNNN', 'OOOO'})
self.assertEqual(set(frames.geometries.toIntersectionId), {'dddd', 'land'})

def test_get_bbox(self):

with mock.patch('sharedstreets.tile.get_tile') as get_tile:
Expand Down

0 comments on commit bfc10b6

Please sign in to comment.