Ejemplo n.º 1
0
        /// <summary>
        /// generate flatarea poylgons for the specified survey using the specified raster
        /// </summary>
        public static void GenerateFlatAreaPolygons(int SurveyID, double MinSlope, double MaxSlope,
            IGeoDataset ClippedRasterDS, ref string ErrorMessage)
        {
            ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null;
            ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null;
            ESRI.ArcGIS.GeoAnalyst.IRasterDescriptor ThisRasterDescriptor;
            ESRI.ArcGIS.GeoAnalyst.IConversionOp ThisConversionOp;
            IGeoDataset FloatRasterDS = null, IntRasterDS = null;
            ESRI.ArcGIS.esriSystem.IVariantArray GPParams;
            ESRI.ArcGIS.GeoAnalyst.ISurfaceOp ThisSurfaceOp;
            ESRI.ArcGIS.SpatialAnalyst.IMathOp ThisMathOp;
            IFeatureClass TempFlatAreasPolyFC = null, TempAggregateFC = null,
                TempAggregateClipFC = null, BoundaryFC = null, FlatAreasFC = null;
            IQueryFilter ThisQueryFilter;
            IFeatureCursor FlatAreasCursor, TempAggregateClipCursor;
            IFeatureBuffer FlatAreasFBuffer;
            IFeature TempAggregateClipFeature;
            string TempFlatAreasPolyFCName, TempAggregateFCName, InternalErrors = "", TempClipFCName;
            object zFactor;
            NPSGlobal NPS;
            int SurveyIDIndex;

            TempClipFCName = "NPS_TEMP_ClippedFC";
            TempAggregateFCName = "NPS_TEMP_FlatAreasAggrFC";
            TempFlatAreasPolyFCName = "NPS_TEMP_FlatAreasFC";
            NPS = NPSGlobal.Instance;
            zFactor = 1;

            BoundaryFC = Util.GetFeatureClass(NPS.LYR_SURVEY_BOUNDARY, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false) return;

            FlatAreasFC = Util.GetFeatureClass(NPS.LYR_FLAT_AREAS, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false) return;

            SurveyIDIndex = FlatAreasFC.FindField("SurveyID");
            if (SurveyIDIndex == -1)
            {
                ErrorMessage = "Could not find a SurveyID field on the " + ((IDataset)FlatAreasFC).Name + " FeatureClass.";
                return;
            }

            Util.SetProgressMessage("Deleting old temp files");
            Util.DeleteDataset(TempFlatAreasPolyFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempAggregateFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempAggregateFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);

            ThisSurfaceOp = new ESRI.ArcGIS.GeoAnalyst.RasterSurfaceOpClass();

            try
            {
                Util.SetProgressMessage("Computing slope values from DEM");

                FloatRasterDS = ThisSurfaceOp.Slope(ClippedRasterDS,
                    ESRI.ArcGIS.GeoAnalyst.esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, ref zFactor);
            }
            catch (Exception ex)
            {
                ErrorMessage = "Error occured while converting DEM evelation values to float slope values. " + ex.Message;
                return;
            }

            try
            {
                Util.SetProgressMessage("Converting DEM values to integers");

                ThisMathOp = new ESRI.ArcGIS.SpatialAnalyst.RasterMathOpsClass();
                IntRasterDS = ThisMathOp.Int(FloatRasterDS);
            }
            catch (Exception ex)
            {
                ErrorMessage = "Error occured while converting DEM float slope values to slope integer values. " + ex.Message;
                return;
            }

            try
            {
                Util.SetProgressMessage("Building flat area polygons from DEM");

                ThisQueryFilter = new QueryFilterClass();
                ThisQueryFilter.WhereClause = " Value >= " + MinSlope + " AND Value <= " + MaxSlope;

                ThisRasterDescriptor = new ESRI.ArcGIS.GeoAnalyst.RasterDescriptorClass();
                ThisRasterDescriptor.Create(IntRasterDS as IRaster, ThisQueryFilter, "Value");

                ThisConversionOp = new ESRI.ArcGIS.GeoAnalyst.RasterConversionOpClass();
                TempFlatAreasPolyFC = ThisConversionOp.RasterDataToPolygonFeatureData(ThisRasterDescriptor as IGeoDataset,
                    NPS.Workspace, TempFlatAreasPolyFCName, true) as IFeatureClass;
            }
            catch (Exception ex)
            {
                ErrorMessage = "Error occured while generating a FeatureClass of flat "
                + "areas from the default DEM file. " + ex.Message;
                return;
            }

            try
            {
                Util.SetProgressMessage("Aggregating flat area polygons");

                ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor();
                GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass();

                GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempFlatAreasPolyFCName));
                GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempAggregateFCName));
                GPParams.Add("1 Meters");

                GeoResult = ThisGeoProcessor.Execute("AggregatePolygons_management", GPParams, null);
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("Geoprocessor error:\r\nTask:AggregatePolygons_management\r\n"
                    + "Params:{0},{1},{2}\r\nException:{3}", System.IO.Path.Combine(NPS.DatabasePath, TempFlatAreasPolyFCName),
                    System.IO.Path.Combine(NPS.DatabasePath, TempAggregateFCName), "1 Meters", ex.Message);
                ThisGeoProcessor = null;
                return;
            }

            ThisGeoProcessor = null;

            Util.DeleteLayerFromMap(TempAggregateFCName);

            TempAggregateFC = Util.GetFeatureClass(TempAggregateFCName, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false)
            {
                ErrorMessage = "Could not find temporary FeatureClass " + TempAggregateFCName + ". " + ErrorMessage;
                return;
            }

            Util.SetProgressMessage("Validating flat area polygons");

            TempAggregateClipFC = Util.GP_Clip_analysis(TempClipFCName, TempAggregateFC, BoundaryFC, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false) return;

            FlatAreasFBuffer = FlatAreasFC.CreateFeatureBuffer();
            FlatAreasCursor = FlatAreasFC.Insert(true);

            ThisQueryFilter = null;
            ThisQueryFilter = new QueryFilterClass();
            ThisQueryFilter.WhereClause = "SHAPE_AREA > 25000";
            TempAggregateClipCursor = TempAggregateClipFC.Search(ThisQueryFilter, false);

            Util.SetProgressMessage("Importing flat area polygons to Survey");

            while ((TempAggregateClipFeature = TempAggregateClipCursor.NextFeature()) != null)
            {
                if (TempAggregateClipFeature.Shape == null) continue;

                FlatAreasFBuffer.Shape = TempAggregateClipFeature.ShapeCopy;
                FlatAreasFBuffer.set_Value(SurveyIDIndex, SurveyID);

                FlatAreasCursor.InsertFeature(FlatAreasFBuffer);
            }

            TempAggregateClipCursor = null;
            FlatAreasCursor = null;

            Util.SetProgressMessage("Cleaning up temp files");
            Util.DeleteDataset(TempFlatAreasPolyFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempAggregateFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempAggregateFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);

            Util.CreateFillerPolygon(FlatAreasFC, SurveyID, ref InternalErrors);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// generate exlcuded area polygons for the specified survey and elevation
        /// </summary>
        public static void GenerateExcludedAreasPolygons(int SurveyID, int MaximumElevInFeet, bool IsAboveElevation,
            IGeoDataset ClippedRasterDS, ref string ErrorMessage)
        {
            ESRI.ArcGIS.SpatialAnalyst.IMathOp ThisMathOp;
            IGeoDataset IntRasterDS = null;
            IQueryFilter ThisQueryFilter;
            ESRI.ArcGIS.GeoAnalyst.IRasterDescriptor ThisRasterDescriptor;
            ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult GeoResult = null;
            ESRI.ArcGIS.Geoprocessing.IGeoProcessor ThisGeoProcessor = null;
            ESRI.ArcGIS.esriSystem.IVariantArray GPParams;
            IFeatureClass NewElvPolyFC, BoundaryFC, ElvPolyFC;
            ESRI.ArcGIS.GeoAnalyst.IConversionOp ThisConversionOp;
            string TempElvFCName, TempAggFCName, InternalErrors = "", TempClipFCName;
            IFeatureBuffer InsertBuffer;
            IFeatureCursor InsertCursor, TempCursor;
            IFeature TempFeature;
            int ShapeAreaIndex;
            double CurrentShapeArea, MaxShapeArea;
            int SurveyIDIndex;
            NPSGlobal NPS;

            NPS = NPSGlobal.Instance;
            TempElvFCName = "NPS_TEMP_TempElvPolyFCName";
            TempAggFCName = "NPS_TEMP_TempElevationFC";
            TempClipFCName = "NPS_TEMP_TempClipFCName";

            ElvPolyFC = Util.GetFeatureClass(NPS.LYR_EXCLUDED_AREAS, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false) return;

            BoundaryFC = Util.GetFeatureClass(NPS.LYR_SURVEY_BOUNDARY, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false) return;

            SurveyIDIndex = ElvPolyFC.FindField("SurveyID");
            if (SurveyIDIndex == -1)
            {
                ErrorMessage = "Could not find the SurveyID field on the Excluded Areas FeatureClass.";
                return;
            }

            Util.SetProgressMessage("Deleting old temp files");
            Util.DeleteDataset(TempElvFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempAggFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);
            Util.DeleteDataset(TempAggFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);

            try
            {
                Util.SetProgressMessage("Converting DEM values to integers");

                ThisMathOp = new ESRI.ArcGIS.SpatialAnalyst.RasterMathOpsClass();
                IntRasterDS = ThisMathOp.Int(ClippedRasterDS);
            }
            catch (Exception ex)
            {
                ErrorMessage = "Error occured while converting DEM float slope values to slope integer values. " + ex.Message;
                return;
            }

            try
            {
                Util.SetProgressMessage("Building excluded area polygons from DEM");

                //determine which values to exclude
                ThisQueryFilter = new QueryFilterClass();

                if (IsAboveElevation)
                    ThisQueryFilter.WhereClause = " Value >= " + MaximumElevInFeet;
                else
                    ThisQueryFilter.WhereClause = " Value <= " + MaximumElevInFeet;

                ThisRasterDescriptor = new ESRI.ArcGIS.GeoAnalyst.RasterDescriptorClass();
                ThisRasterDescriptor.Create(IntRasterDS as IRaster, ThisQueryFilter, "Value");

                ThisConversionOp = new ESRI.ArcGIS.GeoAnalyst.RasterConversionOpClass();
                NewElvPolyFC = ThisConversionOp.RasterDataToPolygonFeatureData(ThisRasterDescriptor as IGeoDataset,
                    NPS.Workspace, TempElvFCName, true) as IFeatureClass;
            }
            catch (Exception ex)
            {
                ErrorMessage = "Error occured while generating a FeatureClass of flat "
                + "areas from the default DEM file. " + ex.Message;
                return;
            }

            try
            {
                Util.SetProgressMessage("Aggregating excluded area polygons");

                ThisGeoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessor();
                GPParams = new ESRI.ArcGIS.esriSystem.VarArrayClass();

                GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempElvFCName));
                GPParams.Add(System.IO.Path.Combine(NPS.DatabasePath, TempAggFCName));
                GPParams.Add("1 Meters");

                GeoResult = ThisGeoProcessor.Execute("AggregatePolygons_management", GPParams, null);
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("Geoprocessor error:\r\nTask:AggregatePolygons_management\r\n"
                    + "Params:{0},{1},{2}\r\nException:{3}", System.IO.Path.Combine(NPS.DatabasePath, TempElvFCName),
                    System.IO.Path.Combine(NPS.DatabasePath, TempAggFCName), "1 Meters", ex.Message);
                ThisGeoProcessor = null;
                return;
            }

            ThisGeoProcessor = null;

            Util.DeleteLayerFromMap(TempAggFCName);

            NewElvPolyFC = Util.GetFeatureClass(TempAggFCName, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false)
            {
                ErrorMessage = "Could not find temporary FeatureClass " + TempAggFCName + ". " + ErrorMessage;
                return;
            }

            Util.SetProgressMessage("Validating excluded area polygons");

            NewElvPolyFC = Util.GP_Clip_analysis(TempClipFCName, NewElvPolyFC, BoundaryFC, ref ErrorMessage);
            if (string.IsNullOrEmpty(ErrorMessage) == false) return;

            InsertBuffer = ElvPolyFC.CreateFeatureBuffer();
            InsertCursor = ElvPolyFC.Insert(true);

            ThisQueryFilter = null;
            ThisQueryFilter = new QueryFilterClass();
            TempCursor = NewElvPolyFC.Search(ThisQueryFilter, false);

            ShapeAreaIndex = NewElvPolyFC.FindField(NewElvPolyFC.AreaField.Name);

            MaxShapeArea = (double)Util.SafeConvert(Util.GetFirstRecordValue(BoundaryFC,
                BoundaryFC.AreaField.Name, "SurveyID=" + SurveyID), typeof(double));

            Util.SetProgressMessage("Importing excluded area polygons to Survey");

            while ((TempFeature = TempCursor.NextFeature()) != null)
            {
                if (TempFeature.Shape == null) continue;

                CurrentShapeArea = (double)Util.SafeConvert(TempFeature.get_Value(ShapeAreaIndex), typeof(double));
                if ((CurrentShapeArea / MaxShapeArea) >= 0.95) continue;

                InsertBuffer.Shape = TempFeature.ShapeCopy;
                InsertBuffer.set_Value(SurveyIDIndex, SurveyID);

                InsertCursor.InsertFeature(InsertBuffer);
            }

            TempCursor = null;
            InsertCursor = null;

            Util.SetProgressMessage("Cleaning up temp files");
            Util.DeleteDataset(TempElvFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempAggFCName, esriDatasetType.esriDTFeatureClass, ref InternalErrors);
            Util.DeleteDataset(TempClipFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);
            Util.DeleteDataset(TempAggFCName + "_Tbl", esriDatasetType.esriDTTable, ref InternalErrors);

            Util.CreateFillerPolygon(ElvPolyFC, SurveyID, ref InternalErrors);
        }