|
POST
|
Hmmm, that's what the "try: except TypeError:" handler is supposed to silence, and it does for me on 10.2.2. I remember from GISSE that you're on 10.3. The bug # I referenced in the code suggests adding `print " "` to the except clause.
... View more
07-08-2015
04:00 PM
|
0
|
1
|
2128
|
|
POST
|
Peter, your variables look fine to me. The only suggestion I might make is to pass your inputs in to the script using arguments and access them with sys.argv or arcpy.GetParameterAsText. And I'd only recommend that if you're going to be using the code as a commandline or ArcToolbox script tool and you may want to use different inputs/workspaces when running the code. I write code that ranges from simple step-by-step scripts to large libraries that are spread across multiple files in python packages. Here's how I roughly think about when deciding whether to modularise/refactor code: For straightforward scripts, I will hardcode variables such as you have done. If I want to reuse the script with different inputs, I will pass the inputs as parameters/arguments and access them with sys.argv or arcpy.GetParameterAsText If it's a long and complex script, I might turn some of the steps into functions to enhance readability or if I want to reuse a bit of code within the script if parts of the code could be reused in other scripts, I will turn that bit of code into a function or class and put it in a module that can be imported. I'll group similar bits of code in individual modules, i.e a module for statistics related code, one for certain types of filesystem IO, one for geometry operations, one for interacting with ArcObjects, etc...
... View more
07-08-2015
03:54 PM
|
1
|
0
|
1172
|
|
POST
|
For me, the main reasons to refactor code into functions (and classes, modules, packages...) are readability and reusability. On readability - if I have a long script with a number of steps, each with a number of lines of code, I will usually bust each step out into a function so it's easier to follow the overall logic of the script. On reusability - if I find myself writing the same code over and over again to do the same thing with different inputs, I will turn that code into a function. I don't see anything in your code that requires moving into a function. It's a very straightforward script, is very readable and there's nothing in it that is reusable. A cautionary note... it's easy to get too caught up in refactoring and go too far. I was recently reviewing another programmers code where everything was a function, and functions called functions which called more functions ad-infinitum and understanding what the script was doing meant jumping all over the place in the file. It was basically unreadable.
... View more
07-06-2015
05:29 PM
|
1
|
3
|
1172
|
|
POST
|
The OP's question is referring to running a tool from ArcToolbox and my answer to your question about running a tool from a Python Addin using the pythonaddins.GPToolDialog won't work here.
... View more
06-16-2015
04:18 PM
|
0
|
0
|
2808
|
|
POST
|
I think ArcGIS might only load the toolbox once to improve performance. I workaround this by making a temporary copy of the toolbox every time I want to call it. import os, sys
import glob
import shutil
import tempfile
import threading
import time
import arcpy, pythonaddins
class Button(object):
# ...
def onClick(self):
GPToolDialog('path/to/toolbox.pyt', 'ToolName')
def GPToolDialog(toolbox, tool):
"""
* Workaround for NIM089253 - "TypeError: GPToolDialog() takes at most 1 argument (2 given)
http://support.esri.com/en/bugs/nimbus/TklNMDg5MjUz
* Workaround for defaults only refreshing on first run - copy the pyt (and xml docs)
which forces Arc to reload it every time.
"""
tmpdir = tempfile.mkdtemp(prefix='toolbox')
tmpfile = os.path.join(tmpdir,os.path.basename(toolbox))
docs=glob.glob(toolbox[:-4]+'*.xml')
shutil.copy(toolbox,tmpdir)
for doc in docs:shutil.copy(doc,tmpdir)
try:
result=pythonaddins.GPToolDialog(tmpfile,tool)
except TypeError:
pass
finally:
#Give ArcMap time to load the toolbox before it gets cleaned up
threading.Thread(target=rm, args = (tmpdir,3)).start()
return None
def rm(d, wait=1):
time.sleep(wait)
try: shutil.rmtree(d)
except: pass
... View more
06-15-2015
06:38 PM
|
3
|
3
|
2129
|
|
POST
|
You could use a dict instead of a list print("Verifying that coordinate systems are the same...")
InSHP = {'outline':outline, 'DA':DA, 'soil':soil}
DEMSR = arcpy.Describe(DEM).spatialReference.PCSCode
for l in InSHP:
print l, InSHP
sr = arcpy.Describe(InSHP ).spatialReference.PCScode
if sr != DEMSR:
InSHP = arcpy.Project_management(InSHP , InSHP [:-4] + "PRJ.shp", DEMSR)
print InSHP
print InSHP['outline']
... View more
06-11-2015
04:29 PM
|
2
|
2
|
4634
|
|
POST
|
Still seems to be broken at 10.2.2, sigh. Mosaic dataset of single band rasters, source type = thematic.
... View more
06-02-2015
09:39 PM
|
0
|
0
|
2995
|
|
POST
|
It would help if you could describe exactly *what* is not working and provide any python exceptions you are getting (hint: if you have the ArcMap python window open, any exceptions raised by your addin will be visible).
... View more
05-08-2015
04:26 AM
|
0
|
1
|
774
|
|
POST
|
Assuming it's row[3] that contains the NUL, try: row[3]=v.replace('\x00','')
... View more
05-04-2015
07:56 PM
|
1
|
2
|
3666
|
|
POST
|
Bit hard without the actual error message. Can you add that to your question?
... View more
05-04-2015
06:32 PM
|
0
|
6
|
3666
|
|
POST
|
Sephe, arcpy.da cursors are preferred to the legacy arcpy cursors. Accessing data using cursors ArcGIS 10.1 added a new data access module (arcpy.da). The previously existing cursors (that are still listed under arcpy) are still functional and valid; however, the new arcpy.da cursors include significantly faster performance. In most cases, the help will illustrate the use of the arcpy.da cursors.
... View more
05-02-2015
02:46 PM
|
2
|
0
|
1227
|
|
POST
|
try:version = user.ClientName.split(':')[1] except IndexError:version = ''
... View more
04-27-2015
04:04 PM
|
1
|
0
|
1096
|
|
POST
|
in __init__ or onClick set arcpy.env.overwriteOutput = True And in future, please post your code as a formatted code block, not as a screenshot. https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet
... View more
04-27-2015
03:54 PM
|
0
|
1
|
1834
|
|
POST
|
When running your add in, have the ArcMap python window open (Geoprocessing - Python menu) so you can see any error messages.
... View more
04-27-2015
03:44 AM
|
0
|
3
|
1834
|
|
POST
|
Excuse the brevity, using my phone to reply. Use the split() method i.e user.ClientName.split(':')[0]
... View more
04-27-2015
01:33 AM
|
1
|
2
|
1096
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 07-24-2022 03:08 PM | |
| 1 | 07-30-2025 03:00 PM | |
| 1 | 06-10-2025 08:06 PM | |
| 5 | 05-20-2025 07:56 PM | |
| 1 | 05-04-2025 10:34 PM |