|
POST
|
After much trial and error I found a post that helped... http://forums.arcgis.com/threads/58298-Python-Script-Help-Spaces-in-filename From my expereince, if I made a function with loop to remove the " ' "s from the layers with spaces (the Layers with spaces got these " ' "s unlike the other layers) it was set in an infinite loop b/c it would not remove. The forum above did not work for doing this: arcpy.mapping.AddLayer(dataFrame,Layer.strip("'"),"AUTO_ARRANGE") as Layer has no method .strip. Although, when I tried it when I defined the Layer it worked and took out the " ' "s when it was filled as a parameter: Layer=arcpy.mapping.Layer(item.strip("'"))
... View more
10-11-2013
05:20 AM
|
0
|
0
|
2097
|
|
POST
|
I wrote a script tool where a map is generated, a checkbox of layers can be checked to add the layers to the map, and when I run it some layers will add to the new map and some layers, throw "Object: CreateObject Layer invalid data source." I did notice all of the layers that error have a space in their names. They are valid layers as in when AddData is used manually they can add to a map. I know the list of the layers is a Python multiValue list, not a Python list, taking out one if the items and testing it's type yields that the item is a unicode object. I also noticed when I print the mutliValues out the ones with spaces have single quotes surrounding them and the ones w/o spaces do not have single quotes surrounding them... 'file path here' Object: CreateObject Layer invalid data source 'file path here' Object: CreateObject Layer invalid data source filePathHere Added: FilePath.lyr I convert them to layers in the script when I iterate through the multiValue list like so: try: if countyLayers[0]: for item in countyLayers.split(';'): try: item1=arcpy.mapping.Layer(item) arcpy.mapping.AddLayer(dataFrame,item1,"AUTO_ARRANGE") item2=str(item.split("\\")[-1]) arcpy.AddMessage("Added: " + item2) arcpy.RefreshTOC() del item1, item2 except Exception as e: arcpy.AddMessage(e.message) pass except: arcpy.AddMessage("No county data specified to add") pass
... View more
10-10-2013
10:42 AM
|
0
|
1
|
5778
|
|
POST
|
I am writing a tool to generate a MXD and some metadata. It works but I am wondering if there is a better way than I am using to write the Tool Validator class than I am? Wondering how I would add another value list (not derived)? My value list now is to pick a mxd from a list of mxds in a template folder. I would like to add a value list to make a liks of layer files to add from a folder of template layer files. I would not mind changing below code so getting the list of mxds from a folder is a seperate function, but when I do that I get errors.
import arcpy
import os
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""
def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
return
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
folderPath=r"folderPathHere"#Folder of Map Templates
mapTemplates = []
for filename in os.listdir(folderPath):
fullpath = os.path.join(folderPath, filename)
if os.path.isfile(fullpath):
basename, extension = os.path.splitext(fullpath)
if extension.lower() == ".mxd":
mapTemplates.append(fullpath)
self.params[0].filter.list = mapTemplates
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
... View more
10-04-2013
05:30 AM
|
0
|
0
|
654
|
|
POST
|
I wanted to do this same thing for having a list of MXDs show in a drop down list on a tool. I understand the easy way to add a List of Values manually. However, I want one to be auto-generated in case an MXD is added in a certain folder. I tried copying this a bit but I do not understand it so much. Do you need to add anything in the main code of the tool to signify this information in the Validation code? Where is it that it assigns the list generated to the filter list?
import arcpy
import os
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""
def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()
self.mapTemplateParam = self.params[0]
self.type1Param = self.params[1]
return
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
self.mapTemplateParam.Value = self.mapTemplateParam.filter.list[0]
self.getMapTemlateList();
return
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
self.getMapTemplateList()
return
def getMapTemplateList(self):
folderPath=r"folderPathHere"#Folder of Map Templates
mapTemplates = []
for filename in os.listdir(folderPath):
fullpath = os.path.join(folderPath, filename)
if os.path.isfile(fullpath):
basename, extension = os.path.splitext(fullpath)
if extension.lower() == ".mxd":
mapTemplates.append(fullpath)
self.mapTemplateParam.filter.list = mapTemplates
return
I get error line 4, line 18 and a IndexError: list index out of range?
... View more
10-03-2013
10:47 AM
|
0
|
0
|
1534
|
|
POST
|
Google has a python course, very good. https://developers.google.com/edu/python/
... View more
08-29-2013
11:38 AM
|
0
|
0
|
1935
|
|
POST
|
Is it possible to check out the publisher extension with arcpy or python somehow? I do not see Publisher available in the extension codes.
... View more
08-26-2013
12:01 PM
|
0
|
0
|
900
|
|
POST
|
Cool beans! Your solution worked, all I did was add the scratch workspace part to mine and everything ran successfully and with great timing! Thanks for the writing of the code so cleanly, it is very clear, I will try to write in that manner. I seem to get to so excited when I write code and cannot think of the explainations so clearly.
... View more
07-18-2013
05:53 AM
|
0
|
0
|
1962
|
|
POST
|
It makes the files for the first value and then takes all day and does not finish. If I do one value manually, it takes 2-3 hours. Does anyone know any kind of monitoring I can build in this or any ideas of why this script seems to stall out?
... View more
07-17-2013
09:42 AM
|
0
|
0
|
1962
|
|
POST
|
I have a cost distance tool setup for a batch process. To take a bunch of inputs from a folder and then use the same cost raster and file saving conventions. My tool will run but it never runs the process of the cost distance. It just sits there and does not run. I tried making it with arcpy the regular way and tried with gp arcpy. Either way it does the same thing. Anyone know what I can do to make this run? #Cost distance for all hubs #by rangermry, 7-11-2013 using Python 2.6.5 import arcpy, os from arcpy.sa import * from arcpy import env #User inputs #Input Rasters Location Folder inputFolder=arcpy.GetParameterAsText(0) #Cost Raster a.k.a. Impedence Layer inCostRaster=arcpy.GetParameterAsText(1) #Derived inputs #Make output folder #Take input folder name as variable. rootFolName=str(inputFolder.split("\\")[-1]) #Take input folder name off of root folder path. newRoot=inputFolder.replace(rootFolName,"") outputFolder=os.path.join(newRoot,"Cost_Distance") if not os.path.isdir(outputFolder): os.makedirs(outputFolder) arcpy.AddMessage("-Folder made for output : "+outputFolder+'\n') #Environmental Workspace env.workspace=inputFolder #Get all Raster Items from inputFolder as workspace rasterList=arcpy.ListRasters() arcpy.AddMessage("-Obtaining all raster inputs\n.") try: for rast in rasterList: if not rast=="imped": fullpath = os.path.join(inputFolder, rast) arcpy.AddMessage("-Processing file : "+rast+'\n') #Set Local Variables inSourceData=fullpath outBkLinkRaster=outputFolder+"\\BkLk_"+rast.replace("value","") outDistRaster=outputFolder+"\\CD_"+rast.replace("value","") #Execute CostDistance arcpy.AddMessage("Proceeding to cost distance.\n") #Check out Spatial Analyst Licesne arcpy.CheckOutExtension("spatial") arcpy.gp.CostDistance_sa(inSourceData,inCostRaster,outDistRaster,"",outBkLinkRaster) arcpy.AddMessage("-Processed file: "+rast+"Saving file...") arcpy.AddMessage("-Saved file! \n ") except IOError: #Print traceback error. tb=sys.exc_info()[2] tbinfo=traceback.format_tb(tb)[0] arcpy.AddMessage("PYTHON ERRORS:\nTraceback info: "+tbinfo+"\nError Info:"+str(sys.exc_info()[1]))
... View more
07-15-2013
09:20 AM
|
0
|
3
|
2467
|
|
POST
|
I am trying this now, maybe it will work: #Extract by Attributes for all hubs #by rangermry, 7-11-2013 using Python 2.6.5
import arcpy, os
from arcpy.sa import *
from arcpy import env
#Check out Spatial Analyst Licesne
arcpy.CheckOutExtension("Spatial")
#User inputs
ras=arcpy.GetParameterAsText(0)
saveSpace=arcpy.GetParameterAsText(1)
#Derived inputs
rasName=str(ras.split("\\")[-1])
rasWkspce=ras.replace(rasName,"")
env.workspace=rasWkspce
i=0.0
while i <= myRaster.maximum:
i+=1
inSQLClause="Value="+str(i)
num=str((str(i)).split(".")[0])
attExtract=ExtractByAttributes(rasName,inSQLClause)
attExtract.save(os.path.join(saveSpace,"Value"+num))
... View more
07-11-2013
10:36 AM
|
0
|
0
|
1021
|
|
POST
|
Oops, made mistake for input parameter should be (1) for the second one.
... View more
07-11-2013
09:47 AM
|
0
|
0
|
1021
|
|
POST
|
Please help, I get an error? Error is that it cannot save? The odd thing is it runs if I do not set it up as a toolbox and put the input name of the raster seperate from the folder name? Any ideas or is that how it has to work?
#Extract by Attributes for all hubs
#by rangermry, 7-11-2013 using Python 2.6.5
import arcpy, os
from arcpy.sa import *
#Check out Spatial Analyst Licesne
arcpy.CheckOutExtension("Spatial")
ras=arcpy.GetParameterAsText(0)
saveSpace=arcpy.GetParameterAsText(1)
i=0.0
while i <= myRaster.maximum:
i+=1
inSQLClause="Value="+str(i)
num=str((str(i)).split(".")[0])
attExtract=ExtractByAttributes(ras,inSQLClause)
attExtract.save(os.path.join(saveSpace,"Value"+num))
... View more
07-11-2013
08:44 AM
|
0
|
2
|
1611
|
|
POST
|
Okay, setting up the modules to run with all the objects being passed to them may help my problem. Does anything need to be in __init__.py?
... View more
06-07-2013
11:27 AM
|
0
|
0
|
1006
|
|
POST
|
I have been diving through Python this year and have gotten stuck at modules. I have four scripts that perform some awesome fixes for MXDs when there is a database change. However, I have to put the same functions and variables at the top of each one of the scripts. I was thinking of turning this into a module or package? I cannot seem to figure these module/packages out. My question is for Python 2.6.5/ArcGIS 10... What would need to be in __init__.py? How would I set up the global variables/functions for all the scripts to use? How do I register all the scripts to be able to recognize what went on in the others? I have tried the helps out there but am further lost. Maybe someone can help me and then I can fix and post my product?
... View more
06-07-2013
10:18 AM
|
0
|
3
|
1368
|
|
POST
|
I get that, how do I assign the value list to the parameter or is that what the above does?
... View more
06-03-2013
11:51 AM
|
0
|
0
|
1287
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 12-18-2015 04:46 AM | |
| 2 | 12-18-2015 06:15 AM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:24 AM
|