Exemplo n.º 1
0
 bool WriteData(VectorDataType data, out string dataGuid)
 {
     try
     {
         return(WriteData2Sql(data, out dataGuid));
     }
     catch
     {
         dataGuid = null;
         return(false);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 读数据
        /// </summary>
        /// <param name="dataGuid"></param>
        /// <param name="outFileFullPath">导出路径,不包括文件名</param>
        /// <returns></returns>
        VectorDataType ReadData(string dataGuid, string outFileFullPath)
        {
            //修改:输出文件目录不存在的时候创建目录
            //修改日期:20130911
            //修改人:XiongZK
            if (!Directory.Exists(outFileFullPath))
            {
                Directory.CreateDirectory(outFileFullPath);
            }

            string                tableName       = String.Empty;
            string                wktReference    = String.Empty;
            List <string>         geomList        = new List <string>();
            string                geoType         = String.Empty;
            List <AttributeModel> lstattr         = new List <AttributeModel>();//属性列
            List <AttributeObj>   lstAttributeObj = new List <AttributeObj>();

            try
            {
                GetTableName(dataGuid, ref tableName);
                if (string.IsNullOrEmpty(tableName))
                {
                    return(null);
                }
                GetRefAndGeoType(tableName, ref wktReference, ref geoType);
                GetAttributeColumn(tableName, ref lstattr);
                GetSpatialAttrbute(tableName, ref geomList);
                lstAttributeObj = GetAttribute(tableName, lstattr);

                string    outFileFullname = Path.Combine(outFileFullPath, tableName + ".shp");
                ShpWriter shpWriter       = new ShpWriter(outFileFullname);
                shpWriter.DoExport(lstattr, Utility.GetGeoTypeByString(geoType), geomList, lstAttributeObj, wktReference);

                VectorDataType vdt = new VectorDataType();
                vdt.DataGuid = dataGuid;
                vdt.Filename = outFileFullname;
                return(vdt);
            }
            catch (Exception ex)
            {
                throw new Exception("NpgsqlVectorDataService:ReadVector出错,错误原因:" + ex.Message);
            }
        }
Exemplo n.º 3
0
        public bool WriteData2Sql(VectorDataType data, out string dataGuid, out string tablename)
        {
            if (!File.Exists(data.Filename))
            {
                dataGuid  = null;
                tablename = null;
                return(false);
            }

            string    tableName = Path.GetFileNameWithoutExtension(data.Filename).ToUpper();
            string    filename  = Path.GetFileName(data.Filename);
            ShpReader shpReader = new ShpReader(data.Filename);

            // 检查矢量文件的有效性
            if (!shpReader.IsValidDataSource())
            {
                dataGuid  = null;
                tablename = null;
                return(false);
            }
            // 创建索引表
            //if (!IsTableNameExist(_connStr, _geometryColumnsTableName))
            //{
            //    CreateGeometryColumnsTable(_connStr);
            //}
            int srid = shpReader.GetSrid();

            try
            {
                using (NpgsqlConnection sqlConn = new NpgsqlConnection(_connStr))
                {
                    sqlConn.Open();
                    //建表
                    Dictionary <string, string> attr = shpReader.GetAttributeType();
                    List <string> shpAttr            = new List <string>();
                    Hashtable     hashTable          = new Hashtable();
                    foreach (KeyValuePair <string, string> item in attr)
                    {
                        shpAttr.Add(item.Key);
                        hashTable.Add(item.Key, item.Value);
                    }

                    if (IsTableNameExist(_connStr, tableName))
                    {
                        NpgsqlCommand NpgsqlCommand = new NpgsqlCommand(string.Format("drop table {0}", tableName.ToUpper()), sqlConn);
                        NpgsqlCommand.ExecuteNonQuery();
                    }

                    string sqlCommStr = String.Format("CREATE TABLE {0}(SID SERIAL primary key,", tableName);
                    for (int i = 0; i < shpAttr.Count; i++)
                    {
                        string attrName = shpAttr[i];
                        sqlCommStr += String.Format("{0} {1},", attrName, Utility.GdalTypeToNpSqlType(hashTable[attrName].ToString()));
                    }
                    sqlCommStr += String.Format("geom geometry)");
                    NpgsqlCommand sqlComm = new NpgsqlCommand(sqlCommStr, sqlConn);
                    sqlComm.ExecuteNonQuery();
                    //向表中添加数据
                    int pFeatureCount = shpReader.GetFeatureCount();

                    string        sqlStr   = String.Format("select Max(SID) from {0}", tableName);
                    NpgsqlCommand sqlC     = new NpgsqlCommand(sqlStr, sqlConn);
                    object        obj      = sqlC.ExecuteScalar();
                    int           maxValue = (obj is System.DBNull) ? 0 : Convert.ToInt32(obj);
                    int           maxNum   = maxValue + 1;
                    for (int i = 0; i < pFeatureCount; i++)
                    {
                        try
                        {
                            string sqlInsert = String.Format("insert into {0} values({1},", tableName, maxNum + i);
                            Dictionary <string, string> attr1 = shpReader.GetOneFeatureAttribute(i, shpAttr);
                            foreach (KeyValuePair <string, string> item in attr1)
                            {
                                if (hashTable[item.Key].ToString() == "OFTString")
                                {
                                    if (item.Value.Contains("'"))
                                    {
                                        string newValue = item.Value.Replace("'", "''");
                                        sqlInsert += String.Format("'{0}',", newValue);
                                    }
                                    else
                                    {
                                        sqlInsert += String.Format("'{0}',", item.Value);
                                    }
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(item.Value))
                                    {
                                        sqlInsert += String.Format("{0},", item.Value);
                                    }
                                    else
                                    {
                                        sqlInsert += String.Format("'{0}',", item.Value);
                                    }
                                }
                            }
                            sqlInsert          += String.Format("'{0}')", shpReader.GetOneFeatureGeomWkt(i));
                            sqlComm.CommandText = sqlInsert;
                            sqlComm.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            //NpgsqlEventLog.
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (NpgsqlConnection sqlConn = new NpgsqlConnection(_connStr))
                {
                    sqlConn.Open();
                    string        sqlCommStr = string.Format("drop table {0}", tableName.ToUpper());
                    NpgsqlCommand sqlComm    = new NpgsqlCommand(sqlCommStr, sqlConn);
                    sqlComm.ExecuteNonQuery();
                    sqlConn.Close();
                }
                throw new Exception("NpgsqlVectorDataService:WriteVector出错,错误原因:" + ex.Message);
            }

            //zhangheng 2014-06-24
            //如果索引表中已有记录,先删除.
            //DelIndexData(filename);

            //InsertIntoGeomColmn(_connStr, data.DataGuid, filename, tableName, shpReader.GetDeminsion(), shpReader.GetGeomType(), shpReader.GetSridWkt());

            dataGuid  = data.DataGuid;
            tablename = tableName;
            return(true);
        }
Exemplo n.º 4
0
        public bool WriteData2Sql(VectorDataType data, out string dataGuid)
        {
            string tableName = null;

            return(WriteData2Sql(data, out dataGuid, out tableName));
        }
Exemplo n.º 5
0
 bool WriteDataAndTile(VectorDataType data, out string dataGuid)
 {
     throw new NotImplementedException();
 }