|
POST
|
When creating a buffer in a script, isn't it supposed to be added to the map by default? The code below creates the buffer just fine, but I have to add it to the map manually. Neither refreshing via code or with F5 or View - Refresh helps. I think this is the reason setting update_layer in the try block doesn't work. I know it's update_layer throwing the error because when I included it in the error message, that threw an error stating update_layer was undefined. Code & error below. Thanks for any ideas. Code: import arcpy from arcpy import env env.workspace = r"\\TestServer\TestCases_Geodatabase.gdb" mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] case_number = arcpy.GetParameterAsText(0) #------------------------------------------------------------------------------- # Buffer Processing # def CreateBuffer(): # set buffer parameters case = "Cases" output ="Buffer_200" dist = "200 feet" side = "OUTSIDE_ONLY" # delete existing temporary buffer, if any if arcpy.Exists(output): arcpy.Delete_management(output) # create buffer arcpy.Buffer_analysis(case, output, dist, side) # update symbology try: source_layer = r"\\TestServer\Map Templates\GIS_Lyr_Files\Buffer.lyr" update_layer = arcpy.mapping.ListLayers(mxd, "B*", df)[0] arcpy.mapping.UpdateLayer(df, update_layer, source_layer, True) except Exception as e: arcpy.AddError("Updating symbology \n sl = " + source_layer + "\n" + e.message) def main(): CreateBuffer() arcpy.RefreshTOC arcpy.RefreshActiveView if __name__ == '__main__': main() Error: Updating symbology sl = \\TestServer\Map Templates\GIS_Lyr_Files\Buffer.lyr list index out of range Completed script Buffer200... Failed to execute (Buffer200). Failed at Wed Mar 28 08:22:06 2012 (Elapsed Time: 56.00 seconds)
... View more
03-28-2012
05:29 AM
|
0
|
6
|
1641
|
|
POST
|
I'll try that, but I'm thinking now that I don't actually need to keep the copy of the buffer. It was done that way previously, but once a series of maps are produced, I don't see a need to keep a copy of it around, especially when it can be reproduced at any time. I'm trying to automate things for people in another department, but if they can get the maps they want, no need to stick with the way they were doing it. They'll never know, or have a need to know, how it's done.
... View more
03-27-2012
11:50 AM
|
0
|
0
|
530
|
|
POST
|
The script below creates a buffer around a parcel in a feature class called 'tempBuffer'. This part works, except for an unrelated problem (creates buffers around all parcels, not just the selected one) I'll post in another thread if I can't figure it out. The problem is that I can't then append the feature from tempBuffer to the Buffer feature class. The script and error messages are below. Nothing else is doing anything with Buffer, which in my test environment is limited by a definition query to the same case_numberanyway. Any ideas? Thanks. edit: When run as a tool, and not from PyScripter, no error pops up. But, the Buffer attribute table in ArcMap doesn't show the new buffer, even after refreshing, while ArcCatalog does. Either way, it doesn't show in the map. Script:
import arcpy
from arcpy import env
env.workspace = r"\\TestServer\TestCases_Geodatabase.gdb"
buffer_200 = "Buffer"
#-------------------------------------------------------------------------------
# Buffer Processing
#
def CreateBuffer():
# set buffer parameters
case = "Cases"
output ="tempBuffer"
dist = "200 feet"
side = "OUTSIDE_ONLY"
case_number = raw_input("Enter Case #")
# delete existing temporary buffer, if any
if arcpy.Exists(output):
arcpy.Delete_management(output)
# create temporary buffer
arcpy.Buffer_analysis(case, output, dist, side)
# copy tempBuffer to Buffer feature class
try:
print "Starting Append..."
arcpy.Append_management(output, buffer_200, "NO_TEST", "First", "")
except Exception as e:
print "Unable to copy temporary buffer to Buffer feature class"
print e.message
Error:
Starting Append...
Unable to copy temporary buffer to Buffer feature class
ERROR 999999: Error executing function.
Cannot acquire a lock.
Cannot acquire a lock. [The table Buffer is being written by another process.]
Failed to execute (Append).
... View more
03-27-2012
09:51 AM
|
0
|
2
|
834
|
|
POST
|
The code below, run as a script added in a toolbox to ArcMap, errors out as shown after a little over a minute. tempBuffer is created, the Case_ field is added, and the case number is added. Actually, only part of the case number was added. the number was ZFY-1234, and only -1234 was added. Why would 1), this would take over a minute, 2) CalculateField not use the whole value and 3) most importantly, why raise this error? buffer_200 hasn't even had anything done to it yet. Editing the geodb with buffer_200 is started, but nothing's been done with that class prior to Append. Thanks for any help.
import arcpy
from arcpy import env
env.workspace = r"\\SDE.GDBServerPath\TestCases_Geodatabase.gdb"
case_number = arcpy.GetParameterAsText(0)
buffer_200 = "Buffer"
#-------------------------------------------------------------------------------
# Buffer Processing
#
def CreateBuffer():
# set buffer parameters
case = "Cases"
output ="tempBuffer"
dist = "200 feet"
side = "OUTSIDE_ONLY"
# create temporary buffer
arcpy.Buffer_analysis(case, output, dist, side)
# add Case_ field and set it's value to the case number
arcpy.AddField_management(output, "Case_", "TEXT")
arcpy.CalculateField_management(output, "Case_", case_number)
# copy tempBuffer to Buffer feature class
arcpy.Append_management(output, buffer_200, "NO_TEST")
# delete tempBuffer
arcpy.DeleteFeatures_management(output)
def main():
CreateBuffer()
arcpy.RefreshTOC
arcpy.RefreshActiveView
pass
if __name__ == '__main__':
main()
Error Message:
Start Time: Thu Mar 22 12:50:21 2012
Running script BufferAndGetOwnerData...
<class 'arcgisscripting.ExecuteError'>: ERROR 999999: Error executing function.
Cannot acquire a lock.
Cannot acquire a lock. [The table Buffer is being written by another process.]
Cannot acquire a lock. [The table Buffer is being written by another process.]
Failed to execute (Append).
Failed to execute (BufferAndGetOwnerData).
Failed at Thu Mar 22 12:51:31 2012 (Elapsed Time: 1 minutes 10 seconds)
... View more
03-22-2012
10:09 AM
|
0
|
1
|
5741
|
|
POST
|
Hmm, well, ok. I know many GIS tasks require multiple steps, just thought that since there are toolbar tools that can create in an existing feature class, they would be available through arcpy. Who knows, maybe the toolbar methods use the same method with an intermediate temp feature class behind the scenes. I'm trying to create more automated procedures for coworkers that don't know much ArcGIS. The less actual editing they have to do, the better for all of us. Thanks for the in_memory info, I'll check that out.
... View more
03-21-2012
04:49 AM
|
0
|
0
|
546
|
|
POST
|
Now that my generic python question has been answered (thanks again!), I have a specific arcpy question, which may have a generic quality as well. I frequently use the Construct Polygons tool to create parcel polygons out of line features I've created. The polygons are created in an existing feature class. That works great. But I can't figure out how to duplicate this behavior (polygons added to an existing file) in a script. The closest I can come is using Feature to Polygon, which does create the polygons, but in a new feature class. I suppose I could then copy the new polygons to the existing table and delete the created one, but this seems like an extra step compared to the Topology toolbar tool. I know some other tools on the toolbar work this way compared to their toolbox equivalents also. Is there a function I'm missing, or is this just the way it is? Thanks.
import arcpy
lineFeature = arcpy.GetParameterAsText(0)
polygonFeature = arcpy.GetParameterAsText(1)
arcpy.FeatureToPolygon_management(lineFeature, polygonFeature, "", "NO_ATTRIBUTES", "")
... View more
03-20-2012
11:24 AM
|
0
|
2
|
824
|
|
POST
|
Success! Thanks Mathew, and everyone else. Not real sure why that worked for outside the same folder while the original worked, only, in the same folder, but that's ok. I though i took the isdir() syntax directly from documentation somewhere, but who knows? Also switched to PyScripter instead of PythonWin. Doubt it made a difference in the code, but is a much nicer scripting environment. Now I'll try it with some arcpy imports. Again, thanks much.
... View more
03-20-2012
08:00 AM
|
0
|
0
|
564
|
|
POST
|
Thanks for all the help. I reinstalled Python per the link in Mathew's suggestion, as well as the other fixes recommended. I modified the script as shown below to check the if statement, and the problem seems to be that os.path.isdir() and isfile() isn't recognizing files and directories (except for itself). It doesn't even recognize the other python script in the folder as a file. Is isdir/isfile deprecated and no longer supported? Even though the script recognizes itself, and runs properly from within the folder. Kind of baffling. Revised script:
import os
prompt = "Please enter the directory path: "
dirPath = raw_input(prompt)
listing = os.listdir(dirPath)
fileName = r"C:\FileList.txt"
fileList = open(fileName, "w+")
fileList.write(dirPath + "\n\n")
fileList.write("DIRECTORIES:\n")
for dir in listing:
if os.path.isdir(dir):
fileList.write(dir + "\n")
else:
fileList.write("Not a directory: " + dir + "\n")
#s += "\n".join(dir)
#fileList.write(s + "\n")
listing = os.listdir(dirPath)
fileList.write("\n")
fileList.write("FILES:\n")
for file in listing:
if os.path.isfile(file):
fileList.write(file + "\n")
else:
fileList.write("Not a file: " + file + "\n")
fileList.close()
prompt = "File list created at: " + fileName + "\nPress Enter"
ok = raw_input(prompt)
and output when run from a script not in the folder entered at the prompt (these are the correct files & folders):
C:\Users\gkeith\Desktop\PythonScripts
DIRECTORIES:
Not a directory: abc00000.txt
Not a directory: abc0000x.txt
Not a directory: abc000xx.txt
Not a directory: abc00zzz.txt
Not a directory: abc0xxxx.txt
Not a directory: abcuvwxyz.txt
Not a directory: abcxxxxx.txt
Not a directory: FileLister2.py
Not a directory: New folder
Not a directory: New folder (2)
Not a directory: New folder (3)
Not a directory: RenamePDF.py
FILES:
Not a file: abc00000.txt
Not a file: abc0000x.txt
Not a file: abc000xx.txt
Not a file: abc00zzz.txt
Not a file: abc0xxxx.txt
Not a file: abcuvwxyz.txt
Not a file: abcxxxxx.txt
FileLister2.py
Not a file: New folder
Not a file: New folder (2)
Not a file: New folder (3)
Not a file: RenamePDF.py
... View more
03-20-2012
05:24 AM
|
0
|
0
|
564
|
|
POST
|
IDLE returns NONE as well. It doesn't look like the link to the repairing installs thread you mentioned posted, though.
... View more
03-15-2012
09:28 AM
|
0
|
0
|
1212
|
|
POST
|
Yup, definitely some system or environment setting throwing a wrench in things. What does this return? import os
os.getenv("PYTHON") In the PythonWin interactive window, that returns NONE when I print it. Nothing happens if I don't use print. Don't use backslashes in paths. It escapes the next character. That one I actually know and just forgot, thanks. Still, FileList.txt gets created, it just doesn't contain the list of files. "DIRECTORIES", "??" and "FILES" do print to the file. Thanks.
... View more
03-15-2012
09:00 AM
|
0
|
0
|
1212
|
|
POST
|
I don't think it's a problem with the code itself, since it runs fine when in the same directory as the other files or in ArcMap, but here's an example. Just the last one I worked on, the file listing one I mentioned. One thing that may help - getcwd() always seems to reference the python install path, not whatever path I'm actually working in. It may be a system or environment variable issue.
import os
prompt = "Please enter the directory path: "
dirPath = raw_input(prompt)
listing = os.listdir(dirPath)
fileName = "C:\FileList.txt"
s = "??"
fileList = open(fileName, "w+")
fileList.write(dirPath + "\n\n")
fileList.write("DIRECTORIES:\n")
for dir in listing:
if os.path.isdir(dir):
fileList.write(dir + "\n")
s += dir.join("\n")
fileList.write(s + "\n")
listing = os.listdir(dirPath)
fileList.write("\n")
fileList.write("FILES:\n")
for file in listing:
if os.path.isfile(file):
fileList.write(file + "\n")
fileList.close()
prompt = "File list created at: " + fileName + "\nPress Enter"
ok = raw_input(prompt)
... View more
03-15-2012
06:28 AM
|
0
|
0
|
1212
|
|
POST
|
I'm not sure if this question is directly related to ArcMap, but since that's mostly what I use Python for I'll give it a shot. When I write a script in Python, it runs fine if it's in the same folder as the file(s), such as the mxd, it references. If the script is in another folder, it doesn't run, at least not correctly. This happens with scripts for ArcMap, as well as other projects. For example, I have a script to print directories and files in a folder. If the script is in that folder, it runs fine. If it's in another folder, it only prints its own name (a copy is in the other folder, so original is still there). This doesn't happen when running something in ArcMap itself, either a script or python window. One last thing: the sysadmin didn't install Python from the ArcMap discs. I manually installed it according to other posts in the forums, and everything looks ok, V. 2,6, etc, but maybe I missed something, like setting the right path? Anyway, thanks for any help, and sorry if this isn't really an ArcMap python issue.
... View more
03-15-2012
05:46 AM
|
0
|
12
|
2023
|
|
POST
|
I've found similar problems with PythonWin when running a script. Someone on the forums (sorry, forget who) posted that you first have to do an import arcpy in the Idle window before it's available in the script window. That's new to me, but it appears to work.
... View more
03-08-2012
08:37 AM
|
0
|
0
|
2714
|
|
POST
|
One advantage of formatting using code tags like Dan suggests is that others here can see if indenting is the problem. The way it's posted now, it looks like none of the code under your for statements is indented. That will cause a logic error, in that Python thinks you have an empty for loop and just moves sequentially through your code without looping. On the other hand, maybe your actual code is indented, but it doesn't display in the forum that way if you haven't used code tags. In that case you wouldn't have the no indent problem, but no one here can tell that.
... View more
02-23-2012
11:42 AM
|
0
|
0
|
565
|
|
POST
|
Do you mean you want to design forms, 'windows', etc? There are several GUI packages, the most common being Tkinter, afaik. I haven't done very much with it myself, but there are many tutorials available. There may be better packages out there. If you're looking for a graphical design interface, like Visual Basic or forms in Access, then I'm not aware of any. Which isn't to say there are none.
... View more
02-17-2012
12:07 PM
|
0
|
0
|
1913
|
| Title | Kudos | Posted |
|---|---|---|
| 6 | 08-22-2019 07:41 AM | |
| 1 | 05-05-2014 04:30 AM | |
| 1 | 08-15-2018 06:23 AM | |
| 3 | 08-06-2018 07:31 AM | |
| 1 | 03-30-2012 08:38 AM |
| Online Status |
Offline
|
| Date Last Visited |
12-12-2021
01:00 PM
|