Back to Avenue Source Code Home

'---------------------------------
'Name: Shapefile.SplitUniqueAttributes
'Date: 1-8-01
'
' prompts the user for a SINGLE shapefile on disk
'
' prompts the user for a field from shapefile to split on
'
' finds all unique attributes within field in shapefile
'
' exports each set of records with unique attribute value
' as a separate shapefile in same directory
'
'Returns:
'None
'
'Called By:
'MANY
'
'Calls:
'Util.CreateThemeFromSrc
'Util.GetFieldFromVtab
'Util.GetUniqueValuesInFieldList
'View.ZoomFullExtentArgs
'
'Arguments:
'None
'---------------------------------

MsgBox.Report("Shapefile.SplitUniqueAttributes" + NL +
              "1. prompts the user for a SINGLE shapefile on disk" + NL +
              "2. prompts the user for a field from shapefile to split on" + NL +
              "3. finds all unique attributes within field in shapefile" + NL +
              "4. exports each set of records with unique attribute value" + NL +
              "   as a separate shapefile in same directory", "About Script....")

'---------------------------------------
'retrieve srcname of shapefile to be split
'---------------------------------------
theFileName = FileDialog.Show("*.shp", "ShapeFiles", "Select Theme To Be Split")
if ( theFileName = NIL ) then
  return NIL
end

'---------------------------------------
'retrieve prefix for all exported shapes
'---------------------------------------
thePrefixString = MsgBox.Input("Enter the name of a prefix for all exported shapes:", "Prefix Name", "unq")
if ( thePrefixString = NIL ) then
  return NIL
end

'---------------------------------------
'make theme from srcname
'---------------------------------------
theTheme = av.Run("Util.CreateThemeFromSrc", theFileName.AsString)
if ( theTheme = NIL ) then
  return NIL
end
theFtab = theTheme.GetFtab

'---------------------------------------
'retrieve field to extract unique records
'---------------------------------------
theField = av.Run("Util.GetFieldFromVtab", theFtab)
if ( theField = NIL ) then
  return NIL
end

theFtab.GetSelection.SetAll

'---------------------------------------
'retrieve unique values in field
'---------------------------------------
theUniqueValuesList = av.Run("Util.GetUniqueValuesInFieldList", {theFtab, theFtab.GetSelection, theField})
if ( theUniqueValuesList.Count = 0 ) then
  return NIL
end

theExportFtabList = List.Make

'---------------------------------------
'generate new shapefile for each value
'---------------------------------------
for each val in theUniqueValuesList
  if ( val.Is(String) ) then
    theQueryString = "([" + theField.GetName + "]" ++ "=" ++ val ++ ")"
  else
    theQueryString = "([" + theField.GetName + "]" ++ "=" ++ val.AsString ++ ")"
  end
  if ( theFtab.Query(theQueryString, theFtab.GetSelection, #VTAB_SELTYPE_NEW).Not ) then
    MsgBox.Error("Unable to execute query with string:" ++ theQueryString, "Error")
    continue
  end
  theFtab.UpdateSelection
  theExportFileString = theFileName.ReturnDir.AsString + "\" + thePrefixString + val.AsString + ".shp"
  theExportFtabList.Add(theFtab.ExportClean(theExportFileString.AsFileName, TRUE))
end

'---------------------------------------
'insert new themes in view?
'---------------------------------------
if ( MsgBox.YesNo("Would you like to insert split themes in a View?", "View Themes", FALSE) ) then
  theNewView = View.Make
  theNewView.GetWin.Open  
  for each expFtab in theExportFtabList
    theTheme = Theme.Make(expFtab.GetSrcName)
    theNewView.AddTheme(theTheme)
  end
  av.Run("View.ZoomFullExtentArgs", theNewView)
end

av.PurgeObjects

return NIL



Last Updated: Marco Morais