-
Notifications
You must be signed in to change notification settings - Fork 7
/
faster.py
43 lines (34 loc) · 1.18 KB
/
faster.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
import sys, csv, time
import pandas as pd
import geopandas as gpd
from shapely.wkt import loads
# monitor performance
start_time = time.time()
def log(message):
global start_time
print('{:,.3f}s: {}'.format(time.time()-start_time, message))
start_time = time.time()
filepath = './example_geometries.csv'
crs = {'init': 'epsg:32154'}
df = pd.read_csv(filepath)
log('loaded csv')
# create a geometry geoseries from the wkt
geometry = gpd.GeoSeries(df['geometry'].map(lambda x: loads(x)))
log('created geometry')
gdf = gpd.GeoDataFrame(data=df[['id', 'value']], crs=crs, geometry=geometry)
gdf = gdf.set_index('id')
log('converted df to gdf')
sidx = gdf.sindex
log('created spatial index')
# projection is in meters, buffer itself is unit agnostic
gdf['buffer'] = gdf['geometry'].buffer(500)
log('buffered geometries')
def summarize(buffer):
possible_matches_index = list(sidx.intersection(buffer.bounds))
possible_matches = gdf.iloc[possible_matches_index]
precise_matches = possible_matches[possible_matches.intersects(buffer)]
return precise_matches['value'].sum()
sums = gdf['buffer'].map(summarize)
sums.name = 'value sum'
log('calculated value sums')
print(sums.head())