Ejemplo n.º 1
0
        private void _CalculateFlowDir()
        {
            OnProgress("Filling DEM...");

            // - Fill & calculate flow direction
            IHydrologyOp   hydroOp         = new RasterHydrologyOpClass();
            IRasterMakerOp rasterMaker     = new RasterMakerOpClass();
            ILogicalOp     logicalOp       = new RasterMathOpsClass();
            IConditionalOp conditionalOp   = new RasterConditionalOpClass();
            IGeoDataset    fillTemp        = null;
            IGeoDataset    flowTemp        = null;
            IGeoDataset    flowTemp2       = null;
            IGeoDataset    demNulls        = null;
            IGeoDataset    zeroRaster      = null;
            IWorkspace     resultWorkspace = null;

            try
            {
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)hydroOp);
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)rasterMaker);
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)conditionalOp);
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)logicalOp);

                object zLimit = null;
                fillTemp = hydroOp.Fill((IGeoDataset)_punchedDEM, ref zLimit);

                //Make output permanent
                resultWorkspace = GetResultRasterWorkspace();
                ITemporaryDataset tempFillDataset = ((IRasterAnalysisProps)fillTemp).RasterDataset as ITemporaryDataset;
                string            fillPath        = CreateTempFileName(_GetResultDir(), "filldem", "");
                string            fillFileName    = System.IO.Path.GetFileName(fillPath);
                tempFillDataset.MakePermanentAs(fillFileName, resultWorkspace, "GRID");
                _filledDEM = ((IRasterWorkspace)resultWorkspace).OpenRasterDataset(fillFileName).CreateDefaultRaster() as IRaster;

                OnProgress("Calculating flow direction...");
                flowTemp = hydroOp.FlowDirection((IGeoDataset)fillTemp, false, true);

                //Set holes to flowdir of 0
                object boxedFlowTemp = flowTemp;
                zeroRaster = rasterMaker.MakeConstant(0.0, true);
                flowTemp2  = conditionalOp.Con(logicalOp.IsNull((IGeoDataset)fillTemp), zeroRaster, ref boxedFlowTemp);
                demNulls   = logicalOp.IsNull((IGeoDataset)_dem);
                string flowPath = CreateTempFileName(_GetResultDir(), "flowdir", "");
                _flowDir = GeoprocessingTools.SetNull((IRaster)demNulls, (IRaster)flowTemp2, flowPath);
            }
            finally
            {
                UrbanDelineationExtension.ReleaseComObject(flowTemp);
                UrbanDelineationExtension.ReleaseComObject(flowTemp2);
                UrbanDelineationExtension.ReleaseComObject(demNulls);
                UrbanDelineationExtension.ReleaseComObject(zeroRaster);
                UrbanDelineationExtension.ReleaseComObject(conditionalOp);
                UrbanDelineationExtension.ReleaseComObject(logicalOp);
                UrbanDelineationExtension.ReleaseComObject(rasterMaker);
                UrbanDelineationExtension.ReleaseComObject(hydroOp);
                UrbanDelineationExtension.ReleaseComObject(resultWorkspace);
            }

            OnProgress("Flow direction calculated.");
        }
Ejemplo n.º 2
0
        private void _CalculateFlowDir()
        {
            OnProgress("Filling DEM...");

            // - Fill & calculate flow direction
            IHydrologyOp   hydroOp     = new RasterHydrologyOpClass();
            IRasterMakerOp rasterMaker = new RasterMakerOpClass();
            ILogicalOp     logicalOp   = new RasterMathOpsClass();
            IGeoDataset    flowTemp    = null;
            IRaster        flowTemp2   = null;
            IGeoDataset    demNulls    = null;
            IGeoDataset    zeroRaster  = null;

            try
            {
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)hydroOp);
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)rasterMaker);
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)logicalOp);

                string fillPath = CreateTempFileName(_GetResultDir(), "filldem", "");
                _filledDEM = GeoprocessingTools.Fill(_punchedDEM, fillPath);

                OnProgress("Calculating flow direction...");
                flowTemp = hydroOp.FlowDirection((IGeoDataset)_filledDEM, false, true);

                //Set holes to flowdir of 0
                zeroRaster = rasterMaker.MakeConstant(0.0, true);
                string flowTemp2Path = CreateTempFileName(_GetTempDir(), "flowtemp2", "");
                flowTemp2 = GeoprocessingTools.Con((IRaster)logicalOp.IsNull((IGeoDataset)_filledDEM), (IRaster)zeroRaster, (IRaster)flowTemp, flowTemp2Path);
                demNulls  = logicalOp.IsNull((IGeoDataset)_dem);
                string flowPath = CreateTempFileName(_GetResultDir(), "flowdir", "");
                _flowDir = GeoprocessingTools.SetNull((IRaster)demNulls, flowTemp2, flowPath);
            }
            finally
            {
                UrbanDelineationExtension.ReleaseComObject(flowTemp);
                UrbanDelineationExtension.ReleaseComObject(flowTemp2);
                UrbanDelineationExtension.ReleaseComObject(demNulls);
                UrbanDelineationExtension.ReleaseComObject(zeroRaster);
                UrbanDelineationExtension.ReleaseComObject(logicalOp);
                UrbanDelineationExtension.ReleaseComObject(rasterMaker);
                UrbanDelineationExtension.ReleaseComObject(hydroOp);
            }

            OnProgress("Flow direction calculated.");
        }
Ejemplo n.º 3
0
        private void _DelineateInletCatchments()
        {
            OnProgress("Delineating inlet catchments...");
            //Determine output path
            string outputDir          = _GetResultDir();
            string outputPathSmooth   = SetupOp.CreateTempFileName(outputDir, "Catchments_smooth", ".shp");
            string outputPathDetailed = SetupOp.CreateTempFileName(outputDir, "Catchments_detailed", ".shp");
            string outputNameSmooth   = System.IO.Path.GetFileNameWithoutExtension(outputPathSmooth);
            string outputNameDetailed = System.IO.Path.GetFileNameWithoutExtension(outputPathDetailed);

            //Calculate catchments
            IHydrologyOp hydroOp       = new RasterHydrologyOpClass();
            IGeoDataset  seedGrid      = null;
            IGeoDataset  catchmentGrid = null;

            try
            {
                SetAnalysisEnvironment((IRasterAnalysisEnvironment)hydroOp);
                seedGrid      = (IGeoDataset)_DrainagePointsToSeedGrid(_drainClass, "SeedPts", SetupOp.EID_FIELD_NAME);
                catchmentGrid = hydroOp.Watershed((IGeoDataset)_flowDir, seedGrid);
                OnProgress("Converting catchments to polygon...");
                IFeatureClass smoothCatchmentClass   = RasterToPolygon(catchmentGrid, outputNameSmooth, outputDir, true);
                IFeatureClass detailedCatchmentClass = RasterToPolygon(catchmentGrid, outputNameDetailed, outputDir, false);

                if (_smooth)
                {
                    _catchmentClass = smoothCatchmentClass;
                }
                else
                {
                    _catchmentClass = detailedCatchmentClass;
                }

                _MarkForDisposal((IDataset)seedGrid);
                _MarkForDisposal(catchmentGrid as IDataset);
            }
            finally
            {
                UrbanDelineationExtension.ReleaseComObject(hydroOp);
            }
            OnProgress("Inlet catchments delineated.");
        }
Ejemplo n.º 4
0
        private IRasterLayer calWSCons(string  _outPath)
        {
            //��դ�� pMapAlgebraOp
            try
            {
                if (pMapAlgebraOp==null)
                {
                    pMapAlgebraOp = new RasterMapAlgebraOpClass();
                }
                string strExp = "";
                string sRainPath = "";

                //��ֱ���ý�����ʴ�����ݻ��ǽ������ݼ���
                IRaster pRasterR = null;
                if (rbtnR.Checked)
                {
                    pRasterR = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbR.Text)) as IRasterLayer).Raster;
                    pMapAlgebraOp.BindRaster(pRasterR as IGeoDataset, "R");
                }
                else
                {
                    sRainPath = this.txtPcpPath.Text;
                    //
                    IWorkspaceFactory pWsF = new RasterWorkspaceFactoryClass();
                    IRasterWorkspace pRWs;
                    IRasterDataset pRDs = new RasterDatasetClass();
                    IRasterLayer pRLyr = new RasterLayerClass();

                    pRWs = pWsF.OpenFromFile(sRainPath, 0) as IRasterWorkspace;
                    string sFileName="";
                    for (int i = 1; i < 13; i++)
                    {
                        sFileName = this.txtPcpPrefix.Text + i.ToString() + this.txtPcpSuffix.Text;
                        pRDs = pRWs.OpenRasterDataset(sFileName);
                        pMapAlgebraOp.BindRaster(pRDs as IGeoDataset, sFileName);
                        strExp = strExp + "[" + sFileName + "] + ";
                    }
                    //cal total pcp
                    strExp = strExp.TrimEnd("+ ".ToCharArray());
                    IGeoDataset pGeoDsPcp = pMapAlgebraOp.Execute(strExp);
                    pMapAlgebraOp.BindRaster(pGeoDsPcp, "Pcp");

                    //cal Ri
                    strExp = "";
                    string sFileNameE = "",sDsName="",strExpR="";
                    for (int i = 1; i < 13; i++)
                    {
                        sFileName = this.txtPcpPrefix.Text + i.ToString() + this.txtPcpSuffix.Text;
                        sFileNameE="["+sFileName+"]";
                        strExp = "1.735 * pow(10,1.5 * log10((" + sFileNameE + " * " + sFileNameE + ") / [Pcp]) - 0.08188)";
                        IGeoDataset pGeoDsRi = pMapAlgebraOp.Execute(strExp);
                        sDsName = "Pcp" + i.ToString();
                        pMapAlgebraOp.BindRaster(pGeoDsRi, sDsName);
                        pMapAlgebraOp.UnbindRaster(sFileName);
                        strExpR = strExpR + "[" + sDsName + "] + ";
                    }
                    //cal R
                    strExpR = strExpR.TrimEnd("+ ".ToCharArray());
                    IGeoDataset pGeoDsR = pMapAlgebraOp.Execute(strExpR);
                    pMapAlgebraOp.BindRaster(pGeoDsR, "R");
                    for (int i = 1; i < 13; i++)
                    {
                        sDsName = "Pcp" + i.ToString();
                        pMapAlgebraOp.UnbindRaster(sDsName);
                    }
                }

                //����K����
                IRaster pRasterSclay = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilClay.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSclay as IGeoDataset, "clay");

                IRaster pRasterSsand = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilSand.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSsand as IGeoDataset, "sand");

                IRaster pRasterSslit = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilSlit.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSslit as IGeoDataset, "slit");

                IRaster pRasterSOrg = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilOrganic.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterSOrg as IGeoDataset, "org");

                //cal K
                //K =(0.2 + 0.3 * exp(-0.0256 * soil_sand * (1.0 - soil_silt / 100.0))) * pow((soil_silt * 1.0 / (soil_clay * 1.0 + soil_silt * 1.0)),0.3) * (1.0 - 0.25 * soil_oc / (soil_oc * 1.0 + exp(3.72 - 2.95 * soil_oc))) * (1.0 - (0.7 * ksd) / (ksd + exp(-5.51 + 22.9 * ksd)))
                strExp = "(0.2 + 0.3 * Exp(-0.0256 * [sand] * (1.0 - [slit] / 100.0))) * Pow(([slit] * 1.0 / ([clay] * 1.0 + [slit] * 1.0)), 0.3) * (1.0 - 0.25 * [org] * 0.58 / ([org] * 0.58 + Exp(3.72 - 2.95 * [org] * 0.58))) * (1.0 - (0.7 * (1.0 - [sand] / 100.0)) / ((1.0 - [sand] / 100.0) + Exp(-5.51 + 22.9 * (1.0 - [sand] / 100.0))))";
                IGeoDataset pGeoDsK = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsK, "K");
                pMapAlgebraOp.UnbindRaster("clay");
                pMapAlgebraOp.UnbindRaster("sand");
                pMapAlgebraOp.UnbindRaster("slit");
                pMapAlgebraOp.UnbindRaster("org");

                //cal L*S
                IHydrologyOp pHydrologyOp = new RasterHydrologyOpClass();
                object objZLimit = System.Type.Missing;
                IRaster pRasterDem = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbDem.Text)) as IRasterLayer).Raster;
                //Fill Dem
                IGeoDataset pGeoDsDemFill =pHydrologyOp.Fill(pRasterDem as IGeoDataset,ref objZLimit);
                // cal FlowDirection
                IGeoDataset pGeoDsFlowDir = pHydrologyOp.Fill(pGeoDsDemFill, ref objZLimit);
                //cal FlowAccumulation
                IGeoDataset pGeoDsFlowAcc = pHydrologyOp.Fill(pGeoDsFlowDir, ref objZLimit);
                pMapAlgebraOp.BindRaster(pGeoDsFlowAcc, "FlowAcc");

                //cal Slope with Deg
                ISurfaceOp pSurfaceOp = new RasterSurfaceOpClass();
                object objZFactor = System.Type.Missing;
                IGeoDataset pGeoDsSlope = pSurfaceOp.Slope(pGeoDsDemFill, esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, ref objZFactor);
                // bind raster data "Slope"
                pMapAlgebraOp.BindRaster(pGeoDsSlope, "Slope");
                //cal S
                strExp = "Con([Slope] < 5,10.8 * Sin([Slope] * 3.14 / 180) + 0.03,[Slope] >= 10,21.9 * Sin([Slope] * 3.14 / 180) - 0.96,16.8 * Sin([Slope] * 3.14 / 180) - 0.5)";
                IGeoDataset pGeoDsS = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsS, "S");

                //cal m
                strExp = "Con([Slope] <= 1,0.2,([Slope] > 1 & [Slope] <= 3),0.3,[Slope] >= 5,0.5,0.4)";
                IGeoDataset pGeoDsM = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsM, "m");
                //cal ls
                strExp = "[S] * Pow(([FlowAcc] * "+this.txtCellSize.Text+" / 22.1),[m])";

                IGeoDataset pGeoDsLS = pMapAlgebraOp.Execute(strExp);
                pMapAlgebraOp.BindRaster(pGeoDsLS, "LS");

                pMapAlgebraOp.UnbindRaster("m");
                pMapAlgebraOp.UnbindRaster("S");
                pMapAlgebraOp.UnbindRaster("Slope");
                pMapAlgebraOp.UnbindRaster("FlowAcc");

                IRaster pRasterC = null;
                if (rbtnVegCover.Checked)
                {
                    pRasterC = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbC.Text)) as IRasterLayer).Raster;
                    pMapAlgebraOp.BindRaster(pRasterC as IGeoDataset, "C");
                }
                else
                {
                    //cal vegetation cover
                    IRaster pRasterNDVI = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbC.Text)) as IRasterLayer).Raster;
                    IRasterBandCollection pRasterBandCollection = pRasterNDVI as IRasterBandCollection;
                    IRasterBand pRasterBand = pRasterBandCollection.Item(0);
                    pRasterBand.ComputeStatsAndHist();
                    IRasterStatistics pRasterStatistics = pRasterBand.Statistics;
                    double dMax = pRasterStatistics.Maximum;
                    double dMin = pRasterStatistics.Minimum;
                    pMapAlgebraOp.BindRaster(pRasterNDVI as IGeoDataset, "NDVI");
                    if (dMin < 0)
                    {
                        dMin = 0;
                    }
                    //veg%yr% = (ndvi%yr% - ndvi%yr%min) / (ndvi%yr%max - ndvi%yr%min)
                    strExp = "([NDVI] - "+dMin+") / ("+dMax +" - "+dMin+")";
                    IGeoDataset pGeoDsC = pMapAlgebraOp.Execute(strExp);
                    pMapAlgebraOp.BindRaster(pGeoDsC, "C");
                    pMapAlgebraOp.UnbindRaster("NDVI");
                }
                //����P����
                IRaster pRasterP = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbP.Text)) as IRasterLayer).Raster;
                pMapAlgebraOp.BindRaster(pRasterP as IGeoDataset, "P");
                if (_outPath != "")
                {
                    strExp = _outPath + " = [R] * [K] * [LS] * (1 - [C]) * [P]";
                }
                else
                {
                    strExp = "[R] * [K] * [LS] * (1 - [C]) * [P]";
                }

                IGeoDataset pGeoDsSr = pMapAlgebraOp.Execute(strExp);
                IRaster pOutRaster = pGeoDsSr as IRaster;
                IRasterLayer pOutRasterLayer = new RasterLayerClass();
                pOutRasterLayer.CreateFromRaster(pOutRaster);
                //��������
                //string strOutDir = _outPath.Substring(0, _outPath.LastIndexOf("\\"));
                int startX = _outPath.LastIndexOf("\\");
                int endX = _outPath.Length;
                string sRLyrName = _outPath.Substring(startX + 1, endX - startX - 1);
                pOutRasterLayer.Name = sRLyrName;
                pMapAlgebraOp = null;
                return pOutRasterLayer;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return null;
            }
        }