Beispiel #1
0
        public IRasterDataset CreateRasterSurf(string sDir, string sName, string sFormat, IPoint pOrigin, int nCol, int nRow, double cellsizeX, double cellsizeY, rstPixelType ePixelType, ISpatialReference2 pSR, bool bPerm)
        {
            //IWorkspaceFactory rWksFac = new RasterWorkspaceFactory();

            //IWorkspace wks = rWksFac.OpenFromFile(sDir, 0);

            //IRasterWorkspace2 rWks = wks as IRasterWorkspace2;

            //int numbands = 1;

            //IRasterDataset pRDS;// = new RasterDatasetClass();
            //pRDS = rWks.CreateRasterDataset(sName, sFormat, pOrigin, nCol, nRow, cellsizeX, cellsizeY, numbands, ePixelType, pSR, bPerm);
            //return pRDS;

            IWorkspaceFactory pworkspaceFactory = new RasterWorkspaceFactory();

            ESRI.ArcGIS.Geodatabase.IWorkspaceName pworkspaceName = pworkspaceFactory.Create(null, "MyWorkspace", null, 0);
            ESRI.ArcGIS.esriSystem.IName           pname          = (IName)pworkspaceName;
            ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor       = (IWorkspace)pname.Open();
            IPoint            originpoint = pOrigin;
            IRasterWorkspace2 rasterws    = (IRasterWorkspace2)inmemWor;

            //用于计算的 float型的栅格数据
            IRasterDataset demdataset = rasterws.CreateRasterDataset("Dataset", "MEM", originpoint, nCol, nRow,
                                                                     (double)cellsizeX, (double)cellsizeY, 1, rstPixelType.PT_DOUBLE, null, true);

            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pname);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceFactory);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceName);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor);
            }
            catch { }
            GC.Collect();
            return(demdataset);
        }
Beispiel #2
0
        /// <summary>
        /// 创建人员图层
        /// </summary>
        /// <param name="DataSetName"></param>
        /// <param name="AliaseName"></param>
        /// <param name="SpatialRef"></param>
        /// <param name="GeometryType"></param>
        /// <param name="PropertyFields"></param>
        /// <returns></returns>
        private void CreateDrawnLayer()
        {
            try
            {
                if (axMapControl1.LayerCount >= 2)
                {
                    axMapControl1.DeleteLayer(0);
                }
            }
            catch
            { }

            IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass();

            ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0);
            ESRI.ArcGIS.esriSystem.IName           name          = (IName)workspaceName;
            ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor      = (IWorkspace)name.Open();
            IField        oField        = new FieldClass();
            IFields       oFields       = new FieldsClass();
            IFieldsEdit   oFieldsEdit   = null;
            IFieldEdit    oFieldEdit    = null;
            IFeatureClass oFeatureClass = null;
            IFeatureLayer oFeatureLayer = null;

            try
            {
                oFieldsEdit = oFields as IFieldsEdit;
                oFieldEdit  = oField as IFieldEdit;

                //创建图元属性
                FieldClass feadName = new FieldClass();
                IFieldEdit edit     = feadName;
                edit.Name_2 = "Name";
                edit.Type_2 = esriFieldType.esriFieldTypeString;
                oFieldsEdit.AddField(feadName);

                FieldClass feadID = new FieldClass();
                IFieldEdit editID = feadID;
                editID.Name_2 = "ID";
                editID.Type_2 = esriFieldType.esriFieldTypeString;
                oFieldsEdit.AddField(editID);


                IGeometryDef     geometryDef     = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                //geometryDefEdit.AvgNumPoints_2 = 5;
                geometryDefEdit.GeometryType_2     = esriGeometryType.esriGeometryPolyline;
                geometryDefEdit.GridCount_2        = 1;
                geometryDefEdit.HasM_2             = false;
                geometryDefEdit.HasZ_2             = false;
                geometryDefEdit.SpatialReference_2 = axMapControl1.SpatialReference;
                oFieldEdit.Name_2        = "SHAPE";
                oFieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
                oFieldEdit.GeometryDef_2 = geometryDef;
                oFieldEdit.IsNullable_2  = true;
                oFieldEdit.Required_2    = true;
                oFieldsEdit.AddField(oField);
                oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass("DrawnDS", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
                (oFeatureClass as IDataset).BrowseName = "DrawnDS";
                oFeatureLayer              = new FeatureLayerClass();
                oFeatureLayer.Name         = "Drawn";
                oFeatureLayer.FeatureClass = oFeatureClass;
            }
            catch
            {
            }
            finally
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oField);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(name);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceName);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass);
                }
                catch { }

                GC.Collect();
            }

            DrawnLayer = oFeatureLayer;
            axMapControl1.Map.AddLayer(oFeatureLayer);
            UniqueValueRenderFlyr(oFeatureLayer);
            axMapControl1.Refresh();
        }
Beispiel #3
0
        public IRaster TINToDEM(ITin pTin)
        {
            IPoint pOrigin = pTin.Extent.LowerLeft;


            IWorkspaceFactory pworkspaceFactory = new RasterWorkspaceFactory();

            ESRI.ArcGIS.Geodatabase.IWorkspaceName pworkspaceName = pworkspaceFactory.Create(null, "MyWorkspace", null, 0);
            ESRI.ArcGIS.esriSystem.IName           pname          = (IName)pworkspaceName;
            ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor       = (IWorkspace)pname.Open();
            IPoint            originpoint = pOrigin;
            IRasterWorkspace2 rasterws    = (IRasterWorkspace2)inmemWor;

            int nCol, nRow;

            nCol = 500;
            nRow = 500;
            double cellsizeX, cellsizeY;

            cellsizeX = pTin.Extent.Width / nCol;
            cellsizeY = pTin.Extent.Height / nRow;

            //用于计算的 float型的栅格数据
            IRasterDataset demdataset = rasterws.CreateRasterDataset("Dataset", "MEM", originpoint, nCol, nRow,
                                                                     (double)cellsizeX, (double)cellsizeY, 1, rstPixelType.PT_DOUBLE, null, true);

            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pname);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceFactory);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pworkspaceName);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor);
            }
            catch { }

            //IRawPixels pRawPixels = GetRawPixels(pRDS, 0);
            IRaster pRaster    = demdataset.CreateDefaultRaster();
            IPnt    pBlockSize = new DblPnt();

            //nCol = 50;
            //nRow = 50;
            pBlockSize.X = nCol;
            pBlockSize.Y = nRow;
            IPixelBlock pPixelBlock = pRaster.CreatePixelBlock(pBlockSize);
            //IPixelBlock pPixelBlock = pRawPixels.CreatePixelBlock(pBlockSize);
            IPixelBlock3 pPixelBlock3 = pPixelBlock as IPixelBlock3;

            //object val = pPixelBlock.get_SafeArray(0);
            ITinSurface pTinSurf = pTin as ITinSurface;
            // IRasterProps pRasterProps = pRawPixels as IRasterProps;
            IRasterProps pRasterProps = pRaster as IRasterProps;
            object       nodata;

            //pOrigin.X = pOrigin.X + (cellsize * 0.5);
            //pOrigin.Y = pOrigin.Y + (cellsize * nRow) - (cellsize * 0.5);
            pOrigin.X = pOrigin.X;
            pOrigin.Y = pOrigin.Y + (cellsizeY * nRow);
            nodata    = pRasterProps.NoDataValue;
            IGeoDatabaseBridge2 pbridge2 = (IGeoDatabaseBridge2) new GeoDatabaseHelperClass();

            //这个pOrigin为栅格左上角
            //pbridge2.QueryPixelBlock(pTinSurf, pOrigin.X, pOrigin.Y, cellsize, cellsize, esriRasterizationType.esriElevationAsRaster, nodata, ref val);
            //if (pTin.ProcessCancelled)
            //    return null;
            //val.GetType();
            CalPixelArray(pTinSurf, pOrigin.X, pOrigin.Y, cellsizeX, cellsizeY, ref pPixelBlock3);
            IPnt pOffset = new DblPnt();

            pOffset.X = 0;
            pOffset.Y = 0;
            //pPixelBlock3.set_PixelData(0, val);
            //pRawPixels.Write(pOffset, (IPixelBlock)pPixelBlock3);//写入硬盘
            IRasterEdit prasteredit = pRaster as IRasterEdit;

            prasteredit.Write(pOffset, (IPixelBlock)pPixelBlock3);
            //pRDS = OpenOutputRasterDataset(sDir, sName);

            //IPixelBlock pb = pRaster.CreatePixelBlock(pBlockSize);
            //pRaster.Read(pOffset,pb);
            return(pRaster);
        }
Beispiel #4
0
        /// <summary>
        /// 在内存中创建图层
        /// </summary>
        /// <param name="DataSetName">数据集名称</param>
        /// <param name="AliaseName">别名</param>
        /// <param name="SpatialRef">空间参考</param>
        /// <param name="GeometryType">几何类型</param>
        /// <param name="PropertyFields">属性字段集合</param>
        /// <returns>IfeatureLayer</returns>
        public static IFeatureLayer CreateFeatureLayerInmemeory(string DataSetName, string AliaseName, ISpatialReference SpatialRef, esriGeometryType GeometryType, IFields PropertyFields)
        {
            IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass();

            ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0);
            ESRI.ArcGIS.esriSystem.IName           name          = (IName)workspaceName;
            ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor      = (IWorkspace)name.Open();
            IField        oField        = new FieldClass();
            IFields       oFields       = new FieldsClass();
            IFieldsEdit   oFieldsEdit   = null;
            IFieldEdit    oFieldEdit    = null;
            IFeatureClass oFeatureClass = null;
            IFeatureLayer oFeatureLayer = null;

            try
            {
                oFieldsEdit = oFields as IFieldsEdit;
                oFieldEdit  = oField as IFieldEdit;
                for (int i = 0; i < PropertyFields.FieldCount; i++)
                {
                    oFieldsEdit.AddField(PropertyFields.get_Field(i));
                }
                IGeometryDef     geometryDef     = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                geometryDefEdit.AvgNumPoints_2     = 5;
                geometryDefEdit.GeometryType_2     = GeometryType;
                geometryDefEdit.GridCount_2        = 1;
                geometryDefEdit.HasM_2             = false;
                geometryDefEdit.HasZ_2             = false;
                geometryDefEdit.SpatialReference_2 = SpatialRef;
                oFieldEdit.Name_2        = "SHAPE";
                oFieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
                oFieldEdit.GeometryDef_2 = geometryDef;
                oFieldEdit.IsNullable_2  = true;
                oFieldEdit.Required_2    = true;
                oFieldsEdit.AddField(oField);
                oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass(DataSetName, oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
                (oFeatureClass as IDataset).BrowseName = DataSetName;
                oFeatureLayer              = new FeatureLayerClass();
                oFeatureLayer.Name         = AliaseName;
                oFeatureLayer.FeatureClass = oFeatureClass;
            }
            catch
            {
            }
            finally
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oField);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(name);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceName);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass);
                }
                catch { }

                GC.Collect();
            }
            return(oFeatureLayer);
        }
        private IRaster CreateRaster(Point2D ptLeftTop, double[] dbResolution, int[] nSize)
        {
            try
            {
                IWorkspaceFactory pworkspaceFactory = new RasterWorkspaceFactory();
                ESRI.ArcGIS.Geodatabase.IWorkspaceName pworkspaceName = pworkspaceFactory.Create(null, "MyWorkspace", null, 0);
                ESRI.ArcGIS.esriSystem.IName           pname          = (IName)pworkspaceName;
                ESRI.ArcGIS.Geodatabase.IWorkspace     inmemWor       = (IWorkspace)pname.Open();
                IRasterWorkspace2 rasterWs = (IRasterWorkspace2)inmemWor;

                //Define the spatial reference of the raster dataset.
                ISpatialReference sr = new UnknownCoordinateSystemClass();
                //Define the origin for the raster dataset, which is the lower left corner of the raster.
                IPoint origin = new PointClass();
                origin.PutCoords(ptLeftTop.X, ptLeftTop.Y);
                //Define the dimensions of the raster dataset.
                int    width   = nSize[0];        //This is the width of the raster dataset.
                int    height  = nSize[1];        //This is the height of the raster dataset.
                double xCell   = dbResolution[0]; //This is the cell size in x direction.
                double yCell   = dbResolution[1]; //This is the cell size in y direction.
                int    NumBand = 1;               // This is the number of bands the raster dataset contains.
                //Create a raster dataset in TIFF format.
                IRasterDataset rasterDataset = rasterWs.CreateRasterDataset("", "MEM",
                                                                            origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
                                                                            true);

                //If you need to set NoData for some of the pixels, you need to set it on band
                //to get the raster band.
                IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
                IRasterBand           rasterBand;
                IRasterProps          rasterProps;
                rasterBand  = rasterBands.Item(0);
                rasterProps = (IRasterProps)rasterBand;
                //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
                //rasterProps.NoDataValue = -9999f;
                //Create a raster from the dataset.
                IRaster raster = rasterDataset.CreateDefaultRaster();

                //Create a pixel block using the weight and height of the raster dataset.
                //If the raster dataset is large, a smaller pixel block should be used.
                //Refer to the topic "How to access pixel data using a raster cursor".
                //IPnt blocksize = new PntClass();
                //blocksize.SetCoords(width, height);
                //IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as IPixelBlock3;

                ////Populate some pixel values to the pixel block.
                //System.Array pixels;
                //pixels = (System.Array)pixelblock.get_PixelData(0);
                //for (int i = 0; i < width; i++)
                //{
                //    for (int j = 0; j < height; j++)
                //    {
                //        pixels.SetValue(dbData[i, j], i, j);
                //    }
                //}
                //pixelblock.set_PixelData(0, (System.Array)pixels);

                ////Define the location that the upper left corner of the pixel block is to write.
                //IPnt upperLeft = new PntClass();
                //upperLeft.SetCoords(ptLeftTop.X, ptLeftTop.Y);

                ////Write the pixel block.
                //IRasterEdit rasterEdit = (IRasterEdit)raster;
                //rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);
                //rasterEdit.Refresh();

                //Release rasterEdit explicitly.
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterDataset);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterWs);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(sr);
                // GC.Collect();

                return(raster);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(null);
            }

            return(null);
        }