private void IDWInterpolation(IFeatureClass pointFeature, IRaster refRaster, string name) { IFeatureClassDescriptor pointDescript = new FeatureClassDescriptorClass(); pointDescript.Create(pointFeature, null, name); object extend = (refRaster as IGeoDataset).Extent; IRasterProps refProps = refRaster as IRasterProps; object cell = refProps.MeanCellSize().X; IInterpolationOp interpla = new RasterInterpolationOpClass(); IRasterAnalysisEnvironment IDWEnv = interpla as IRasterAnalysisEnvironment; IDWEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cell); IDWEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extend); IGeoDataset output = interpla.IDW((IGeoDataset)pointDescript, 2, null, null); IGeoDataset input = refRaster as IGeoDataset; IExtractionOp op = new RasterExtractionOpClass(); output = op.Raster(output, input); var clipRaster = (IRaster)output; ISaveAs pSaveAs = clipRaster as ISaveAs; IWorkspaceFactory workspaceFac = new RasterWorkspaceFactoryClass(); var groups = Regex.Match(name, @"^(\d+)_(\d+)_(\d+)$").Groups; name = string.Format("{0:D2}{1:D2}", int.Parse(groups[2].Value), int.Parse(groups[3].Value)); IDataset outDataset = pSaveAs.SaveAs(fileName + name + ".img", workspaceFac.OpenFromFile(filePath, 0), "IMAGINE Image"); System.Runtime.InteropServices.Marshal.ReleaseComObject(outDataset); }
//裁剪 private IRaster ShpLayerClipRaster(IFeatureLayer featureLayer, IRaster raster) { IFeatureLayer pFeaLyr; IRasterAnalysisEnvironment pEnv; IGeoDataset pTempDS; pFeaLyr = featureLayer; pTempDS = pFeaLyr.FeatureClass as IGeoDataset; IConversionOp pConOp = new RasterConversionOpClass(); pEnv = pConOp as IRasterAnalysisEnvironment; IRasterProps pProp = raster as IRasterProps; object cellSize = 0.01; pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSize); IRasterConvertHelper rsh = new RasterConvertHelperClass(); IRaster tempRaster = rsh.ToRaster1(pTempDS, "Grid", pEnv); IRaster pOutRaster; IExtractionOp pExtrOp = new RasterExtractionOpClass(); pOutRaster = (IRaster)pExtrOp.Raster((IGeoDataset)raster, (IGeoDataset)tempRaster); return(pOutRaster); }
public static void exportRasterData(string parth, IRasterLayer rasterLayer, float[,] rasterMat) //输出栅格数据 { string directory = parth.Substring(0, parth.LastIndexOf("\\")); string name = parth.Substring(parth.LastIndexOf("\\") + 1); IWorkspaceFactory workspaceFac = new RasterWorkspaceFactoryClass(); IRasterWorkspace2 rasterWorkspace2 = workspaceFac.OpenFromFile(directory, 0) as IRasterWorkspace2; IRasterInfo rasterInfo = (rasterLayer.Raster as IRawBlocks).RasterInfo; IPoint originPoint = new Point(); originPoint.PutCoords(rasterInfo.Origin.X, rasterInfo.Origin.Y - (rasterLayer.Raster as IRasterProps).Height * (rasterLayer.Raster as IRasterProps).MeanCellSize().Y); IRasterProps rasterProps = rasterLayer.Raster as IRasterProps; IRasterDataset rasterDataSet = rasterWorkspace2.CreateRasterDataset(name, "IMAGINE Image", originPoint, rasterProps.Width, rasterProps.Height, rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, 1, rstPixelType.PT_FLOAT, rasterProps.SpatialReference, true) as IRasterDataset2; IRaster2 raster2 = rasterDataSet.CreateDefaultRaster() as IRaster2; IPnt pntClass = new Pnt(); pntClass.X = rasterProps.Width; pntClass.Y = rasterProps.Height; IRasterCursor rasterCursor = raster2.CreateCursorEx(pntClass); IRasterCursor inRasterCursor = (rasterLayer.Raster as IRaster2).CreateCursorEx(pntClass); IRasterEdit rasterEdit = raster2 as IRasterEdit; if (rasterEdit.CanEdit()) { IPixelBlock3 pixelBlock3 = rasterCursor.PixelBlock as IPixelBlock3; IPixelBlock3 inPixelBlock3 = inRasterCursor.PixelBlock as IPixelBlock3; System.Array pixels = (System.Array)rasterMat; pixelBlock3.set_PixelData(0, (System.Array)pixels); rasterEdit.Write(rasterCursor.TopLeft, (IPixelBlock)pixelBlock3); System.Runtime.InteropServices.Marshal.ReleaseComObject(pixelBlock3); } rasterEdit.Refresh(); IGeoDataset inDataset = rasterLayer.Raster as IGeoDataset; IGeoDataset outDataset = rasterDataSet as IGeoDataset; IExtractionOp op = new RasterExtractionOpClass(); var outDataset1 = op.Raster(outDataset, inDataset); var clipRaster = (IRaster)outDataset1; ISaveAs pSaveAs = clipRaster as ISaveAs; System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterCursor); System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(raster2); System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterDataSet); System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterWorkspace2); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFac); if (File.Exists(parth)) { File.Delete(parth); } workspaceFac = new RasterWorkspaceFactoryClass(); IDataset outdataset = pSaveAs.SaveAs(name, workspaceFac.OpenFromFile(directory, 0), "IMAGINE Image"); System.Runtime.InteropServices.Marshal.ReleaseComObject(outdataset); return; }
internal static RasterExtractionResult SummariseRaster(IGeoDataset pClippingPolygon, ExtractionLayerConfig pExtractionLayerConfig) { // set the analysis extent to be that of the polygon ServerLogger logger = new ServerLogger(); logger.LogMessage(ServerLogger.msgType.debug, "SummariseCategoricalRaster", 99, "Categorical raster clip beginning.."); IEnvelope tAnalysisExtent = pClippingPolygon.Extent; bool pCategoricalSummary = pExtractionLayerConfig.ExtractionType == ExtractionTypes.CategoricalRaster; IGeoDataset pRasterToClip = pExtractionLayerConfig.LayerDataset; IRasterAnalysisEnvironment tRasterAnalysisEnvironment = new RasterAnalysisClass(); object tAnalysisEnvelopeCastToObject = (System.Object)tAnalysisExtent; // object tAnotherBizarreMissingObject = Type.Missing; object tSnapObject = (System.Object)pRasterToClip; tRasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref tAnalysisEnvelopeCastToObject, ref tSnapObject); tRasterAnalysisEnvironment.SetAsNewDefaultEnvironment(); // extract the subset of the raster IExtractionOp2 tExtractionOp = new RasterExtractionOpClass(); // note we want to base the extraction on a raster (in an IGeoDataset) rather than an IPolygon. // That's because the catchment polygon may be multipart, and the operation doesn't work with multipart. // Also the polygon has a max of 1000 vertices. // And raster mask extraction is probably faster since the // polygon is converted internally to a grid anyway. IGeoDataset tClipped; if (pRasterToClip as IRaster != null) { logger.LogMessage(ServerLogger.msgType.debug, "SummariseRaster", 99, "Input was in raster form, using directly to clip..."); tClipped = tExtractionOp.Raster(pRasterToClip, pClippingPolygon); } else { // POLYGON VERSION: tExtractionOp.Polygon(pClipRaster,pPolygon,true) // sometimes we need to be able to pass in a polygon but rather than using the polygon // method we'll manually convert to a mask raster to avoid the issues above // It would save work to do this once for each request rather than repeat the conversion // for each layer - but we might want a different environment (snap extent etc) for each. logger.LogMessage(ServerLogger.msgType.debug, "SummariseCategoricalRaster", 99, "Converting input polygon to mask raster for clip..."); IRasterConvertHelper tConvertPolygonToRaster = new RasterConvertHelperClass(); // convert it to a raster with the same cell size as the input IRasterProps tRst = pRasterToClip as IRasterProps; IPnt tRstCellSize = tRst.MeanCellSize(); double x = tRstCellSize.X; double y = tRstCellSize.Y; double cellSize = Math.Round(Math.Min(x, y), 0); object tCellSizeAsObjectForNoGoodReason = (System.Object)cellSize; tRasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref tCellSizeAsObjectForNoGoodReason); IGeoDataset tPolyAsRast = tConvertPolygonToRaster.ToRaster1(pRasterToClip, "GRID", tRasterAnalysisEnvironment) as IGeoDataset; logger.LogMessage(ServerLogger.msgType.debug, "SummariseCategoricalRaster", 99, "...done, proceeding with clip"); tClipped = tExtractionOp.Raster(pRasterToClip, tPolyAsRast); } // now we have the clipped raster we need to summarise it differently depending on whether // we want a summary by category/value (for categorical rasters) or by stats (for float rasters) Dictionary <string, double> tResults; if (pCategoricalSummary) { tResults = WatershedDetailExtraction.SummariseRasterCategorically(tClipped); if (tResults.Count > 100) { // sanity check: don't sum up more than 100 different values tResults = WatershedDetailExtraction.SummariseRasterStatistically(tClipped); pCategoricalSummary = false; } } else { tResults = WatershedDetailExtraction.SummariseRasterStatistically(tClipped); } tRasterAnalysisEnvironment.RestoreToPreviousDefaultEnvironment(); return(new RasterExtractionResult(pExtractionLayerConfig.ParamName, pCategoricalSummary, tResults)); //return tResults; }
internal static RasterExtractionResult SummariseRaster(IGeoDataset pClippingPolygon, ExtractionLayerConfig pExtractionLayerConfig) { // set the analysis extent to be that of the polygon ServerLogger logger = new ServerLogger(); logger.LogMessage(ServerLogger.msgType.debug, "SummariseCategoricalRaster", 99, "Categorical raster clip beginning.."); IEnvelope tAnalysisExtent = pClippingPolygon.Extent; bool pCategoricalSummary = pExtractionLayerConfig.ExtractionType==ExtractionTypes.CategoricalRaster; IGeoDataset pRasterToClip = pExtractionLayerConfig.LayerDataset; IRasterAnalysisEnvironment tRasterAnalysisEnvironment = new RasterAnalysisClass(); object tAnalysisEnvelopeCastToObject = (System.Object)tAnalysisExtent; // object tAnotherBizarreMissingObject = Type.Missing; object tSnapObject = (System.Object)pRasterToClip; tRasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref tAnalysisEnvelopeCastToObject, ref tSnapObject); tRasterAnalysisEnvironment.SetAsNewDefaultEnvironment(); // extract the subset of the raster IExtractionOp2 tExtractionOp = new RasterExtractionOpClass(); // note we want to base the extraction on a raster (in an IGeoDataset) rather than an IPolygon. // That's because the catchment polygon may be multipart, and the operation doesn't work with multipart. // Also the polygon has a max of 1000 vertices. // And raster mask extraction is probably faster since the // polygon is converted internally to a grid anyway. IGeoDataset tClipped; if (pRasterToClip as IRaster != null) { logger.LogMessage(ServerLogger.msgType.debug, "SummariseRaster", 99, "Input was in raster form, using directly to clip..."); tClipped = tExtractionOp.Raster(pRasterToClip, pClippingPolygon); } else { // POLYGON VERSION: tExtractionOp.Polygon(pClipRaster,pPolygon,true) // sometimes we need to be able to pass in a polygon but rather than using the polygon // method we'll manually convert to a mask raster to avoid the issues above // It would save work to do this once for each request rather than repeat the conversion // for each layer - but we might want a different environment (snap extent etc) for each. logger.LogMessage(ServerLogger.msgType.debug, "SummariseCategoricalRaster", 99, "Converting input polygon to mask raster for clip..."); IRasterConvertHelper tConvertPolygonToRaster = new RasterConvertHelperClass(); // convert it to a raster with the same cell size as the input IRasterProps tRst = pRasterToClip as IRasterProps; IPnt tRstCellSize = tRst.MeanCellSize(); double x = tRstCellSize.X; double y = tRstCellSize.Y; double cellSize = Math.Round(Math.Min(x, y), 0); object tCellSizeAsObjectForNoGoodReason = (System.Object)cellSize; tRasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref tCellSizeAsObjectForNoGoodReason); IGeoDataset tPolyAsRast = tConvertPolygonToRaster.ToRaster1(pRasterToClip, "GRID", tRasterAnalysisEnvironment) as IGeoDataset; logger.LogMessage(ServerLogger.msgType.debug, "SummariseCategoricalRaster", 99, "...done, proceeding with clip"); tClipped = tExtractionOp.Raster(pRasterToClip, tPolyAsRast); } // now we have the clipped raster we need to summarise it differently depending on whether // we want a summary by category/value (for categorical rasters) or by stats (for float rasters) Dictionary<string, double> tResults; if (pCategoricalSummary) { tResults = WatershedDetailExtraction.SummariseRasterCategorically(tClipped); if (tResults.Count > 100) { // sanity check: don't sum up more than 100 different values tResults = WatershedDetailExtraction.SummariseRasterStatistically(tClipped); pCategoricalSummary = false; } } else { tResults = WatershedDetailExtraction.SummariseRasterStatistically(tClipped); } tRasterAnalysisEnvironment.RestoreToPreviousDefaultEnvironment(); return new RasterExtractionResult(pExtractionLayerConfig.ParamName, pCategoricalSummary, tResults); //return tResults; }