コード例 #1
0
        /// <summary>
        /// Exports the selected database to fieldAttributes filegeodatabase
        /// </summary>
        /// <param name="pOnlyApproved"></param>
        /// <returns></returns>
        public static FeatureSet GetDistrictsFeatureSetFromAdmAdrMdb(ref ToolStripProgressBar pPgBar, string pMdbFile, int pOnlyApproved = 0)
        {
            // Setup SQL string to select all or only some streets
            string mSql;

            if (pOnlyApproved == 1)
            {
                mSql = "SELECT OBJECTID, ABBREVIATION, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, APPROVED FROM ADRDISTRICT WHERE APPROVED = 'Y'";
            }
            else
            {
                mSql = "SELECT OBJECTID, ABBREVIATION, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, APPROVED FROM ADRDISTRICT";
            }

            // Setup fieldAttributes dictionary to hold road names
            var mDistrictNames = 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(mSql, mOdbcConn);
            var mDataTable   = new DataTable();

            mDataAdapter.Fill(mDataTable);
            int ctr1   = 0;
            int total1 = mDataTable.Rows.Count;

            pPgBar.Value = ctr1;
            foreach (DataRow mRow in mDataTable.Rows)
            {
                if (mDistrictNames.Keys.Contains(mRow["OBJECTID"].ToInt()))
                {
                    Utilities.LogDebug("Key " + mRow["OBJECTID"].ToInt() + " already exists in districts");
                }
                mDistrictNames.Add(mRow["OBJECTID"].ToInt(), mRow);
                ctr1++;
                if (ctr1 % 100 == 0)
                {
                    double mFraction1 = (((double)ctr1 / (double)total1) * 100);
                    pPgBar.ProgressBar.Value = (int)(Math.Round(mFraction1));
                    Application.DoEvents();
                }
            }

            Debug.WriteLine("Done loading district names");
            Debug.WriteLine(ctr1);
            mOdbcConn.Close();
            mDataAdapter = null;
            mOdbcConn    = null;

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

            if (mDriver == null)
            {
                return(null);
            }

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

            if (mSource == null)
            {
                return(null);
            }

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

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

            // Create simplified roads
            var mDistrict = new District();

            int mTotal2 = mLayer.GetFeatureCount(1);

            Debug.WriteLine("Number of features" + mTotal2);

            pPgBar.Value = 0;
            OSGeo.OGR.Feature mFeature;

            int mFeatureIndex = 0;

            while (null != (mFeature = mLayer.GetNextFeature()))
            {
                if (mFeature == null)
                {
                    Debug.WriteLine("Feature #{0} is null", mFeatureIndex);
                }
                else
                {
                    OSGeo.OGR.Geometry mGeometry = mFeature.GetGeometryRef();
                    if (mGeometry != null)
                    {
                        mGeometry.FlattenTo2D();
                        byte[] mWkb = new Byte[mGeometry.WkbSize()];
                        string mWkt = "";
                        mGeometry.ExportToWkb(mWkb);
                        mGeometry.ExportToWkt(out mWkt);

                        IMultiPolygon mPolygon;

                        int mDistrictObjectId = mFeature.GetFieldAsInteger("OBJECTID");

                        if (mDistrictObjectId > 0)
                        {
                            var       mWkbReader = new WkbReader();
                            IGeometry mGeom      = mWkbReader.Read(mWkb);
                            if (mGeom.GetType() == typeof(DotSpatial.Topology.MultiPolygon))
                            {
                                var mPolygons = new List <Polygon>();
                                mPolygons.Add(mGeom as Polygon);

                                var mPolygonsN = new List <Polygon>();
                                foreach (var mPart in mPolygons)
                                {
                                    if (mPart != null)
                                    {
                                        mPolygonsN.Add(mPart);
                                    }
                                }
                                mPolygon = new DotSpatial.Topology.MultiPolygon(mPolygonsN.ToArray <Polygon>());
                            }
                            else
                            {
                                mPolygon = mGeom as IMultiPolygon;
                            }

                            if (mDistrictNames.ContainsKey(mDistrictObjectId))
                            {
                                var mDistrictName = mDistrictNames[mDistrictObjectId];


                                var mDistrictAbbreviation = mDistrictName["ABBREVIATION"].ToString();
                                var mNameArabic           = mDistrictName["NAMEARABIC"].ToString();
                                var mNameLatin            = mDistrictName["NAMEENGLISH"].ToString();
                                var mNamePopularArabic    = mDistrictName["NAMEPOPULARARABIC"].ToString();
                                var mNamePopularLatin     = mDistrictName["NAMEPOPULARENGLISH"].ToString();
                                var mApproved             = mDistrictName["APPROVED"].ToString();
                                mDistrict.AddNewRow(mGeom, mDistrictObjectId, mDistrictAbbreviation, mNameArabic, mNameLatin, mNamePopularArabic, mNamePopularLatin, mApproved);
                            }
                            else
                            {
                                Debug.WriteLine("No matching abbreviation");
                            }
                        }
                        else
                        {
                            Debug.WriteLine("No abbreviation");
                        }
                    }
                    else
                    {
                        Debug.WriteLine("No geometry");
                    }
                }

                if (mFeatureIndex % (mTotal2 / 20) == 0)
                {
                    double mFraction = (double)mFeatureIndex / (double)mTotal2;
                    int    mProgress = (int)Math.Round(mFraction * 100);
                    pPgBar.ProgressBar.Value = mProgress;
                    Application.DoEvents();
                }
                mFeatureIndex++;
            }

            mDistrict.UpdateExtent();
            Debug.WriteLine("Done creating districts...");
            return(mDistrict);
        }