The intent is to get two lines of arcpy, one for each of these tasks, that I can use in a loop and apply to all the layers. There is a large group of layers (over 40), for each of which I need to (1) add an area column to the attribute table and then (2) calculate the area using "Calculate Geometry". This saves the new data into the actual dataset.ĬursorFieldNames = arcpy.da.UpdateCursor(tempPgs,CursorFieldNames)ĪreaValue = row.I have a large project I am working on and am stumped on one step. The full list of tokens is at the arcpy.da.UpdateCursor help page.Īs before, your output row is stored in a temporary scratch space until you call the updateRow method on the cursor. or Using a "simpler" token greatly improves performance compared to which contains all of the geometry information. When reading geometry, you can choose one of a number of geometry tokens, e.g. arcpy.da.UpdateCursor also has better performance than arcpy.UpdateCursor, partially because it skips unimportant fields, especially geometry. This requires some more work up-front, but the resulting row objects are Python lists, which makes it easier to read and write data when iterating through cursor rows. arcpy.da.UpdateCursor) you need to pass in a list of field names as the second parameter in the cursor constructor. When using the new cursors in the data access module (i.e. Row.setValue("Shape_area",AreaValue) #Write area value to field GeometryField = arcpy.Describe(tempPgs).shapeFieldName #Get name of geometry fieldĪreaValue = row.getValue(geometryField).area #Read area value as double This saves the new data into the actual dataset. Your output row is stored in a temporary scratch space until you call the updateRow method on the cursor. You need to use the getValue and setValue methods to get the geometry from the row (as a geometry object and set the area value to the row as a float. arcpy.UpdateCursor) the cursor object is an iterable containing row objects. Here's the Python code I would use for this method: tempPgs = "LayerName"Īrcpy.AddField_management(tempPgs, "Shape_area", "DOUBLE") In your expression, you can specify the unit of the returned area by using the flag, replacing SQUAREKILOMETERS with the units on the Calculate Field help page. This returns a Python list object, which cannot be cast into a float object. The expression you had before performed a split() command on the result of !SHAPE.AREA!. When accessing the properties of the Geometry object using like in !shape.area!, you should use the Python 9.3 parser. This is specified in the third parameter of the Calculate Field geoprocessing tool. When using field calculator, there are three different expression types that use different expression parsers. Some of this is borrowed from my previous answer about using SearchCursor. There are three different ways to find and store polygon area into a feature class with arcpy: 1) field calculator, 2) "classic" arcpy cursors, and 3) arcpy.da cursors. Grd2 = nDD.get(grd1) # GridID on the polygon downstreamĪrcpy.Merge_management(, tempMerged)Īrcpy.Dissolve_management(tempMerged, tempPgs)Īrcpy.AddField_management(tempPgs, field, 'DOUBLE')Īrcpy.DeleteField_management(tempPgs, fd)Īrcpy.Append_management(tempPgs, outpgs, "TEST")Īrcpy.Append_management(tempPgs, outpgs, "NO_TEST") Grd1 = prow.GridID # GridID on the current polygon Polyrows = arcpy.SearchCursor(inpgs, where1) # Identify and search for shapes smaller than minimum area Relevant parts of my current code looks like this: #/./Īrcpy.AddField_management(outpgs, 'Shape_area', 'LONG') Running ArcGIS Basic 10.1 SP1 with Python 2.7 on Windows 7. arcpy.AddField_management(tempPgs, "Shape_area", 'DOUBLE')Īrcpy.CalculateField_management(tempPgs, "Shape_area", exp) Modified from the help pages I thought the following would work, but so far no luck. I have also tried the Field calculator from Python. Any alternative solution that accomplishes the same task (getting the area value into correct field) will also work. I was planning on using arcpy.updateCursor to insert the value once it's calculated (there's only one feature in the FC at this stage), so easiest is if it can be returned as a variable. I would have thought this to be a quite common and simple task, but despite a lot of Googleing I have been unable to find a working solutions so far. The polygon is stored in a regular shapefile, and is projected. I create a new polygon from merging two together, and I'd like to add the area of the resulting polygon to a field in the output file. I am trying to calculate the area of a polygon within my Python script.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |