<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Alternative libraries to arcpy to make the &amp;quot;find conntected&amp;quot; function? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1625037#M74404</link>
    <description>&lt;P&gt;You are correct, it's a simple starting point for collecting connected identifiers that can then be interpolated into a SELECT clause for feature selection. I didn't implement a network model here because that's a bit out of scope, but if all you need is simple connections, using a function like this is a start for building the graph with OID@ as the nodes.&lt;/P&gt;&lt;P&gt;If you already have the topographical connections encoded in fields like `To_Feature`/`From_Feature` networxx would work well. Otherwise you'll still need something like what I have to build the relations.&lt;/P&gt;&lt;P&gt;Iterating through the immediately adjacent lines and then inserting them into a networxx graph is a good solution, you can also just roll your own graph using Python dictionaries, or a custom Graph class. The main functionality that's needed would be just building those relationships on the fly using simple Cursors and Geometry functions.&lt;/P&gt;&lt;P&gt;I did also notice that the toolbox you shared does include a nax import:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;...
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("network")

NDpath = arcpy.GetParameter(0)

# Create network dataset object
ND = arcpy.nax.NetworkDataset(NDpath)
...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jun 2025 12:12:01 GMT</pubDate>
    <dc:creator>HaydenWelch</dc:creator>
    <dc:date>2025-06-19T12:12:01Z</dc:date>
    <item>
      <title>Alternative libraries to arcpy to make the "find conntected" function?</title>
      <link>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1621141#M74320</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use &lt;EM&gt;ArcGIS Desktop &lt;/EM&gt;and I&amp;nbsp;would like to build with python the function behind the standard GIS tool "find connected", which is able to select all the feature topologically connected to each other, starting from a given line feature.&lt;/P&gt;&lt;P&gt;I do not need the arcpy.TraceGeometricNetwork function because even though it really gives you all the feature connected to each other, in doing so, it creates other layers, different from the original and I am looking for a solution that selects features inside their original layers.&lt;/P&gt;&lt;P&gt;Does anyone know any other python library that could have this type of funtion related no networks?&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2025 13:09:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1621141#M74320</guid>
      <dc:creator>TeresaBartolomei</dc:creator>
      <dc:date>2025-06-05T13:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative libraries to arcpy to make the "find conntected" function?</title>
      <link>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1621554#M74326</link>
      <description>&lt;P&gt;You can implement it yourself using Cursors:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path
from arcpy import (
    Parameter,
    Polyline,
    Geometry,
)
from arcpy.da import (
    SearchCursor,
)
from arcpy._mp import (
    Layer
)

def find_connected(line: Polyline, feature_classes: list[str|Layer]) -&amp;gt; dict[str, list[int]]:
    """Find all features in each feature class that are connected to the input line
    
    Args:
        line (Polyline): The connecting line that will be used to filter the input features
        feature_classes (list[str|Layer]): A list of paths or Layers that will be searched for connected features
    
    Returns:
        ( dict[str, list[int]] ): A mapping of feature names to connected feature OIDs
    """
    connected = {}
    for fc in feature_classes:
        if isinstance(fc, Layer):
            name = fc.name
        else:
            name = Path(fc).name
            
        connected[name] = [row[0] for row in SearchCursor(fc, ['OID@'], spatial_filter=line, spatial_relationship='INTERSECTS')]
    return connected&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a more modular version if you don't need all the OIDs immediately (say you're just gonna loop over them later):&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def find_connected(line: Polyline, feature_class: str|Layer) -&amp;gt; Generator[int, None, None]:
    """Find all features in each feature class that are connected to the input line
    
    Args:
        line (Polyline): The connecting line that will be used to filter the input features
        feature_class (str|Layer): The feature class to find connections in
    
    Yields:
        ( int ): Consume this generator 
    """
    yield from (
        row[0] 
        for row in SearchCursor(
            feature_class, 
            ['OID@'], 
            spatial_filter=line, 
            spatial_relationship='INTERSECTS'
            )
        )

def get_connected_for(line: Polyline, feature_classes: list[str|Layer], as_list: bool=True) -&amp;gt; dict[str, list[int]]:
    """Get a mapping of connected features for multiple feature classes
    
    Args:
        line (Polyline) : The connecting line
        feature_classes (list[str|Layer]) : The feature classes to find connections with
        as_list (bool): Flag for returning a sequence of generators or populated lists
    Returns:
        (dict[str, list[int]]) : A mapping of the input fcs to the OIDs of the connected features
    """
    return {
        fc if isinstance(fc, str) else fc.longName: list(find_connected(line, fc)) if as_list else find_connected(line, fc)
        for fc in feature_classes
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first function makes a generator for the supplied connector and feature class, the second will map a sequence of feature classes to either a list of the connected OIDs or to a generator object that can be iterated over one at a time. If you have a ton of connections per line, the generator solution will be a lot more memory efficient, but if you know the upper bound for the number of connections is small (&amp;lt;10 or so) immediately converting that connection generator to a list will be more memory efficient&lt;/P&gt;&lt;P&gt;Here's a quick sample of the size (in bytes) of both calls with ~2-3 connections each:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; cxns = get_connected_for(line, ['fc1', 'fc2'], as_list=False)
&amp;gt;&amp;gt;&amp;gt; cxns
{'fc1': &amp;lt;generator object find_connected at 0x000002504CC24400&amp;gt;, 'fc2': &amp;lt;generator object find_connected at 0x000002507BBEBA60&amp;gt;}
&amp;gt;&amp;gt;&amp;gt; getsize(cxns)
1292

...

&amp;gt;&amp;gt;&amp;gt; cxns = get_connected_for(line, ['fc1', 'fc2'])
&amp;gt;&amp;gt;&amp;gt; cxns
{'fc1': [1, 2], 'fc2': [1, 2, 3]}
&amp;gt;&amp;gt;&amp;gt; getsize(cxns)
564&lt;/LI-CODE&gt;&lt;P&gt;Note: The first example is larger because the generators are a fixed size. That means even if there are a million connections in there, it'll still only be 1292 bytes.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jun 2025 17:29:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1621554#M74326</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-06-06T17:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative libraries to arcpy to make the "find conntected" function?</title>
      <link>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1625008#M74403</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/607017"&gt;@HaydenWelch&lt;/a&gt;&amp;nbsp;reviewing your code, if I have understood correctly it returns the ObjectID's of the features that intersect the input Polyline? I think&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/851041"&gt;@TeresaBartolomei&lt;/a&gt;&amp;nbsp;is looking for a solution that would select all connected edges in a graph, as she specifically mentioned networks, so not just the immediate adjacent lines.&lt;/P&gt;&lt;P&gt;If you wanted to program this you are basically looking at using the&amp;nbsp;&lt;A href="https://networkx.org/documentation/stable/index.html" target="_self"&gt;networkx&lt;/A&gt; module.&lt;/P&gt;&lt;P&gt;I have also &lt;A href="https://www.arcgis.com/home/item.html?id=b2227f745a6c4e1c94dd57810729d2a9" target="_self"&gt;used this useful tool&lt;/A&gt; for coding up connected edges by an ID. This does not require network analyst as it is using networkx.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jun 2025 09:42:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1625008#M74403</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2025-06-19T09:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative libraries to arcpy to make the "find conntected" function?</title>
      <link>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1625037#M74404</link>
      <description>&lt;P&gt;You are correct, it's a simple starting point for collecting connected identifiers that can then be interpolated into a SELECT clause for feature selection. I didn't implement a network model here because that's a bit out of scope, but if all you need is simple connections, using a function like this is a start for building the graph with OID@ as the nodes.&lt;/P&gt;&lt;P&gt;If you already have the topographical connections encoded in fields like `To_Feature`/`From_Feature` networxx would work well. Otherwise you'll still need something like what I have to build the relations.&lt;/P&gt;&lt;P&gt;Iterating through the immediately adjacent lines and then inserting them into a networxx graph is a good solution, you can also just roll your own graph using Python dictionaries, or a custom Graph class. The main functionality that's needed would be just building those relationships on the fly using simple Cursors and Geometry functions.&lt;/P&gt;&lt;P&gt;I did also notice that the toolbox you shared does include a nax import:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;...
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("network")

NDpath = arcpy.GetParameter(0)

# Create network dataset object
ND = arcpy.nax.NetworkDataset(NDpath)
...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jun 2025 12:12:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1625037#M74404</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-06-19T12:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative libraries to arcpy to make the "find conntected" function?</title>
      <link>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1625053#M74405</link>
      <description>&lt;P&gt;Yeah I think she did that to ensure that the input was indeed a connected network and not some load of unconnected nonsense! That tool could avoid that extension and build the graph as you have suggested.&amp;nbsp; Anyway glad I had not somehow misunderstood your code sample.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jun 2025 13:22:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alternative-libraries-to-arcpy-to-make-the-quot/m-p/1625053#M74405</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2025-06-19T13:22:46Z</dc:date>
    </item>
  </channel>
</rss>

