1

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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 9, 2016 at 22:31
0

2 Answers 2

5

A few remarks:

  1. 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"...
  2. 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).
  3. 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()
answered Aug 10, 2016 at 0:44
2
  • #2 I did not know, I'll have to make use of that! Commented 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. Commented Aug 10, 2016 at 22:02
3

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.

answered Aug 9, 2016 at 23:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.