/3.2 (Extension.1 Name: "GarpDatasets" FirstRootClassName: "PMenu" Roots: 2 Roots: 4 Roots: 5 Roots: 6 Roots: 7 Version: 32 About: "GarpDatasets Extension allows you prepare custom environmental layers for use with DesktopGarp." InstallScript: 5 UninstallScript: 7 ExtVersion: 1 ) (PMenu.2 Child: 3 Label: "&GARP" ) (Choice.3 Label: "Clip Datasets..." Click: "GarpDatasets.ClipResGrids" Shortcut: "Keys.None" ) (Script.4 Name: "GarpDatasets.ClipResGrids" SourceCode: "'========================================================================================================================\n' Name : clipresgrids.ave\n' Version : 2.1\n' Author : Blaine Ray\n' Email : blaine@ukans.edu\n' Date : June 28, 2001\n' Desc. : Clips one or more grids in the view by a selected rectangle graphic and stores the clipped grids\n' in a new directory.\n' Input : One or more overlaying grids and one of the following: (1) a selected overlaying rectangle graphic, \n' ( 2) a polygon shapefile, or (3) a set of user-specified coordinates.\n' Output : Grids of the same file name that are clipped, resampled, and stored in a new directory as defined by the user.\n'========================================================================================================================\n\n\n' Title for pop-up message boxes\ntheHeader = \"Clip and Resample Grid Themes\"\n\n' Make sure the Spatial Analyst extension is loaded\nexttest=Extension.Find(\"Spatial Analyst\")\nif (exttest=NIL) then\n m sgbox.error(\"You must have the Spatial Analyst extension loaded\",theHeader)\n Exit\nend\n\n' Get the path to save new grids to\nnewpath = MsgBox.Input(\"Input the path to which the newly clipped grids will be placed\",theHeader,\"c:\\temp\\\")\nif (newpath = NIL) then\n msgbox.error(\"You must enter a valid path.\",theHeader)\n Exit\nend\n\n' Get the current view\ntheView = av.GetActiveDoc\n\n' Get themes in the current view\ntheThemes = theView.getthemes\n\n' Make empty grid and feature theme lists\ngridThemes = List.Make\nshpThe mes = List.Make\n\n' Populate grid and feature theme lists\nFor each i in theThemes\n if (i.is(GTHEME)) then\n gridThemes.add(i)\n elseif (i.is(FTHEME)) Then\n shpThemes.add(i)\n end\nEnd\n\n' Make sure that at least one grid is present in the view\nif (gridThemes.Count = 0) then\n msgbox.error(\"At least one grid theme must be present in the view.\",theHeader)\n exit\nend\n\n' Select an input type to use as a clip layer\ninputtype = MsgBox.ChoiceAsString({\"Selected Rectangle Graphic\",\"User-Specified Coordinates\",\"P olygon Theme\"},\"Select a clip item.\",theheader)\nif (inputtype = NIL) then\n msgbox.error(\"You must select a clipping method.\",theHeader)\n Exit\nend\n\n' Initialize boolean variable maskflag as being false (no analysis mask is being used)\nmaskflag = FALSE\n\n' Determine the input type to initialize the clipping boundary\nIf (inputtype = \"Selected Rectangle Graphic\") THEN\n \n ' Get the selected rectangle\n theGraphics=theview.getgraphics\n theSelg=theGraphics.getselected\n \n ' Make sure a graphic is selected\n i f (theSelG.count=0) then\n msgbox.error(\"Must select a single graphic in the view.\",theHeader)\n EXIT\n end\n \n ' Make sure only one graphic is selected\n if (theSelG.count>1) then\n msgbox.error(\"More than one graphic is selected, select only one\",theHeader)\n EXIT\n end\n \n ' Make sure the selected graphic is a rectangle\n agraphic=thegraphics.getselected.get(0)\n if (agraphic.getshape.is(rect).not) then\n msgbox.error(\"Shape must be a rectangle\",theHeader)\n EXIT\n end\n \n ' Set theRect as the rectangle shape\n theRect = agraphic.getshape\n\nElseIf (inputtype = \"User-Specified Coordinates\") THEN\n\n ' Ask user to enter bounding rectangle inputs\n llx = msgbox.input(\"Input the llx (origin) coordinate.\",theheader,\"\").asnumber\n lly = msgbox.input(\"Input the lly (origin) coordinate.\",theheader,\"\").asnumber\n urx = msgbox.input(\"Input the urx coordinate.\",theheader,\"\").asnumber \n ury = msgbox.input(\"Input the ury coordinate.\",theheader,\"\").asnumber\n\n ' Set theRect as the four user inputs\n t heRect = rect.makeXY(llx,lly,urx,ury)\n\nElseIf (inputtype = \"Polygon Theme\") THEN\n\n ' Make sure at least one feature theme is present in the view\n If (shpthemes.count = 0) Then\n msgbox.error(\"No shapefiles present in the view.\",theHeader)\n Exit\n End\n \n ' Ask user to select a shapefile to use as a clip layer\n cliptheme = MsgBox.list(shpthemes,\"Select a shapefile containing a single shape for clipping.\" ,theHeader)\n if (cliptheme = NIL) then\n msgbox.error(\"You must select a clip shapefile.\",the Header)\n Exit\n end\n\n ' Get the feature table of the selected shape file\n clipftab = cliptheme.getftab\n\n ' Use the first shape in the shapefile as a clip shape\n clipshp = clipftab.returnvalue((clipftab.findfield(\"shape\")),0)\n \n ' Get the minimum bounding box for the clip shape\n clipextent = clipshp.returnextent\n\n ' Set theRect as the minimum bounding box for the clip shape\n theRect = clipextent\n \n ' Ask user if values outside of the shape but within the clip boundaries should be masked to a va lue of NODATA\n maskflag = MsgBox.YesNo(\"Mask values outside of theme as NO DATA?\", theheader, TRUE)\n \nEnd\n \n' Get the x,y coordinate for the lower left corner of clipping shape\nrectorigin = theRect.returnorigin\n \n' Get the x,y coordinate for the upper right corner of clipping shape\nrectsize = theRect.returnsize\nrectUR = Point.make(rectorigin.getx+rectsize.getx, rectorigin.gety+rectsize.gety)\n\n' Ask user to select one or more grids to be clipped\nGridthemelist = MsgBox.multilist(gridthemes,\"Select one or m ore grids to be clipped by the selected rectangle.\" ,theHeader)\nif (Gridthemelist = NIL) then\n msgbox.error(\"You must select at least one grid theme.\",theHeader)\n Exit\nend\n\n' Initialize the variable maxcellsz to 0 prior to the looping process\nmaxcellsz = 0\n\n' Initialize the string variable gmsg to be empty prior to the looping process\ngmsg = \"\"\n\n' Get the number of grids to be clipped \nnumgrids = gridthemelist.count\n\n' Generate a summary message for the grids to be clipped (describe number of rows, column s, and cell sizes for each grid)\nFor each gthm in Gridthemelist\n\n ' Current grid name\n gname = gthm.getname\n\n ' Current grid\n ggrid = gthm.getgrid\n\n ' Current number of rows\n grows = ggrid.getnumrowsandcols.get(0).asstring\n \n ' Current number of columns\n gcols = ggrid.getnumrowsandcols.get(1).asstring\n \n ' Get current cell size\n gcellsz = ggrid.getcellsize\n\n ' Obtain the maximum grid cell size\n if (gcellsz > maxcellsz) then\n maxcellsz = gcellsz\n END\n\n ' Get new summary message text\n gtem pmsg = (gname+\" > Dimensions: \"+grows+\" x \"+gcols+\"; Cell Size: \"+gcellsz.asstring+NL) \n\n ' Add the text to the summary message\n gmsg = gmsg + gtempmsg\n\nEND\n\n' Output the summary message \nmsgbox.Report(gmsg,theheader)\n\n' Ask user to declare if the grids should be resampled\nresampleflag = MsgBox.YesNo(\"Resample grids to a different cell size?\"+NL+\"Note: All grids will be resampled to the same cell size.\", theheader, TRUE)\n\n' If grids are to be resampled, get the new sample size and the resampling method \nif (resampleflag) Then\n \n ' Initialize resampledone flag to be equal to FALSE\n resampledone = FALSE\n\n ' Until user finalizes output grid cell size, loop through the following processes \n While (resampledone <> TRUE)\n \n ' Get the new cell size for the output grids\n newcellsz = MsgBox.Input(\"Input the new desired cell size (default is the maximum input grid cell size).\",theheader,maxcellsz.asstring).asnumber\n if (newcellsz = NIL) then\n msgbox.error(\"You must input a valid cell size.\",theH eader)\n Exit\n end\n\n ' If grids are resampled, obtain new bounding coordinates (add a row/column to each side)\n If (resampleflag) Then\n \n 'Get new coordinates (add a row/column to each side)\n neworgx = rectorigin.getx - newcellsz\n neworgy = rectorigin.gety - newcellsz\n newURx = rectUR.getx + newcellsz\n newURy = rectUR.gety + newcellsz\n \n ' Set the new bounding rectangle coordinates\n outrect = rect.makeXY(neworgx,neworgy,newURx,newURy)\n End\n \n ' Get output grid properties (number of rows, number of columns, and cell size) \n newnumrows = ((outrect.getwidth) / newcellsz).round\n newnumcols = ((outrect.getheight) / newcellsz).round\n newnumcells = newnumrows * newnumcols\n \n ' Convert afforementioned values to strings\n newstrrows = newnumrows.asstring\n newstrcols = newnumcols.asstring\n newstrcells = newnumcells.asstring\n \n ' Allow user to determine if the current grid cell size is appropriate\n resampledone = MsgBox.Ye sNo(\"Output resampled grid dimensions: \"+newstrrows+\" x \"+newstrcols+NL+\"Total number of cells: \"+newstrcells+NL+\"Is this OK?\",theheader,FALSE)\n \n End\nEnd\n\n ' Make a resampling method string list\n resampletypelist = {\"Nearest Neighbor\",\"Bilinear Interpolation\",\"Cubic Convolution\"}\n \n ' Get the resampling method for the output grids\n resampletype = MsgBox.listasstring(resampletypelist,\"Select a resampling method.\",theheader)\n if (resampletype = NIL) then\n msgbox.error(\"You must select a resampling method.\",theHeader)\n Exit\n end\n \n ' Get a resampling enumeration type\n if (resampletype = \"Nearest Neighbor\") Then\n resampleenum = #GRID_RESTYPE_NEAREST\n elseif (resampletype = \"Bilinear Interpolation\") Then\n resampleenum = #GRID_RESTYPE_BILINEAR \n elseif (resampletype = \"Cubic Convolution\") Then\n resampleenum = #GRID_RESTYPE_CUBIC\n End\n\n\n' Ask user to select an output file format\nOutputType = MsgBox.ChoiceAsString({\"ARC/INFO Grid\", \"ASCII Grid\"},\"Select the file format for the output grids.\",theheader)\nif (OutputType = NIL) then\n msgbox.error(\"You must select an output file format.\",theHeader)\n Exit\nend\n\n\n'============================================================================================================\n\n' Begin main looping process\nFor each i in Gridthemelist\n \n ' Get the curent grid\n theGrid = i.getgrid\n\n ' Get the current cell size\n cellsz=theGrid.getcellsize\n\n ' If resampleflag is TRUE then resample the grid \n If (resampleflag) Then \n\n ' Reset the analysis environment\n Grid.Reset\n\n ' Resample the grid\n resgrid = thegrid.resample(newcellsz, resampleenum)\n\n ' Set analysis environment cell size to equal the newly declared cell size\n Grid.SetAnalysisCellSize(#GRID_ENVTYPE_VALUE, newcellsz)\n\n Else\n\n ' If resampleflag is FALSE then obtain new bounding coordinates (add a row/column to each side)\n neworgx = rectorigin.getx - cellsz\n neworgy = rectorigin.gety - cellsz\n newURx = rectUR.getx + cellsz\n newURy = re ctUR.gety + cellsz\n\n ' Set the new bounding rectangle coordinates\n outrect = rect.makeXY(neworgx,neworgy,newURx,newURy)\n resgrid = thegrid\n \n ' Set the analysis cell size to be equal to the current grid's cell size\n Grid.SetAnalysisCellSize(#GRID_ENVTYPE_VALUE, cellsz)\n \n End\n\n ' Set a temporary Analysis Environment\n Grid.SetAnalysisExtent(#GRID_ENVTYPE_VALUE, outrect)\n\n ' Since shapes are already projected, set the projection as the NULL value\n theprojection=Prj.MakeNull\n\n ' If mas kflag is TRUE then make a mask grid from the selected shapefile\n If (maskflag) Then\n clipgrid = Grid.MakeFromFTab (clipftab, theprojection, Nil, Nil)\n \n ' Set the analysis mask to be equal to clipgrid\n Grid.SetAnalysisMask(clipgrid)\n End\n \n ' Extract grid with new bounding coordinates from the resgrid \n maskgrid = resgrid.ExtractbyRect(outrect, theProjection, FALSE)\n\n ' Get the source name of the current grid\n gridname = thegrid.getsrcname\n \n ' Determine which file format to save the ou tput grid as\n If (OutputType = \"ARC/INFO Grid\") Then\n\n ' Assign the same name to the grid but save it in the previously specified path\n maskgrid.SaveDataSet((newpath+gridname.getname.asstring).asfilename)\n \n ' Create a new grid theme \n gthm = GTheme.Make(maskgrid)\n \n ' Add new theme to the view\n theView.AddTheme(gthm)\n \n ' Turn new grid theme on\n gthm.setvisible(true)\n\n ElseIf (OutputType = \"ASCII Grid\") Then\n \n ' Save the grid as an ascii grid in the previously specif ied path\n maskgrid.SaveAsAscii((newpath+gridname.getname.asstring+\".asc\").asfilename) \n \n End\nEnd\n\n" ) (Script.5 Name: "GarpDatasets.Install" SourceCode: "' Install ONLY if there is an active project\nif (av.GetProject = nil) then\n return nil\nend\n\nmnuNamedExt = self.Get(0)\n\n' Install the menu in the View DocGUI after the Window menu\nviewGUI = av.GetProject.FindGUI( \"View\" )\nif (viewGUI <> nil) then\n windowMenu = viewGUI.GetMenuBar.FindByLabel(\"Window\") \n place = viewGUI.GetMenuBar.GetControls.Find( windowMenu ) \n if (place <> -1) then \n ' Add it after the Window menu\n viewGUI.GetMenuBar.Add( mnuNamedExt, place) \n else\n ' Make it the last item \n viewGUI.GetMenuBar.Add( mnuNamedExt, viewGUI.GetMenuBar.GetControls.count ) \n end\nend\n" ) (Script.6 Name: "GarpDatasets.Make" SourceCode: "' Create the extension object\n' NOTE - add dependencies to last empty list - if applicable\noInstallScr = av.FindScript(\"GarpDatasets.Install\")\noUninstScr = av.FindScript(\"GarpDatasets.Uninstall\")\n\ntestavx = Extension.Make( \"GarpDatasets.avx\".AsFileName, \"GarpDatasets\", \n oInstallScr, oUninstScr, {})\n \n' Set other properties testavx.SetProjectSaveScript( av.FindScript( \"ProjectSave\" ))\ntestavx.SetAbout( \"GarpDatasets Extension allows you prepare custom environmental layers for use with DesktopGarp.\")\ntes tavx.SetExtVersion( 1 )\n\n' Add controls\ntestavx.Add(av.FindGUI(\"View\").GetMenuBar.GetControls.Get(8))\n'testavx.Add(av.FindGUI(\"View\").GetMenuBar.GetControls.Get(8).GetControls.Get(0))\n\n' Add scripts\n' Note: this assumes that each script you want to include is in a SEd \n' and its name begins with \"Test.\"\nfor each d in av.GetProject.GetDocs\n if (d.Is(SEd) AND (d.GetName.Left( \"GarpDatasets\".count + 1) = \"GarpDatasets.\")) then\n testavx.Add(d.GetScript)\n end\nend\n\ntestavx.commit" ) (Script.7 Name: "GarpDatasets.Uninstall" SourceCode: "' Don't uninstall if there is no project\nif (av.GetProject = nil) then\n return nil\nend\n\n' Clean up even if the project is closing\n' don't uninstall extension objects if project is closing\nif (av.GetProject.IsClosing) then\n return nil\nend\n\n' Remove objects owned by the extension from the project\n\n' Remove the menu\nViewGUI = av.FindGUI( \"View\" )\nif (viewGUI <> nil) then\n viewGUI.GetMenuBar.Remove( self.Get(0))\nend" )