POST
|
I ran your code, and sure enough, it runs fine the first time but fails after that. Not only that, but if I try to open the mxd that has gone through the process, I can't save it in Arcmap. All I can do is a Save a Copy. At this point the mxd can't quite figure out what its name is and is for all practicable purposes corrupt. Seems like a bug to me. That said, the code below will work repeatedly if you can work it into your process. The issue was the filesystem calls os.rename and os.remove. So we just do it all with arcpy:
originalDoc = r'C:\temp\Untitled.mxd'
# Make a new MXD Name...
baseFldr = os.path.dirname(originalDoc)
baseName = os.path.basename(originalDoc)
newDoc = os.path.join(baseFldr,"temp123_" + baseName)
# Open the original Document, and save a copy
mxd = arcpy.mapping.MapDocument(originalDoc)
mxd.saveACopy(newDoc)
mxd = arcpy.mapping.MapDocument(newDoc)
mxd.saveACopy(originalDoc)
mxd = None
This will be slower than what you were trying to do if the mxd's are large, as you said. The basic issue is the mixing of filesystem calls and saveACopy. Probably the fastest way to handle your work flow is eliminate the saveACopy calls completely. Something like this:
originalDoc = r'C:\temp\Untitled.mxd'
# Make a new MXD Name...
baseFldr = os.path.dirname(originalDoc)
baseName = os.path.basename(originalDoc)
old_doc = os.path.join(baseFldr,"orig_" + baseName)
# make a copy of the original with a new name
shutil.copy2(originalDoc, old_doc)
# Open the original Document, do the work, and set work_done flag
mxd = arcpy.mapping.MapDocument(originalDoc)
# do some work here if necessary, set flag depending on if
#anything was done or not. We'll set to true for demonstration
work_done = True
if work_done:
mxd.save()
# move the original to an archive folder
afol = r'C:\temp\Archive'
dest = os.path.join(afol, baseName)
shutil.move(old_doc, dest)
else:
os.remove(old_doc)
mxd = None
good luck, Mike
... View more
05-04-2013
06:14 AM
|
0
|
0
|
345
|
POST
|
You are making it way too hard on yourself. Let's simplify the code a little:
def onMouseDownMap(self, x, y, button, shift):
# set up environment
arcpy.env.workspace = "C:/temp/python/test.gdb"
Parcels = "C:/temp/python/test.gdb/Parcels"
mxd = arcpy.mapping.MapDocument("current")
df = mxd.activeDataFrame
# build point geometry, run your getSearchDistanceInches function
pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference)
searchdistance = getSearchDistanceInches(df.scale, 3)
# make an arcpy.mapping layer obj and do the selection by location
plyr = arcpy.mapping.Layer(Parcels)
arcpy.SelectLayerByLocation_management(plyr, "INTERSECT", pointGeom, "%d INCHES" % searchdistance, "NEW_SELECTION")
# all done, now add our layer to the map
arcpy.mapping.AddLayer(df, plyr, "BOTTOM")
arcpy.RefreshActiveView()
There are 2 kinds of feature layers used in arcpy code. The type made by MakeFeatureLayer_management is a geoprocessing layer and does not work with some parts arcpy.mapping, like AddLayer. The other kind is an arcpy.mapping layer object, which can be a feature layer or a number of other kinds of layers. If it is feature layer, then it will work with at least some of the geoprocessing tools like SelectLayerByLocation that expect a feature layer. If this seems confusing, don't feel bad. It confuses me too. Here's some more confusing stuff: You can create an arcpy.mapping.Layer object from a saved .lyr file, like you were doing. Now, the documentation doesn't tell you this, but you can also make a layer object from a feature class on a disk, from another layer in the toc, or even from a geoprocessing layer made with MakeFeatureLayer_management. So all the work you were doing to make a .lyr file was unnecessary. Just make an arcpy.mapping layer straight from the feature class, select against that layer, and add it to the map. You were raising an error in your code so that's why it seemed nothing was happening. The addin environment is really tough to debug in, since you don't get to see the traceback and usually don't even know an error has occurred. So I get my code running first by executing it in the Python window. Then, when everything is running correctly, I stick the code into a method or function in the addin. good luck, Mike
... View more
05-03-2013
10:20 AM
|
1
|
0
|
2820
|
POST
|
Let's look at this logically. The text element is storing the text attribute as a unicode string. We can confirm this in the Arcmap Python window:
>>> textoGeral
<TextElement object at 0x19a71b70[0x19a6d480]>
>>> print textoGeral.text
Condição: Sizigia
>>> textoGeral.text
u'Condi\xe7\xe3o: Sizigia'
>>>
But you are feeding it a byte string. We can see that in your code. (utf-8 is not unicode, it is a bunch of bytes that represent unicode). So somewhere in the arcpy.mapping code, your bytes are getting coerced into a unicode string. For some reason, that coercion is failing to work correctly on your machine (but not mine and Jeffery's). It seems to me that if we eliminate the need for the coercion, then you might get happier results. I suggest you feed the text element pure unicode:
a = "Condição: ".decode('utf-8')
b = condicao.decode('utf-8')
txt = a + b
textoGeral.text = txt
There may be something deeper going on, so this may not help. But it's worth a shot. good luck, Mike
... View more
05-02-2013
03:11 PM
|
0
|
0
|
258
|
POST
|
I believe the only rule for args is that they are pickleable, so this leaves out a bunch of arcpy objects. As far as printing goes, it works fine for me running in a Windows cmd window. The only issue is that the output gets jumbled up from time to time if 2 or more processes try to print something at the same time. thanks for posting all this, Mike
... View more
04-30-2013
03:45 PM
|
0
|
0
|
580
|
POST
|
James Crandall has given a very good answer on how to manipulate dates in Python. I would add a couple of things. First, don't try to import timedelta. That will raise an exception. But you don't need it, since it's part of datetime, so just leave that out and James's code will work perfectly. Second, if you want to re-create a string with the same, original format from your new datetime object, simply add this line to the bottom of James's code: new_datestr = later.strftime("%m/%d/%Y %H:%M") good luck, Mike
... View more
04-29-2013
09:50 AM
|
0
|
0
|
980
|
POST
|
I tried your code, and it works just fine here. I get "Condição Sizigia" in my layout text. So I'm just guessing, but maybe it is your source code encoding. Make sure the text editor you are using saves the module file encoded to utf-8. One other thing you might try is to change the coding header in your module from "# coding: utf-8" to "# -*- coding: utf-8 -*-" I'm not sure either of these suggestions will help, but they are worth a try. Like I said, I cannot repeat the issue you are having. good luck, Mike
... View more
04-28-2013
03:29 PM
|
0
|
0
|
258
|
POST
|
The first argument for arcpy.mapping.AddLayer has to be a DataFrame instance, but you are passing it a string, the name of the dataframe. You've already hooked to the DataFrame instance in your code, so just change this line: arcpy.mapping.AddLayer(df_Target, new_shplyr, "TOP") to this: arcpy.mapping.AddLayer(dataFrame, new_shplyr, "TOP") I think that will fix you up. good luck Mike
... View more
04-28-2013
02:32 PM
|
1
|
0
|
1170
|
POST
|
I'm not really clear on what you are trying to do, but you seem to want a list of the values in the Sum_Count field. All you have to do is replace your code: rows = arcpy.SearchCursor(featureClass) row = rows.next() while row: print row.Sum_Count row = rows.next() with: rows = arcpy.SearchCursor(featureClass) value_lst = [row.Sum_Count for row in rows] Now you'll have a list of the values in the Sum_Count field, and you can do what you want with them later in the code. The error you were getting was raised when you tried to reference the row object after the last next() call, which always returns None when the end-of-file is encountered. Use a value from the value_lst instead, and that error will go away. good luck, Mike
... View more
04-27-2013
04:09 PM
|
0
|
0
|
369
|
POST
|
A dll is a file which contains functions and/or classes that have been compiled. Generally these files are written in C or C++, or possibly VB or C#, and then compiled into the binary dll. You don't open the file, but rather access the functions/classes through code. In Python, you'd do that with ctypes, swig, or another ffi library. But you have to have some documentation about what callables are in the dll, what arguments are required, and what the return values are, or the file will be of no use to you. good luck, Mike
... View more
04-10-2013
12:51 PM
|
0
|
0
|
140
|
POST
|
To allow overwriting of the featureclass, add this to the top of the script: arcpy.env.overwriteOutput = True And your layer is getting added to the mxd file on the disk, not to the current version that's displayed. Maybe I'm misunderstanding what you want, but if you want the layer added to the current display, then you'll have reference the current mxd: mxd = arcpy.mapping.MapDocument('Current') To keep all the other layers out of a map, do this: arcpy.gp.AddOutputsToMap = False This may or may not help, but hopefully it will get you started in the right direction. Mike
... View more
12-13-2012
02:51 PM
|
0
|
0
|
210
|
POST
|
What version of ArcGIS are you using? And tell us more about what you mean when you say "what is the script to use to move to the python script for the rest of the program." Mike
... View more
12-11-2012
02:34 PM
|
0
|
0
|
186
|
POST
|
After you get your extent, to select the features in a layer named 'Stands' intersecting the extent rectangle, do this:
mxd = arcpy.mapping.MapDocument('CURRENT')
df = mxd.activeDataFrame
ext = df.extent
lyrs = arcpy.mapping.ListLayers(mxd, "Stands", df)
if lyrs:
lyr = lyrs[0]
else:
return
a = arcpy.Array()
a.add(ext.lowerLeft)
a.add(ext.lowerRight)
a.add(ext.upperRight)
a.add(ext.upperLeft)
a.add(ext.lowerLeft)
thepoly = arcpy.Polygon(a)
arcpy.SelectLayerByLocation_management('Stands', 'Intersect', thepoly, 0, 'New_Selection')
You can read more about SelectLayerByLocation in the online help. But it sounds like you really don't want to select features as much as grab some attributes. If that is the case, then I'd set up a search cursor, curse through the records, and if the shape of a feature intersects the poly we made above, grab the attributes of interest. good luck, Mike
... View more
12-06-2012
04:04 PM
|
0
|
0
|
241
|
POST
|
In Arcmap, go to Geoprocessing on the top menu bar, and then choose Geoprocessing options. Down where it says Script Tool Editor/Debugger, in the slot that says Editor, put in the full path to idle.pyw. On my machine, it is "C:\Python27\ArcGIS10.1\Lib\idlelib\idle.pyw" It will probably be similar on yours. good luck Mike
... View more
11-23-2012
01:49 PM
|
0
|
0
|
394
|
POST
|
Could you maybe have a lock problem here? If you run scripts out of process, you will sometimes get this type of error when accessing data sources that are open, or have been open, in the current mxd. On the selectbylayer issue, I've noticed the blinking on several toolbox tools. I haven't noticed memory leaks, but I haven't looked for them either. SelectbyLayer and Calculate are 2 that I've noticed that do this. Started with 10.1. Mike
... View more
11-20-2012
02:47 PM
|
0
|
0
|
628
|
Title | Kudos | Posted |
---|---|---|
1 | 05-03-2013 10:20 AM | |
1 | 11-16-2012 09:48 AM | |
1 | 04-28-2013 02:32 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|