POST
|
Dear all, System: Windows Vista ArcGIS 9.3 Workstation Python 2.5 I want to know any python code for ???Export Map??? (under file tab) to TIF in ArcMap. I want to save time for doing it for 100 shape files. Thank you. Elaine
... View more
12-09-2013
02:25 AM
|
0
|
2
|
597
|
POST
|
Hello System Windows Vista / ArcGIS 9.3 I have ten shape files. Each of them has the fields starting with D in the attribute table. (D8739, D1983) Some have 10 D fields, and other have about 20. Now I want to delete the D fields using python. However, no D field was deleted after running the code below. Please kindly help to modify it and thank you in advance ##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds ##Created By: Elaine Kuo ##Date: 26/05/2012 #Import standard library modules import arcgisscripting import os #Create the Geoprocessor object gp = arcgisscripting.create(9.3) #Set the workspace. gp.Workspace= "H:/temp_D/test_1" #gp.Workspace= "H:/temp_stage_2_3_polygon_limit_1984_no_R_O/P_Turdidae_22" #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D" #outWorkspace= "H:/temp_stage_2_3_polygon_limit_1984_no_R_O" #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses() # Loop through every item in the list that was just generated for fc in fcs: # Break out the name, no path or extension, using the describe object. desc = gp.describe(fc) featureName = desc.name # Make temporary featureclasses gp.MakeFeatureLayer(fc,"lyr") # Get a list of the fields in the featureclass fields = gp.ListFields("lyr") # Loop through every item in the list that was just generated for field in fields: if field.name[1:4] == "D": gp.deletefield ("lyr", field.name) #Validate the new feature class name for the output workspace. #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace) # copy feature #gp.CopyFeatures("lyr", OutFeatureClass) # clear memory of layers gp.Delete("lyr") gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
... View more
07-27-2013
10:41 PM
|
0
|
2
|
2483
|
POST
|
You forget the 2nd parameter in Calculate Field names the field to calculate values for. You have named this "ALL"; so what your inner 'for' loop does is add to an already selected set on your feature layer (creates a new selection on the 1st iteration), then for whatever species ID was found to be populated (1), "ALL" is then calculated as 1. The subtlety I think you're missing here is your field calculation doesn't do a sum of any kind. It doesn't matter if performing the calculation inside or outside the loop (except it is more efficient to do it outside, after the selection is 'compiled', for lack of a better word). An example with a single row: speciesA, speciesB, speciesC, ALL 1, 0, 1, 1 Of course, the value of ALL for this row is 1, correct? (This is not a sum.) So you can see, if you ran the calculation within your 'for' loop, you'd be calculating the value to 1 twice, once for speciesA and once for speciesC. (which of course is not necessary) Wayne Thanks for reminding. 1. Your explanation on putting the gp.CalculateField outside the loop is very clear. I will modify my code according to your advice. 2. sorry for misusing the word "sum." Actually I just want to record whether the GridCell is 1, not the total number of "1" summed up. Elaine
... View more
07-18-2013
08:12 PM
|
0
|
0
|
1685
|
POST
|
gp.CalculateField("lyr", "All", "1", "PYTHON_9.3") Run this outside your loop, then follow that with another 'select by attributes' - I believe it has a 'switch selection' parameter. Then run your field calculation again, as in: => I think the "gp.CalculateField" is in need in the loop, because the information of every field of Species ID (Dxxxx) needs to be updated in the field "All." The field "All" is a summary field of the multiple Species ID (Dxxxx).
... View more
07-18-2013
03:43 PM
|
0
|
0
|
1685
|
POST
|
Hello Wayne, I tested my code and it worked well. Actually I want to have a summary field of the presence (1) of species ID (column) in the GridCell ID (row). In other words, the GridCell of the field "All" should be assigned as 1, as long as the GridCells of D8729, D6745, D2765 is 1. Thanks for reminding and help, Wayne.
##Script Name: calculate sum
##Description: calculate sum of merged range sizes of a taxonomy of migratory birds
##Created By: Elaine Kuo
##Date: 07/18/2015
#Import standard library modules
import arcgisscripting
import os
#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
#Set the workspace.
gp.Workspace= "H:/temp_D/test_1"
#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp_D/test"
#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()
# Loop through every item in the list that was just generated
for fc in fcs:
# Break out the name, no path or extension, using the describe object.
desc = gp.describe(fc)
featureName = desc.name
# Add a field to this shapefile, of type LONG
gp.AddField (fc, "All", "Long", 10,10)
# Make temporary featureclasses
gp.MakeFeatureLayer(fc,"lyr")
# Get a list of the fields in the featureclass
fields = gp.ListFields("lyr", "D*", "Long")
# Loop through every item in the list that was just generated
for field in fields:
gp.toolbox = "Data Management"
# Select records to be copied (C*, i.e. C7658)
query = "\"%s\" = 1" % field.Name
gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query)
# copy values in existing fields to the new field
gp.CalculateField("lyr", "All", "1", "PYTHON_9.3")
#Validate the new feature class name for the output workspace.
#OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)
# clear memory of layers
gp.Delete("lyr")
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
E.
... View more
07-18-2013
02:48 PM
|
0
|
0
|
1685
|
POST
|
Hello System Windows Vista ArcGIS 9.3 Now I have a shape file. Its attribute table consisted of fields of GridCell ID (GID) and species ID starting with D. (Dxxxx (such D8729, D6745, D2765)) The rows are GridCell ID (0-4800). Each cell in the attribute table has either 1 and 0. I would like to create a new field called All. In the field All (Long 9, 9), the cell would should be assigned 1 when the cell of Dxxxx is 1. Otherwise, the cell would be 0. The code can run the requirement above for one field. Please kindly advise how to add the loop process for the multiple fields of Dxxxx Thank you in advance. the code ##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds ##Created By: Elaine Kuo ##Date: 07/18/2015 #Import standard library modules import arcgisscripting import os #Create the Geoprocessor object gp = arcgisscripting.create(9.3) #Set the workspace. gp.Workspace= "H:/temp_D/test_1" #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D/test" #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses() # Loop through every item in the list that was just generated for fc in fcs: # Break out the name, no path or extension, using the describe object. desc = gp.describe(fc) featureName = desc.name # Add a field to this shapefile, of type LONG gp.AddField (fc, "All", "Long", 10,10) # Make temporary featureclasses gp.MakeFeatureLayer(fc,"lyr") # Get a list of the fields in the featureclass fields = gp.ListFields("lyr", "D*", "Long") # Loop through every item in the list that was just generated for field in fields: gp.toolbox = "Data Management" # Select records to be copied (C*, i.e. C7658) query = "\"%s\" = 1" % field.Name gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query) # copy values in existing fields to the new field gp.CalculateField("lyr", "All", "1", "PYTHON_9.3") #Validate the new feature class name for the output workspace. #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace) # clear memory of layers gp.Delete("lyr") gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
... View more
07-18-2013
01:16 AM
|
0
|
7
|
3479
|
POST
|
System: ArcGIS 9.3 and Vista Hello I am using the function add join to combine three shape files into one shape file. (by python script) Each of them has about 650 columns in the attribute table. However, the add join always failed. The error message is Could not load data from the data source If you can correct the problem, click the refresh button to refresh data. Possible problems can include bad network connection, invalid field, etc. An invalid SDE statement was used. (I am sure nothing to do with invalid field,* because all the shape files were produced by the add join method. ) I am wondering if the cause was the over-limit column number. If so, please kindly advise alternative methods to complete the combination. Thank you. Elaine
... View more
07-10-2013
10:28 PM
|
0
|
1
|
519
|
POST
|
System: ArcGIS 9.3 and Vista Hello I am using the function add join to combine three shape files into one shape file. (by python script) Each of them has about 650 columns in the attribute table. However, the add join always failed. The error message is Could not load data from the data source If you can correct the problem, click the refresh button to refresh data. Possible problems can include bad network connection, invalid field, etc. An invalid SDE statement was used. (I am sure nothing to do with invalid field, because all the shape files were produced by the add join method. ) I am wondering if the cause was the over-limit column number. If so, please kindly advise alternative methods to complete the combination. Thank you. Elaine
... View more
07-10-2013
10:26 PM
|
0
|
2
|
602
|
POST
|
Thank you very much. Eventually the code worked very well.
... View more
07-02-2013
03:42 AM
|
0
|
0
|
396
|
POST
|
Hello, Thanks for the immediate response. However, all ID_* fields remained after I ran the code. (no error message) We need more try to figure out how to make the "delete multiple fields" work.:) Elaine The whole code is
################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################
#Import standard library modules
import arcgisscripting
import os
#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"
#Set the input workspace
workingfolder= "H:/temp_D/test"
gp.Workspace = workingfolder
#Set the output workspace
outputfolder= "H:/temp_D"
outWorkspace= outputfolder
source_fc = "H:/temp_D/testt/A_grid.shp"
#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()
gp.Toolbox = "Data Management"
# Make a FeatureClass layer
gp.MakeFeatureLayer_management (source_fc, "source_lyr")
# A counter to separate the first time through the loop from all other times
counter = 0
# Loop through every item in the list that was just generated
for fc in fcs:
if counter == 0:
# set up first Layer for AddJoin
gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")
else:
# set up second and following Layers for AddJoin
gp.AddJoin_management("source_lyr", "A_grid.GID", fc, "GID", "KEEP_ALL")
counter += 1
# convert a layer to a featureclass
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"
gp.CopyFeatures_management ("source_lyr", out_shape)
# delete Field (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
fieldList = gp.ListFields(out_shape)
for field in fieldList:
if field.name[1:3] == "ID_":
gp.deletefield (out_shape, field.name)
# Don't bother removing 50 joins, just kill the layer
# clear memory of layers
gp.Delete("source_lyr")
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
... View more
07-01-2013
07:13 PM
|
0
|
0
|
396
|
POST
|
################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################
#Import standard library modules
import arcgisscripting
import os
#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"
#Set the input workspace
workingfolder= "H:/temp_D/test"
gp.Workspace = workingfolder
#Set the output workspace
outputfolder= "H:/temp_D"
outWorkspace= outputfolder
source_fc = "H:/temp_D/testt/A_grid.shp"
Entity = "H:/temp_D/test/geoc0283.shp"
#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()
gp.Toolbox = "Data Management"
# Make a FeatureClass layer
gp.MakeFeatureLayer_management (source_fc, "source_lyr")
$ A counter to separate the first time through the loop from all other times
counter = 0
# Loop through every item in the list that was just generated
for fc in fcs:
if counter == 0:
# set up first Layer for AddJoin
gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")
else:
# set up second and following Layers for AddJoin
gp.AddJoin_management("source_lyr", "A_grid.GID", fc, "GID", "KEEP_ALL")
counter += 1
# convert a layer to a featureclass
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"
gp.CopyFeatures_management ("source_lyr", out_shape)
# delete Field (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_1; GID_1")
# Don't bother removing 50 joins, just kill the layer
# clear memory of layers
gp.Delete("source_lyr")
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
Thanks a lot. The code worked wonderfully for three shapefiles. As for delete fields, I am thinking if wildcard will help in this condition. (The field names of the three AddJoin bird range are "FID_1; GID_1", "FID_12; GID_12", and "FID_12_13; GID_12_13") Therefore, I replaced the gp.delete with the following code.
# delete Field (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_*; GID_*")
The code worked well, but none of the three FID and GID fields were deleted. Please kindly help and thank you. (It is the first time my problem can be solved so quickly. Thanks greatly.)
... View more
07-01-2013
03:37 PM
|
0
|
0
|
607
|
POST
|
################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################
#Import standard library modules
import arcgisscripting
import os
#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"
#Set the input workspace
workingfolder= "H:/temp_D/test"
gp.Workspace = workingfolder
#Set the output workspace
outputfolder= "H:/temp_D"
outWorkspace= outputfolder
source_fc = "H:/temp_D/testt/A_grid.shp"
Entity = "H:/temp_D/test/geoc0283.shp"
#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()
gp.Toolbox = "Data Management"
# Make a FeatureClass layer
gp.MakeFeatureLayer_management (source_fc, "source_lyr")
# Loop through every item in the list that was just generated
for fc in fcs:
# have Layers for AddJoin
gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")
# convert a layer to a featureclass
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"
gp.CopyFeatures_management ("source_lyr", out_shape)
# delete Field (need to clean up with a lot more ID fields that the code you have below since now there are 50 GID values.)
gp.deletefield (out_shape, "FID_1; GID_1")
# Don't bother removing 50 joins, just kill the layer
# clear memory of layers
gp.Delete("source_lyr")
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
Thanks. Here are the test report: 1. For grid and 1 bird range shape file => the code worked well. 2. For grid and 2 bird range shape files=> the error message: gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL") ExecuteError: ERROR 999999: Error executing function. A locator with this name does not exist. Failed to execute (AddJoin). Please kindly help.
... View more
07-01-2013
03:25 PM
|
0
|
0
|
607
|
POST
|
Great. So this is the core of any loop you would build if you needed to process multiple bird range shape files against the grid. Thanks for the encouragement. I want "one final shape file that has 50 fields for all of those bird ranges." Actually, I tried the below code for a grid to a bird range only. It failed in RemoveJoin, saying "ERROR 000800: The value is not a member of geoc0283. Failed to execute (RemoveJoin)." (geoc0283 is the name of one bird range).
################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##01 July 2013
################################################## #######################
#Import standard library modules
import arcgisscripting
import os
#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"
#Set the input workspace
workingfolder= r"H:/temp_D/test"
gp.Workspace = workingfolder
#Set the output workspace
outputfolder= r"H:/temp_D"
outWorkspace= outputfolder
source_fc = r"H:/temp_D/testt/A_grid.shp"
Entity = r"H:/temp_D/test/geoc0283.shp"
#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()
# Loop through every item in the list that was just generated
for fc in fcs:
gp.Toolbox = "Data Management"
# Make a FeatureClass layer
gp.MakeFeatureLayer_management (source_fc, "source_lyr")
# have Layers for AddJoin
gp.AddJoin_management("source_lyr", "GID", fc, "GID", "KEEP_ALL")
# convert a layer to a featureclass
out_name = gp.Describe("source_lyr").name
out_name = gp.ValidateTableName(out_name, outputfolder)
out_shape = outputfolder + "/" + out_name + ".shp"
gp.CopyFeatures_management ("source_lyr", out_shape)
# delete Field
gp.deletefield (out_shape, "FID_1; GID_1")
# Remove the join
gp.RemoveJoin("source_lyr", fc)
# clear memory of layers
gp.Delete("source_lyr")
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
... View more
07-01-2013
05:06 AM
|
0
|
0
|
607
|
POST
|
Thank you for the help. The AddJoin code below worked well. As you said, I want to add a loop to the AddJoin of a grid and multiple bird range. Please kindly advise how to code it. Thank you.
################################################## ########################
##AddJoin feature layers to a feature layer
##Elaine Kuo
##30 June 2013
################################################## #######################
#Import standard library modules
import arcgisscripting
import os
#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.QualifiedFieldNames = "UNQUALIFIED"
#Get a list of the featureclasses in the input folder
source_fc = r"H:/temp_D/testt/A_grid.shp"
Entity = r"H:/temp_D/test/geoc0283.shp"
output_fc = r"H:/temp_D/test/grid_rng.shp"
gp.Toolbox = "Data Management"
# convert a featureclass to a layer
gp.MakeFeatureLayer_management (source_fc, "source_lyr")
# have Layers for AddJoin
gp.AddJoin_management("source_lyr", "GID", Entity, "GID", "KEEP_ALL")
# convert a layer to a featureclass
gp.CopyFeatures_management ("source_lyr", output_fc)
# delete Field
gp.deletefield (output_fc, "FID_1; GID_1")
# Remove the join
gp.RemoveJoin("source_lyr", "geoc0283")
# clear memory of layers
gp.Delete("source_lyr")
gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
... View more
07-01-2013
04:07 AM
|
0
|
0
|
821
|
Title | Kudos | Posted |
---|---|---|
2 | 02-19-2011 02:51 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|