/// <summary> /// Runs the selected density operations on the input feature class. /// </summary> /// <param name="pFC"></param> /// <returns></returns> private IRaster Density(IFeatureClass pFC) { IRaster pRaster = null; try { IDensityOp pDensityOp = new RasterDensityOpClass(); //create a feature class descriptor to specify a field IFeatureClassDescriptor pFCD = new FeatureClassDescriptorClass(); if (m_sField != "None") { pFCD.Create(pFC, null, m_sField); } else { pFCD.Create(pFC, null, null); } //run the density op //set the output resolution //make sure this scale factor is always in right units. object oScaleFactor = 1; switch (m_sDensityUnits + m_sMapUnits) { case "per square km" + "Meter": oScaleFactor = 1000; break; case "per square mile" + "Meter": oScaleFactor = 1609.344; break; case "per square mile" + "Foot_US": case "per square mile" + "Foot": oScaleFactor = 5280; break; case "per square km" + "Foot_US": case "per square km" + "Foot": oScaleFactor = 3280.84; break; case "per square nm" + "Meter": oScaleFactor = 1852; break; case "per square nm" + "Foot_US": case "per square nm" + "Foot": oScaleFactor = 6076.12; break; } object oRadius = m_dRadius; IGeoDataset pOutRaster = null; if (m_sDensityType == "Kernel") { pOutRaster = pDensityOp.KernelDensity((IGeoDataset)pFCD, ref oRadius, ref oScaleFactor); } else { esriGeometryType thisType = pFC.ShapeType; if (thisType == esriGeometryType.esriGeometryPolyline) { pOutRaster = pDensityOp.LineDensity((IGeoDataset)pFCD, ref oRadius, ref oScaleFactor); } else if (thisType == esriGeometryType.esriGeometryPoint) { IRasterNeighborhood pNbr = new RasterNeighborhoodClass(); if (m_sRadiusType == "Cells") { pNbr.SetCircle(m_dRadius, esriGeoAnalysisUnitsEnum.esriUnitsCells); } else { pNbr.SetCircle(m_dRadius, esriGeoAnalysisUnitsEnum.esriUnitsMap); } pOutRaster = pDensityOp.PointDensity((IGeoDataset)pFCD, pNbr, ref oScaleFactor); //ref oCellSize); } } pRaster = (IRaster)pOutRaster; //check output spatial reference IRasterProps pRProps = (IRasterProps)pRaster; pRProps.SpatialReference = m_pLayerSR; //save the raster to disk IWorkspace pOutWS = clsStatic.OpenWorkspace(m_sOutPath); ISaveAs pSaveAs = (ISaveAs)pRaster; if (pSaveAs.CanSaveAs("GRID") == true) { pSaveAs.SaveAs(pFC.AliasName + "_" + m_sOutName, pOutWS, "GRID"); } IMap pMap = ArcMap.Document.FocusMap; //calculate raster stats IRasterDataset pRDS = clsStatic.OpenRasterDataset(m_sOutPath, pFC.AliasName + "_" + m_sOutName); CalcStats(pRDS); //add the layer to the map IRasterLayer pRL = clsStatic.RasterDataset2RasterLayer(m_sOutPath, pFC.AliasName + "_" + m_sOutName, false, pMap); IRgbColor pRGB1 = new RgbColorClass(); IRgbColor pRGB2 = new RgbColorClass(); pRGB2.Blue = 0; pRGB2.Green = 0; pRGB2.Red = 255; pRGB1.Blue = 255; pRGB1.Green = 0; pRGB1.Red = 0; //pRL.CreateFromRaster(pRaster); CreateRasterStretchRenderer(pRL, pRGB1, pRGB2); pMap.AddLayer(pRL); return(pRaster); } catch (Exception ex) { clsStatic.ShowErrorMessage(ex.ToString()); } return(pRaster); }
public void RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo, string FileName) { try { //IRaster pRaster = pRasterLayer.Raster; //IRasterProps pProps = pRaster as IRasterProps; //object cellSizeProvider = pProps.MeanCellSize().X; //IGeoDataset pInputDataset = pRaster as IGeoDataset; //IExtractionOp2 pExtractionOp = new ESRI.ArcGIS.SpatialAnalyst.RasterExtractionOpClass(); //IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment; //pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider); //object extentProvider = clipGeo.Envelope; //object snapRasterData = Type.Missing; //pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData); IRasterProps pProps = pRasterLayer.Raster as IRasterProps; object cellSizeProvider = pProps.MeanCellSize().X; IGeoDataset pInputDataset = pRasterLayer.Raster as IGeoDataset; //IGeoDataset pInputDataset = pRasterDataset as IGeoDataset;//此种方式也只是得到前3个波段 //设置格格处理环境 IExtractionOp2 pExtractionOp = new RasterExtractionOpClass(); IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment; pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider); object extentProvider = clipGeo.Envelope; object snapRasterData = Type.Missing; pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData); IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true);//裁切操作 // IGeoDataset pOutputDataset = pExtractionOp.Rectangle(pInputDataset, clipGeo.Envelope, true); IRaster clipRaster; //裁切后得到的IRasterzzz if (pOutputDataset is IRasterLayer) { IRasterLayer rasterLayer = pOutputDataset as IRasterLayer; clipRaster = rasterLayer.Raster; } else if (pOutputDataset is IRasterDataset) { IRasterDataset rasterDataset = pOutputDataset as IRasterDataset; clipRaster = rasterDataset.CreateDefaultRaster(); } else if (pOutputDataset is IRaster) { clipRaster = pOutputDataset as IRaster; //增加阈值设定 IRasterProps temRastPro = clipRaster as IRasterProps; temRastPro.NoDataValue = pProps.NoDataValue; // temRastPro.PixelType = pProps.PixelType; } else { return; } //保存裁切后得到的clipRaster //如果直接保存为img影像文件 IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass(); IWorkspace pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(FileName), 0); ISaveAs pSaveAs = clipRaster as ISaveAs; //bool cansave = pSaveAs.CanSaveAs("TIFF"); bool cansave = pSaveAs.CanSaveAs("BMP"); bool zzz = cansave; IRasterDataset pRD = pSaveAs.SaveAs(System.IO.Path.GetFileName(FileName), pWorkspace, "TIFF") as IRasterDataset;//以img格式保存 //Note, SaveAs will return a RasterDataset, to prevent from ISaveAs holding the output, //.NET ReleaseCOMObject needs to be called to release the referene to the output raster dataset: //System.Runtime.InteropServices.Marshal.ReleaseComObject(outRasterDS); System.Runtime.InteropServices.Marshal.ReleaseComObject(pRD); MessageBox.Show("\"" + FileName + "\"成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exp) { MessageBox.Show("截取样本失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
/// <summary> /// 根据范围提取数据 /// </summary> /// <param name="pFileName">目标数据名称(不包含路径)</param> /// <param name="clipExtent">几何范围</param> /// <param name="pDesWS">目标数据工作空间</param> /// <param name="format">目标数据类型</param> /// <param name="pResRatser">源栅格数据</param> /// <param name="bIn">true:表明提取几何范围内部的数据;false:提取几何范围外部的数据</param> /// <param name="eError"></param> private void StractData(string pFileName, IGeometry clipExtent, IWorkspace pDesWS, string format, IRaster pResRatser, bool bIn, out Exception eError) { eError = null; IExtractionOp extraction = new RasterExtractionOpClass(); #region 进行裁切 IGeoDataset pOrgGeoDt = pResRatser as IGeoDataset; IGeoDataset pDesGeoDt = null; IPolygon pPoly = clipExtent as IPolygon; ICircularArc pArc = clipExtent as ICircularArc; IPointCollection pPointColl = clipExtent as IPointCollection; IEnvelope pRec = clipExtent as IEnvelope; if (pPoly != null) { //几何范围是多边形 pDesGeoDt = extraction.Polygon(pOrgGeoDt, pPoly, checkBoxClip.Checked); } else if (pArc != null) { //几何范围是圆形 pDesGeoDt = extraction.Circle(pOrgGeoDt, pArc, checkBoxClip.Checked); } else if (pRec != null) { //几何范围是矩形 pDesGeoDt = extraction.Rectangle(pOrgGeoDt, pRec, checkBoxClip.Checked); } else if (pPointColl != null) { //几何范围是点集 pDesGeoDt = extraction.Points(pOrgGeoDt, pPointColl, checkBoxClip.Checked); } #endregion #region 进行保存 IRaster pDesRaster = pDesGeoDt as IRaster; ISaveAs pSaveAs = pDesRaster as ISaveAs; if (format == "TIF") { if (!pSaveAs.CanSaveAs("TIFF")) { eError = new Exception("该栅格数据不能保存为tiff格式!"); return; } pSaveAs.SaveAs(pFileName, pDesWS, "TIFF"); } else if (format == "IMG") { if (!pSaveAs.CanSaveAs("IMAGINE Image")) { eError = new Exception("该栅格数据不能保存为IMG格式!"); return; } pSaveAs.SaveAs(pFileName, pDesWS, "IMAGINE Image"); } else { //geodatabase结构数据需要用另外的方式来保存数据:PDB、GDB //geodatabase根据已有的rasterDataset生成目标rasterDataset(clipExtent几何限制条件没用上) IRaster2 pOrgRaster2 = pResRatser as IRaster2; if (pOrgRaster2 == null) { return; } IRasterDataset mOrgRDataset = pOrgRaster2.RasterDataset; //源栅格数据集 if (mOrgRDataset.CanCopy()) { mOrgRDataset.Copy(m_DesRasterCollName, pDesWS); } } #endregion }