<jupyter_start><jupyter_text>Calculate Node WeightsCalculate the relative weight (popularity) of every node in a boundary polygon in an effort to find possible starting points for routing.<jupyter_code>import geopandas as gpd
import pandas as pd
import psycopg2
from sqlalchemy import create_engine
# database connections
src_conn = psycopg2.connect(database='stc', user='postgres', password='<PASSWORD>', host='localhost')
dest_conn = create_engine('sqlite:///seq.db')
# layer info
parent_uid = 'lu_uid'
parent_layer = 'lu'
sql = "SELECT {}, geom FROM {}".format(parent_uid, parent_layer)
pgeo = gpd.GeoDataFrame.from_postgis(sql, src_conn)

# pull out the nodes in the polygons
pgeo['coords'] = pgeo.geometry.boundary.apply(lambda x: x[0].coords)
sp = pgeo[[parent_uid, 'coords']]
d = []
for r in sp.iterrows():
    k = r[1][0]
    v = r[1][1]
    for i in v:
        d.append((k,i))
coord_pop = pd.DataFrame(d, columns=[parent_uid, 'node'])

coord_pop['weight'] = coord_pop.groupby(['node'])[parent_uid].transform('count')
coord_pop.drop_duplicates('node', inplace=True)
coord_pop.sort_values('weight').tail()
coord_pop['weight'].value_counts()
coord_pop.dtypes
coord_pop['node'] = coord_pop['node'].astype(str)
# write it all to sqlite for reference by later steps
coord_pop.to_sql('node_weights', con=dest_conn, if_exists='replace', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14202898550724638, 'fraction_numerical': 0.005072463768115942, 'mean_word_length': 6.546448087431694, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '239513', 'license': 'MIT', 'n_tokens_mistral': 484, 'n_tokens_neox': 472, 'n_words': 116, 'path': 'docs/notebooks/NodeWeights.ipynb', 'repo_name': 'LaurentGoderre/block-sequence', 'size': '1378'}