Select to view content in your preferred language

Crashing pythonwin.exe when using Rescale By Function

2840
4
Jump to solution
05-26-2015 09:07 AM
NickMoylan
New Contributor II

Hello,

I am attempting to use the Rescale By Function tool rescale my raster values using the "LARGE" transformation function, however instead of using the calculated value for the midpoint parameter, I would like to use the MEDIAN. In this code I convert the float raster to integer, get the median using zonal stats, convert the Result Object to long integer, and pass it into the Rescale By Function parameters.

# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Local variables:
as_ppm = "C:\\Users\\nmoylan\\Documents\\Desktop Items\\Strategic Evaluations\\BC\\QUEST WEST\\Z9 Geochem and Grids\\as_ppm"
Constant_value = "1000"
process_mask = "C:\\Users\\nmoylan\\Documents\\Desktop Items\\Strategic Evaluations\\BC\\QUEST WEST\\Z9 Geochem and Grids\\process_mask"
as_ppb_int = "C:\\Database\\ARCGIS\\Geoprecessing\\Scratch\\as_ppb_int"
as_median = "C:\\Database\\ARCGIS\\Geoprecessing\\Scratch\\as_median"
as_ppb = "C:\\Database\\ARCGIS\\Geoprecessing\\Scratch\\as_ppb"
Final_Test = "C:\\Database\\ARCGIS\\Geoprecessing\\Scratch\\Final_Test"

# Process: Times
arcpy.gp.Times_sa(as_ppm, Constant_value, as_ppb)

# Process: Int - makes the MEDIAN option available in Zonal Stats
arcpy.gp.Int_sa(as_ppb, as_ppb_int)

# Process: Build Raster Attribute Table - Greater than 100000 values and 500 unique values
arcpy.BuildRasterAttributeTable_management(as_ppb_int, "Overwrite")

# Process: Zonal Statistics - to find the MEDIAN
arcpy.gp.ZonalStatistics_sa(process_mask, "VALUE", as_ppb_int, as_median, "MEDIAN", "DATA")

# Convert the Result Object from Zonal Stats to Long Integer Value
median_Result = long(arcpy.GetRasterProperties_management(as_median, "MEAN").getOutput(0))
print median_Result

# Process: Rescale by Function - rescale integer raster from 0 to 1 passing in MEDIAN value
arcpy.gp.RescaleByFunction_sa(as_ppb_int, Final_Test, "LARGE", median_Result, "5",  "# # # #", "0", "1")
print "Complete"

When I run it I get the following (6833 being the MEDIAN):

the dialog eventually dissapears without producing the 'Final_Test' raster.

I really just need a way of using the median as opposed to the mean to perform the LARGE transformation function.

Thanks,

Nick

0 Kudos
1 Solution

Accepted Solutions
SepheFox
Frequent Contributor

A few things I noticed:

I think the extension name is case sensitive (not sure). It is written as "Spatial" in the help files.

Your paths are very long, and have spaces in them. The best way is to make a folder directly in the root of your C drive for GIS analysis, such as C:\gis\, and to avoid spaces in folder names. This prevents a lot of issues.

The syntax for raster analysis tools is:

outTimes = Times_sa(inRaster, inConstant)

The gp syntax is outdated.

View solution in original post

4 Replies
SepheFox
Frequent Contributor

A few things I noticed:

I think the extension name is case sensitive (not sure). It is written as "Spatial" in the help files.

Your paths are very long, and have spaces in them. The best way is to make a folder directly in the root of your C drive for GIS analysis, such as C:\gis\, and to avoid spaces in folder names. This prevents a lot of issues.

The syntax for raster analysis tools is:

outTimes = Times_sa(inRaster, inConstant)

The gp syntax is outdated.

NickMoylan
New Contributor II

Thanks Sephe Fox,

The paths were ok to have spaces (this was the test data, real data resides on a lengthy server path)

Changing the syntax of the tools cause python to produce an actual error rather than crashing:

     Rescale By Function() takes at most 4 arguments (currently 7 total)

the solution is to create a transformation function object to pass into the tool, works great now:

     # Create the TfLarge object - to pass into the Rescale By Function Tool

     midpoint = median_Result

     spread = 5

     lowerthresh = "#"

     valbelowthresh = "#"

     upperthresh = "#"

     valabovethresh = "#"

     myTfFunction = TfLarge(midpoint, spread, lowerthresh, valbelowthresh, upperthresh, valabovethresh)

     # Process: Rescale by Function - rescale integer raster from 0 to 1 passing in MEDIAN value

     Final_Test = RescaleByFunction(as_ppb_int, myTfFunction, "0", "1")

http://resources.arcgis.com/en/help/main/10.2/index.html#//005m000000s9000000

DanPatterson_Retired
MVP Emeritus

Could you explain your RescaleByFunction  it looks very different from those in the help file.

0 Kudos
NickMoylan
New Contributor II

Hi Dan,

The original script i was working with was an Export from a ModelBuilder Model I was working on.  ModelBuilder seems to have exported it with extra arguments

0 Kudos