forked from earthlab-education/earth-analytics-fall-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip_data.py
50 lines (35 loc) · 1.27 KB
/
clip_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"A module to clip vector data using geopandas"
# Create function to clip point data using geopandas
def clip_points(shp, clip_obj):
'''
Docs Here
'''
poly = clip_obj.geometry.unary_union
return(shp[shp.geometry.intersects(poly)])
# Create function to clip line and polygon data using geopandas
def clip_line_poly(shp, clip_obj):
'''
docs
'''
# Create a single polygon object for clipping
poly = clip_obj.geometry.unary_union
spatial_index = shp.sindex
# Create a box for the initial intersection
bbox = poly.bounds
# Get a list of id's for each road line that overlaps the bounding box and subset the data to just those lines
sidx = list(spatial_index.intersection(bbox))
#shp_sub = shp[shp.index.isin(sidx)]
shp_sub = shp.iloc[sidx]
# Clip the data - with these data
clipped = shp_sub.copy()
clipped['geometry'] = shp_sub.intersection(poly)
# Return the clipped layer with no null geometry values
return(clipped[clipped.geometry.notnull()])
# Final clip function that handles points, lines and polygons
def clip_shp(shp, clip_obj):
'''
'''
if shp["geometry"].iloc[0].type == "Point":
return(clip_points(shp, clip_obj))
else:
return(clip_line_poly(shp, clip_obj))