/// <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 }
public bool SetRegionToNoDataValue() { try { if (m_pSrcRaster == null || m_pClipPolygon == null || double.IsNaN(m_dbNoDataValue)) { return(false); } IGeoDataset pSrcGeoDataset = m_pSrcRaster as IGeoDataset; IExtractionOp pRasterExtractionOp = new RasterExtractionOpClass(); IRasterProps pSrcRasterProps = m_pSrcRaster as IRasterProps; double dbCellSize = (pSrcRasterProps.MeanCellSize().X + pSrcRasterProps.MeanCellSize().Y) / 2; //设置范围和分辨率 IRasterAnalysisEnvironment pRasterAnalysisEnv = pRasterExtractionOp as IRasterAnalysisEnvironment; pRasterAnalysisEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, dbCellSize); pRasterAnalysisEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, m_pClipPolygon.Envelope, Type.Missing); //pRasterAnalysisEnv.OutSpatialReference = (m_pSrcRaster as IRasterProps).SpatialReference; //保留区域外的值,区域内的设置为原始栅格的无效值 IGeoDataset pDstGeoDataset = pRasterExtractionOp.Rectangle(pSrcGeoDataset, m_pClipPolygon.Envelope, true); //逐点判断像素是否在区域内,在区域内则改变为设置值,否则不变 IRelationalOperator pRelationalOp = m_pClipPolygon as IRelationalOperator; if (pDstGeoDataset is IRaster) { //得到原始栅格的对象,用于修改 IRaster2 pSrcRaster2 = m_pSrcRaster as IRaster2; IRasterDataset2 pSrcRasterDataset2 = pSrcRaster2.RasterDataset as IRasterDataset2; IRaster pTmpRaster = pSrcRasterDataset2.CreateFullRaster(); IRasterEdit pSrcEdit = pTmpRaster as IRasterEdit; //得到图层NoDataValue IRasterProps rasterProps = pSrcRaster2 as IRasterProps; double noData = ClsGDBDataCommon.getNoDataValue(rasterProps.NoDataValue); //得到输出的栅格 IRaster2 pDstRaster2 = pDstGeoDataset as IRaster2; IRasterProps pDstRasterProps = pDstRaster2 as IRasterProps; IRasterCursor pDstRasterCursor = pDstRaster2.CreateCursorEx(null); //pDstRasterCursor.Reset(); do { //得到当前处理的块 IPixelBlock3 pixelBlock3 = pDstRasterCursor.PixelBlock as IPixelBlock3; int nWidth = pixelBlock3.Width; int nHeight = pixelBlock3.Height; IPnt ptLeftTop = pDstRasterCursor.TopLeft; //block值转数组时,NoData转换时有时为NoData,有时为栅格中的最小值 System.Array array = pixelBlock3.get_PixelData(0) as System.Array; //逐点判断: 判断像素是否在区域内,在区域内则改变为设置值,否则不变 for (int i = 0; i < nWidth; i++) { for (int j = 0; j < nHeight; j++) { double dbX = double.NaN, dbY = double.NaN; //得到当前像素点的地图坐标 int nCurrentX = Convert.ToInt32(ptLeftTop.X + i); int nCurrentY = Convert.ToInt32(ptLeftTop.Y + j); pDstRaster2.PixelToMap(nCurrentX, nCurrentY, out dbX, out dbY); IPoint ptInMap = new PointClass(); ptInMap.X = dbX; ptInMap.Y = dbY; //判断是否在区域内 bool bFlag = pRelationalOp.Contains(ptInMap as IGeometry); if (bFlag) //在当前区域内 { object oValidValue = getValidType(pDstRasterProps, m_dbNoDataValue); array.SetValue(oValidValue, i, j); } else { double v = Convert.ToDouble(array.GetValue(i, j)); if (v == 0 || v < -3.4e15 || v > 3.4e15) //if (v == 0 || Math.Abs(v -noData) <1e18) { int col, row; pSrcRaster2.MapToPixel(dbX, dbY, out col, out row); //表示getpixelvalue为null表示nodata object obj = pSrcRaster2.GetPixelValue(0, col, row); if (obj == null) { object oValidValue = getValidType(pDstRasterProps, m_dbNoDataValue); array.SetValue(oValidValue, i, j); } else { array.SetValue(obj, i, j); } } } } } pixelBlock3.set_PixelData(0, array); //得到当前区域块在原图中的左上角像素坐标, 直接修改原栅格的数据 int nPixelLeftX = -1, nPixelLeftY = -1; double dbMapLeftTopX = double.NaN, dbMapLeftTopY = double.NaN; pDstRaster2.PixelToMap(Convert.ToInt32(ptLeftTop.X), Convert.ToInt32(ptLeftTop.Y), out dbMapLeftTopX, out dbMapLeftTopY); //得到当前块左上角的地理坐标 pSrcRaster2.MapToPixel(dbMapLeftTopX, dbMapLeftTopY, out nPixelLeftX, out nPixelLeftY); IPnt ptPixelLeftTop = new PntClass(); ptPixelLeftTop.SetCoords(nPixelLeftX, nPixelLeftY); if (pSrcEdit.CanEdit()) { pSrcEdit.Write(ptPixelLeftTop, pixelBlock3 as IPixelBlock); //pSrcEdit.Refresh(); } else { return(false); } } while (pDstRasterCursor.Next() == true); //更新 pSrcEdit.Refresh(); } else { return(false); } return(true); } catch (System.Exception ex) { return(false); } }