|
POST
|
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 48, in AddLayer assert isinstance(data_frame, DataFrame) AssertionError
... View more
07-13-2017
08:09 AM
|
0
|
7
|
4449
|
|
POST
|
Joshua, I believe this is "Run Python script in Process" for the custom tool. The input layer for creating a new feature layer is always set as that is being pulled from a database we use. The output layer is whatever I choose to name it (which i try to keep it the same as the original except for one addition at the end - which department it's for). That is a small change but is being referenced later in the script. It works in the python module, but not in the custom script. I ran the script with the addition you recommended but it gets the error saying that there is an error in running: arcpy.mapping.AddLayer(df, addLayer) any ideas?
... View more
07-13-2017
07:59 AM
|
0
|
9
|
4449
|
|
POST
|
So I'm working on a custom tool that generates KMZs for reports to go out ot various different departments. One thing that we do is turn certain fields off not to show every attribute in the table. Thus, I settled on using MakeFeatureLayer_management tool to create a new feature layer with only the fields I want shown in the report. Here is what my code looks like: visibleFields = ["Field1", "Field2", "Field3", "Field4", "Field4"]
field_info = arcpy.Describe('Input Feature Layer').fieldInfo
for index in xrange(0, field_info.count):
if field_info.getfieldname(index) not in visibleFields:
field_info.setvisible(index,"HIDDEN")
arcpy.MakeFeatureLayer_management('Input Feature Layer','Output Feature Layer',"","",field_info)
arcpy.RefreshTOC()
arcpy.RefreshActiveView() When I run this in the python module in ArcMap, the output feature layer is automatically added to the Table of Contents. But when I run this in my custom tool, the output feature layer isn't added to the Table of Contents and my custom tool spits out an error because the tool cannot progress as the output feature layer does not exist in the Table of Contents and cannot be referred on. So how would I be able to add the newly created feature layer to the table of contents in the script when the feature layer is created in program memory?
... View more
07-13-2017
06:08 AM
|
0
|
11
|
6479
|
|
POST
|
Joshua, I figured out how to delete the program memory with the following line: arcpy.Delete_management('in_memory',"") I have that line at the beginning of my script to clear the memory and then at the end to clean it up before exiting the custom tool. This way no errors may occur with the temp data being used. Thanks for your help Joshua!
... View more
07-13-2017
05:23 AM
|
0
|
0
|
2985
|
|
POST
|
How do you remove them from program memory? I did use arcpy.Delete_management to remove the feature layer (which did it make it disappear from TOC and data frame, but now when I try to test my script as a custom tool, I get an error saying that feature layer already exists. The goal is to run this tool at least 4 times a year. I was hoping that the naming of the new feature layer didn't have to change. I currently have at the end of the tool arcpy.Delete_management to delete the newly created feature layer every time to ensure that it can be used again when generating a report.
... View more
07-12-2017
02:04 PM
|
0
|
0
|
2985
|
|
POST
|
Joshua, when you use: arcpy.MakeFeatureLayer_management('input feature','output feature',"","",field_info) do, you know where the new feature is automatically created? Trying to run tests but get an error since it already exists in it's location. Need to be able to change where it is saved and all. Also need to delete the original.
... View more
07-12-2017
01:29 PM
|
0
|
3
|
2985
|
|
POST
|
So far I've tried it in the interactive command prompt and it definitely added the new layer to the TOC. I'll have to test it out in the script tool and post my results here. From my understanding, is this a temporary layer? So tat if I closed my mxd, that would disappear? Or would I have to add a line at the end to delete the new layer that was created after my tool is done running?
... View more
07-12-2017
09:06 AM
|
0
|
0
|
2985
|
|
POST
|
Thanks Joshua. This is for ArcGIS DesktopArcMap. It definitely was much more easier to read then the discussion I had posted! Also was easily able t follow it it. I'll have to try that with my script. Thanks again! Do you know if you have to pass another line to pull in the new feature class or it automatically does it when you run the arcpy.MakeFeatureLayer_management line?
... View more
07-12-2017
07:44 AM
|
0
|
6
|
2985
|
|
POST
|
So I've been doing some research into this but have gotten conflicting outputs on whether or not it is possible to turn fields on and off using python in custom tools. I am working on a custom tool that generates KMZs on a state by state level for different departments within the company, but we do not let them see all the attributes within each feature class, thus turning some fields off before we use Map to KML tool. Since I am currently trying to automate this process, I want the custom tool to first turn the fields I do not want shown in the report off before the tool gets to the actual execution in regards to generating the KMZs. One such discussion I looked at was this on titled Turn fields off by script. While Wayne's solution seemed to work for the OP, there were parts of that solution I did not understand. Is there an easier way to approach this or if someone can help break down Wayne's solution, that would be appreciated. I look forward to seeing the ideas everyone has for this. For reference, here's what the solution to the other discussion: import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, '*')[0]
LayerNeedsFieldsTurnedOff = arcpy.mapping.ListLayers(mxd, 'your target layer name', df)[0]
# fill in your desired fields to remain visible
desiredFields = ['fieldname0', 'fieldname1', 'fieldname2', 'etc']
field_info = arcpy.Describe(LayerNeedsFieldsTurnedOff).fieldInfo
for i in range(field_info.count):
if field_info.getfieldname(i) not in desiredFields:
field_info.setvisible(i, 'HIDDEN')
arcpy.MakeFeatureLayer_management(LayerNeedsFieldsTurnedOff, 'temp_layer', '', '', field_info)
refLyr = arcpy.mapping.Layer('temp_layer')
# rename the ref layer the same as your target layer
refLyr.name = 'your target layer name'
arcpy.ApplySymbologyFromLayer_management(refLyr, LayerNeedsFieldsTurnedOff)
arcpy.mapping.UpdateLayer(df, LayerNeedsFieldsTurnedOff, refLyr, False)
mxd.save()
print 'cleaning up-'
if arcpy.Exists('temp_layer'):
print '\'temp_layer\' still in memory...deleting now...'
arcpy.Delete_management('temp_layer')
print 'deleting obj refs...'
del mxd,LayerNeedsFieldsTurnedOff, refLyr print 'done.'
... View more
07-12-2017
07:13 AM
|
0
|
8
|
3877
|
|
POST
|
I agree Alexander, realized that after I had responded to Kevin's approach. As of right now, I have my tool set to the function method you provided. Made notes of the method tat Blake suggested too (that one I don't think you have to alter the code on a yearly basis).
... View more
07-10-2017
11:54 AM
|
0
|
2
|
938
|
|
POST
|
Kevin, On a slightly separate note, is it possible to make the year a rolling year? For example, leaving 2017 for now is fine, but say it gets to be 2018, would I have to go back into the code to change the year manually, or can the year change in the code by what year it's in?
... View more
07-10-2017
11:38 AM
|
0
|
4
|
4093
|
|
POST
|
Thank you for that Kevin! I am contemplating between using your method and Alexander's. As you said, having it in one line makes it harder to read (but your method is in my opinion a bit more simple compared to having to define a function).
... View more
07-10-2017
11:31 AM
|
0
|
0
|
4093
|
|
POST
|
Alexander, Thank you so much! I wasn't aware of bisect in python or it's functionality but I tried it the way you recommended it except instead of doing import datetime as dt, I left it as import datetime only and used my predefined date variable instead of today. still resulted in getting the right value!
... View more
07-10-2017
11:24 AM
|
1
|
0
|
4093
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 07-26-2018 09:12 AM | |
| 1 | 07-18-2017 01:12 PM | |
| 1 | 03-09-2017 09:57 AM | |
| 2 | 03-22-2018 09:49 AM | |
| 1 | 09-19-2017 01:18 PM |