I'm trying to write a python script that runs a definition query. I'm having a problem with saving the query directly to a layer in a map document and not as its own layer file. Here is my code:
import arcpy
import os
import datetime
#getting the right layer to modify
mxd = arcpy.mapping.MapDocument(r"U:\FullPath.mxd")
layerName = "BuildingFootprintsCopy"
dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]
print dataFrame
layers = arcpy.mapping.ListLayers(mxd, layerName)
print layers
layerFromMap = arcpy.mapping.ListLayers(mxd, layerName, dataFrame)[0]
arcpy.mapping.AddLayer(dataFrame, layerFromMap, "AUTO_ARRANGE")
#Definition Query
date2 = datetime.date.today() - datetime.timedelta(days=730)
print date2
dQueryVar1 = 'TOTAL_NUMB_FLOORS = ' + "2"
dQueryVar2 = "DATE_BUILT >=" + " date " + "'" + str(date2) + "'"
layerFromMap.definitionQuery = dQueryVar1 + " AND " + dQueryVar2
layerFromMap.save(mxd) #This is the line that throws the error.
print "finished"
This is the error I'm getting:
Traceback (most recent call last):
File "U:\TestDefinitionQueryWithDate.py", line 35, in <module>
layerFromMap.save(mxd)
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
TypeError: save() takes exactly 1 argument (2 given)
I've tried:
layerFromMap.save(r"U:\FullPath.mxd")
That returns the exact error. I'm not sure why I'm passing 2 arguments instead of 1. I've also tried :
layerFromMap.save()
That returns an error for not specifying a path.
How can I save my layer as the layer in the mxd I ran the definition query on and not as its own path? Can I save it in such a way that there is one layer with all of the data it originally contained with the definition Query on it?
2 Answers 2
A few remarks:
mxd.save()
is the way to go. When you add a layer to/modify an existing layer in an mxd, you save the mxd, not the layer. It's like adding something (picture/text/etc) to a word doc, you save the word doc, not the "something"...- In most cases you can use
dataFrame = mxd.activeDataFrame
instead of listing the data frames (unless there's multiple data frames in the mxd and you need a specific one). - You don't need to add the layer, just modify it in place (unless you want to duplicate it on purpose).
An example:
import arcpy
import os
import datetime
#getting the right layer to modify
mxd = arcpy.mapping.MapDocument(r"U:\FullPath.mxd")
layerName = "BuildingFootprintsCopy"
dataFrame = mxd.activeDataFrame
layerFromMap = arcpy.mapping.ListLayers(mxd, layerName, dataFrame)[0]
#Definition Query
date2 = datetime.date.today() - datetime.timedelta(days=730)
print date2
dQueryVar1 = 'TOTAL_NUMB_FLOORS = ' + "2"
dQueryVar2 = "DATE_BUILT >=" + " date " + "'" + str(date2) + "'"
layerFromMap.definitionQuery = dQueryVar1 + " AND " + dQueryVar2
mxd.save()
-
#2 I did not know, I'll have to make use of that!2016年08月10日 01:57:39 +00:00Commented Aug 10, 2016 at 1:57
-
That was a good answer. I'll think about what my code is actually doing more in the future. I did not realize that There was no reason to add a new layer.user66821– user668212016年08月10日 22:02:16 +00:00Commented Aug 10, 2016 at 22:02
Instead of
layerFromMap.save(mxd)
just do:
mxd.save()
It will save every layer in your MapDocument to that MXD and will retain any definition queries that you have applied to their Properties.