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); } }