I am getting an error "Cannot acquire a lock" when trying to calculate values in multiple fields.

19181
16
04-19-2018 10:36 AM
BobNoyes
Occasional Contributor

So, I have created a script that adds 6 fields to a shapefile: Situs_city, Situs_St, Situs_Zip, Latitude, Longitude, and GIS_Acres. Further, the script is set to calculate the values for the Latitude, Longitude, and GIS_Acres fields. The script will work fine until after creating the GIS_acres field. Once this field is created the next step is to calculate and that is when I get an Error 999999: cannot acquire a lock. The weird aspect is once in a while the script will run all the way through just fine... anyway, I am stumped. I am pretty new to creating python scripts. If anyone has a suggestion that would be awesome.

import arcpy
import os

# Step 1  -  Copy new [Taxlots_TEST_DeleteME.shp] from \\cove\Department Shares\Common\Assessor\ArcGIS to E:\STAGING (IKRIT)
#set variables

in_data = "S:\Common\Assessor\ArcGIS\Taxlots_TEST_DeleteME.shp"
Shapefile = "E:\\STAGING\\Taxlots_TEST_DeleteME.shp"
#out_data = "E:\STAGING\Taxlots_TEST_DeleteME.shp"
arcpy.Copy_management(in_data, Shapefile)
print "Step 1 complete"

#Step 2: Add Situs fields
# Create a new field - Situs_City (string, 25)
arcpy.AddField_management(Shapefile, "Situs_City", "TEXT", "","","25","","NULLABLE","NON_REQUIRED","")
print "Step 2a: Add Situs_City complete."

# Create a new field - Situs_St (string, 2)
arcpy.AddField_management(Shapefile, "Situs_St", "TEXT", "#", "#", "2", "#", "NULLABLE", "NON_REQUIRED", "#")
print "Step 2b: Add Situs_St complete."

# Create a new field - Situs_Zip (string, 10)
arcpy.AddField_management(Shapefile, "Situs_Zip", "TEXT", "#", "#", "10", "#", "NULLABLE", "NON_REQUIRED", "#")
print "Step 2c: Add Situs_Zip complete."
print "Step 2: Create situs fields complete."

# Step 3: Create and calculate Latitude and Longitude fields.
latLonRef = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
Taxlot_shp = "E:\STAGING\Taxlots_TEST_DeleteME.shp"
featureClassesList = Taxlot_shp.split(";")
field_Type = "DOUBLE"
field_precision_1 = 12
field_scale_1 = 8

for featureClass in featureClassesList:
 arcpy.AddMessage("Calculating XY coordinates for: " + featureClass)
 arcpy.AddField_management(featureClass, "Latitude", field_Type, field_precision_1, field_scale_1)
 arcpy.AddField_management(featureClass, "Longitude", field_Type, field_precision_1, field_scale_1)
 rows = arcpy.UpdateCursor(featureClass, "", latLonRef)
for row in rows:
 feat = row.getValue("shape")
 cent = feat.centroid
                # To get the polygon area: cent = feat.area
 row.Latitude = cent.Y
 row.Longitude = cent.X
 rows.updateRow(row)
  #arcpy.AddMessage(str(lat) + ", " + str(lon))
print "Step 3: Add Lat and Long complete"

# Step 4: Create a new field - GIS_Acres (Double, 15, 3)
Shapefile3 = "E:\STAGING\Taxlots_TEST_DeleteME.shp"
#Set local variables
field_Name = "GIS_Acres"
field_Type = "DOUBLE"
field_Precision = 15 #total number of digits stored
field_Scale = 4 #number of decimals places

arcpy.AddField_management(Shapefile3, field_Name, field_Type, field_Precision, field_Scale)
arcpy.CalculateField_management(Shapefile3, field_Name, '!SHAPE.area@ACRES!', "PYTHON_9.3")
print "Step 4: Calculate acres complete"

print "Congratulations! You have completed adding fields to to the Taxlots feature class. "

0 Kudos
16 Replies
BobNoyes
Occasional Contributor

Thanks for responding! The code in lines 48-67 was from the old configuration I put together and I am not sure how to rewrite it to just calculate the Latitude and longitude fields. My goal is to do in the script what I would do if I right clicked on that field in the attribute table and ran the calculate geometry tool for the x and y coordinates using the wgs84 coordinate system. Nothing fancy.

I think your add geometry attributes suggestion looks like just the thing! I will give it a shot.

Bob Noyes

GIS Analyst

Josephine County

541-474-5244

GIS Webpage<http://www.co.josephine.or.us/SectionIndex.asp?SectionID=129>

rnoyes@co.josephine.or.us<mailto:rnoyes@co.josephine.or.us>

“A road map always tells you everything, except how to refold it<http://thinkexist.com/quotation/a_road_map_always_tells_you_everything_except_how/156219.html>” unknown

0 Kudos
BobNoyes
Occasional Contributor

Okay, I am feeling a bit dense here.... It might be obvious that I am not a programmer 🙂 but using the syntax for the Add Geometry Tool I get:

import arcpy

#Set variable
out_fc = "Database Connections/jocoA_Staging.sde/jocoA_Staging.DBO.Assessor/jocoA_Staging.DBO.Taxlots_TEST_DeleteME"
latLonRef = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

#set workspace
arcpy.env.workspace = "Database Connections/jocoA_Staging.sde/jocoA_Staging.DBO.Assessor/jocoA_Staging.DBO.Taxlots_TEST_DeleteME"


#populate Latitude field
arcpy.AddGeometryAttributes_management("Latitude", "CENTROID_INSIDE", "", "", latLonRef)
print "Step complete"

As you might guess I get an error:

Runtime error  Traceback (most recent call last):   File "<string>", line 22, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 2170, in AddGeometryAttributes     raise e ExecuteError: ERROR 000622: Failed to execute (Add Geometry Attributes). Parameters are not valid. ERROR 000628: Cannot set input into parameter Coordinate_System.

Two (perhaps more) things come to mind: 1) env.workspace to a feature class may not be copacetic and 2) I am not calling the latitude field correctly. 

The original way I had this coded worked fine when ran separately but in the script with the other components it would get the lock error. 

What am I missing?

0 Kudos
DanPatterson_Retired
MVP Emeritus

latlongref.... http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/add-geometry-attributes.htm

Use 'raw' formatting (the little 'r') ... plus... provide the rest of the path, it is incomplete

latLonRef = r"????????Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

RebeccaStrauch__GISP
MVP Emeritus

I’m writing on an iPad so may have typos.p or bad spacing.

A few things.  The first argument for the command is the input feature, not a field name. 

The command will ADD new fields based on the second argument 

arcpy.AddGeometryAttributes_management(out_fc, "CENTROID_INSIDE", "", "", latLonRef)

You may want to copy you few records from the feature class to a temp file geodatabase (fgdb) and test that first.  SDE may add some restrictions for other reasons (permissions, locks).  Get it worked in the fgdb so you can see the logic, then try it on a copy in sde.  I think once you see how it adds the field it will make more sense.

BobNoyes
Occasional Contributor

Gold star for you! I was wondering about the file geodatabase vs SDE and was considering trying it with the FGDB. You motivated me. I set up a feature dataset in wgs 84 since I was having errors with the projection portion of the script, even after Dan's reminder to type in a complete path (doh!). Here is what I came up with:

#Set variable
out_fc = "E:\STAGING\Taxlots_staging.gdb\Staging\Taxlots_TEST_DeleteME"
#latLonRef = "E:\Coordinate_System\World\WGS 1984.prj"

#set workspace
arcpy.env.workspace = r"E:\STAGING\Taxlots_staging.gdb\Staging"

#Step 1: populate Latitude field
arcpy.AddGeometryAttributes_management(out_fc, "CENTROID_INSIDE", "", "", "")
print "Step 1 complete"

#Step 2: Rename INSIDE_X to Longitude
arcpy.AlterField_management(out_fc, "INSIDE_X", "Longitude", "Longitude")
print "Step 2 complete"

#Step 3: Rename INSIDE_Y to Latitude
arcpy.AlterField_management(out_fc, "INSIDE_Y", "Latitude", "Latitude")
print "Step 3 complete"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks for your help!!

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Glad you were able to resolve your issue. Remember to mark all helpful comments and then one as answered or a if none, then mark as assumed answered. That will close this thread.

DanPatterson_Retired
MVP Emeritus

For geometry etc... sounds like a job for Pro... Calculate Fields