Select to view content in your preferred language

How to list all polylines within any one polygon!?

1074
10
04-13-2023 01:48 PM
nsidaniel
Regular Contributor

I am trying to see what polylines are within any one polygon. That is, I'd like to print a list of what circuits a county contains.

To do this, I'm trying to iterate through some KMZs (after converting them to layers).

I then intersect the polygons (counties) with the polylines (circuits) and create a temp layer that should contain all the counties that contain lines in them.

I then use Search Cursor to print what polygons (counties) contain polylines (circuits).

Unfortunately, instead of combining all the polygon and polyline data into one attribute table, it iterates through each KMZ, creates a temp layer, then deletes it. It then prints the final temp layer KMZ comparison.

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]

county = ["Dude County"]
projectfilepath = "C:\\Users\\User\\Documents\\ArcGIS\\Projects\\ARCPY_test\\"
datafilepath = "C:\\Users\\User\\Documents\\ArcGIS\Data\\"
to_KMZs_filepath = "Circuits_KMZs\\"
parcel = "Counties_(v17a)"

gdb = datafilepath + to_KMZs_filepath
counties = m.listLayers(parcel)

import os 

for dirpath, dirnames, filenames in os.walk(rootdir):
    for file in filenames:
        if file.endswith("kmz"):
            try:
                arcpy.conversion.KMLToLayer(
                    in_kml_file = m.addDataFromPath(os.path.join(gdb,file)),
                    output_folder = projectfilepath,
                    output_data = file + "_LAYER",
                    include_groundoverlay="NO_GROUNDOVERLAY"
                )
                line = m.listLayers(file + "\Polylines")
                arcpy.analysis.Intersect(
                    in_features = [counties, line], 
                    out_feature_class = r"memory\temp_intersect",
                    output_type = "POINT"
                )
            except Exception:
                arcpy.AddMessage(os.path.join(rootdir,file))

fields = ["LABEL", "NAME_1"]
with arcpy.da.SearchCursor(r"memory\temp_intersect", fields) as u_cursor:
    for label, name_1 in u_cursor:
        if label in county:
            rows = arcpy.SearchCursor(r"memory\temp_intersect", fields="NAME_1")
            for row in rows:
                print("{0}".format(row.getValue("NAME_1")))

 

Tags (1)
0 Kudos
10 Replies
nsidaniel
Regular Contributor

True, I appreciate the heads up nevertheless, since you kindly labeled it, I knew. I'll only be creating the lists county by county

0 Kudos