Example #1
0
 async static void getDoubFeat(string filePath, int s, int ss)
 {
     await Task.Run(() =>
     {
         OSGeo.OGR.Ogr.RegisterAll();
         OSGeo.OGR.Driver dr       = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
         OSGeo.OGR.DataSource ds   = dr.Open(filePath, 0);
         OSGeo.OGR.Layer layer     = ds.GetLayerByIndex(0);
         OSGeo.OGR.Feature oriFeat = layer.GetFeature(s);
         for (int i = s + 1; i < ss; i++)
         {
             OSGeo.OGR.Feature nextFeat = layer.GetFeature(i);
             if (StaticTools.isSame(oriFeat, nextFeat, 1))
             {
                 ids.Add(s);
                 break;
             }
             nextFeat.Dispose();
         }
         oriFeat.Dispose();
         layer.Dispose();
         ds.Dispose();
         tickTime++;
     }
                    );
 }
        /************************************  对比并筛选 ***************************************************/

        /// <summary>
        /// 通过ID数组提取出所有要素,创建动作
        /// </summary>
        /// <param name="dzLine"></param>
        /// <param name="pdLing"></param>
        /// <param name="savePath"></param>
        public static void selectFeat(string dzx, string pdx, string savePath)
        {
            Stopwatch ssww = new Stopwatch(); ssww.Start();

            //获得数组
            int[] a = getMinIdGroupM(dzx, pdx);

            Console.WriteLine("ID to Feater...");
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr      = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource dzDS    = dr.Open(dzx, 0);
            OSGeo.OGR.Layer      dzLayer = dzDS.GetLayerByIndex(0);
            if (System.IO.File.Exists(savePath))
            {
                System.IO.File.Delete(savePath);
            }
            OSGeo.OGR.DataSource ds        = dr.CreateDataSource(savePath, null);
            OSGeo.OGR.Layer      fileLayer = ds.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null);
            //get featuer by ID
            for (int i = 0; i < a.Length; i++)
            {
                fileLayer.CreateFeature(dzLayer.GetFeature(a[i]));
                dzLayer.GetFeature(a[i]).Dispose();
            }
            dzLayer.Dispose();
            dzDS.Dispose();
            fileLayer.Dispose();
            ds.Dispose();
            _2_坡度线.OutLine.CutImg.cleanPdxM(savePath);
            ssww.Stop();
            Console.WriteLine("提取轮廓线完成!用时:{0}", ssww.Elapsed.ToString());
        }
Example #3
0
        /// <summary>
        /// 整合坡度线(Line)
        /// </summary>
        /// <param name="subPdx"></param>
        /// <returns></returns>
        public static string zhengHePdLine(List <string> subPdx)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");

            string newPath = StaticTools.tempFilePath("shp", "整合线");

            OSGeo.OGR.DataSource newDs    = dr.CreateDataSource(newPath, null);/////////////////
            OSGeo.OGR.Layer      newLayer = newDs.CreateLayer("Lines",
                                                              dr.Open(subPdx[0], 0).GetLayerByIndex(0).GetSpatialRef(),
                                                              dr.Open(subPdx[0], 0).GetLayerByIndex(0).GetGeomType(), null);

            for (int i = 0; i < subPdx.Count; i++)
            {
                OSGeo.OGR.DataSource dsLine    = dr.Open(subPdx[i], 0);///////////////////////
                OSGeo.OGR.Layer      lineLayer = dsLine.GetLayerByIndex(0);
                for (int j = 0; j < lineLayer.GetFeatureCount(0); j++)
                {
                    newLayer.CreateFeature(lineLayer.GetFeature(j));
                }
                dsLine.Dispose();
            }
            newDs.Dispose();
            return(newPath);
        }
Example #4
0
        async static void subAdd(Feature poiFeat, string dzx, Layer newLayer)
        {
            await Task.Run(() =>
            {
                OSGeo.OGR.Ogr.RegisterAll();
                OSGeo.OGR.Driver dr        = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
                OSGeo.OGR.DataSource dzxDS = dr.Open(dzx, 0);
                OSGeo.OGR.Layer dzxLayer   = dzxDS.GetLayerByIndex(0);
                int dzxCount = dzxLayer.GetFeatureCount(0);
                OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef();
                for (int i = 0; i < dzxLayer.GetFeatureCount(0); i++)
                {
                    OSGeo.OGR.Feature dzxFeat  = dzxLayer.GetFeature(i);
                    OSGeo.OGR.Geometry dzxGeom = dzxFeat.GetGeometryRef();

                    if (poiGeom.Within(dzxGeom))
                    {
                        newLayer.CreateFeature(dzxFeat);
                    }
                    dzxGeom.Dispose();
                    dzxFeat.Dispose();
                }
                poiFeat.Dispose();
                dzxDS.Dispose();
                tickTime++;
            });
        }
Example #5
0
        public static void selectDZXFromPointM(string point, string dzx, string savePath)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");

            OSGeo.OGR.DataSource poiDS    = dr.Open(point, 0);
            OSGeo.OGR.Layer      poiLayer = poiDS.GetLayerByIndex(0);

            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }
            OSGeo.OGR.DataSource newDS    = dr.CreateDataSource(savePath, null);
            OSGeo.OGR.Layer      newLayer = newDS.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null);

            tickTime = 0;
            int poiCount = poiLayer.GetFeatureCount(0);

            for (int i = 0; i < poiCount; i++)
            {
                subAdd(poiLayer.GetFeature(i), dzx, newLayer);
            }
            while (tickTime < poiCount)
            {
                Thread.Sleep(2222);
                Console.WriteLine("getFeatureByPoint:{0}/{1}", tickTime, poiCount);
            }

            Console.WriteLine("【本次提取到{0}个要素】", newLayer.GetFeatureCount(0));
            newDS.Dispose();
            poiDS.Dispose();
        }
        /// <summary>
        /// 通过是否相交和面积差,获得最小面积差相交要素的ID数组--------多线OK
        /// </summary>
        /// <param name="dzx"></param>
        /// <param name="pdx"></param>
        /// <returns></returns>
        private static int[] getMinIdGroupM(string dzx, string pdx)
        {
            Console.WriteLine("起用多线程提取轮廓线ID....");

            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr      = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource pdDS    = dr.Open(pdx, 0);
            OSGeo.OGR.Layer      pdLayer = pdDS.GetLayerByIndex(0);

            int pdCount = pdLayer.GetFeatureCount(0);

            for (int pdi = 0; pdi < pdCount; pdi++)
            {
                OSGeo.OGR.Feature pdFeat = pdLayer.GetFeature(pdi);
                select(pdFeat, dzx);
            }

            while (tickTime < pdCount)
            {
                Console.WriteLine("第{0}个完成,共{1}个", tickTime, pdCount);
                Thread.Sleep(2000);
            }

            pdLayer.Dispose();
            pdDS.Dispose();
            Console.WriteLine("多线程提取ID完成.....");
            return(minIdG.ToArray());
        }
Example #7
0
        /*************************************  点角式  *****************************************/
        /// <summary>
        /// 清理等后生成新的文件
        ///
        /// </summary>
        public static void claenPoint(string filePath, string savefile, double jiaodu, int cishu)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            //进来的Layer
            OSGeo.OGR.DataSource oriDs    = dr.Open(filePath, 0);
            OSGeo.OGR.Layer      oriLayer = oriDs.GetLayerByIndex(0);
            //出去的Layer
            if (System.IO.File.Exists(savefile))
            {
                savefile.deleteFiles();
            }
            OSGeo.OGR.DataSource targDs    = dr.CreateDataSource(savefile, null);
            OSGeo.OGR.Layer      targLayer = targDs.CreateLayer("targ", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null);

            int featCount = oriLayer.GetFeatureCount(0);

            for (int i = 0; i < featCount; i++)
            {
                ///进来的Featuer
                OSGeo.OGR.Feature oriFeat = oriLayer.GetFeature(i);

                ///把一个Featuer转为点数组
                OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef();
                OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0);
                int     pointCount         = subGeom.GetPointCount();
                Point[] aFeat = new Point[pointCount];
                for (int c = 0; c < pointCount; c++)
                {
                    aFeat[c].X = subGeom.GetX(c);
                    aFeat[c].Y = subGeom.GetY(c);
                    aFeat[c].Z = subGeom.GetZ(c);
                }

                ///调选点方法,得到一个新的Featuer
                Geometry newGeom = null;
                if (aFeat.Length > cishu * 3)
                {
                    newGeom = JID(aFeat, jiaodu, cishu);
                }
                else
                {
                    oriFeat.Dispose();
                    continue;
                }
                if (newGeom != null)
                {
                    Feature temp = new Feature(new FeatureDefn(""));
                    temp.SetGeometry(newGeom);
                    targLayer.CreateFeature(temp);
                    temp.Dispose();
                }
                oriFeat.Dispose();
                Console.WriteLine("已完成{0}/{1}", i, featCount);
            }
            oriDs.Dispose();
            targDs.Dispose();
        }
        /// <summary>
        /// 子线程提取轮廓线ID
        /// </summary>
        /// <param name="pdFeat"></param>
        /// <param name="dzx"></param>
        async static void select(Feature pdFeat, string dzx)
        {
            await Task.Run(() =>
            {
                double afterCha = -1;
                int yesID       = -1;

                OSGeo.OGR.Ogr.RegisterAll();
                OSGeo.OGR.Driver dr       = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
                OSGeo.OGR.DataSource dzDS = dr.Open(dzx, 0);
                OSGeo.OGR.Layer dzxLayer  = dzDS.GetLayerByIndex(0);

                OSGeo.OGR.Geometry pdGeom = pdFeat.GetGeometryRef();

                int dzCount = dzxLayer.GetFeatureCount(0);

                for (int dzi = 0; dzi < dzCount; dzi++)
                {
                    OSGeo.OGR.Feature dzFeat  = dzxLayer.GetFeature(dzi);
                    OSGeo.OGR.Geometry dzGeom = dzFeat.GetGeometryRef();

                    if (pdGeom.Intersect(dzGeom))
                    {
                        double cha = Math.Abs(dzGeom.GetArea() - pdGeom.GetArea());
                        if (afterCha == -1)
                        {
                            afterCha = cha;
                            yesID    = dzi;
                        }
                        else if (cha < afterCha)
                        {
                            afterCha = cha;
                            yesID    = dzi;
                        }
                    }

                    dzGeom.Dispose();
                    dzFeat.Dispose();
                }

                if (yesID != -1)
                {
                    minIdG.Add(yesID);
                }
                dzDS.Dispose();
                dzxLayer.Dispose();
                pdGeom.Dispose();
                pdFeat.Dispose();
                tickTime++;
            });
        }
Example #9
0
        /// <summary>
        /// 得到shp文件的维度
        /// </summary>
        /// <returns></returns>
        public int GetDeminsion()
        {
            int      deminsion = -1;
            Feature  pFeature  = _ogrLayer.GetFeature(0);
            Geometry pGeom     = pFeature.GetGeometryRef();

            deminsion = pGeom.GetCoordinateDimension();
            return(deminsion);
        }
Example #10
0
        public static void selectFromPoint(string point, string slopPoly)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr        = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource poiDS     = dr.Open(point, 0);
            OSGeo.OGR.DataSource slopDS    = dr.Open(slopPoly, 1);
            OSGeo.OGR.Layer      poiLayer  = poiDS.GetLayerByIndex(0);
            OSGeo.OGR.Layer      slopLayer = slopDS.GetLayerByIndex(0);

            List <int> staIds    = new List <int>();
            int        poiCount  = WorkFlow.pointIds.Count;
            int        slopCount = slopLayer.GetFeatureCount(0);

            for (int i = 0; i < poiCount; i++)
            {
                OSGeo.OGR.Feature  poiFeat = poiLayer.GetFeature(WorkFlow.pointIds[i]);
                OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef();
                for (int t = 0; t < slopCount; t++)
                {
                    OSGeo.OGR.Feature  slopFeat = slopLayer.GetFeature(t);
                    OSGeo.OGR.Geometry slopGeom = slopFeat.GetGeometryRef();
                    if (poiGeom.Within(slopGeom))
                    {
                        staIds.Add(t);
                        WorkFlow.pointIds[i] = -1;
                    }
                    slopFeat.Dispose();
                }
                poiFeat.Dispose();
            }
            Console.WriteLine("【本次提取到{0}个要素】", staIds.Count);
            while (WorkFlow.pointIds.IndexOf(-1) > -1)
            {
                WorkFlow.pointIds.Remove(-1);
            }

            for (int i = 0; i < slopCount; i++)
            {
                if (staIds.IndexOf(i) == -1)
                {
                    slopLayer.DeleteFeature(i);
                }
            }
            slopDS.deleteFeatUpdate();
            slopDS.Dispose();
            poiDS.Dispose();
        }
Example #11
0
        /// <summary>
        /// 清理原文件
        /// </summary>
        /// <param name="filePath"></param>
        public static void claenPoint(string filePath, double jiaodu, int cishu)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr       = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource oriDs    = dr.Open(filePath, 1);
            OSGeo.OGR.Layer      oriLayer = oriDs.GetLayerByIndex(0);

            int featCount = oriLayer.GetFeatureCount(0);

            for (int i = 0; i < featCount; i++)
            {
                OSGeo.OGR.Feature  oriFeat = oriLayer.GetFeature(i);
                OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef();
                OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0);

                int pointCount = subGeom.GetPointCount();

                Point[] aFeat = new Point[pointCount];

                for (int c = 0; c < pointCount; c++)
                {
                    aFeat[c].X = subGeom.GetX(c);
                    aFeat[c].Y = subGeom.GetY(c);
                    aFeat[c].Z = subGeom.GetZ(c);
                }

                OSGeo.OGR.Geometry newGeom = null;
                if (aFeat.Length > cishu * 3)
                {
                    newGeom = JID(aFeat, jiaodu, cishu);
                }
                else
                {
                    oriFeat.Dispose();
                    continue;
                }
                if (newGeom != null)
                {
                    oriFeat.SetGeometry(newGeom);
                    oriLayer.SetFeature(oriFeat);
                }
                Console.WriteLine("cleanPoint已完成{0}/{1}", i, featCount);
                oriFeat.Dispose();
            }
            oriDs.Dispose();
        }
Example #12
0
        public FeatureWrapper GetFeature(long index)
        {
            try
            {
                var feature = _ogrLayer.GetFeature(index);
                if (feature == null)
                {
                    return(null);
                }

                var featureWrapper = new FeatureWrapper(feature);
                return(featureWrapper);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Example #13
0
        public static void selectDZXFromPoint(string point, string dzx, string savePath)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr    = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource poiDS = dr.Open(point, 0);
            OSGeo.OGR.DataSource dzxDS = dr.Open(dzx, 0);
            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }
            OSGeo.OGR.DataSource newDS    = dr.CreateDataSource(savePath, null);
            OSGeo.OGR.Layer      poiLayer = poiDS.GetLayerByIndex(0);
            OSGeo.OGR.Layer      dzxLayer = dzxDS.GetLayerByIndex(0);
            OSGeo.OGR.Layer      newLayer = newDS.CreateLayer("", dzxLayer.GetSpatialRef(), dzxLayer.GetGeomType(), null);

            int poiCount = poiLayer.GetFeatureCount(0);
            int dzxCount = dzxLayer.GetFeatureCount(0);

            for (int i = 0; i < poiCount; i++)
            {
                OSGeo.OGR.Feature  poiFeat = poiLayer.GetFeature(i);
                OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef();
                for (int t = 0; t < dzxCount; t++)
                {
                    OSGeo.OGR.Feature  dzxFeat = dzxLayer.GetFeature(t);
                    OSGeo.OGR.Geometry dzxGeom = dzxFeat.GetGeometryRef();

                    if (poiGeom.Within(dzxGeom))
                    {
                        newLayer.CreateFeature(dzxFeat);
                    }
                    dzxFeat.Dispose();
                    Console.WriteLine("getFeatureByPoint:{0}/{1}", i, poiCount);
                }
                poiFeat.Dispose();
            }
            Console.WriteLine("【本次提取到{0}个要素】", newLayer.GetFeatureCount(0));
            newDS.Dispose();
            dzxDS.Dispose();
            poiDS.Dispose();
        }
Example #14
0
 /// <summary>
 /// Returns the geometry corresponding to the Object ID
 /// </summary>
 /// <param name="oid">Object ID</param>
 /// <returns>geometry</returns>
 public SharpMap.Geometries.Geometry GetGeometryByID(uint oid)
 {
     using (OSGeo.OGR.Feature _OgrFeature = _OgrLayer.GetFeature((int)oid))
         return(this.ParseOgrGeometry(_OgrFeature.GetGeometryRef()));
 }
        public static Boolean CleanFileGDBNames(frmMain pFrm, string pTgtFileGDB, string pSrcPGeoMDB, string pTgtFeatClass)
        {
            try
            {
                // Connect to database
                var mOdbcConn = new OdbcConnection("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" + pSrcPGeoMDB + ";Uid=Admin;Pwd=;");

                // Load road names
                pFrm.Log("Start loading names");

                var mRoadNames = new Dictionary <int, DataRow>();

                var mDataAdapter = new OdbcDataAdapter(sqlStatements.selectRoadNamesSQL, mOdbcConn);
                var mDataTable   = new DataTable();
                mDataAdapter.Fill(mDataTable);

                int ctr1   = 0;
                int total1 = mDataTable.Rows.Count;

                foreach (DataRow mRow in mDataTable.Rows)
                {
                    int mRoadId;
                    if (int.TryParse(mRow["ADRROADID"].ToString(), out mRoadId))
                    {
                        mRoadNames.Add(mRoadId, mRow);
                    }
                    ctr1++;
                    if (ctr1 % 50 == 0)
                    {
                        Application.DoEvents();
                    }
                }
                pFrm.Log("Done loading road names");

                mOdbcConn.Close();
                mDataAdapter.Dispose();
                mDataAdapter = null;
                mOdbcConn.Dispose();
                mOdbcConn = null;

                // Load district names
                Gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");

                DataSource      districtDataSource = Ogr.Open(DistrictImport.GetShapefileName(), 0);
                OSGeo.OGR.Layer districtLayer      = districtDataSource.GetLayerByIndex(0);
                if (districtLayer == null)
                {
                    pFrm.Log("Could not load district names layer");
                    goto CleanUpDistricts;
                }

                pFrm.Log("Done loading district names");

                var mFileGDBDrv = OSGeo.OGR.Ogr.GetDriverByName("FileGDB");
                var mTgtDSrc    = mFileGDBDrv.Open(pTgtFileGDB, 1);
                var mTgtLyr     = mTgtDSrc.GetLayerByName(pTgtFeatClass);

                if (mTgtLyr == null)
                {
                    pFrm.Log("Could not open layer " + pTgtFeatClass + " for update");
                    goto CleanUp;
                }
                pFrm.Log("Opened target FileGDB for update");

                Feature mFeat = null;

                int mNumTgtFeats = mTgtLyr.GetFeatureCount(1);

                // Clean all existing district and street names
                pFrm.Log("Processing");

                for (int idxFeature = 0; idxFeature < mNumTgtFeats; idxFeature++)
                {
                    if (null != (mFeat = mTgtLyr.GetFeature(idxFeature)))
                    {
                        Geometry mGeometry = mFeat.GetGeometryRef();
                        double[] p         = new double[2];
                        mGeometry.GetPoint(0, p);

                        if (pTgtFeatClass == "Address_unit_signs")
                        {
                            int mRoadID = mFeat.GetFieldAsInteger("ROADID");
                            mFeat.SetField("ROADNAME_EN", mRoadNames.GetStringOrNull(mRoadID, "NAMEENGLISH"));
                            mFeat.SetField("ROADNAME_AR", mRoadNames.GetStringOrNull(mRoadID, "NAMEARABIC"));
                            mFeat.SetField("ROADNAME_POP_EN", null);
                            mFeat.SetField("ROADNAME_POP_AR", null);
                            mFeat.SetField("DESCRIPTION_EN", mRoadNames.GetStringOrNull(mRoadID, "DESCRIPTIONENGLISH"));
                            mFeat.SetField("DESCRIPTION_AR", mRoadNames.GetStringOrNull(mRoadID, "DESCRIPTIONARABIC"));
                        }
                        else if (pTgtFeatClass == "Street_name_signs")
                        {
                            int mRoadID1 = mFeat.GetFieldAsInteger("ROADID_P1");
                            int mRoadID2 = mFeat.GetFieldAsInteger("ROADID_P2");
                            mFeat.SetField("ROADNAME_EN_P1", mRoadNames.GetStringOrNull(mRoadID1, "NAMEENGLISH"));
                            mFeat.SetField("ROADNAME_AR_P1", mRoadNames.GetStringOrNull(mRoadID1, "NAMEARABIC"));
                            mFeat.SetField("ROADNAME_EN_P2", mRoadNames.GetStringOrNull(mRoadID2, "NAMEENGLISH"));
                            mFeat.SetField("ROADNAME_AR_P2", mRoadNames.GetStringOrNull(mRoadID2, "NAMEARABIC"));
                        }
                        else if (pTgtFeatClass == "Address_guide_sign")
                        {
                            int    mRoadID   = mFeat.GetFieldAsInteger("ROADID");
                            string tmpRoadEn = mRoadNames.GetStringOrNull(mRoadID, "NAMEENGLISH");
                            string tmpRoadAr = mRoadNames.GetStringOrNull(mRoadID, "NAMEARABIC");
                            mFeat.SetField("ROADNAME_EN", tmpRoadEn);
                            mFeat.SetField("ROADNAME_AR", tmpRoadAr);
                        }

                        districtLayer.SetSpatialFilter(mGeometry);
                        using (Feature matchDistrict = districtLayer.GetFeature(0))
                        {
                            if (matchDistrict != null &&
                                matchDistrict.IsFieldSet("NAMELATIN") &&
                                matchDistrict.IsFieldSet("NAMEARABIC"))
                            {
                                string tmpDistEn = matchDistrict.GetFieldAsString("NAMELATIN");
                                string tmpDistAr = matchDistrict.GetFieldAsString("NAMEARABIC");
                                mFeat.SetField("DISTRICT_EN", tmpDistEn);
                                mFeat.SetField("DISTRICT_AR", tmpDistAr);
                            }
                            else
                            {
                                mFeat.SetField("DISTRICT_EN", null);
                                mFeat.SetField("DISTRICT_AR", null);
                                pFrm.Log("No district name match for " + pTgtFeatClass + " with coordinates: " + p[0].ToString() + ", " + p[1].ToString());
                            }
                        }

                        Application.DoEvents();

                        mTgtLyr.SetFeature(mFeat);

DoEventsOnBatchSize:

                        if (idxFeature % 250 == 0 || idxFeature == mNumTgtFeats)
                        {
                            pFrm.Log("Processed " + idxFeature + " out of" + mNumTgtFeats + " features");
                            //Thread.Sleep(250);
                            mTgtLyr.SyncToDisk();
                        }

                        if (mGeometry != null)
                        {
                            mGeometry.Dispose();
                        }

                        if (mFeat != null)
                        {
                            mFeat.Dispose();
                        }
                    }
                }

CleanUp:

                if (mTgtLyr != null)
                {
                    mTgtLyr.Dispose();
                }
                if (mTgtDSrc != null)
                {
                    mTgtDSrc.Dispose();
                }
                if (mFileGDBDrv != null)
                {
                    mFileGDBDrv.Dispose();
                }

CleanUpDistricts:

                if (districtLayer != null)
                {
                    districtLayer.Dispose();
                }

                if (districtDataSource != null)
                {
                    districtDataSource.Dispose();
                }
            }
            catch (Exception ex)
            {
                pFrm.Log(ex.Message);
                pFrm.Log(ex.StackTrace);
                return(false);
            }
            return(true);
        }
Example #16
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());
        }