コード例 #1
0
        private byte[] GetGeometry()
        {
            Geometry ogrGeometry = _currentFeature.GetGeometryRef();

            ogrGeometry.FlattenTo2D();
            Byte[] wkbGeometry = new Byte[ogrGeometry.WkbSize()];

            ogrGeometry.ExportToWkb(wkbGeometry, wkbByteOrder.wkbXDR);

            return(wkbGeometry);

            ////            System.Data.SqlTypes.SqlBytes x = new System.Data.SqlTypes.SqlBytes(wkbGeometry);
            ////            Microsoft.SqlServer.Types.SqlGeometry c = Microsoft.SqlServer.Types.SqlGeometry.STGeomFromWKB(x, 1111);
            ////             Microsoft.SqlServer.Types.SqlGeometry q = c.STExteriorRing();
            ////            MemoryStream ms = new MemoryStream( wkbGeometry );
            ////            BinaryReader rdr = new BinaryReader(ms);
            ////            Microsoft.SqlServer.Types.SqlGeometry g = new Microsoft.SqlServer.Types.SqlGeometry();
            ////            g.Read( rdr );
            //return geometry;
        }
コード例 #2
0
        /// <summary>
        /// Exports the selected database to fieldAttributes filegeodatabase
        /// </summary>
        /// <param name="progressBar">A progress bar object</param>
        /// <param name="logFunction">A log function</param>
        /// <param name="mdbFile">The outputShapefileName of an addressing database</param>
        /// <param name="approvedOnly">1 to include only approved, 0 to include all</param>
        /// <returns>Feature set</returns>
        public static FeatureSet GetRoadFeatureSetFromAdmAdrMdb(ref ToolStripProgressBar progressBar, Action <string, bool> logFunction, string mdbFile, int approvedOnly = 0)
        {
            // Setup SQL string to select all or only some streets
            string sqlStatement;

            if (approvedOnly == 1)
            {
                sqlStatement = "SELECT ADRROADID, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, ROADTYPE, ADRDISTRICTID, APPROVED FROM ADM_ADRROAD WHERE APPROVED = 1";
            }
            else
            {
                sqlStatement = "SELECT ADRROADID, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, ROADTYPE, ADRDISTRICTID, APPROVED FROM ADM_ADRROAD";
            }

            logFunction(sqlStatement, true);

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

            // Connect to database and load names
            using (var database = new Database(mdbFile))
            {
                var dataTable = database.Query(sqlStatement);
                progressBar.Value = 0;
                foreach (DataRow mRow in dataTable.Rows)
                {
                    int mRoadId;
                    if (int.TryParse(mRow["ADRROADID"].ToString(), out mRoadId))
                    {
                        roadNames.Add(mRoadId, mRow);
                    }
                }

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

                if (ogrDriver == null)
                {
                    return(null);
                }
                logFunction("Loaded driver", true);

                var ogrDataSource = ogrDriver.Open(mdbFile, 0);
                if (ogrDataSource == null)
                {
                    return(null);
                }
                logFunction("Loaded datasource", true);

                var roadSegmentLayer = ogrDataSource.GetLayerByName("ADRROADSEGMENT");
                if (roadSegmentLayer == null)
                {
                    logFunction("Could not load layer", true);
                    return(null);
                }
                logFunction("Loaded layer", true);

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

                int numberOfRoads = roadNames.Count();
                progressBar.Value = 0;
                var roadCounter    = 0;
                var segmentCounter = 0;

                foreach (var roadName in roadNames)
                {
                    roadCounter++;

                    int     roadIdentifier = roadName.Key;
                    DataRow roadData       = roadName.Value;

                    roadSegmentLayer.ResetReading();
                    roadSegmentLayer.SetAttributeFilter("ADRROADID=" + roadIdentifier);

                    OSGeo.OGR.Feature roadSegment;
                    var mWkbReader = new WkbReader();

                    while (null != (roadSegment = roadSegmentLayer.GetNextFeature()))
                    {
                        segmentCounter++;

                        OSGeo.OGR.Geometry mGeometry = roadSegment.GetGeometryRef();

                        if (mGeometry != null)
                        {
                            mGeometry.FlattenTo2D();

                            byte[] mWkb = new Byte[mGeometry.WkbSize()];

                            mGeometry.ExportToWkb(mWkb);

                            IMultiLineString multiLine;

                            IGeometry roadGeometry = mWkbReader.Read(mWkb);

                            if (roadGeometry.GetType() == typeof(DotSpatial.Topology.LineString))
                            {
                                var mLineStrings = new List <LineString>();
                                mLineStrings.Add(roadGeometry as LineString);
                                multiLine = new DotSpatial.Topology.MultiLineString(mLineStrings);
                            }
                            else
                            {
                                multiLine = roadGeometry as IMultiLineString;
                            }

                            int districtIdentifier, roadClass, roadApproved;
                            int.TryParse(roadData["ADRDISTRICTID"].ToString(), out districtIdentifier);
                            var nameArabic        = roadData["NAMEARABIC"].ToString();
                            var nameLatin         = roadData["NAMEENGLISH"].ToString();
                            var namePopularArabic = roadData["NAMEPOPULARARABIC"].ToString();
                            var namePopularLatin  = roadData["NAMEPOPULARENGLISH"].ToString();
                            int.TryParse(roadData["ROADTYPE"].ToString(), out roadClass);
                            int.TryParse(roadData["APPROVED"].ToString(), out roadApproved);

                            simplifiedRoadsFeatureSet.AddNewRow(
                                roadGeometry,
                                districtIdentifier,
                                roadIdentifier,
                                nameArabic,
                                nameLatin,
                                namePopularArabic,
                                namePopularLatin,
                                roadClass,
                                roadApproved);
                        }
                        else
                        {
                            logFunction("No geometry", true);
                        }

                        if (roadCounter % 100 == 0 || roadCounter == numberOfRoads)
                        {
                            progressBar.ProgressBar.Value = (int)Math.Round((double)(roadCounter * 100 / numberOfRoads));
                            Application.DoEvents();
                        }
                    }
                }

                simplifiedRoadsFeatureSet.UpdateExtent();
                logFunction("Done creating featureset", true);
                return(simplifiedRoadsFeatureSet);
            }
        }
コード例 #3
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);
        }