POST
|
Thanks for the reply. I was coming back to my post to add code that worked for me. try:
# Update the street names in the table
with arcpy.da.UpdateCursor(table, "STREET") as cursor:
for row in cursor:
street_name = row[0]
# List of common street prefixes to be removed
prefixes = ['N ', 'S ', 'E ', 'W ', 'North ', 'South ', 'East ', 'West ']
# Remove prefixes from the street name
for prefix in prefixes:
if street_name.startswith(prefix):
row[0] = street_name[len(prefix):].strip()
cursor.updateRow(row)
break Your code did work tho. Again thanks for the reply!
... View more
01-08-2024
09:44 AM
|
1
|
2
|
499
|
POST
|
I am trying to remove prefixes from a table. I have the following code but it removes more than just street prefix. I my code I have E "space", "E ". I need to be able to strip the prefixes so I can sort, then delete duplicates. How can I remove just the prefixes? with arcpy.da.UpdateCursor(table1,'STREET') as cursor:
for row in cursor:
#print row[0]
if row[0].startswith("S "):
#print "Deleting S"
row [0] = row[0].lstrip('S ')
elif row[0].startswith("E "):
#print "Deleting E"
row [0] = row[0].lstrip('E ')
elif row[0].startswith("W "):
#print "Deleting W"
row [0] = row[0].lstrip('W ')
elif row[0].startswith("N "):
#print "Deleting N"
row [0] = row[0].lstrip('N ')
cursor.updateRow(row)
del cursor After code runs i get. Before Code After code E Explorer xplorer E Expedition xpedtion E Exective xecutive E Exchange xchange
... View more
01-08-2024
08:57 AM
|
0
|
6
|
568
|
POST
|
I was able to accomplish what I needed with the following. PR = "PrivateRoads"
CityLimits = 'City_Limits'
arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")
unique_strings = []
with arcpy.da.SearchCursor("CitLyr", 'CITY') as cursor:
for row in cursor:
unique_strings.append(row[0])
#print (unique_strings)
for unique in unique_strings:
arcpy.SelectLayerByAttribute_management("CitLyr", 'NEW_SELECTION', "CITY = '{}'".format(unique))
arcpy.SelectLayerByLocation_management("polyLyr", 'HAVE_THEIR_CENTER_IN', "CitLyr", "", "NEW_SELECTION")
with arcpy.da.UpdateCursor(PR, 'Owner') as cursor:
for row in cursor:
row[0] = unique
cursor.updateRow(row)
... View more
07-13-2023
10:56 AM
|
0
|
0
|
290
|
POST
|
I tried the following but the print only prints one of the cities and for some reason it's populates only a a few private roads that are not inside city limits. Some of these will not be in a city. I honestly thought it would simple and wouldn't' involve .keys or dicts. import sys, os, arcpy
arcpy.env.overwriteOutput = True
workspace = "C:/temp/temp.gdb"
PR = "PrivateRoads"
CityLimits = 'City_Limits'
arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")
arcpy.management.SelectLayerByLocation("polyLyr", "HAVE_THEIR_CENTER_IN", "CitLyr", "", "NEW_SELECTION")
pr_oid_to_city_name = dict()
# Add code here to use a search cursor to scan the polyLyr and
# populate the dict that maps the pr OID to the city name
with arcpy.da.SearchCursor("CitLyr", ['OID@', 'CITY']) as search_cursor:
for oid, city_name in search_cursor:
pr_oid_to_city_name[oid] = city_name
print(pr_oid_to_city_name[oid])
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for the second argument, use False for unversioned data)
edit.startEditing(True)
# Start an edit operation
edit.startOperation()
fields = ['OID@', 'Owner']
with arcpy.da.UpdateCursor(PR, fields) as cursor:
for row in cursor:
oid = row[0]
if oid in pr_oid_to_city_name:
row[1] = pr_oid_to_city_name[oid]
cursor.updateRow(row)
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
... View more
07-12-2023
03:13 PM
|
0
|
0
|
307
|
POST
|
I need to update the PrivateRoads 'Owner' field based on what city the private road is in. I have the following but it take for ever but doesn't populate the correct city. I know I can do a spatial join but I don't want to create a separate layer. import sys, os, arcpy
import arcpy
## Works in file geodatabase
arcpy.env.overwriteOutput = True
workspace = "C:/temp/temp.gdb"
PR = "PrivateRoads"
CityLimits = 'City_Limits'
arcpy.MakeFeatureLayer_management(PR, "polyLyr")
arcpy.MakeFeatureLayer_management(CityLimits, "CitLyr")
with arcpy.da.UpdateCursor(PR, ['SHAPE@', 'OID@', 'Owner']) as cursor:
for row in cursor:
arcpy.management.SelectLayerByLocation("polyLyr", "HAVE_THEIR_CENTER_IN", "CitLyr", "", "NEW_SELECTION")
with arcpy.da.SearchCursor("CitLyr", ['CITY']) as cCursor:
for cRow in cCursor:
row[2] = cRow[0]
cursor.updateRow(row)
... View more
07-11-2023
01:15 PM
|
0
|
3
|
383
|
POST
|
I tried to incorporate you suggestion, but I ran into an issue with the Global ID. At least I believe that is the issue. TypeError: sequence size must match size of the row. import arcpy, os
project = arcpy.mp.ArcGISProject("CURRENT")
arcpy.env.workspace = os.path.dirname(project.filePath)
wp = os.path.dirname(project.filePath)
outPath = r'C:\Temp\Scratchworkspace.gdb'
scr = 'SUBS'
sortedTableName = 'SUBS_ordered'
arcpy.management.MakeFeatureLayer("SUBS", "memory\SubsLyr")
arcpy.SelectLayerByLocation_management("SUBS", "WITHIN_A_DISTANCE", "SUBJECT_PROPERTY", "1 Mile", "NEW_SELECTION")
arcpy.analysis.Select("SUBS", r"memory/SubLayer1")
#--check if output feature class exists, if not, create it
if not os.path.exists(os.path.join(outPath,sortedTableName)):
out_fc = arcpy.management.CreateFeatureclass(outPath,out_name="SUBS_ordered", template=scr)
else:
try:
arcpy.DeleteRows_management(os.path.join(outPath,"SUBS_ordered"))
except:
pass
source_fc = r"memory/SubLayer1"
sort_field_name = "SUB_NAME"
source_fc_fields = [f.name for f in arcpy.ListFields(scr)]
with arcpy.da.SearchCursor(source_fc, "*", sql_clause=(None, f"ORDER BY {sort_field_name}")) as s_cursor:
with arcpy.da.InsertCursor(sortedTableName, "*") as i_cursor:
for row in s_cursor:
i_cursor.insertRow(row) The "SUBS" is in SDE geodatabase and has GlobalID . The "SUBS_ordered" in a file geodatabase doesn't have a GlobalID. How can I leave out the GlobalID or other fields? The other difference between the two is. SubLayer1: Shape_STArea__ , Shape_STLength__ SUBS_ordered : Shape_Area, Shape_Length
... View more
05-25-2023
10:52 AM
|
0
|
1
|
238
|
POST
|
Sorry, it derived from a feature class in a geodatabase. I preform a selection by location, then I export the selected features to the shapefile.
... View more
05-19-2023
01:38 PM
|
0
|
3
|
258
|
POST
|
I will take your advice and create it in a geodatabase.
... View more
05-19-2023
11:41 AM
|
0
|
5
|
471
|
POST
|
That .dbf of that shapefile is using in a different reporting system. I was trying to save having to manually sort every time the .dbf is used. I run this reporting once a day or sometimes more often.
... View more
05-19-2023
07:26 AM
|
0
|
9
|
487
|
POST
|
Blake, this is helpful but it doesn't physically change the order of the SUB_NAME field. I have the following and it does sort the SUB_NAME field of the shapefile but somehow it spatially moves the features, not sure why. Would it best to place this shapefile in to a file geodatabase? fc = 'C:\Temp\SUBS.shp'
datatosort = []
with arcpy.da.SearchCursor(fc,"*") as cursor:
for row in cursor:
datatosort.append(row)
subindex = 2
rowindex = 0
datatosort.sort(key=lambda tup: tup[subindex])
with arcpy.da.UpdateCursor(fc,"*") as cursor:
for row in cursor:
row = datatosort[rowindex]
rowindex += 1
cursor.updateRow(row)
... View more
05-17-2023
09:47 AM
|
0
|
11
|
506
|
POST
|
I need to permanently sort a feature class field so I can add a sequential number based on the sort. I know I can use the sort tool, but I want to sort the existing feature without having to create another feature class. I am able to do the sequential number with the following but I need the feature class "Sub_Name" sorted. anyone have an example of how to accomplish this? import arcpy
from itertools import count
cntr = count()
fc = 'C:\Temp\SUBS.shp'
with arcpy.da.UpdateCursor(fc,["ID","SUB_NAME"],sql_clause=(None,"ORDER BY SUB_NAME")) as cur:
for i, j in cur:
cur.updateRow([next(cntr), j])
... View more
05-16-2023
03:13 PM
|
0
|
14
|
812
|
POST
|
Sorry for the delayed response, I got caught up on on something else. I ran you code and I see, or at least I think I do. Maybe I didn't explain well what I am trying to do. Maybe I am not going about it the correct way? The "States" field of the StSele layer will always most likely be blank. I need the States field updated, like so. if StSele "State" field is empty/blank, check sjpoints (spatialjoin)--> if sjpints field "State_1" has "Ala" I need StSele layer field "States" updated with "Alaska. Again StSele "State" field will most likely be empty or blank. My first table example was wrong, my apologies. attributes after spatial join. StStates Layer StSele layer Spaital Join State State State_1 OR Org Ala Ala Cal Cal Wa Wa What I need the StStates Layer StSele layer Spaital Join State State State_1 OR Oregon Org Ala Alaska Ala Cal California Cal Wa Washington Wa
... View more
05-11-2023
08:24 AM
|
0
|
0
|
468
|
POST
|
How can you print out the field index and fields name of all the fields in a feature class? print out something like this, 0 OBJECTID 1 Shape 2 GlobalID 3 field1 4 field2 5 field3 6 field4 7 field5 etc...
... View more
04-20-2023
01:32 PM
|
0
|
2
|
326
|
POST
|
I guess I am confused. I thought that .get would get the place_dict value, e.g, Alabama, Alaska etc. There is empty/ blanks in the StSel layers "State" field,which is why I am trying to update, and which is why I lift out ' if row[0] in place_dict.keys()' I was able to do what I needed by the following, but I would like to know how to dictionary keys and and dictionary values e.g. [0] import arcpy, os,sys
## Works in file geodatabase
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'C:\Temp\Sates.gdb'
StSele = "States"
StStates = 'Statefile'
sjpoints = "In_memory\sjpoints"
arcpy.SpatialJoin_analysis(StSele, StStates, sjpoints)
search_feats ={f[0] for f in arcpy.da.SearchCursor(sjpoints,"State_1")}
print(search_feats)
place_dict = {"AL": "Alabama", "Ala": "Alaska", "OR": "Oregon", "Cal": "California", "WA": "Wasington"}
#print(place_dict)
with arcpy.da.UpdateCursor(StSele, "State") as upd_cur:
for upd_row in upd_cur:
for key, value in search_feats.items():
val = place_dict.get(value)
upd_row[0] = val
upd_cur.updateRow(upd_row)
... View more
03-15-2023
03:37 PM
|
0
|
0
|
544
|
Title | Kudos | Posted |
---|---|---|
1 | 01-08-2024 09:44 AM | |
1 | 03-07-2023 11:46 AM | |
1 | 11-02-2020 08:24 AM | |
1 | 01-19-2022 09:19 AM | |
1 | 01-14-2022 11:41 AM |
Online Status |
Offline
|
Date Last Visited |
01-08-2024
02:04 PM
|