'Скрипт считает среднее и записывать в новое поле полигонального шейпа. 
'В самом скрипте надо поправить название поля, в котором у тебя хранятся высоты. В скрипте сейчас это выделено звездочками.
'Created: 28.02.2008
'Last updated: 30.03.2008
'Author: Maxim Dubinin (sim@gis-lab.info)
'Based on featureDensity.ave by Tim Schaub, CommEn Space

msgTitle = "FeatureDensity"

theView = Av.getActiveDoc
if (theView.is(view).not) then
  MsgBox.warning("Run from a view",msgTitle)
  return NIL
end
if(theView.getProjection <> Prj.makeNull) then
  MsgBox.warning("You must view the data in it's original projection instead of " + theView.getProjection.asString, msgTitle)
  return NIL
end
viewUnits = theView.getUnits
unitStr = viewUnits.asString.asTokens("_").get(2).lCase

'change to working directory
Av.getProject.getWorkDir.setCWD

' get base theme (or cancel for grid output)
baseTheme = theView.getActiveThemes.get(0)
baseFTab = baseTheme.getFTab
oldBaseSelection = baseFTab.getSelection.clone
baseTheme.clearSelection
baseShpField = baseFTab.findField("shape")
totalRecords = baseFTab.getNumRecords

theFThemes = List.make
for each aTheme in theView.getThemes
  if (aTheme.is(FTheme)) then
    theFThemes.add(aTheme)
  end
end
theFThemes.removeObj(baseTheme)
if (theFThemes.isEmpty) then
  MsgBox.warning("Add an additional point, line, or polygon feature theme to the view",msgTitle)
  return NIL
end

'get feature theme
inputTheme = MsgBox.choice(theFThemes,"Select input feature theme." + NL + "Output densities will be written to " + baseTheme.getName,msgTitle+" - Input Theme")
if (inputTheme=NIL) then
  return NIL
end
inputFTab = inputTheme.getFTab
inputShpField = inputFTab.findField("shape")
if(inputShpField.getType = #FIELD_SHAPEPOLY) then
  theDim = 2
elseif(inputShpField.getType = #FIELD_SHAPELINE) then
  theDim = 1
elseif(inputShpField.getType = #FIELD_SHAPEPOINT) then
  theDim = 0
else
  MsgBox.warning("Only works on polygon, line, or point themes",msgTitle)
  return NIL
end
oldInputSelection = inputFTab.getSelection.clone
inputTheme.clearSelection

'get densityField name
typeList = {"point","line","poly"}
densityFieldName = typeList.get(theDim) + "_dens"
countFieldName = typeList.get(theDim) + "_cnt"
newelevFieldName = typeList.get(theDim) + "_elev"
densityField = baseFTab.findField(densityFieldName)
if((densityField = NIL).not) then
  if(MsgBox.yesNo("Overwrite values in "+densityFieldName+" field?",msgTitle,TRUE).not) then
    return NIL
  end
else
  densityField = Field.make(densityFieldName, #FIELD_FLOAT, 16, 12)
  baseFTab.setEditable(TRUE)
  baseFTab.addFields({densityField})
  baseFTab.setEditable(FALSE)
end

countField = baseFTab.findField(countFieldName)
'******************************************************
elevField = inputFTab.findField("ID")
'******************************************************
if((countField = NIL).not) then
  if(MsgBox.yesNo("Overwrite values in "+countFieldName+" field?",msgTitle,TRUE).not) then
    return NIL
  end
else
  countField = Field.make(countFieldName, #FIELD_LONG, 16, 12)
  baseFTab.setEditable(TRUE)
  baseFTab.addFields({countField})
  baseFTab.setEditable(FALSE)
end

newelevField = baseFTab.findField(newelevFieldName)
if((newelevField = NIL).not) then
  if(MsgBox.yesNo("Overwrite values in "+newelevFieldName+" field?",msgTitle,TRUE).not) then
    return NIL
  end
else
  newelevField = Field.make(newelevFieldName, #FIELD_DOUBLE, 16, 2)
  baseFTab.setEditable(TRUE)
  baseFTab.addFields({newelevField})
  baseFTab.setEditable(FALSE)
end

'do the intersections
baseFTab.setEditable(TRUE)
Av.showMsg("Calculating feature density for "+baseTheme.getName)
Av.setStatus(0)
Av.showStopButton
for each baseRecNum in baseFTab
  intersectedStat = 0
  keepGoing = Av.setStatus((baseRecNum/totalRecords)*100)
  if (keepGoing = FALSE) then
    Av.showMsg(msgTitle+" stopped")
    Av.clearStatus
    baseFTab.setEditable(FALSE)
    return NIL
  end
  aShape = baseFTab.returnValue(baseShpField, baseRecNum)
  shapeArea = aShape.returnArea
  'select records which intersect aShape
  inputFTab.selectByPolygon(aShape,#VTAB_SELTYPE_NEW)
  if(inputFTab.getNumSelRecords >= 0) then
    featurecount = inputFTab.getNumSelRecords
    'if feature is point, add up number of records
    if(theDim = 0) then
      intersectedStat = inputFTab.getNumSelRecords
      totalelev = 0
      for each interRecord in inputFTab.getSelection
        val = inputFTab.returnvalue(elevField,interRecord)
        totalelev = totalelev + val
      end
    else
      'get intersection of each intersected record
      for each interRecord in inputFTab.getSelection 
        theIntersection=aShape.returnIntersection(inputFTab.returnValue(inputShpField,interRecord))
        if (theIntersection.isNull.NOT) then
          'sum up statistic
          if(theDim = 1) then
            intersectedStat = intersectedStat + (theIntersection.returnLength)
          else
            intersectedStat = intersectedStat + (theIntersection.returnArea)
          end
        end ' is NOT NULL
      end ' interRecord
    end ' if theDim <> 0
  end ' test for selection
  'calculate feature density for this shape
  featureDensity = intersectedStat / shapeArea
  'set feature density value
  baseFTab.setValue(densityField, baseRecNum, featureDensity)
  baseFTab.setValue(countField, baseRecNum, featurecount)
  baseFTab.setValue(newelevField, baseRecNum, totalelev/featurecount)
end ' for each baseRecNum
baseFTab.setEditable(FALSE)
Av.clearStatus
Av.showMsg("Feature densities written to "+densityFieldName+" field of "+baseTheme.getName)

'restore selection on input theme
inputFTab.setSelection(oldInputSelection)
baseFTab.setSelection(oldBaseSelection)