예제 #1
0
        public static void getH(string DSMfile, string oriShp)
        {
            Console.WriteLine("开始提取建筑高度!");
            Gdal.AllRegister();
            Ogr.RegisterAll();
            Stopwatch aTime = new Stopwatch(); aTime.Start();
            //创建一个BUFFER,BUFFER距离为1米
            string bufShp = bufferFile(oriShp, 1);

            //打开原文件和Buffer文件
            OSGeo.OGR.Driver dr    = Ogr.GetDriverByName("ESRI shapefile");
            DataSource       oriDs = dr.Open(oriShp, 1);
            DataSource       bufDs = dr.Open(bufShp, 0);

            OSGeo.OGR.Layer bufLayer = bufDs.GetLayerByIndex(0);
            OSGeo.OGR.Layer oriLayer = oriDs.GetLayerByIndex(0);

            //判断原文件中是否有以下字段,没有就创建
            if (oriLayer.FindFieldIndex("MIN", 1) == -1)
            {
                FieldDefn min = new FieldDefn("MIN", FieldType.OFTReal);
                oriLayer.CreateField(min, 1);
            }

            if (oriLayer.FindFieldIndex("MAX", 1) == -1)
            {
                FieldDefn max = new FieldDefn("MAX", FieldType.OFTReal);
                oriLayer.CreateField(max, 1);
            }

            if (oriLayer.FindFieldIndex("HIGHT", 1) == -1)
            {
                FieldDefn hight = new FieldDefn("HIGHT", FieldType.OFTReal);
                oriLayer.CreateField(hight, 1);
            }

            //打开栅格
            Dataset dsmDs = Gdal.Open(DSMfile, Access.GA_ReadOnly);

            double[] transfrom = new double[6];
            dsmDs.GetGeoTransform(transfrom);
            int allX = dsmDs.RasterXSize;
            int allY = dsmDs.RasterYSize;
            //开始计算每个Feature需要读取的栅格参数
            int featCount = oriLayer.GetFeatureCount(0);

            for (int i = 0; i < featCount; i++)
            {
                int[] subRasterOff_Size = subRasterInfo(transfrom, allX, allY, bufLayer.GetFeature(i));

                Feature oriFeat = oriLayer.GetFeature(i);
                Feature bufFeat = bufLayer.GetFeature(i);
                getMaxMinValue(dsmDs, oriFeat, bufFeat, subRasterOff_Size);
                oriLayer.SetFeature(oriFeat);
                oriFeat.Dispose();
                Console.WriteLine("当前处理:{0}/{1}", i, featCount);
            }
            oriLayer.Dispose();
            bufLayer.Dispose();
            oriDs.Dispose();
            bufDs.Dispose();
            dsmDs.Dispose();
            aTime.Stop();
            Console.WriteLine("建筑高度值提取完成!用时:{0}", aTime.Elapsed.ToString());
        }