예제 #1
0
 public void Write(IFeatureClassReader reader, IProgressTracker tracker, string tableName, string displayName, string description)
 {
     if (tracker != null)
     {
         tracker.StartTracking("正在读取要素...", 100);
         tracker.Tracking("正在读取要素...", 30);
     }
     Feature[] features = reader.Read(tracker);
     if (tracker != null)
     {
         tracker.Tracking("读取要素结束。", 100);
     }
     //
     try
     {
         using (IDbCommand cmd = _dbConn.CreateCommand())
         {
             TransactionManager.BeginTransaction(_dbConn, cmd);
             //Create FeatureClass Table
             string[] fields = GetFieldTypes(reader.FieldNames, features);
             _adapter.CreateTable(_dbConn, true, "OID", "SHAPE", tableName, fields, cmd);
             //Create Annotation Table
             string[] annFields = null;
             if (reader.AnnoTable != null)
             {
                 annFields = GetAnnoTableFields();
                 _adapter.CreateTable(_dbConn, true, _adapter.GetAnnonKeyField(), "SHAPE", reader.AnnoTable, annFields, cmd);
             }
             //Insert Row Into BGIS_FeatureClass
             int span = Math.Min(100, features.Length);
             if (tracker != null)
             {
                 tracker.StartTracking("正在导入要素集\"" + displayName + "\"...", features.Length / span + 1);
             }
             //
             InsertFeatureClassRow(cmd, tableName, displayName, description,
                                   (reader.SpatialReference != null ? reader.SpatialReference.ToString() : string.Empty),
                                   0,
                                   string.Empty,
                                   reader.AnnoTable,
                                   reader.Envelope,
                                   reader.ShapeType,
                                   features.Length
                                   );
             //Insert Features
             InsertFeatures(cmd, features, fields, tableName, tracker, span, reader.AnnoTable, annFields);
             //Submit
             TransactionManager.Commit();
         }
     }
     catch (Exception ex)
     {
         TransactionManager.Rollback();
         throw ex;
     }
 }
예제 #2
0
        protected virtual void CreateTables(IDbConnection connection, SystemTable table, IDbCommand cmd)
        {
            if (table.Name == null)
            {
                throw new ArgumentNullException("元数据表名称为空。");
            }
            if (table.Fields == null)
            {
                throw new ArgumentNullException("元数据表\"" + table.Name + "\"没有定义字段。");
            }
            List <string> fieldTypes = new List <string>();

            foreach (SystemField fld in table.Fields)
            {
                fieldTypes.Add(fld.Name + " " + _adapter.GetFieldTypeString(fld.FieldType));
            }
            _adapter.CreateTable(connection, false, "ID", null, table.Name, fieldTypes.ToArray(), cmd);
        }
예제 #3
0
        private void Import(MetaFeatureClassDef fetclassdef, string[] fields, Feature[] features, IDbConnection dbConnection, IProgressTracker tracker)
        {
            IDbTransaction tran = dbConnection.BeginTransaction();

            try
            {
                using (IDbCommand cmd = dbConnection.CreateCommand())
                {
                    cmd.Connection  = dbConnection;
                    cmd.Transaction = tran;
                    int span = Math.Min(100, features.Length);
                    if (tracker != null)
                    {
                        tracker.StartTracking("正在导入要素集\"" + fetclassdef.DataTable + "\"...", features.Length / span + 1);
                    }
                    //Create Table
                    if (tracker != null)
                    {
                        tracker.Tracking("正在创建数据表\"" + fetclassdef.DataTable + "\"...", 0);
                    }
                    string[] fieldTypes = GetFieldTypes(fields, features);
                    _adapter.CreateTable(dbConnection, true,
                                         "OID", "SHAPE",
                                         fetclassdef.DataTable,
                                         fieldTypes, cmd);
                    //Insert row to BGIS_FeatureClass
                    InsertFeatureClassRow(cmd, fetclassdef);
                    //Insert features
                    string flds = string.Empty;
                    for (int i = 0; i < fieldTypes.Length; i++)
                    {
                        flds += (fieldTypes[i].Split(' ')[0] + ",");
                    }
                    flds = flds.Substring(0, flds.Length - 1);
                    int ret = 0;
                    int idx = -1;
                    foreach (Feature fet in features)
                    {
                        idx++;
                        int n = (idx / span);
                        if (idx % span == 0)
                        {
                            tracker.Tracking("正在导入要素(" + (n * span).ToString() + "/" + features.Length.ToString() + ")...", n);
                        }
                        if (fet.Geometry == null)
                        {
                            continue;
                        }
                        string sql = "insert into " + fetclassdef.DataTable + "(" + flds + ") values(" +
                                     fet.OID.ToString() + ",";
                        for (int i = 1; i < fieldTypes.Length - 1; i++)
                        {
                            sql += ("'" + fet.GetFieldValue(i - 1) + "',");
                        }
                        sql            += ("GeomFromText('" + fet.Geometry.ToWkt() + "'))");
                        cmd.CommandText = sql;
                        ret             = cmd.ExecuteNonQuery();
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw ex;
            }
        }