Esempio n. 1
0
 private SpatialEnvelope calculateEnvelope(Geometry geom)
 {
     Envelope env = new Envelope();
     geom.GetEnvelope(env);
     SpatialEnvelope ogrenv = new SpatialEnvelope(env.MinX, env.MinY, env.MaxX, env.MaxY);
     return ogrenv;
 }
Esempio n. 2
0
        public Envelope Union(Envelope otherEnv)
        {
            Geometry a            = this.GetGeometry();
            Geometry b            = otherEnv.GetGeometry();
            Geometry intersection = a.Union(b);

            if (intersection == null)
            {
                return(null);
            }
            else
            {
                Envelope e = new Envelope();
                intersection.GetEnvelope(e);
                return(e);
            }
        }
Esempio n. 3
0
        /************************************* 判断两个Featuer是否重复 *************************************************/
        /// <summary>
        /// 判断两个Featuer是否重复,ori 当前Feat,next 目标Feat
        /// </summary>
        /// <param name="ori"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public static bool isSame(OSGeo.OGR.Feature ori, OSGeo.OGR.Feature next, double fanWei = 0.1)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Geometry oriGeom = ori.GetGeometryRef();
            OSGeo.OGR.Envelope oriEnve = new OSGeo.OGR.Envelope();
            oriGeom.GetEnvelope(oriEnve);
            OSGeo.OGR.Geometry nextGeom = next.GetGeometryRef();
            OSGeo.OGR.Envelope nextEnve = new OSGeo.OGR.Envelope();
            nextGeom.GetEnvelope(nextEnve);
            double oriArea  = oriGeom.GetArea();
            double nextArea = nextGeom.GetArea();
            bool   res      =
                Math.Abs(oriEnve.MaxX - nextEnve.MaxX) < fanWei && //外接矩形差
                Math.Abs(oriEnve.MaxY - nextEnve.MaxY) < fanWei &&
                Math.Abs(oriEnve.MinX - nextEnve.MinX) < fanWei &&
                Math.Abs(oriEnve.MinY - nextEnve.MinY) < fanWei;

            //面积?    && Math.Abs(oriArea - nextArea) < 0.1;
            oriGeom.Dispose();
            oriEnve.Dispose();
            nextGeom.Dispose();
            nextEnve.Dispose();
            return(res);
        }
        public static void AnalyzeRoadBoundingBoxes(string pMdbFile, string pLogFile, Action <Object, bool> aLog, int pOnlyApproved = 0)
        {
            var roadSuspects = new List <RoadSuspect>();
            // Setup SQL string to select all or only some streets
            string sql;

            if (pOnlyApproved == 1)
            {
                sql = "SELECT ADRROADID, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, ROADTYPE, ADRDISTRICTID, APPROVED FROM ADM_ADRROAD WHERE APPROVED = 1 ORDER BY ADRROADID";
            }
            else
            {
                sql = "SELECT ADRROADID, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, ROADTYPE, ADRDISTRICTID, APPROVED FROM ADM_ADRROAD ORDER BY ADRROADID";
            }

            // Setup fieldAttributes dictionary to hold road names
            var mRoads = new Dictionary <int, DataRow>();

            // Connect to database and load names
            var mOdbcConn    = new OdbcConnection("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" + pMdbFile + ";Uid=Admin;Pwd=;");
            var mDataAdapter = new OdbcDataAdapter(sql, 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))
                {
                    mRoads.Add(mRoadId, mRow);
                }
                ctr1++;
                if (ctr1 % 100 == 0)
                {
                    double mFraction1 = (((double)ctr1 / (double)total1) * 100);
                    Application.DoEvents();
                }
            }
            Debug.WriteLine("Done loading road names");
            mOdbcConn.Close();
            mDataAdapter = null;
            mOdbcConn    = null;

            // Connect to database using OGR to load geometries
            var mDriver = Ogr.GetDriverByName("PGeo");

            if (mDriver == null)
            {
                return;
            }

            Debug.WriteLine("Loaded driver");
            var mSource = mDriver.Open(pMdbFile, 0);

            if (mSource == null)
            {
                return;
            }

            Debug.WriteLine("Loaded datasource");
            var mLayer = mSource.GetLayerByName("ADRROADSEGMENT");

            if (mLayer == null)
            {
                return;
            }
            Debug.WriteLine("Loaded layer");

            // Create simplified roads
            var mSimplifiedRoadsFeatureSet = new SimplifiedRoads();

            int mTotal2 = mLayer.GetFeatureCount(0);

            foreach (var mRoadID in mRoads)
            {
                mLayer.SetAttributeFilter("ADRROADID = " + mRoadID.Key);

                OSGeo.OGR.Feature mFeature;
                int           mNumShapes  = 0;
                double        mLength     = 0;
                double        mLeft       = 0;
                double        mBottom     = 0;
                double        mTop        = 0;
                double        mRight      = 0;
                int           mDuplicates = 0;
                List <string> mGeoms      = new List <string>();
                while (null != (mFeature = mLayer.GetNextFeature()))
                {
                    OSGeo.OGR.Geometry mGeom = mFeature.GetGeometryRef();

                    if (mGeom != null)
                    {
                        string wkt;
                        mGeom.ExportToWkt(out wkt);
                        OSGeo.OGR.Envelope mExtent = new OSGeo.OGR.Envelope();
                        mGeom.GetEnvelope(mExtent);
                        if (mTop == 0 || mExtent.MaxY > mTop)
                        {
                            mTop = mExtent.MaxY;
                        }
                        if (mBottom == 0 || mExtent.MinY < mBottom)
                        {
                            mBottom = mExtent.MinY;
                        }
                        if (mLeft == 0 || mExtent.MinX < mLeft)
                        {
                            mLeft = mExtent.MinX;
                        }
                        if (mRight == 0 || mExtent.MaxX > mRight)
                        {
                            mRight = mExtent.MaxX;
                        }

                        if (!mGeoms.Contains(wkt))
                        {
                            mLength = mLength + mGeom.Length();
                            mGeoms.Add(wkt);
                        }
                        else
                        {
                            mDuplicates++;
                        }
                    }

                    mNumShapes++;
                }

                var mBoxWidth    = mRight - mLeft;
                var mBoxHeight   = mTop - mBottom;
                var mBoxDiagonal = Math.Sqrt(mBoxWidth * mBoxWidth + mBoxHeight * mBoxHeight);

                roadSuspects.Add(new RoadSuspect(mRoadID.Key,
                                                 mNumShapes,
                                                 mDuplicates,
                                                 Math.Round(mBoxDiagonal, 1),
                                                 Math.Round(mLength, 1)));


                if (mLength > (2.2 * mBoxDiagonal) || mDuplicates > 0)
                {
                    aLog(String.Format("r: {0}, s: {1}, l: {2}, d: {3}, bb: {4}",
                                       mRoadID.Key,
                                       mNumShapes,
                                       Math.Round(mLength, 1),
                                       mDuplicates,
                                       Math.Round(mBoxDiagonal, 1)),
                         true);
                }
            }

            using (var mCsvWriter = new CsvHelper.CsvWriter(new ExcelSerializer(pLogFile)))
            {
                mCsvWriter.WriteRecords(roadSuspects);
                aLog("Wrote output to " + pLogFile, true);
                aLog("Operation completed...", true);
            }
        }