6

There are two data layers with house numbers and street. My house numbers are point type, and my street layer with road names is line type. What I want to do in my project is to find 15 meters near the street and its name when I choose any of these door numbers.

For example, I click a point and run this code. Then it makes a table with streets 15 meters away from this point. However, it does not tell me which street is on that table. I need street names in that table. In other words, in this table and in this study, I want to learn the street names to which the point is close according to the distance given.

How can I do that?

6
  • This could help you gis.stackexchange.com/questions/21860/… Jan 8, 2021 at 11:54
  • I tried this before but i still can't show the column with street names in the new table. So I don't understand which streets are close to the point. @TimothyDalton
    – Özge A
    Jan 8, 2021 at 12:36
  • Can you share the code where you are trying to join back to the origina ldata? Jan 8, 2021 at 12:39
  • I changed the question I asked above, added some photos and code. If you can look over there and help me accordingly, I would be glad. @TimothyDalton
    – Özge A
    Jan 8, 2021 at 12:54
  • 1
    @ÖzgeA your table near_parks_trails_deneme has the id of the streets within 15m. You can use the Join Table to retrieve the street name?
    – Nil
    Jan 8, 2021 at 13:45

1 Answer 1

6

Add a field named YOL_NAME to Kapi layer, select a feature, then run the following script. (First, backup Kapi and Yol layer data sources)

yol_layer = "Yol"
yol_name_field = "AD" # yol name field in yol layer

kapi_layer = "Kapi"
kapi_yol_field = "YOL_NAME" # newly added yol name field in kapi layer to be populated

with arcpy.da.UpdateCursor(kapi_layer, ["SHAPE@", kapi_yol_field]) as cursor:
    for kapi in cursor:
        yol_cursor = arcpy.da.SearchCursor(yol_layer, ["SHAPE@", yol_name_field])
        # distance dictionary {"AD": distane, "AD": distance, ...}
        ds = {yol[1]: kapi[0].distanceTo(yol[0]) for yol in yol_cursor}
        k = min(ds, key=ds.get) # get the key ("AD") with minimum value
        d = ds[k]               # get the minimum value

        # set "YOL_NAME" based on min value is less than 5 or not
        kapi[1] = k if d <= 5 else ''

        cursor.updateRow(kapi)
        print("Yol Name: " + k)
    
    del yol_cursor

Notes:

  • If there is no road in 5 m, it writes empty string to YOL_NAME.
  • If there are multiple roads within 5 m, then it gets the minimum one.
  • When you have a selection, a cursor will use only the selected ones. That means if you select nothing, the script will use all point features.

Example:

enter image description here


To print all road names within 5m use this script:

...
distance = 5

with arcpy.da.SearchCursor(kapi_layer, ["SHAPE@"]) as cursor:

    for kapi in cursor:
        yol_cursor = arcpy.da.SearchCursor(yol_layer, ["SHAPE@", yol_name_field])

        # distance dictionary {"AD": distane, "AD": distance, ...}
        ds = {yol[1]: kapi[0].distanceTo(yol[0]) for yol in yol_cursor}

        names = [ds[i] for i in ds if ds[i] <= distance]
        print(names)

    del yol_cursor
5
  • Will i run this code with the previous code I wrote? And also i have an error: Runtime error Traceback (most recent call last): File "<string>", line 8, in <module> RuntimeError: Objects in this class cannot be updated outside of the edit session [Kapi]
    – Özge A
    Jan 11, 2021 at 5:39
  • No. just change layers and fields names, select any point and run. Using a tool in toolbox slows you down. Cursors are fast. Jan 11, 2021 at 5:43
  • i have an error: Runtime error Traceback (most recent call last): File "<string>", line 8, in <module> RuntimeError: Objects in this class cannot be updated outside of the edit session [Kapi]
    – Özge A
    Jan 11, 2021 at 5:44
  • Start editing session for Kapi layer, then try again. I am on Ubuntu now, I cannot check. Jan 11, 2021 at 5:54
  • I get an error on the with arcpy.da.UpdateCursor line, I wonder if it could be a problem in terms of syntax.
    – Özge A
    Jan 11, 2021 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.