Back to Avenue Source Code Home

'---------------------------------
'Name: shp2grid.bat
'Date: 1-10-01
'Author: Marco Morais
'
' prompts the user for shapefiles on disk
'
' exports each shapefile to a binary grid in same directory
'
'Returns:
'NILL
'
'Called By:
'MANY
'
'Calls:
'None
'
'Arguments:
'None
'---------------------------------

MsgBox.Report("shp2grid.bat" + NL +
              "1. prompts the user for shapefiles on disk" + NL +
              "2. exports each shapefile to a binary grid" + NL +
              "   of same name in same directory", "About Script....")

'---------------------------------------
'retrieve list of shapefiles to be split
'---------------------------------------
theShapefileFileNameList = FileDialog.ReturnFiles({"*.shp"}, {"ShapeFiles(*.shp)"}, "Select Shapefiles to Export to Grid", 0)
if ( theShapefileFileNameList.Count = 0 ) then
  return NIL
end

'---------------------------------------
'retrieve Grid to be used for setting analysis properties
'---------------------------------------
theAnalysisGridSrcNameList = SourceDialog.ShowClass("Select the Grid to extract Analysis Properties", Grid)
if ( theAnalysisGridSrcNameList.Count <> 1 )  then
  MsgBox.Error("Must select ONE Grid to serve as Analysis Mask", "Error")
  return NIL
end

'----------------------------------------
'set the analysis extent
'----------------------------------------
theAnalysisExtentRect = av.Run("Grid.ReturnExtent", theAnalysisGridSrcNameList.Get(0).GetFileName.AsString)
if ( theAnalysisExtentRect = NIL ) then
  MsgBox.Error("Unable to set Analysis rect", "Error")
  return NIL
end
GRID.SetAnalysisExtent(#GRID_ENVTYPE_VALUE, theAnalysisExtentRect)

'---------------------------------------
'set the analysis cell size
'---------------------------------------
theAnalysisCellSize = av.Run("Grid.ReturnCellSize", theAnalysisGridSrcNameList.Get(0).GetFileName.AsString)
if ( theAnalysisCellSize = NIL ) then
  MsgBox.Error("Unable to set Analysis cell size", "Error")
  return NIL
end
GRID.SetAnalysisCellSize (#GRID_ENVTYPE_VALUE , theAnalysisCellSize)

'---------------------------------------
'set the analysis mask
'---------------------------------------
av.Run("Grid.SetAsMask", theAnalysisGridSrcNameList.Get(0).GetFileName.AsString)

'------------------------------------------
'retrieve list of all shapefile ftabs to convert
'------------------------------------------
theShapefileFtabList = List.Make
for each sfn in theShapefileFileNameList
  theTheme = av.Run("Util.CreateThemeFromSrc", sfn.AsString)
  if ( theTheme = NIL ) then
    MsgBox.Error("Unable to create theme from input Shapefile", "Error")
    return NIL
  end
  theShapefileFtabList.Add(theTheme.GetFTab)
end

'-------------------------------------------
'convert the shapefile filenames to grid strings
'-------------------------------------------
theGridToConvertFileStringList = List.Make
for each sfn in theShapefileFileNameList
  theGridToConvertFileStringList.Add(sfn.AsString.BasicTrim("",".shp"))
end

if ( theShapefileFtabList.Count <> theGridToConvertFileStringList.Count ) then
  MsgBox.Error("Mismatched shapefile and grid filenames", "Error")
  return NIL
end

'-------------------------------------------
'make the grids
'-------------------------------------------
theConvertedGridList = List.Make
theField = av.Run("Util.GetFieldFromVtab", theShapefileFtabList.Get(0))
if ( theField = NIL ) then
  MsgBox.Error("Field to export as Grid not selected", "Error")
  return NIL
end
theFieldName = theField.GetName
thePrj = Prj.MakeNull
i = 0
for each ft in theShapefileFtabList
  theField = ft.FindField(theFieldName)  
  if ( theField = NIL ) then
    theField = av.Run("Util.GetFieldFromVtab", ft)
    if ( theField = NIL ) then
      MsgBox.Error("Field to export as Grid not selected", "Error")
      return NIL
    end
  end
  theGrid = Grid.MakeFromFTab(ft, thePrj, theField, NIL)
  theGTheme = GTheme.Make(theGrid)
  if (theGrid.HasError) then
    MsgBox.Error("Grid not able to be created", "Error")
    return NIL
  end
  theGrid = theGrid.Int
  theGrid = av.Run("Grid.MakeBinary", {theAnalysisGridSrcNameList.Get(0).GetFileName.AsString, theGrid})
  if ( theGrid.Is(Grid).Not Or theGrid.HasError ) then
    MsgBox.Error("Grid not able to be created", "Error")
    return NIL
  end
  theGrid.SaveDataSet(theGridToConvertFileStringList.Get(i).AsFileName)
  theConvertedGridList.Add(theGrid)
  i = i + 1
end

av.PurgeObjects

return NIL



Last Updated: Marco Morais