' Cattle FG Price ' Written by Timothy S. Thomas ' Written on 7/26/00. Last modified 9/11/00. ' This script generates a cost surface from a friction grid. ' Then, it takes price information associated with a point ' shapefile, and computes only positive farmgate prices for ' each point. ' Finally, it takes the maximum value for the collective ' farmgate price surfaces. theView = av.GetActiveDoc theThemeList = theView.GetThemes.Clone fricGTheme = MsgBox.ChoiceAsString(theThemeList,"Select friction grid", "Friction Grid") fricGrid = fricGTheme.GetGrid priceTheme = MsgBox.ChoiceAsString(theThemeList,"Select shapefile with prices", "Price Shapefile") priceFTab = priceTheme.GetFTab fieldList = priceFTab.GetFields.Clone priceField = MsgBox.ChoiceAsString(fieldList,"Select field containing price data", "Price Field") numrecs = priceFTab.GetNumRecords ' "Shape" is a special word in FTabs shapeField = priceFTab.FindField("Shape") ' Permits changes to be made to the FTab priceFTab.SetEditable(True) cellsize = fricGrid.GetCellSize extent = fricGrid.GetExtent thePrj = theView.GetProjection ' The tolerance is used to determine whether a point actually selected ' another point. aTolerance = cellsize ' In order to make the program run faster, I set it up so that it does ' not compute negative farmgate prices. That means that there is a large ' region of cells with No Data. Cells with No Data do not allow comparison ' or other operations with other grids in those cells. So the No Data ' values need to be reclassified as 0. ' To reclassify as 0, one can use ArcView classifications, or dbase files. ' I was not able to figure out how to enter No Data as an entry in a ' dbase file. So I used classifications. ' Unfortunately, I could not figure out a direct way to change the classification, ' so the user has to change the classification himself or herself. ' The messages below describe what to do. msgString1 = "The next screen will be the reclassification dialog. " msgString2 = "Please remove all entries, except for No Data. " msgString3 = "Set No Data to a value of 0." msgString = msgString1 + msgString2 + msgString3 MsgBox.Info(msgString,"Important Note") theParameters = ReclassEditor.Show(fricGTheme) ' check for Cancel from dialog if (theParameters = NIL) then return NIL end fieldName = theParameters.Get(0) theClassList = theParameters.Get(1) gridList = List.Make outList = List.Make ' This is temporary for testing purposes ' numrecs = 5 ' *************************************************************************** ' * ' Conversion factor for switching units from meters (map unit) and * ' 1997US cents per cubic meter-kilometer (from friction grid), to * ' 1997 R$. * ' * ' * ' * conv = 100000 / 1.0731 ' * ' * ' * ' * ' *************************************************************************** ' Convert point file to list of points. I use this list to select ' points one at a time from the point shapefile. for each rec in 0..(numrecs-1) ' Select each point in the shapefile, one at a time if (rec = 0) then pointList = priceFTab.ReturnValue(shapeField,rec).AsMultipoint.AsList else pointList = pointList + priceFTab.ReturnValue(shapeField,rec).AsMultipoint.AsList end end ' I frequently got the message: No more cell layer channerls. ' This happened with larger point shapefiles (e.g., 72). ' To fix this, I tried writing to new grids intermittently (e.g., every 5). ' I though this would cause old grids to be evaluated, but apparently not. ' So now I'm writing GThemes intermittently. This causes grids to be ' evaluated. ' The variable below tells how frequently. freq = 20 for each recnum in 0..(numrecs-1) ' Select each point in the shapefile, one at a time priceFTab.SelectByPoint(pointList.Get(recnum),aTolerance,#VTAB_SELTYPE_NEW) ' Make a grid from that point pointGrid = Grid.MakeFromFTab(priceFTab,thePrj,priceField,{cellsize, extent}) ' Normally, the price in the shapefile would be in prices paid for the ' product once it is brough to market. This is the variable PRW. ' In modifying this program, normally we would have PRW ReturnValue, ' and PSW would not exist as a variable. ' However, in the case of timber, I only had sawnwood price, not log price. ' *************************************************************************** ' * ' Price of cattle per metric ton from price per arroba at slaughterhouse * ' * ' The basic data is in Jan-to-Apr-2000R$ per arroba, * ' and to deflate to June-1997R$ we need to use a deflation factor. * * ' * ' * ' * pricdefl = 1.4940 ' * ' * ' * ' The 1000/30 converts from metric tons to arrobas * ' * prw = (1000/30)*priceFTab.ReturnValue(priceField,recnum)/(pricdefl) ' * ' * ' * ' * ' *************************************************************************** ' The last input to CostDistance tells it to stop when the market price ' equals the transport cost. ' The conversion factor allows the friction units and map units to be ' converted to desired output units. cellGrid = pointGrid.CostDistance(fricGrid,nil,nil,prw*conv) / conv reclassGrid = (prw.AsGrid - cellGrid) .ReclassByClassList(fieldName,theClassList,false) ' Every group of grids gets output to outGrid. ' This is due to memory problems when gridList gets too big. if (gridList.Count = (freq - 1)) then if (recnum = (freq - 1)) then outGrid = reclassGrid.LocalStats(#GRID_STATYPE_MAX,gridList) outGTheme = GTheme.Make(outGrid) else gridList.Add(reclassGrid) outGrid = outGrid.LocalStats(#GRID_STATYPE_MAX,gridList) outGTheme = GTheme.Make(outGrid) end gridList.Empty else gridList.Add(reclassGrid) end ' Taking care of the last point if (recnum = (numrecs - 1)) then if (gridList.Count <> 0) then if (numrecs <= freq) then outGrid = reclassGrid.LocalStats(#GRID_STATYPE_MAX,gridList) else outGrid = outGrid.LocalStats(#GRID_STATYPE_MAX,gridList) end end outGTheme = GTheme.Make(outGrid) theView.AddTheme(outGTheme) end end priceTheme.ClearSelection ' Clears message in message bar av.ClearMsg ' Turns off editing in the FTab priceFTab.SetEditable(false) ' Removes garbage objects av.PurgeObjects