public void WriteToSDEFromPixelArray(IRasterWorkspaceEx irasterWorkspaceEx_0, string string_0) { IRasterDataset rasterDataset = irasterWorkspaceEx_0.CreateRasterDataset(string_0, 3, rstPixelType.PT_SHORT, new RasterStorageDef(), "", new RasterDef(), null); IRaster raster = rasterDataset.CreateDefaultRaster(); IRasterProps rasterProp = raster as IRasterProps; int num = 1000; int num1 = 1000; IEnvelope envelopeClass = new Envelope() { XMin = 100, XMax = 500, YMin = 100, YMax = 500 } as IEnvelope; rasterProp.Extent = envelopeClass; rasterProp.Width = 1000; rasterProp.Height = 1000; IPnt pntClass = new Pnt(); pntClass.SetCoords((double)1000, (double)1000); IPixelBlock3 pixelBlock3 = raster.CreatePixelBlock(pntClass) as IPixelBlock3; pntClass.SetCoords(0, 0); for (int i = 0; i < 3; i++) { object pixelData = pixelBlock3.PixelData[i]; for (int j = 0; j < num; j++) { int num2 = 0; while (num2 < num1) { num2++; } } pixelBlock3.PixelData[i] = pixelData; } (raster as IRasterEdit).Write(pntClass, pixelBlock3 as IPixelBlock); }
//分析的主要实现函数 private IRasterLayer Analyze(IRasterLayer pRasterLayer) { IRaster pRaster = pRasterLayer.Raster; IRasterProps rasterProps = (IRasterProps)pRaster; //设置栅格数据起始点 IPnt pBlockSize = new Pnt(); pBlockSize.SetCoords(rasterProps.Width, rasterProps.Height); //选取整个范围 IPixelBlock pPixelBlock = pRaster.CreatePixelBlock(pBlockSize); //左上点坐标 IPnt tlp = new Pnt(); tlp.SetCoords(0, 0); //读入栅格 IRasterBandCollection pRasterBands = pRaster as IRasterBandCollection; IRasterBand pRasterBand = pRasterBands.Item(0); IRawPixels pRawRixels = pRasterBands.Item(0) as IRawPixels; pRawRixels.Read(tlp, pPixelBlock); //将PixBlock的值组成数组 Array pSafeArray = pPixelBlock.get_SafeArray(0) as Array; //Array转数组 double[,] myDoubleArr = new double[pSafeArray.GetLength(0), pSafeArray.GetLength(1)]; for (int i = 0; i < myDoubleArr.GetLength(0); i++) { for (int j = 0; j < myDoubleArr.GetLength(1); j++) { myDoubleArr[i, j] = Convert.ToDouble(pSafeArray.GetValue(i, j)); } } for (int i = 0; i < myDoubleArr.GetLength(0); i++) { for (int j = 0; j < myDoubleArr.GetLength(1); j++) { if (myDoubleArr[i, j] == 255) { myDoubleArr[i, j] = 0; } } } double[,] ZeroArray = GetArray(myDoubleArr, Convert.ToInt32(textBox4.Text)); double[,] OArray = SumArray(ZeroArray, Convert.ToInt32(textBox4.Text)); double[,] LastArray = ReturnLastArray(OArray, Convert.ToInt32(textBox4.Text)); pPixelBlock.set_SafeArray(0, LastArray); //StreamWriter sw = File.AppendText(@"E:\GIS底层实验\WorkSpace\result\arrry.txt"); //for (int y = 0; y < rasterProps.Height; y++) //{ // for (int x = 0; x < rasterProps.Width; x++) // { // //int value = Convert.ToInt32(pSafeArray.GetValue(x, y)); // Byte value = Convert.ToByte(pSafeArray.GetValue(x, y)); // string TxtCon = ("X:" + Convert.ToString(x) + "," + "Y:" + Convert.ToString(y) + "," + "Value" + pSafeArray.GetValue(x, y) + "\n"); // sw.Write(TxtCon); // } //} //sw.Flush(); //sw.Close(); // 编辑raster,将更新的值写入raster中 IRasterEdit rasterEdit = pRaster as IRasterEdit; rasterEdit.Write(tlp, pPixelBlock); rasterEdit.Refresh(); return(pRasterLayer); }
private byte[] QueryRasterHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; int layerID = Convert.ToInt32(boundVariables["layersID"]); double? min = null; double? max = null; operationInput.TryGetAsDouble("min", out min); operationInput.TryGetAsDouble("max", out max); if ((!min.HasValue) && (!max.HasValue)) { throw new ArgumentException(Name + ": must specify at least one of {min,max}"); } ISpatialReference outSR = GetSpatialReferenceParam(operationInput, "outSR"); IRaster raster = m_layers[layerID].Raster; IRasterBand band = (raster as IRasterBandCollection).Item(0); IRawPixels pixels = band as IRawPixels; IRasterProps properties = band as IRasterProps; int width = properties.Width; int height = properties.Height; double noData = Convert.ToDouble(properties.NoDataValue); IPnt flowDirBlockSize = new Pnt(); flowDirBlockSize.SetCoords(width, height); IPixelBlock pixelBlock = raster.CreatePixelBlock(flowDirBlockSize); IPnt pixelOrigin = new Pnt(); pixelOrigin.SetCoords(0, 0); pixels.Read(pixelOrigin, pixelBlock); System.Array data = (System.Array)(pixelBlock as IPixelBlock3).get_PixelDataByRef(0); bool[,] outData = new bool[width, height]; for (int x = 0; x < width; x += 1) { for (int y = 0; y < height; y += 1) { object value = data.GetValue(x, y); bool cellValue = false; if (value != null) { double numericValue = Convert.ToDouble(value); if ((numericValue != noData) && ((!min.HasValue) || (numericValue > min.Value)) && ((!max.HasValue) || (numericValue < max.Value))) { cellValue = true; } } outData.SetValue(cellValue, x, y); } } BoundingCurve bc = new BoundingCurve(outData); IPolygon resultGeom = bc.GetBoundaryAsPolygon(properties); resultGeom.SpatialReference = properties.SpatialReference; if ((outSR != null) && (outSR.FactoryCode != resultGeom.SpatialReference.FactoryCode)) { resultGeom.Project(outSR); } Feature resultFeature = new Feature(); resultFeature.Geometry = resultGeom; resultFeature.Attributes.Add("Shape_Length", resultGeom.Length); resultFeature.Attributes.Add("Shape_Area", (resultGeom as IArea).Area); resultFeature.Attributes.Add("Shape_Units", DescribeUnits(resultGeom.SpatialReference)); FeatureSet result = new FeatureSet(); result.GeometryType = esriGeometryType.esriGeometryPolygon; result.Features.Add(resultFeature); return Encoding.UTF8.GetBytes(result.ToJsonObject().ToJson()); }