POST
|
I'm really sorry that nothing is working for you 😞 Which Service Pack you are using? I'm on 10 sp5. The tool is working as expected on my machine. Let me know if are on a different service pack - I'll try on that setup. I updated my service pack to service pack 4 and now your tool and the script works. I've updated my tool and its running now. Thanks for all the help... updating the service pack worked. Lesson learned... alway update the service pack... I didn't see a down load for service pack 5 it said it would be release in July... thanks again Ian
... View more
06-20-2012
02:18 PM
|
0
|
0
|
295
|
POST
|
I tried your tool... when I selected a shape file or feature class... I got the invalid input source when I tried it with a .lyr file the tool runs but nothing is added to the open map document I just went back and tried the tool a few more times and its almost like something is added and then immediately disappears. I would give up on this endeavor at this point but my program manager wants the arctoolbox script I created to automatically add the output dataset to the users map once complete... at this point I feel its easier just to give direction on how to add the data in ArcMap.
... View more
06-20-2012
11:05 AM
|
0
|
0
|
1396
|
POST
|
Okay... not sure if I'm making progress or not... I've simplified the code to try to work out the bug. Here is what I'm working with now. import arcpy
try:
mxd = arcpy.mapping.MapDocument(r"D:\Risk_Models\Multi_Species_Sampling\multi_species_overlay.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
newlayer1 = arcpy.mapping.Layer("D:\\Risk_Models\\state_MN_5000_EAB_plots.lyr")
print newlayer1
arcpy.mapping.AddLayer(df, newlayer1, "TOP")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
#save the map to persist the added layer
mxd.save()
del mxd, df, newlayer1
print 'Done'
except Exception as ex:
print ex.args[0] Now the code runs through the arcpy.mapping.Layer line and I do not get the invalid source error like before but the layer still does not add to the specified .mxd and then the new error say... that it is unable to save the map document. I have the .mxd open... does this matter. Ideally I want the users of my tool to have the tool output automatically added to their map document once the tool finishes. Also when I use "CURRENT" for the map document rather than a path it doesn't work. So another question is how do I get it to dynamically recognize the map document the user has created. I don't have any control over what my users name their map documents or where they save them. Thanks for all the help... Ian
... View more
06-20-2012
08:31 AM
|
0
|
0
|
1396
|
POST
|
I'm still gettin gthe CreateObject Layer invalid data source error. I used your code and just swithched out the path names for my mxd and files. I even tried different .mxd docs and both a feature class and a shapefile as the layer source. Not sure what else to do.
... View more
06-19-2012
09:33 AM
|
0
|
0
|
1396
|
POST
|
hmm... State_GA is a feature class in a geodatabase. Would you mind posting the code you got to work? thanks ian
... View more
06-19-2012
06:27 AM
|
0
|
0
|
1396
|
POST
|
Did you ever get this to work... I"m struggling with a similar issue. How ever I'm trying to add a data from a geodatabase... I keep getting the following error. Object: CreateObject Layer invalid data source
... View more
06-18-2012
02:20 PM
|
0
|
0
|
480
|
POST
|
Greeting all... I've written a python scrip that is run in arctool box given user specified inputs... at this point the tool works but I would like to have the tool automatically add the resulting output datasets to the ArcMap table of contents. I've tried a few variation of the following code... keep getting the same error... see below. I think it all goes back to this line of code where its not recognizing which dataset to add to the map. Any help is appreciated. Code... mxd = arcpy.mapping.MapDocument(r"D:\Risk_Models\Sample_Design\sample_design_tool.mxd") df = arcpy.mapping.ListDataFrames(mxd, "*")[0] print "ready to add layer" print sample_plots newlayer1 = arcpy.mapping.Layer(r"D:\Risk_Models\Sample_Design\Sample_design_tool\sample_design_tool.gdb\state_GA") #newlayer2 = arcpy.mapping.Layer(sample_points) print newlayer1 arcpy.mapping.AddLayer(df, newlayer1, "TOP") #arcpy.mapping.AddLayer(df, newlayer2, "Bottom") arcpy.RefreshActiveView() arpy.RefreshTOC() del mxd, df, newlayer1 end code Error... reported in python Traceback (most recent call last): File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in __main__.__dict__ File "D:\Risk_Models\Sample_Design\Sample_design_tool\FHTET_Sample_Design_Tool\FHTET_sample_design_tool_6_18_2012_add_data_update_test.py", line 94, in <module> newlayer1 = arcpy.mapping.Layer(r"D:\Risk_Models\Sample_Design\Sample_design_tool\sample_design_tool.gdb\state_GA") File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\mixins.py", line 255, in __init__ super(LayerMixin, self).__init__(lyrfile) File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 47, in __init__ for arg in args)) ValueError: Object: CreateObject Layer invalid data source
... View more
06-18-2012
12:13 PM
|
0
|
12
|
7718
|
POST
|
I need to batch export information from 400 raster attribute tables... I can get it to write a text file delimited by , but it write all of the records on the same line... how do I get it to recognize the fields as column and format the text file so I can import it into access as a table. here is the code
outfile = open(eco_pa + ".txt", "w")
rows = arcpy.SearchCursor(eco_pa, "OCCUR = 1", "", "COUNT;ECO_ID;SPECIES;OCCUR", "")
for row in rows:
SPECIES = row.getValue("SPECIES")
ECO_ID = row.getValue("ECO_ID")
OCCUR = row.getValue("OCCUR")
COUNT = row.getValue("COUNT")
print SPECIES, ECO_ID, OCCUR, COUNT
outfile.write(str(SPECIES) + "," + str(ECO_ID) + "," + str(OCCUR) + "," + str(COUNT)
outfile.close()
... View more
04-30-2012
02:09 PM
|
1
|
1
|
712
|
POST
|
awsome... this worked... arcpy.CalculateField_management(eco_pa, "OCCUR", "!" + fname + "!", "PYTHON") it didn't like reading it in as a string... thanks Ian
... View more
04-30-2012
09:38 AM
|
0
|
0
|
638
|
POST
|
Just tried that and got an error saying it can't concatenate string and list objects.
... View more
04-30-2012
09:31 AM
|
0
|
0
|
638
|
POST
|
You need to wrap field names in !'s when they are used in the field calculator expression. arcpy.CalculateField_management(eco_pa, "OCCUR", "!" + fname_calc + "!", "PYTHON")
Tip: make your calculation in the ArcMap field calculator and look at the results to see the proper syntax. Thanks... I'm trying that now... I created a model in model builder with the calculate field tool set do do the calculation I wanted and exported that to python... it did not give me the correct syntax. It had the field wrapped in [ ] thanks for your response... I'll let you know how it goes.
... View more
04-30-2012
09:07 AM
|
0
|
0
|
638
|
POST
|
I'm trying to copy data from one field to another field within a loop. The issue I'm having is how to define the field from which data is being copied as a variable. The field name from which I want to copy data changes with each iteration of the loop based on the input datasets name. Here is the code and I highlighted where I'm having trouble... I either get an error or the data does not copy but the script runs... depends on how I s specify the field name variable. See... arcpy.CalculateField_managment section of the code # --------------------------------------------------------------------------- # combine_ecoregion_tree_species.py # Created on: 2012-04-25 09:17:22.00000 # (generated by ArcGIS/ModelBuilder) # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy from arcpy import env # Check out any necessary licenses arcpy.CheckOutExtension("spatial") arcpy.env.overwriteOutput = True # Set Workspace arcpy.env.workspace = r"\\Sxftcsls001\m\SDL\Data\Forest_Parameters\FHTET\240m\20111019_L48" rasterList = arcpy.ListRasters("*", "GRID") listCount = len(rasterList) print listCount for r in rasterList: if(r.startswith ("f")): print r # Local variables: OutWorkspace = "D:\\Risk_Models\\Ecoregion_Tree_Species\\" eco_id = "D:\\Risk_Models\\Ecoregion_Tree_Species\\eco_id" Extent = "-2370945 255315 2273055 3189315" snap = "\\\\Sxftcsls001\\m\\SDL\\Data\\Snap_Grids\\FHTET\\240m\\L48\\snap" species_pa = OutWorkspace + r eco_pa = OutWorkspace + "eco_" + r species_field = str(r) print r print " species code is " + species_field # Process: Con arcpy.env.snapRaster = snap arcpy.env.extent = Extent arcpy.gp.Con_sa(r, 1, species_pa, 0, "\"VALUE\" > 0") # Process: Combine arcpy.gp.Combine_sa([species_pa,eco_id], eco_pa) print "combine complete" # Process: Add Field arcpy.AddField_management(eco_pa, "SPECIES", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") arcpy.AddField_management(eco_pa, "OCCUR", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") print "add field" arcpy.CalculateField_management(eco_pa, "SPECIES", "species_field", "PYTHON") print "Calculate Species Field" fields = arcpy.ListFields(eco_pa) fieldnames = [f.name for f in fields] fname = fieldnames[3] print fname fname_calc = [str(fname)] print fname_calc print fieldnames arcpy.CalculateField_management(eco_pa, "OCCUR", "%s" %fname_calc, "PYTHON") print "calculated Field"
... View more
04-30-2012
08:23 AM
|
0
|
5
|
5187
|
Title | Kudos | Posted |
---|---|---|
1 | 04-30-2012 02:09 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|