Skip to content

Commit 6c1f7aa

Browse files
committed
use method chaining for initial data load
1 parent 7565741 commit 6c1f7aa

1 file changed

Lines changed: 16 additions & 20 deletions

File tree

blocksequence/utils/commands.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,35 @@ def node_weights(ctx, parent_layer, parent_uid):
3333

3434
# get every parent geography in the dataset
3535
logger.debug("Getting %s layer data", parent_layer)
36-
pgeo_table = Table(parent_layer, meta, autoload=True, autoload_with=ctx.obj['src_db'])
36+
pgeo_table = Table(parent_layer, meta, autoload=True, autoload_with=ctx.obj['db'])
3737
pgeo_select = select([pgeo_table.c[parent_uid], pgeo_table.c.geom])
38-
pgeo = gpd.GeoDataFrame.from_postgis(pgeo_select, ctx.obj['src_db'])
3938

40-
# pull out the nodes in the polygons
41-
logger.debug("Calculating node coordinates for every polygon")
42-
pgeo['coords'] = pgeo.geometry.boundary.apply(lambda x: x[0].coords)
43-
44-
# limit to the number of columns we really need
45-
pgeo = pgeo.filter([parent_uid, 'coords'])
46-
47-
# generate node IDs from coordinate pairs
48-
pgeo['node'] = pgeo['coord'].apply(lambda c: get_coord_node(c))
39+
# get all the boundaries from the parent geography and grab the node coordinates
40+
pgeo = (gpd.GeoDataFrame.from_postgis(pgeo_select, ctx.obj['db'])
41+
.pipe(get_coords)
42+
.drop('geom', axis=1))
4943

5044
logger.debug("Grouping nodes to determine popularity")
5145
pgeo['weight'] = pgeo.groupby(['node'])[parent_uid].transform('count')
5246

5347
# cast the node to str for writing to the db
5448
pgeo['node'] = pgeo['node'].astype(str)
55-
pgeo = pgeo.drop('coord', axis=1)
5649

57-
# write it all to sqlite for reference by later steps
50+
# write it all to for reference by later steps
5851
logger.debug("Saving to node_weights table")
59-
pgeo.to_sql('node_weights', con=ctx.obj['dest_db'], if_exists='replace', index=False)
52+
pgeo.to_sql('node_weights', con=ctx.obj['db'], if_exists='replace', index=False)
6053

6154
logger.debug("node_weights end")
6255

63-
def get_coord_node(coord):
64-
"""Create a node identifier from a coordinate pair."""
65-
x = coord[0]
66-
y = coord[1]
67-
c = (round(x, 4), round(y, 4))
68-
return c
56+
def get_coords(df):
57+
"""Pull the coordinate value out from each node in a polygon geometry."""
58+
df['node'] = df.geometry.boundary.apply(lambda x: round_coords(x[0].coords))
59+
return df
60+
61+
def round_coords(coord_pair, precision=5):
62+
x = round(coord_pair[0], precision)
63+
y = round(coord_pair[1], precision)
64+
return (x,y)
6965

7066
def get_circuit_distance(circuit, length_field):
7167
"""Compute the total distance for a complete eulerian circuit."""

0 commit comments

Comments
 (0)