|
POST
|
Hi Peter, Are you rasters classified by pixel value (i.e. value > 3 equals snow covered) or by an actual field attribute? What pixel type are your rasters (i.e. floating point, signed integer)? Below is an example on how you can do this. In the example, the rasters I am working with have a pixel type of floating point. What the script does is convert the floating point rasters to integer by multiplying the raster by the number of decimal places I would like to preserve. For example, I multiply the rasters by 100 to maintain 2 decimal values. Next I add an attribute table to the raster and calculate the total number of pixels. Next I want to find the percentage of pixels with a greater value of 3.25 (which represents snow cover). I multiply this value by 100, sum all the pixels with a value greater than this and divide by the total number of pixels to find the percentage. import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
env.workspace = r"C:\temp\python\rasters"
env.overwriteOutput = True
lstRasters = arcpy.ListRasters("*")
for raster in lstRasters:
outTimes = Times(raster, 100)
outTimes.save(r"C:\temp\python\outTimes.tif")
intRas = Int(r"C:\temp\python\outTimes.tif")
intRas.save(r"C:\temp\python\intRas.tif")
arcpy.BuildRasterAttributeTable_management(r"C:\temp\python\intRas.tif", "Overwrite")
desc = arcpy.Describe(r"C:\temp\python\intRas.tif")
totalpixels = desc.height * desc.width
list = []
pixValue = 3.25 * 100
rows = arcpy.SearchCursor(intRas, "VALUE > " + str(pixValue))
for row in rows:
list.append(row.Count)
print float(sum(list)) / float(totalpixels)
del row, rows
arcpy.Delete_management(r"C:\temp\python\intRas.tif")
arcpy.Delete_management(r"C:\temp\python\outTimes.tif")
... View more
11-03-2011
07:29 AM
|
0
|
0
|
908
|
|
POST
|
You could accomplish this by performing a spatial search rather than an attribute. Try using the 'SelectLayerByLocation_management' function to do this. Here is an example: import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = True
county = "Counties"
hospitals = "Hospitals"
# Create feature layers for the selection
arcpy.MakeFeatureLayer_management(county, "county_FL", "POP2000 > 999999")
arcpy.MakeFeatureLayer_management(hospitals, "hospitals_FL")
# Find all hospitals within counties with a population >= 1,000,000
arcpy.SelectLayerByLocation_management("hospitals_FL", "INTERSECT", "county_FL")
# Create a feature class from the selection
arcpy.CopyFeatures_management("hospitals_FL", "hospitals_selection")
... View more
11-02-2011
03:57 AM
|
0
|
0
|
1018
|
|
POST
|
Hi Alaeddine, Currently, ArcSDE doesn't support columns with identity property set to True. Tables/Feature classes with this property cannot be edited in ArcMap or registered as versioned. When attempting to create non-versioned edits in ArcMap you will receive an error similar to: "Database Row Change: An unexpected failure occured. Underlying DBMS error[Microsoft sql server Native Client 10.0: Cannot insert explicit value for identity column in table 'tableName' when IDENTITY_INSERT is set to OFF.][..tableName]."
... View more
10-31-2011
02:50 AM
|
0
|
10
|
5357
|
|
POST
|
When creating parameters for a script, you need to specify the 'arcpy.GetParameterAsText' function so python knows what inputs/variables to replace. Try the following script: import arcpy, os
from arcpy import env
folder = r"S:\Projects\H107039\GIS\ModelBuilder\Geodatabases"
for (path, dirs, files) in os.walk(folder):
if ".gdb" not in path.lower():
env.workspace = path
databases = arcpy.ListWorkspaces("*", "FileGDB")
Code = 'Code'
Description = 'Desc'
Replace = 'REPLACE'
for database in databases:
arcpy.TableToDomain_management(arcpy.GetParameterAsText(0), Code, Description, database, arcpy.GetParameterAsText(1), arcpy.GetParameterAsText(2), Replace) The parameter data types you have setup within toolbox will be fine and you will not need to update these. Also, when posting code it's good to wrap CODE tags (the '#' tool) the text so that it preserves indentation.
... View more
10-27-2011
07:41 AM
|
0
|
0
|
3917
|
|
POST
|
Hi Steven, You could do something similar to below:
table = "data.dbf"
x = 1
firstfield = True
while x <= 5:
if firstfield:
arcpy.AddField_management(table, "A_Name", "Text", "", "", "10")
arcpy.AddField_management(table, "B_Name", "Text", "", "", "15")
arcpy.AddField_management(table, "C_Name", "Text", "", "", "20")
arcpy.AddField_management(table, "D_Name", "Text", "", "", "25")
firstfield = False
else:
arcpy.AddField_management(table, "A" + str(x) + "_Name", "Text", "", "", "10")
arcpy.AddField_management(table, "B" + str(x) + "_Name", "Text", "", "", "15")
arcpy.AddField_management(table, "C" + str(x) + "_Name", "Text", "", "", "20")
arcpy.AddField_management(table, "D" + str(x) + "_Name", "Text", "", "", "25")
x += 1 At the end of the code you will just need to add the 'arcpy.AddField_management' for the remainding three fields.
... View more
10-27-2011
06:22 AM
|
0
|
0
|
3395
|
|
POST
|
Sure, no problem. Previously I had the following lines of code: database = arcpy.ListWorkspaces("*", "FileGDB")
arcpy.TableToDomain_management(r"S:\Projects\H107039\GIS\Domains.gdb\Landuse_Description", "Code", "Description", database[0], "Landuse Description", "Aurecon Landuse Description", "REPLACE") Whenever you use an 'arcpy.List....' function, python will return a list of values. To select an individual value from the list you need to specify an index value. That is where 'database[0]' comes into play. The first value in the list begins with index 0, second value index 1, etc. The code above simply specifies the first geodatabase in a directory, but there could be multiple geodatabase (probably why you received the error message). Therefore I changed the code to loop through each database in the list: databases = arcpy.ListWorkspaces("*", "FileGDB")
for database in databases:
arcpy.TableToDomain_management(r"S:\Projects\H107039\GIS\Domains.gdb\Landuse_Description", "Code", "Description", database, "Landuse Description", "Aurecon Landuse Description", "REPLACE") The above code will now loop through every database in each sub directory rather than just finding the first one. If you're interested in learning python I highly recommend the course Introduction to Geoprocessing Scripts Using Python.
... View more
10-27-2011
05:25 AM
|
0
|
0
|
3917
|
|
POST
|
That was my mistake. I made an error in the code and updated it afterwards. Try the following: import arcpy, os
from arcpy import env
folder = r"S:\Projects\H107039\GIS\ModelBuilder\Geodatabases"
for (path, dirs, files) in os.walk(folder):
if ".gdb" not in path.lower():
env.workspace = path
databases = arcpy.ListWorkspaces("*", "FileGDB")
for database in databases:
arcpy.TableToDomain_management(r"S:\Projects\H107039\GIS\Domains.gdb\Landuse_Description", "Code", "Description", database, "Landuse Description", "Aurecon Landuse Description", "REPLACE")
... View more
10-27-2011
04:59 AM
|
0
|
0
|
3917
|
|
POST
|
Hi Peter, It may be easiest to do this using a python script rather than model builder. Here is an example on how to do this. The below script will iterate through each sub directory in your 'H:\Projects\H107039_PeterW\workpackages' workspace: import arcpy, os
from arcpy import env
folder = r"H:\Projects\H107039_PeterW\workpackages"
for (path, dirs, files) in os.walk(folder):
if ".gdb" not in path.lower():
env.workspace = path
databases = arcpy.ListWorkspaces("*", "FileGDB")
for database in databases:
arcpy.TableToDomain_management(r"C:\TEMP\Python\domain.dbf", "Code", "Desc", database, "City") You will just need to update the line "arcpy.TableToDomain_management(r"C:\TEMP\Python\domain.dbf", "Code", "Desc", database[0], "City")". The first variable is the path to the table containing the domain. The second variable is the field containing the domain code. Third variable is the field containing the domain description, and the 5th variable is what you would like to name the domain.
... View more
10-27-2011
03:36 AM
|
0
|
0
|
748
|
|
POST
|
Hi Joseph, Adding attachments to polygon feature classes is supported. Try adding the feature class to a blank map document, start an edit session, and then see if you are able to add an attachment through the attribute window. It may be something corrupt with the MXD that is causing the issue.
... View more
10-27-2011
03:35 AM
|
0
|
0
|
1301
|
|
POST
|
Hi Peter, It may be easiest to do this using a python script rather than model builder. Here is an example on how to do this. The below script will iterate through each sub directory in your 'H:\Projects\H107039_PeterW\workpackages' workspace: import arcpy, os
from arcpy import env
folder = r"H:\Projects\H107039_PeterW\workpackages"
for (path, dirs, files) in os.walk(folder):
if ".gdb" not in path.lower():
env.workspace = path
databases = arcpy.ListWorkspaces("*", "FileGDB")
for database in databases:
arcpy.TableToDomain_management(r"C:\TEMP\Python\domain.dbf", "Code", "Desc", database, "City") You will just need to update the line "arcpy.TableToDomain_management(r"C:\TEMP\Python\domain.dbf", "Code", "Desc", database[0], "City")". The first variable is the path to the table containing the domain. The second variable is the field containing the domain code. Third variable is the field containing the domain description, and the 5th variable is what you would like to name the domain.
... View more
10-27-2011
02:58 AM
|
0
|
0
|
3917
|
|
POST
|
I believe you will want to use the 'raw_input' function to do this. Here is an example: import arcpy, os
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = 1
county = raw_input("Please type either WARREN, WASHINGTON, OR ESSEX: ")
key = raw_input("Please type the Parcel ID: ")
# User selects COUNTY and then Print Key value, Select by Attribute tool runs
arcpy.MakeFeatureLayer_management("Parcels", "Parcels_feat")
arcpy.SelectLayerByAttribute_management("Parcels_feat", "NEW_SELECTION", "COUNTY = " + "'" + county + "'")
arcpy.SelectLayerByAttribute_management("Parcels_feat", "NEW_SELECTION", "PARCELS_ID = " + key)
arcpy.CopyFeatures_management("Parcels_feat", "Parcels_Selection")
print "Executed succesfully" This will prompt the user for a county name and a parcel ID.
... View more
10-21-2011
09:35 AM
|
0
|
0
|
661
|
|
POST
|
Hi James, I am assuming your shapefile begins with a number since it is based off of the buffer value. This could be causing the problem. When naming a shapefile/feature class, the name should not: 1. Begin with a number 2. Contain special characters (i.e. !, ?, #...) 3. Contain spaces
... View more
10-17-2011
08:00 AM
|
0
|
0
|
1361
|
|
POST
|
Awesome, your method will be much easier! I was having trouble figuring out how to convert a list to a string. Thanks for the syntax: x = ", ".join(myList)
... View more
10-17-2011
06:11 AM
|
0
|
0
|
1882
|
|
POST
|
I was able to do this by appending the feature classes to a list, writing the list to a text file, reading the text file as a string while removing the last comma, and then finally writing the string using the arcpy.AddMessage function. Here is an example of how to do this with a list of feature classes: import arcpy, os
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
# Create an empty list
list = []
# Create a text file
output = open(r"C:\temp\python\list.txt", "w")
lstFCs = arcpy.ListFeatureClasses("*")
for fc in lstFCs:
list.append(fc)
# write each feature class to the text file
for n in list:
output.write(n + " ")
# read the text file and add commas
output = open(r"C:\temp\python\list.txt", "r")
s = output.read()
s = s.rstrip()
s = s.replace(" ", ", ")
arcpy.AddMessage(s)
# close and delete the text file
output.close()
os.remove(r"C:\temp\python\list.txt")
... View more
10-17-2011
05:04 AM
|
0
|
0
|
1882
|
|
POST
|
I could not reproduce the behavior you are experiencing. Placing this syntax outside the loop created no errors, and the script executed successfully. I'm not sure why the script is running in a continuous loop for you. If I placed the 'del row, rows....' inside the while loop I would receive "NameError: name 'row' is not defined" error message. If the script is executing successfully with this error message, you can run an except to pass this error within the while loop:
try:
del row, rows, row2, rows2
except NameError:
pass Though, this may cause the script to skip over some counties and not update the 'URBPOP' field correctly.
... View more
10-17-2011
04:03 AM
|
1
|
1
|
2053
|
| Title | Kudos | Posted |
|---|---|---|
| 4 | 05-07-2020 05:14 PM | |
| 1 | 03-25-2026 04:16 AM | |
| 1 | 03-16-2026 01:00 PM | |
| 1 | 12-22-2025 10:39 AM | |
| 1 | 01-20-2026 04:04 AM |
| Online Status |
Offline
|
| Date Last Visited |
Thursday
|