POST
|
I'm not sure how to implement that into the code to iterate over the tuple of attributes. What would I put in place of the 'print attr' line, to assign the current attribute in the tuple to the appropriate attribute in the centerline feature I am trying to update? In other words, what takes the place of the explicit "centerrow.Ditch_ID = assetrow.Ditch_ID" attribute assignment? cursor = arcpy.da.SearchCursor(centerline, fields)
for row in cursor:
for attr in row:
row.cursor = assetrow.? #what should this line look like?
#centerrow.Ditch_ID = assetrow.Ditch_ID
#centerrow.Event_ID = assetrow.Event_ID
#centerrow.Comments = assetrow.Comments
#centerrow.Diameter = assetrow.Diameter
#print centerrow.getValue("Ditch_ID")
#centerRows.updateRow(centerrow)
... View more
06-06-2014
06:00 AM
|
0
|
0
|
862
|
POST
|
Excellent, that worked! Is there a way to loop through a set of attributes, or just list out each, such as: centerrow.Ditch_ID = assetrow.Ditch_ID
centerrow.Event_ID = assetrow.Event_ID
centerrow.Comments = assetrow.Comments
centerrow.Diameter = assetrow.Diameter
... View more
06-05-2014
08:42 AM
|
0
|
0
|
862
|
POST
|
I am trying to copy the attribute values from one table to another, inside a loop. I am looping through one table and inside that loop I want to copy the attribute values to another table. I have copied attribute values over before, but using JoinField, but I do not want to use that in this code, as I am already looping through records. I have seen different code attempting to do this, but so far nothing has worked for me. The code I'm working with is: import arcpy import os import string import subprocess arcpy.env.workspace = r"c:\Users\matt\Desktop\Ditch\Database\Centerline.mdb" assetFeatureClass = "[Assets]" assetrows = arcpy.UpdateCursor(assetFeatureClass,"[Processed] <> 'Y'","","","") # for each row in the result set for row in assetrows: print row.getValue("Ditch_ID") #create asset #Insert code to call out ArcObjects utility... centerline = "[Pipeline_Centerline]" #update asset record attributes from asset table rows = arcpy.UpdateCursor(centerline) for row in rows: row.Ditch_ID = assetFeatureClass.Ditch_ID rows.updateRow(row) I get an error that states: Runtime error Traceback (most recent call last): File "<string>", line 24, in <module> AttributeError: 'str' object has no attribute 'Ditch_ID' and I assume what is causing this is the row.Ditch_ID = assetFeatureClass.Ditch_ID section. What I am trying to do in this section is to copy to the current record in the Centerline table the 'Ditch_ID' attribute value from the outer loop's record in the Assets table. Also, I have to do this for several attributes (both tables have the same schema) and need to either iterate over the code for each, or somehow copy several attributes at once.
... View more
06-05-2014
06:23 AM
|
0
|
9
|
2420
|
POST
|
Booya! It finally worked! I noticed that in Jake's last reply that in the SearchCursor function in the append command it reads "...(row.Asset_ID)" rather than "...auRows.Asset_ID)" and that allowed me to get through that bit of code. The rest of Jake's code for running the UpdateCursor worked and the attribute 'Processed' flag did get changed to 'Y'! thanks a million guys! The one thing I am noticing though, is that I cannot run my entire code all at once (i.e. I can't copy and paste the whole code in the ArcMap Python screen) or I will get an error stating, "Runtime error <type 'exception.NameError'>: name 'row' is not defined". If I run all the code down to the 'del row, rows' section, then after everything runs run that code, no errors, and then I can paste in the UpdateCursor section with no errors, just not all at the same time. I will eventually want to run this from an executable or in batch and not within ArcMap, and am wondering if I will get the same error or not. Any ideas why this error occurs with the whole code, but not if fed in sections? Again, I really appreciate all the help on this! Now I just have to work on how to make these records spatial, at their measure values, a whole other can of worms! FYI: final code that seems to work for me, at least piecemeal: import arcpy
from arcpy import env
import datetime
import os
env.workspace = "c:\Users\matt\Desktop\Projects\Assets\Assets.mdb"
#Make a TableView of Non-Spatial Asset records that need to be processed
arcpy.MakeQueryTable_management (["Assets"], "Asset_Line", "ADD_VIRTUAL_KEY_FIELD", "",[["Asset_ID"], ["Ditch_ID"], ["Begin_Station"], ["End_Station"]], "[Assets.Processed] <> 'Y'")
#Add queried data to Assets_Unprocessed table-to be used by ArcObjects utility to create spatial features
arcpy.Append_management ("Asset_Line", "Assets_Unprocessed")
#Copy attribute data to spatial Asset features
#Call ArcObjects utility to snap 'out of ditch' linear features to spatial Asset features
#Call ArcObjects utility to snap related assets to spatial Asset features
au = "Assets_Unprocessed"
lstIds = []
fld = "Asset_ID"
auRows = arcpy.SearchCursor(au)
for row in auRows:
lstIds.append(row.Asset_ID)
del row, auRows
table2 = "Assets"
for ID in lstIds:
rows = arcpy.UpdateCursor(table2)
for row in rows:
if row.Asset_ID == ID:
row.Processed = 'Y'
rows.updateRow(row)
... View more
04-30-2014
07:15 AM
|
0
|
0
|
66
|
POST
|
and FYI, I am running Arc 10.0 SP5 and running my python code within an ArcMap arcpy session.
... View more
04-29-2014
01:39 PM
|
0
|
0
|
483
|
POST
|
I have attached a file geodb containing the Assets table and Assets_Unprocessed that I am trying to query and update. The Asset table is queried in a MakeQueryTable for records that have not been processed, and dumped into the table Assets_Unprocessed, to be used by other utilities. Once these records have been processed, I need to flag them as such by changing the 'N' to 'Y' in the Processed attribute within the Assets table. The latest reply mentioned using a SearchCursor without the with...as statement, but I cannot get that to work either (error returns 'Cursor' object has no attribute 'Asset_ID' when I run the SearchCursor code: au = "Assets_Unprocessed"
lstIds = []
fld = "Asset_ID"
auRows = arcpy.SearchCursor(au)
for row in auRows:
lstIds.append(auRows.Asset_ID)
... View more
04-29-2014
01:24 PM
|
0
|
0
|
483
|
POST
|
Greg, Thanks for the input, I see how that could resolve my issue. I tried running that SearchCursor section of code you supplied, but an error was returned: Runtime error <type 'exceptions.AttributeError'>: 'Cursor' object has no attribute '__exit__' I am unfamiliar with the with...as statement so I'm not sure what is missing or needed here. I understand the process of building a list of Asset_ID's from Assets_Unprocessed then searching for records in Assets with those Asset_ID's, but it seems I'm having difficulty in first building that list with the SearchCursor and the use of the with...as statement.
... View more
04-29-2014
07:59 AM
|
0
|
0
|
483
|
POST
|
I am trying to update an attribute in one table, if that record is found in a MakeQuery table (I made a table view of all records not yet processed, and once processed, I want to turn the 'Processed' flag to 'Y'). rows = arcpy.UpdateCursor("Assets") for row in rows: if row.Processed == "N" and row.Asset_ID == "Asset_Unprocessed.Asset_ID": row.Processed = "Y" rows.updateRow(row) del row del rows I am trying to update in the Assets table the 'Processed' flag to 'Y', IF the Assets key is found in the 'Assets_Unprocessed' table. This does not throw an error, it just does not find and update my test data properly. The issue is the comparison to the Assets_Unprocessed table (the rest of the code works), and I've tried several different ways to type that table (full path, brackets, etc.) and can't seem to get that part to work.
... View more
04-28-2014
02:54 PM
|
0
|
10
|
1027
|
POST
|
Try changing your workspace to the following: env.workspace = "database connections\connection to db.sde" You are missing the '.sde' for the connection file, and you will not have to specify the database name and owner. Python will use the credentials stored within the SDE connection file. That. Is. Awesome! Thank you, that resolved my issue and I was able to run the script! This gets me one step closer to utilizing the 'Attach' table and photo display support I am looking for. Thanks again for your insight into this issue!
... View more
06-27-2012
07:55 AM
|
0
|
0
|
2249
|
POST
|
Have you tried inserting Z values to each X,Y vertex in the new Z enabled feature class? Where are your Z values coming from? I've added elevation to a feature class by pulling the average from a DEM to a defined polygon mask, but never actually added Z values to a feature class. There is this in the help that may give you some more information on how to go about solving your problem. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00q80000009s000000.htm Edit: Also, try setting the Z-values in the environment settings as per this thread. http://forums.arcgis.com/threads/22826-Converting-2D-Features-to-3D My issue concerns going forward and having the new feature support Z values, so that new data collection would contain Z and/or M values, not so much pushing legacy data into a newly built Z enabled feature. I am ultimately looking for a script or tool to take a customer's non Z supported database and convert it easily to a Z enabled database, which could contain 10, 20, 50 or more features, and building out all new features manually would be a huge hassle. I feel that my code above is close, but there seems to be an issue or a software defect that will not allow me to use a (non Z supported) template when creating new Featureclasses, and if that were resolved, I could automate the 'updating' of current features to support Z or M values. Is there a known or suspected defect concerning this, or is there something in my code that is wrong or missing?
... View more
06-27-2012
07:04 AM
|
0
|
0
|
1212
|
POST
|
Hi Matt, It's actually a little tricky to calculate field values from one table to another outside of ArcMap. Here is some sample code that I was able to get to work: import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
fc = "Airports"
table = "Airports__ATTACH"
# Add a field to the feature class and calculate the OBJECTIDs to this new field
arcpy.AddField_management(fc, "OBJECTID2", "LONG")
arcpy.CalculateField_management(fc, "OBJECTID2", "!OBJECTID!", "PYTHON_9.3")
# Join the new OBJECTID2 field to the attachment table (original OBJECTID will not join)
arcpy.JoinField_management(table, "FK_GUID", fc, "PK_GUID", "OBJECTID2")
# Update the REL_OBJECTID field with the OBJECTID2 values
rows = arcpy.UpdateCursor(table)
for row in rows:
row.REL_OBJECTID = row.OBJECTID2
rows.updateRow(row)
del row, rows
# Delete the OBJECTID2 fields from the feature class and table
arcpy.DeleteField_management(fc, "OBJECTID2")
arcpy.DeleteField_management(table, "OBJECTID2") I'm not sure what I am doing wrong here, I took your code, changed the env.workspace and featureclass and ATTACH table and tried to run it through the cmd prompt, and keep getting an error stating the dataset does not exist. Here is my code:
import arcpy
from arcpy import env
env.workspace = "database connections\connection to db\Fire.DBO"
fc = "Event"
table = "Event__ATTACH"
# Add a field to the feature class and calculate the OBJECTIDs to this new field
print "attempting to add field..."
arcpy.AddField_management(fc, "OBJECTID2", "LONG")
print "added field 'OBJECTID2'..."
arcpy.CalculateField_management(fc, "OBJECTID2", "!OBJECTID!", "PYTHON_9.3")
print "Calculated field..."
# Join the new OBJECTID2 field to the attachment table (original OBJECTID will not join)
arcpy.JoinField_management(table, "FK_GUID", fc, "PK_GUID", "OBJECTID2")
# Update the REL_OBJECTID field with the OBJECTID2 values
rows = arcpy.UpdateCursor(table)
for row in rows:
row.REL_OBJECTID = row.OBJECTID2
rows.updateRow(row)
del row, rows
# Delete the OBJECTID2 fields from the feature class and table
arcpy.DeleteField_management(fc, "OBJECTID2")
arcpy.DeleteField_management(table, "OBJECTID2")
and here is the error message, from the cmd prompt: C:\Users\matt\Desktop>AttachTableJoins.py attempting to add field... Traceback (most recent call last): File "C:\Users\matt\Desktop\AttachTableJoins.py", line 9, in <module> arcpy.AddField_management(fc, "OBJECTID2", "LONG") File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", ne 2625, in AddField raise e arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Table: Dataset Event does not exist or is not supported Failed to execute (AddField). I know the tables are there and there are no feature datasets in the database, so I don't know what that error message is coming from.
... View more
06-26-2012
02:24 PM
|
0
|
0
|
2249
|
POST
|
I am trying to write a python script to take an existing featureclass that does not support Z or M values and enable enable them. I know that this cannot be done on an existing featureclass (why, I'm not sure!) so I wrote my script to create a new featureclass using the original as a template, then copy over all existing data, delete the original featureclass, then rename the new one to the original name. Sounds great, but the issue now is that if you create a featureclass using a featureclass that does not support Z values, the newly created one will also not support Z values, even is explicitly set to 'ENABLED' in the code. Does anyone know how this can be done, or if there is any errors in my code:
import arcpy
from arcpy import env
import os
import datetime
env.workspace = "database connections\BASE.sde\BASE.BASE.Test"
Path = r"database connections\BASE.sde\BASE.BASE.Test\BASE.BASE."
arcpy.env.overwriteOutput = True
#Prompt user for FC to copy
FC_Copy = raw_input("Enter Featureclass name to copy: ")
#Copy original FC to a new '_Temp' FC
arcpy.CreateFeatureclass_management(env.workspace, FC_Copy+"_Temp", "POLYLINE", Path + FC_Copy, "ENABLED", "ENABLED")
print "Table " + FC_Copy + "_Temp added..."
#Copy over existing data-how to ensure ObjectID's don't change?
arcpy.Copy_management(Path + FC_Copy, Path + FC_Copy + "_Temp")
print "Copied original records to " + FC_Copy + "_Temp..."
#Delete original FC
FC = Path + FC_Copy
arcpy.Delete_management(FC)
print "Original Table, " + FC + ", has been deleted..."
#Rename copied FC to original name
arcpy.Rename_management(Path + FC_Copy + "_Temp", FC_Copy)
print "New Featureclass, " + FC_Copy + "_Temp, renamed to " + FC_Copy + ". Z-enabling script is completed successfully"
... View more
06-26-2012
09:50 AM
|
0
|
3
|
7072
|
POST
|
I am trying to write a python script to copy an attribute value from one table to an attribute in another table, based on a primary key to foreign key match. Basically, this is a workaround for relating an '_ATTACH' table to its parent table's record (the attachment record was created outside of Arc, so the 'REL_OBJECTID' attribute does not automatically get populated with the parent tables' record's 'OBJECTID', so I am trying to systematically repair this link). I cannot figure out the syntax in using an updatecurser (or even if this is the way to do this!). Let's say that I have a table, 'Table1' with a primary key, PK_GUID, field that is populated, and it has a system created attachment table, 'Table1_ATTACH' with a foreign key, FK_GUID, populated with the parent's primary key value (this is the link I can use in my where clause to link the records): I need for the attribute Table1_ATTACH.REL_OBJECTID to be populated with Table1.OBJECTID I attempted to write a script to do this, mimicking some bad code I found in my google search: #define the parent table Table2 = r"database connections\connection to db\db.DBO.Table1__ATTACH" #create a cursor on the child table's record that is linked to the parent table's record on the non-ESRI keys Rows = arcpy.UpdateCursor("db.DBO.Table1__ATTACH", "select * from db.DBO.Table1 where db.DBO.Table1.PK_GUID = db.DBO.Table1_ATTACH.FK_GUID", "", "REL_OBJECTID") for row in rows: row.REL_OBJECTID = T2.OBJECTID Am I anywhere near close on this one?
... View more
06-05-2012
10:13 AM
|
0
|
4
|
9684
|
POST
|
Just seeing if I can refresh this post, and see if anyone out there knows why ArcCatalog crashes with my code. I'm basically trying to use Python script to create a linear feature within an existing feature dataset, and using an existing feature class as a template. Solution!: I needed to resolve the spaces that were in the connection paths by placing an 'r' (MUST be lower case!) in front of each path. This code worked (I made a variable, 'Path', for the above path name to make deployment of this code more efficient): arcpy.CreateFeatureclass_management(Path, r"Exposures_Platte_Line_Test2", "POLYLINE", Path +"\BASE.BASE.Exposures_Platte")
... View more
05-18-2012
12:15 PM
|
0
|
0
|
312
|
POST
|
I GOT IT! Not sure why it took me so long or how picky python is, but this code worked: import arcpy from arcpy import env env.workspace = r"database connections\connection to KM-BASE.sde" arcpy.AddField_management("BASE.BASE.Exposures_Platte", "STATE", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "") The issue was I needed to fully qualify the table name, even though I had to physically login to that database as the schema owner. I thought that login would have resolved the name schema, but I guess not. I did have issues with a schema lock, although I was the only one logged in, but I just killed Catalog and reopened it, logged in as the schema owner, and copied the code in, and it worked! Thanks everyone for looking into this issue! As a side issue to this situation, instead of adding new fields to an existing feature class within that feature dataset, now I am trying to copy that existing feature class and paste a new feature class (this one a linear feature instead of a point feature) into that same feature dataset, using this code: import arcpy from arcpy import env env.workspace = r"database connections\connection to KM-BASE.sde" #Use existing Span (point) feature (Exposures_Platte) as template to create new linear Span feature class arcpy.CreateFeatureclass_management("database connections/connection to KM-BASE.sde", "BASE.BASE.KMC.Exposures_Platte_Line", "POLYLINE", "BASE.BASE.Exposures_Platte", "ENABLED", "ENABLED") and ArcCatalog crashes after about a minute of thinking, no error thrown. Any ideas on this issue? Solution!: I needed to resolve the spaces that were in the connection paths by placing an 'r' (MUST be lower case!) in front of each path. This code worked (I made a variable, 'Path', for the above path name to make deployment of this code more efficient): arcpy.CreateFeatureclass_management(Path, r"Exposures_Platte_Line_Test2", "POLYLINE", Path +"\BASE.BASE.Exposures_Platte")
... View more
05-18-2012
12:13 PM
|
0
|
0
|
538
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|