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?
-
This could help you gis.stackexchange.com/questions/21860/…Timothy Dalton– Timothy Dalton2021年01月08日 11:54:49 +00:00Commented 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– Özge A2021年01月08日 12:36:36 +00:00Commented Jan 8, 2021 at 12:36
-
Can you share the code where you are trying to join back to the origina ldata?Timothy Dalton– Timothy Dalton2021年01月08日 12:39:24 +00:00Commented 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– Özge A2021年01月08日 12:54:03 +00:00Commented 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– Nil2021年01月08日 13:45:52 +00:00Commented Jan 8, 2021 at 13:45
1 Answer 1
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:
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
-
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– Özge A2021年01月11日 05:39:05 +00:00Commented 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.Kadir Şahbaz– Kadir Şahbaz2021年01月11日 05:43:16 +00:00Commented 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– Özge A2021年01月11日 05:44:46 +00:00Commented Jan 11, 2021 at 5:44
-
Start editing session for Kapi layer, then try again. I am on Ubuntu now, I cannot check.Kadir Şahbaz– Kadir Şahbaz2021年01月11日 05:54:40 +00:00Commented 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– Özge A2021年01月11日 06:52:55 +00:00Commented Jan 11, 2021 at 6:52