<jupyter_start><jupyter_text>Example<jupyter_code>eb = ps.open(ps.examples.get_path("eberly_net.shp"))
# note eberly has chain==segment, forall chains. not necessarily true for all chains in polyline shapefiles
# will have to generalize this
segments = [ chain for chain in eb] 
ep = ps.open(ps.examples.get_path("eberly_net_pts_offnetwork.shp"))
points = {}
for i,pnt in enumerate(ep):
    points[i] = pnt

#points
#plotting
figsize(20,20)
network = []
for segment in segments:
    head,tail = segment.vertices
    x1,y1 = head
    x2,y2 = tail
    network.append((x1,x2))
    network.append((y1,y2))
    network.append('b')
plot(*network)
for i,point in points.iteritems():
    x,y = point
    plot(x,y, 'o')
xlim(-1,25)
ylim(-1,10)
res = snapPointsOnSegments(points, segments)
#plotting
figsize(20,20)
network = []
for segment in segments:
    head,tail = segment.vertices
    x1,y1 = head
    x2,y2 = tail
    network.append((x1,x2))
    network.append((y1,y2))
    network.append('b')
plot(*network)
for i,point in points.iteritems():
    snapped = res[i]
    x,y = snapped[1]
    #print point,snapped
    plot(x,y, 'o')
xlim(-1,25)
ylim(-1,10)
pnts = res.keys()
pnts.sort()
for pnt in pnts:
    print pnt, res[pnt]
wvn = ps.open("Waverly.shp")
chains = [ chain for chain in wvn]
bus = ps.open("Fake_Waverly_Bus.shp")
points = {}
for i,pnt in enumerate(bus):
    points[i] = pnt
segments = []
for chain in chains:
    nc = len(chain.vertices)
    for i in range(nc-1):
        segments.append(ps.cg.Chain(chain.vertices[i:i+2]))

chain.vertices[i:i+2]
res = snapPointsOnSegments(points, segments)
figsize(20,20)
network = []
for segment in segments:
    head,tail = segment.vertices
    x1,y1 = head
    x2,y2 = tail
    network.append((x1,x2))
    network.append((y1,y2))
    network.append('b')
plot(*network)
for i,point in points.iteritems():
    x,y = point
    plot(x,y, 'o')

#plotting
figsize(20,20)
network = []
for segment in segments:
    head,tail = segment.vertices
    x1,y1 = head
    x2,y2 = tail
    network.append((x1,x2))
    network.append((y1,y2))
    network.append('b')
plot(*network)
for i,point in points.iteritems():
    snapped = res[i]
    x,y = snapped[1]
    #print point,snapped
    plot(x,y, 'o')

def snapping(pointShpFile,lineShpFile, viz=True):
    shpf = ps.open(lineShpFile)
    segments = []
    for chain in shpf:
        nc = len(chain.vertices)
        for i in range(nc-1):
            segments.append(ps.cg.Chain(chain.vertices[i:i+2]))
    pntf = ps.open(pointShpFile)
    points = {}
    for i,pnt in enumerate(pntf):
        points[i] = pnt
        
    
    p2s = snapPointsOnSegments(points, segments)
    
    if viz:
        figsize(20,20)
        network = []
        for segment in segments:
            head,tail = segment.vertices
            x1,y1 = head
            x2,y2 = tail
            network.append((x1,x2))
            network.append((y1,y2))
            network.append('b')
        plot(*network)
        for i,point in points.iteritems():
            snapped = p2s[i]
            x,y = snapped[1]
            #print point,snapped
            plot(x,y, 'o')
            
    


sf = ps.examples.get_path('geodanet/streets.shp')
pf = ps.examples.get_path('geodanet/crimes.shp')
res = snapping(pf,sf)
 
len(res) == 287<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14505428226779252, 'fraction_numerical': 0.02563329312424608, 'mean_word_length': 2.9347568208778174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '222500', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1256, 'n_words': 262, 'path': 'pysal/network/snapping.ipynb', 'repo_name': 'Aniq55/pysal', 'size': '3316'}