|
POST
|
Since you're new to Python, I thought I'd show you how formatting strings help with the syntax. Much easier to debug when a SQL expression or pathname gets complicated!
for row in rowCursor:
feat = row.getValue(shapefieldname)
clipRas = "{0}.img".format(row.getValue(field))
arcpy.Clip_management(OUTraster, "#", clipRas, feat, "0", "ClippingGeometry")
rowCursor.next()
... View more
04-01-2013
03:52 PM
|
0
|
0
|
900
|
|
POST
|
Stuart, I don't see the problem, two things to check: 1. Make sure your dbf table listed there is in the current workspace or a fully specified path, for example: CalcDiff(r"C:\workspace\pipetable.dbf", "PIPE_LENGTH", "DISTANCE") if this is a model element named Input table you can do this: CalcDiff(r"%Input table%", "PIPE_LENGTH", "DISTANCE") 2. Function and variable names case-sensitive in Python, so make sure you have the caps right.
... View more
04-01-2013
01:08 PM
|
0
|
0
|
5180
|
|
POST
|
Using your attachment as reference I can see you take a string variable and check to see what the string is using the "Calculate Value" (CV) tool and then set the data type to Boolean. But I am not quite sure what to put in the code block or expression field and string variable a precondition to the "Calculate Value" tool? In the CV tool, you fill in the expression with the one I put in the label: "%String%" == "A" Leave the CV code block blank. If the test were complicated, you would put a function in the code block. but in this case you do not need it. String is the name of a data element in your model. The value of String gets put there before Python sees it, so it evaluates at run time to: "A" == "A" which returns 1 which is returned as True if you set the output type to Boolean. Then grab the magic wand tool and connect the output of Calculate Value to the first tool in a process chain you want to run when "%String%" == "A". Select "Precondition" from the context menu. Do this the same with the other one ("%String%" == "B"). Set the output as a precondition to processing chain B. If this evaulates to false ("B" == "A" - is false), the second processing chain will not run. Neat, isn't it? Hope this helps.
... View more
04-01-2013
12:22 PM
|
0
|
0
|
7260
|
|
POST
|
Eric, Python scripting makes sense when things get complicated, epecially if you need more control over error messages and tool validation. However, there are several approaches within Model Builder, some involving a script tool, some just using Calculate Value. Here's my favorite method, using the "model-only" Calculate Value tool, to calculate a Boolean (true/false) value and connecting its output as preconditions to the processing chains. In this example, I use the Merge Branch tool to provide the same output whether I use one option or the other. [ATTACH=CONFIG]23118[/ATTACH] I highly recommend this set of blog posts: ArcGIS blog: if you are stuck at "if" (5 parts) Using Calculate Value, filters, and preconditions, you can build some pretty sophisticated tools within ModelBuilder.
... View more
04-01-2013
09:38 AM
|
0
|
0
|
7260
|
|
POST
|
Are you saying any variable set to a string in the original script needs to have this format? No, this is specific to the function arcpy.mapping.MapDocument(). There is a code sample in the help. Desktop Help 10.1: Map Document I read how to post but I was kind of confused. Pretty simple really, just paste your code into your forum post and surround it with these tags [noparse]
# my code goes here
[/noparse]
... View more
04-01-2013
09:10 AM
|
0
|
0
|
1422
|
|
POST
|
I think the third option is a very creative solution that would be pretty easy to implement using parameter validation code.
... View more
03-31-2013
01:24 PM
|
0
|
0
|
1627
|
|
POST
|
Good news: I found the page describing how a hillshade is calculated. Here I'm pretty sure I can construct a model to output the ground level shade from this reference. And here's the "But" But this hillshade algorithm does not show how to account for the length of the shadow when the "Model Shadows" option in the standard hillshade model is toggled on. What are you trying model with respect to shadow length? ArcGIS is not a true 3-d modeling environment though sometimes we can do simple approximations using the basic raster tools.
... View more
03-30-2013
04:37 PM
|
0
|
0
|
2085
|
|
POST
|
I've looked at it but it isn't checked. Not the 'Extent' nor 'snap raster' options. Jelle, The default extent settings for most tools is MAXOF. So your large output is what I'd expect if the extent has not been set. If you set the extent to the smaller area, your output raster will be the smaller area. Set the snap raster to your land use raster if you want the output raster cells to "line up" with the input land use raster (no shifting or resampling). Another option to try is the Clip_management tool. Clip has the side benefit of being dramatically faster than Extract By Mask.
... View more
03-30-2013
04:30 PM
|
0
|
0
|
3819
|
|
POST
|
I think this path is incorrect: timeZoneBoundaries = "G/London_Street/TimeZones/euroTimeZones" should be timeZoneBoundaries = "G:/London_Street/TimeZones/euroTimeZones" [thread=48475]How to post Python code[/thread]
... View more
03-30-2013
04:18 PM
|
0
|
0
|
603
|
|
POST
|
... is there a way specify field types in python where the finished CSV file would not require a schema.ini file to import fields in the correct format such as: date, Long, Double and also control the field width. If the field properties you are getting by default aren't correct you must create a schema.ini file - or edit an existing one in that folder - to get the field types you want. Alhough writing schema.ini in Python is not that difficult (the format is not that complicated) there is a another approach: The Python standard library includes the csv module, which you may be helpful to you as it will not convert data on input. You could read and append your csv data using Python lists, create a table with the schema you want, and then add the data to the table using an insert cursor. In this way you could bypass all the schema.ini jazz.
... View more
03-30-2013
04:08 PM
|
0
|
0
|
1048
|
|
POST
|
It is showing a range of 3.4e23 to -3.4e23. This means your output is corrupted. This can happen sometimes with raster operations when there is a bug involving bit depth, or you did something that produces invalid results. This kind of sounds like a bug. Have you tried clipping your raster using Clip_management? That may not only work, but I learned last week is dramatically faster than the Extract By Mask tool. (Clip_management isn't coded to have to work with Map Algebra so it's a bit of a "shortcut".)
... View more
03-29-2013
05:21 PM
|
0
|
0
|
3498
|
|
POST
|
So far my model looks like: Input raster -> PrepareSSTData (tool) -> Output Raster -> ? -> RasterCalculator (with MAexpression) -> Output raster(2) Is there a way to [connect Output Raster to Raster Calculator] in Model Builder? Raster Calculator accepts raster layer inputs -- so if you open the Raster Calculator tool inside ModelBuilder you should see "Output Raster" in the pick list to use in expressions. Is it not there?
... View more
03-29-2013
05:05 PM
|
0
|
0
|
4635
|
|
POST
|
The sort ascending must also be done in the field calculator before doing the calculation. Sorry, Stuart, I have bad news for you. The Calculate Field code block is executed one row at a time - so you are dependent on the sort order of the input table. (The Sort tool was (finally) introduced at 10.0 -- I have wanted it since 8.0 was released!) Seems to me the best approach to this would be to do this in Python with a sorted update cursor instead of Calculate Field. You could create a script tool, but I bet you can get by using the Calculate Value tool in Model Builder: Expression: CalcDiff(r"pipetable.dbf", "PIPE_LENGTH", "DISTANCE") Code Block:
import arcgisscripting
gp = arcgisscripting.create(9.3)
def CalcDiff(tbl,valField,diffField):
procFields = valField + ";" + diffField
sortField = valField
Rows = gp.UpdateCursor(tbl, "", "", procFields, sortField)
lastVal = 0
for Row in Rows:
val = float(Row.getValue(valField))
# calc the difference between the last value and this one
diff = val - lastVal
Row.setValue(diffField,diff)
Rows.updateRow(Row)
lastVal = val
del Row, Rows
return tbl
Data Type: Table
... View more
03-29-2013
04:54 PM
|
0
|
0
|
5180
|
|
POST
|
I ran into an issue that was solved by the new arcpy.env.scratchFolder functionality to force working in a folder. Unfortunately I was using 10.0, not 10.1. So, I created some Python functions that support this functionality in 9.x and 10.0. I ran into an issue when the scratch workspace is an invalid path -- so this is a fix for 10.1 as well. Any comments, fixes, or suggestions are welcomed. ModelBuilder users could use these functions in a Calculate Value code block to generate a "safe" scratch folder in their model tools.
def ScratchFolder():
"""env.scratchFolder for all versions of ArcGIS geoprocessing
1) If supported, return arcpy.env.scratchFolder (ArcGIS 10.1 or later)
2) Return a value based on ScratchWorkspace (ArcGIS 9.3 - 10.0)
a) Folder: scratchWorkspace
b) GDB: scratchWorkspace/../scratch
c) Not set or invalid: TEMP/scratch
Curtis Price - U.S. Geological Survey - [email protected]
http://www.usgs.gov - science for a changing world
"""
import os
import arcgisscripting
gp = arcgisscripting.create()
try:
sw = gp.scratchWorkspace
if sw:
# check for invalid scratchWorkspace path
if not gp.Exists(sw):
gp.AddIDMessage("Warning", 873, "Invalid Scratch Workspace", sw)
raise
sw = gp.scratchFolder
except:
try:
sw = gp.scratchWorkspace
try:
swType = gp.Describe(sw).dataType
if swType == "Folder":
pass
elif swType == "Workspace":
pth = os.path.dirname(sw)
sw = os.path.join(pth, "scratch")
else:
raise
except:
sw = os.path.join(os.environ["TEMP"], "scratch")
finally:
sw = os.path.realpath(sw)
if not gp.Exists(sw): os.mkdir(sw)
return sw
def ScratchGDB():
"""env.scratchGDB for all versions of ArcGIS geoprocessing
1) If supported, return arcpy.env.scratchGDB (ArcGIS 10.1 or later)
2) Return a value based on ScratchWorkspace (ArcGIS 9.3 - 10.0)
a) GDB: scratchWorkspace
b) Folder: scratchWorkspace/scratch.gdb
c) Not set or invalid: TEMP\scratch.gdb
Curtis Price - U.S. Geological Survey - [email protected]
http://www.usgs.gov - science for a changing world
"""
import os
import arcgisscripting
gp = arcgisscripting.create()
try:
sw = gp.scratchWorkspace
if sw:
# check for invalid scratchWorkspace path
if not gp.Exists(sw):
gp.AddIDMessage("Warning", 873, "Invalid Scratch Workspace", sw)
raise
sw = gp.scratchFolder
except:
try:
sw = gp.scratchWorkspace
swType = gp.Describe(sw).dataType
if swType == "Workspace":
pass
elif swType == "Folder":
sw = os.path.join(sw, "scratch.gdb")
except:
sw = os.path.join(os.environ["TEMP"], "scratch.gdb")
finally:
sw = os.path.realpath(sw)
if not gp.Exists(sw):
gp.CreateFileGDB_management(os.path.dirname(sw),
os.path.basename(sw))
return sw
... View more
03-29-2013
02:47 PM
|
0
|
0
|
1108
|
|
POST
|
I ran into an issue that was solved by the new arcpy.env.scratchFolder functionality to force working in a folder. Unfortunately I was using 10.0, not 10.1. So, I created some Python functions that support this functionality in 9.x and 10.0. I ran into an issue when the scratch workspace is an invalid path -- so this is a fix for 10.1 as well. Any comments, fixes, or suggestions are welcomed. ModelBuilder users could use these functions in a Calculate Value code block to generate a "safe" scratch folder in their model tools.
def ScratchFolder():
"""env.scratchFolder for all versions of ArcGIS geoprocessing
1) If supported, return arcpy.env.scratchFolder (ArcGIS 10.1 or later)
2) Return a value based on ScratchWorkspace (ArcGIS 9.3 - 10.0)
a) Folder: scratchWorkspace
b) GDB: scratchWorkspace/../scratch
c) Not set or invalid: TEMP/scratch
Curtis Price - U.S. Geological Survey - [email protected]
http://www.usgs.gov - science for a changing world
"""
import os
import arcgisscripting
gp = arcgisscripting.create()
try:
sw = gp.scratchWorkspace
if sw:
# check for invalid scratchWorkspace path
if not gp.Exists(sw):
gp.AddIDMessage("Warning", 873, "Invalid Scratch Workspace", sw)
raise
sw = gp.scratchFolder
except:
try:
sw = gp.scratchWorkspace
try:
swType = gp.Describe(sw).dataType
if swType == "Folder":
pass
elif swType == "Workspace":
pth = os.path.dirname(sw)
sw = os.path.join(pth, "scratch")
else:
raise
except:
sw = os.path.join(os.environ["TEMP"], "scratch")
finally:
sw = os.path.realpath(sw)
if not gp.Exists(sw): os.mkdir(sw)
return sw
def ScratchGDB():
"""env.scratchGDB for all versions of ArcGIS geoprocessing
1) If supported, return arcpy.env.scratchGDB (ArcGIS 10.1 or later)
2) Return a value based on ScratchWorkspace (ArcGIS 9.3 - 10.0)
a) GDB: scratchWorkspace
b) Folder: scratchWorkspace/scratch.gdb
c) Not set or invalid: TEMP\scratch.gdb
Curtis Price - U.S. Geological Survey - [email protected]
http://www.usgs.gov - science for a changing world
"""
import os
import arcgisscripting
gp = arcgisscripting.create()
try:
sw = gp.scratchWorkspace
if sw:
# check for invalid scratchWorkspace path
if not gp.Exists(sw):
gp.AddIDMessage("Warning", 873, "Invalid Scratch Workspace", sw)
raise
sw = gp.scratchFolder
except:
try:
sw = gp.scratchWorkspace
swType = gp.Describe(sw).dataType
if swType == "Workspace":
pass
elif swType == "Folder":
sw = os.path.join(sw, "scratch.gdb")
except:
sw = os.path.join(os.environ["TEMP"], "scratch.gdb")
finally:
sw = os.path.realpath(sw)
if not gp.Exists(sw):
gp.CreateFileGDB_management(os.path.dirname(sw),
os.path.basename(sw))
return sw
... View more
03-29-2013
02:47 PM
|
0
|
0
|
1384
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 08-11-2021 01:26 PM | |
| 5 | 12-10-2021 04:58 PM | |
| 1 | 02-27-2017 09:30 AM | |
| 2 | 12-04-2023 01:05 PM | |
| 1 | 04-12-2016 10:17 AM |
| Online Status |
Offline
|
| Date Last Visited |
06-19-2024
12:10 AM
|