|
POST
|
I am trying to use InsertCurosr to transfer rows from one table to another. Yes I could just use append but I am trying to see if the InsertCurosr is faster but I am getting the error "DescribeData: Method Field1AFieldName does not exist". Here is what I have currently. The Source Label currently does not have Field1A, Target does have the Field1A field. import arcpy,os
#### Transfer Source table rows into Target Table
arcpy.env.workspace = r'C:\Temp\Test.gdb'
Target = r"C:\Temp\Test.gdb\TargetTable"
Source = r"C:\Temp\Test.gdb\SourceTable"
arcpy.DeleteRows_management(Target)
dsc = arcpy.Describe(Target)
fields = dsc.fields
out_fields = [dsc.OIDFieldName, dsc.Field1AFieldName]
fieldnames = [field.name for field in fields if field.name not in out_fields]
with arcpy.da.SearchCursor(Source,fieldnames) as sCur:
with arcpy.da.InsertCursor(Target,fieldnames) as iCur:
for row in sCur:
iCur.insertRow(row)
... View more
06-21-2021
11:42 AM
|
0
|
10
|
3573
|
|
POST
|
I was some what able to get what I need with the following. How do I slice the path to stop at the second to last folder ? for example, C:\Cases\blah\2020 blah\yoyo\folder1 --> with the current code I get all of this. C:\Cases\blah\2020 blah\ yoyo --> I needs this populated in the path field rootPath = r'C:\Cases\2020 blah'
shps = []
field = 'Path'
for dirpath, dirnames, files in os.walk(rootPath):
for x in files:
if x == "Shapefile.shp":
shps.append(os.path.join(dirpath, x))
for fc in shps:
arcpy.AddField_management(fc, field, 'TEXT')
dirname = os.path.dirname(fc) #just the name of the folder dirname = os.path.basename(os.path.dirname(fc))
with arcpy.da.UpdateCursor(fc,field) as cur:
for row in cur:
row[0]= dirname
cur.updateRow(row)
arcpy.Merge_management(shps, r"C:\Temp\MergedTest.shp")
... View more
04-28-2021
10:31 AM
|
0
|
2
|
2784
|
|
POST
|
Oh no, my bad I have been messing with it for a few hours and must have missed it. I have the updated code and I don't get an error but it doesn't populate all of the features 'Path' field and on the ones it does populate its not the path to shapefile, it is to another folder. For example the a good path would be r'C:\Temo\blah\2020 blah\case\yoyo' but some of them are populated with r'C:\Temo\blah\2020 blah\case\Notifications' even though the shapefile is not in that 'Notifications' folder. rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
#rootPath = r'C:\Temo\blah\2020 blah\case\yoyo' #works with specific folder location
counter = 0
matches = []
field = 'Path'
for root, dirs, files in os.walk(rootPath):
for filename in files:
if filename == "shapefile.shp":
match = ( os.path.join(root, filename))
matches.append (match)
counter = counter + 1
arcpy.AddField_management(os.path.join(root,match),field, 'TEXT')
with arcpy.da.UpdateCursor(os.path.join(root,match),field) as cur:
for row in cur:
row[0]= os.path.join(root, filename)
cur.updateRow(row)
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")
... View more
04-27-2021
08:10 AM
|
0
|
0
|
2795
|
|
POST
|
With the following I can get to work with one single shapefile to a specific folder location but not with a sub folder. It also only works if the "Path' field is already there. It won't let me add a field with line 13, ERROR 000622: Failed to execute (Add Field). Parameters are not valid. ERROR 000628: Cannot set input into parameter field_precision. rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
#rootPath = r'C:\Temo\blah\2020 blah\case\yoyo' #works with specific folder location
counter = 0
matches = []
field = 'Path'
for root, dirs, files in os.walk(rootPath):
for filename in files:
if filename == "shapefile.shp":
match = ( os.path.join(root, filename))
matches.append (match)
counter = counter + 1
#arcpy.AddField_management(os.path.join(root,match),field, "Path", 'TEXT') #Erros with ERROR 000622: Failed to execute (Add Field). Parameters are not valid.
ERROR 000628: Cannot set input into parameter field_precision.
with arcpy.da.UpdateCursor(os.path.join(root,match),field) as cur:
for row in cur:
row[0]= rootPath
cur.updateRow(row)
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")
... View more
04-26-2021
02:34 PM
|
0
|
0
|
2805
|
|
POST
|
I see but I am not sure how put it together, do I put the "shapefile.shp" in a directory then use with arcpy.da.UpdateCursor to update the "Path" field with the dirnames? I guess I am uncertain on how to put it together.
... View more
04-23-2021
07:51 AM
|
0
|
0
|
2825
|
|
POST
|
I need to merge certain shapefiles with a specific name in sub directories. I can do the merge but where I am stuck on is including each shapefile specific root path to each shapefile 'Path' field before the merge. I have the following. rootpath will like below, Shapefile.shp will always be the same C:\Tem\blah\2020 blah\Case1\Shapefile.shp C:\Tem\blah\2020 blah\Case2\Shapefile.shp C:\Tem\blah\2020 blah\Notice\Shapefile.shp C:\Tem\blah\2020 blah\Use\Shapefile.shp rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
counter = 0
matches = []
for root, dirs, files in os.walk(rootPath):
for filename in files:
if filename == "shapefile.shp":
match = ( os.path.join(root, filename))
matches.append (match)
counter = counter + 1
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")
... View more
04-22-2021
02:51 PM
|
0
|
9
|
2877
|
|
POST
|
Duh, I feel dumb. Got it working with the following with your help, thanks! dict1 = dict()
with arcpy.da.SearchCursor(fc,['Pin','Perm_COUNT','P_Num']) as cursor:
for row in cursor:
if row[0] not in (None, "", " "): # forgot the [0]
dict1.setdefault(row[0],[]).append(str(row[2]))
#print (dict1)
with arcpy.da.UpdateCursor(fc,['Pin','Perm_COUNT','Permits']) as cursor:
for row in cursor:
if row[0] in dict1: #added to pass only attributes in dict
row[2] = ",".join(dict1[row[0]])
#print (",".join(dict1[row[0]]))
cursor.updateRow(row
... View more
04-22-2021
07:42 AM
|
0
|
0
|
4663
|
|
POST
|
I have the following and I having a hard time by passing some blank attributes. I know what the problem is, the problem is that row Pin has blanks and when the codes tries to populate the Permits row it throws the 'The row contains a bad value' Error. It's trying to bunch all of 'P_num' that have a blank into the Permits row. How can I get passed this? dict1 = dict()
with arcpy.da.SearchCursor(fc,['Pin','Perm_COUNT','P_Num']) as cursor:
for row in cursor:
if row not in (None, "", " "):
dict1.setdefault(row[0],[]).append(str(row[2]))
#print (dict1)
with arcpy.da.UpdateCursor(fc,['Pin','Perm_COUNT','Permits']) as cursor:
for row in cursor:
row[2] = ",".join(dict1[row[0]])
#print (",".join(dict1[row[0]]))
cursor.updateRow(row)
... View more
04-21-2021
03:04 PM
|
0
|
2
|
4735
|
|
POST
|
I have a table and in that table I have field Fld_1 and I would like to move the text after the comma to the front of the attribute and remove the comma. Not all attributes will have a comma. I am able to split the filed attrubutes but am not sure how to work with the comma ','. with arcpy.da.UpdateCursor(Table, ['Fld_1']) as cursor: for row in cursor: row[0] = " ".join(row[0].split()[:3]) cursor.updateRow(row) del cursor Example Island Village, The --> The Island Village
... View more
12-18-2020
10:06 AM
|
1
|
11
|
3141
|
|
POST
|
Sorry about that, I didn't see the option to address the syntax. Even after select Python as the syntax there is not numbers for each row. The following is given me an error and it seems no what I do the script always stops at a certain process no matter if I change the process. but I just run just the #Creat SelParsA1 section in the projects python window it will run but not in the script so I am not sure what I am doing wrong. import arcpy, string, os
from importlib import reload
#Zooms to selected parcel in layout
#arcpy.env.addOutputsToMap = True
arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
lyr = map.listLayers("TAXLOTS")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("MAPFRAME_ELEMENT")[0]
arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)
values = arcpy.GetParameterAsText(0)
fieldName = "DXF_TEXT"
values = values.split(";") # split values into list
values = ["'{0}'".format(v) for v in values] # add single quotes
whereClause = "{0} IN ({1})".format(fieldName, ",".join(values))
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)
Name = arcpy.GetParameterAsText(1)
for lyt in aprx.listLayouts():
for elm in lyt.listElements("TEXT_ELEMENT","Text"):
if elm.text == "Template":
elm.text = (Name)
#Zooms to selected parcel
ext = mf.getLayerExtent(lyr)
mf.camera.setExtent(ext)
mf.camera.scale *= 16
#Exports the selected parcels for SUBJECT_PROPERTY.
if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")
arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")
#Update Subject Property symbology
#layer = m.listLayers('SUBJECT_PROPERTY')[0]
#arcpy.ApplySymbologyFromLayer_management(str(layer), "Symbology_SUBJECT_PROPERTY.lyrx")
del aprx, map, lyr, mf, lyt
'''
#Remove layer Subject property
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]# assumes data to be added to first map listed
lyr = m.listLayers()[0]
m.removeLayer(lyr)
del aprx, m,lyr,
'''
#Udpate Subject Property datasource
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = map.listLayers("SUBJECT_PROPERTY")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT",'*')[0]
try:
cp = lyr.connectionProperties
cp['connection_info']['database'] = wp
cp['dataset'] = 'SUBJECT_PROPERTY.shp'
lyr.updateConnectionProperties(lyr.connectionProperties, cp)
except:
pass
del aprx, map, lyt, lyr, mf
reload(arcpy)
arcpy.ClearWorkspaceCache_management()
#Creat SelParsA1
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = map.listLayers("SUBJECT_PROPERTY")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT",'*')[0]
arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)
TAX = "TAXLOTS"
SP2 = "SUBJECT_PROPERTY"
arcpy.SelectLayerByLocation_management(TAX, "WITHIN_A_DISTANCE", SP2, "600 Feet", "NEW_SELECTION")
try:
if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
arcpy.SelectLayerByAttribute_management("TAXLOTS", "REMOVE_FROM_SELECTION", "DXF_TEXT = ' ' OR DXF_TEXT IS NULL") #"\"DXF_TEXT\" = ' '"
arcpy.FeatureClassToFeatureClass_conversion("TAXLOTS", wp, "SelParsA1")
except:
pass
arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")
del lyr
lyr = map.listLayers("SelParsA1")[0]
try:
cp = lyr.connectionProperties
cp['connection_info']['database'] = wp
cp['dataset'] = 'SelParsA1.shp'
lyr.updateConnectionProperties(lyr.connectionProperties, cp)
except:
pass Error: Traceback (most recent call last):
File "D:\GIS Folder\Python_ArcGISPro\NotificationMapScripts\NotificationMapScriptTest_E.py", line 86, in <module>
arcpy.SelectLayerByLocation_management(TAX, "WITHIN_A_DISTANCE", SP2, "600 Feet", "NEW_SELECTION")
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 8909, in SelectLayerByLocation
raise e
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 8906, in SelectLayerByLocation
retval = convertArcObjectToPythonObject(gp.SelectLayerByLocation_management(*gp_fixargs((in_layer, overlap_type, select_features, search_distance, selection_type, invert_spatial_relationship), True)))
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.
The base table definition string "SUBJECT_PROPERTY" is invalid.
The database was not found.
Failed to execute (SelectLayerByLocation).
Failed to execute (NotificatonMapScript). Runs in projects python window but not not included in the script posted above import arcpy, string, os
from importlib import reload
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyt = aprx.listLayouts()[0]
lyr = m.listLayers("SUBJECT_PROPERTY")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT",'*')[0]
arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)
TAX = "TAXLOTS"
SP2 = "SUBJECT_PROPERTY"
arcpy.SelectLayerByLocation_management(TAX, "WITHIN_A_DISTANCE", SP2, "600 Feet", "NEW_SELECTION")
try:
if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
arcpy.SelectLayerByAttribute_management("TAXLOTS", "REMOVE_FROM_SELECTION", "DXF_TEXT = ' ' OR DXF_TEXT IS NULL") #"\"DXF_TEXT\" = ' '"
arcpy.FeatureClassToFeatureClass_conversion("TAXLOTS", wp, "SelParsA1")
except:
pass
arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")
del lyr
... View more
12-02-2020
10:45 AM
|
0
|
0
|
1226
|
|
POST
|
It appears that the toolbox script has issues in Pro python, this same script runs fine in ArcMap. I got past the Symobology by deleting m, lyr, lyt, mt after the first process but I had to re-add them before the Symobology process. Not sure why it's holding on to these, is there a way to clear the processes in Pro? Changed the last process of ApplySymobologyFromLayer_Managment to Add field #Add field to lyr SP = "SUBJECT_PROPERTY" arcpy.AddField_management(SP,"BUFF_DIST","DOUBLE") SP is a layer that is created by the first process and is in my layout in Pro, it's not being used by any process other than it's in the current layout . I do see 4 "SUBJECT_PROPERTY.shp.*****.11336.8984.sr.lock" in the explorer. Error after trying to use Add Field with the same script; ERROR 000464: Cannot get exclusive schema lock. Either being edited or in use by another application or service. Failed to execute (AddField).
... View more
11-16-2020
03:45 PM
|
0
|
0
|
1292
|
|
POST
|
I was able to move the layer. Thanks. aprx = arcpy.mp.ArcGISProject("CURRENT") m = aprx.listMaps()[0] for lyr in m.listLayers(): print("Map: {0} Layers".format(lyr.name)) for lyr in m.listLayers(): if lyr.name == "Sections": moveLayer = lyr if lyr.name == "Taxparcels": refLayer = lyr m.moveLayer(refLayer,moveLayer,"BEFORE")
... View more
11-13-2020
12:47 PM
|
0
|
0
|
4918
|
|
POST
|
I tried to use the following but get error. import arcpy aprx = arcpy.mp.ArcGISProject("CURRENT") m = aprx.listMaps()[0] #for lyr in m.listLayers(): m.moveLayer("Taxparcels","Sections","BEFORE") Error; Traceback (most recent call last): File "<string>", line 7, in <module> File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\utils.py", line 191, in fn_ return fn(*args, **kw) File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py", line 1802, in moveLayer return convertArcObjectToPythonObject(self._arc_object.moveLayer(*gp_fixargs((reference_layer, move_layer, insert_position), True))) ValueError: Sections
... View more
11-13-2020
11:46 AM
|
0
|
0
|
4921
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 08-01-2024 07:19 AM | |
| 1 | 07-26-2024 09:38 AM | |
| 1 | 01-08-2024 09:44 AM | |
| 1 | 03-07-2023 11:46 AM | |
| 1 | 11-02-2020 08:24 AM |
| Online Status |
Offline
|
| Date Last Visited |
07-14-2025
07:49 AM
|