You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
826 lines
35 KiB
826 lines
35 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Data.SQLite; |
|
using System.IO; |
|
using System.Windows.Forms; |
|
using System.Data; |
|
using System.Threading; |
|
using Kingo.Plugin.ShapeToKOApp.Class; |
|
using Kingo.Plugin.ShapeToKOApp.XSDClass; |
|
using Kingo.Plugin.ShapeToKOApp.Helper; |
|
using Kingo.Plugin.ShapeToKOApp.Enum; |
|
|
|
namespace Kingo.Plugin.ShapeToKOApp.KoDataBase |
|
{ |
|
public class KODBFeatureLayer : IDisposable |
|
{ |
|
public int LayerIndex { get; set; } |
|
public object Tag { get; set; } |
|
public event MessageReport Report; |
|
public ISpatialDataSource SpatialDataSource { get; set; } |
|
public string FeatureLayerName { get; set; } |
|
public List<ShapeFieldInfo> FieldInfos { get; set; } |
|
public string OutputFileName { get; set; } |
|
public int WKID { get; set; } |
|
public bool EncryptData { get; set; } |
|
public int MinScale { get; set; } |
|
public int MaxScale { get; set; } |
|
public string DisplayField { get; set; } |
|
public string LayerLabelName { get; set; } |
|
public DEFeatureClassInfo DEFeatureClassInfo { get; set; } |
|
public ItemInfo ItemInfo { get; set; } |
|
public Renderer Render { get; set; } |
|
public Renderer ItemInfoRender { get; set; } |
|
public AdvancedDrawingInfo AdvDrawingInfo { get; set; } |
|
public LabelingInfo LabelingInfo { get; set; } |
|
public Symbol FeatureSymbol { get; set; } |
|
|
|
private Thread doWorkThread = null; |
|
|
|
public KODBFeatureLayer() |
|
{ |
|
FeatureLayerName = "KOFDT"; |
|
OutputFileName = string.Format("{0}\\{1}.ko", Application.StartupPath, FeatureLayerName); |
|
} |
|
public void CreateLayerUdp() |
|
{ |
|
|
|
int wkid = this.WKID; |
|
|
|
if (!System.IO.File.Exists(OutputFileName)) |
|
{ |
|
if (FeatureSymbol is XSDClass.FSymbol) |
|
{ |
|
if (wkid == 0) |
|
{ |
|
//临时处理宁波独立坐标系 |
|
File.WriteAllBytes(OutputFileName, Properties.Resources.TemplateNB); |
|
} |
|
else |
|
{ |
|
File.WriteAllBytes(OutputFileName, Properties.Resources.Template); |
|
} |
|
} |
|
else if (FeatureSymbol is XSDClass.MSymbol) |
|
{ |
|
File.WriteAllBytes(OutputFileName, Properties.Resources.TemplatePoint); |
|
} |
|
else if (FeatureSymbol is XSDClass.LSymbol) |
|
{ |
|
File.WriteAllBytes(OutputFileName, Properties.Resources.TemplateLine); |
|
} |
|
} |
|
|
|
SQLiteConnection sqlConnection = new SQLiteConnection(@"Data Source=" + OutputFileName); |
|
sqlConnection.Open(); |
|
try |
|
{ |
|
|
|
string dllPath = Application.StartupPath + "\\stgeometry_sqlite.dll"; |
|
if (!File.Exists(dllPath)) |
|
{ |
|
dllPath = Environment.SystemDirectory + "\\stgeometry_sqlite.dll"; |
|
} |
|
if (!File.Exists(dllPath)) |
|
{ |
|
OnReport(string.Format("未能加载sql扩展程序集,请检查软件运行目录下是否存在'{0}'!", "stgeometry_sqlite.dll"), -1); |
|
return; |
|
} |
|
string sload = string.Format("SELECT load_extension('{0}','SDE_SQL_funcs_init')", dllPath); |
|
LoadSqlliteExtension(sqlConnection, dllPath); |
|
SQLiteCommand cmdload = new SQLiteCommand(sqlConnection); |
|
SQLiteTransaction pTran = sqlConnection.BeginTransaction(); |
|
try |
|
{ |
|
|
|
int nSrid = 0; |
|
XSDClass.ProjectedCoordinateSystem spatialReference = null; |
|
if (wkid == 0) |
|
{ |
|
nSrid = 300001; |
|
} |
|
else |
|
{ |
|
//改变空间参考 |
|
nSrid = ChangeSpatialRef(sqlConnection, wkid, out spatialReference); |
|
|
|
//修改GDB_Items的Definition字段 |
|
UpdateGDB_Items_Definition(sqlConnection, spatialReference); |
|
} |
|
|
|
//添加字段 |
|
AddFields(sqlConnection); |
|
//修改GDB_ServiceItems的ItemInfo字段 |
|
UpdateGDB_ServiceItems_ItemInfo(sqlConnection, wkid); |
|
//修改GDB_ServiceItems的AdvancedDrawingInfo字段 |
|
UpdateGDB_ServiceItems_AdvancedDrawingInfo(sqlConnection); |
|
//清除原有数据 |
|
cmdload.CommandText = String.Format("delete from {0}", FeatureLayerName); |
|
int resturt = cmdload.ExecuteNonQuery(); |
|
OnReport("更新元数据完成"); |
|
//开始插入数据 |
|
InsertDataRecords(sqlConnection, nSrid); |
|
pTran.Commit(); |
|
if (this.EncryptData) |
|
{ |
|
EncryptTable(); |
|
} |
|
if (sqlConnection != null) |
|
{ |
|
sqlConnection.Close(); |
|
sqlConnection.Dispose(); |
|
sqlConnection = null; |
|
} |
|
OnReport(string.Format("插入数据完成!")); |
|
} |
|
catch (Exception ex) |
|
{ |
|
pTran.Rollback(); |
|
OnReport(string.Format("制作失败!原因:{0}", ex.Message)); |
|
} |
|
finally |
|
{ |
|
|
|
pTran.Dispose(); |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
OnReport(string.Format("制作失败!原因:{0}", ex.Message)); |
|
} |
|
finally |
|
{ |
|
if (sqlConnection != null) |
|
{ |
|
sqlConnection.Close(); |
|
sqlConnection.Dispose(); |
|
sqlConnection = null; |
|
} |
|
GC.Collect(); |
|
} |
|
|
|
} |
|
|
|
private void LoadSqlliteExtension(SQLiteConnection connectiont, string path) |
|
{ |
|
try |
|
{ |
|
string extensionPath = path; |
|
connectiont.LoadExtension(extensionPath, "SDE_SQL_funcs_init"); |
|
} |
|
catch (Exception ex) |
|
{ |
|
throw ex; |
|
} |
|
} |
|
|
|
|
|
#region 改变空间参考 |
|
private int ChangeSpatialRef(SQLiteConnection conn, int srid, out XSDClass.ProjectedCoordinateSystem spatialReference) |
|
{ |
|
try |
|
{ |
|
int newSrid = InsertSpatialReference(srid, conn, out spatialReference); |
|
|
|
string sqlUpdateGDB_Layers = string.Format("update GDB_Layers set minx={0},miny={1},maxx={2},maxy={3},srid={4} where table_name='{5}'", |
|
SpatialDataSource.Extent.XMin, SpatialDataSource.Extent.YMin, SpatialDataSource.Extent.XMax, SpatialDataSource.Extent.YMax, |
|
newSrid, FeatureLayerName |
|
); |
|
SQLiteCommand cmd = new SQLiteCommand(sqlUpdateGDB_Layers, conn); |
|
int result = cmd.ExecuteNonQuery(); |
|
return newSrid; |
|
} |
|
catch (Exception ex) |
|
{ |
|
OnReport(string.Format("改变空间参考失败!原因:{0}", ex), -1); |
|
spatialReference = null; |
|
return -1; |
|
} |
|
} |
|
|
|
private int InsertSpatialReference(int srid, SQLiteConnection conn, out XSDClass.ProjectedCoordinateSystem spatialReference) |
|
{ |
|
try |
|
{ |
|
string sqlSelect = "SELECT * FROM st_aux_spatial_reference_systems where auth_srid=" + srid.ToString(); |
|
SQLiteCommand cmd = new SQLiteCommand(sqlSelect, conn); |
|
SQLiteDataReader objReader = cmd.ExecuteReader(); |
|
if (objReader.HasRows) |
|
{ |
|
objReader.Read(); |
|
spatialReference = new XSDClass.ProjectedCoordinateSystem() |
|
{ |
|
WKID = Convert.ToInt32(objReader.GetValue(2)), |
|
LatestWKID = Convert.ToInt32(objReader.GetValue(2)), |
|
WKT = objReader.GetValue(3).ToString(), |
|
XOrigin = Convert.ToDouble(objReader.GetValue(4)), |
|
YOrigin = Convert.ToDouble(objReader.GetValue(5)), |
|
XYScale = Convert.ToDouble(objReader.GetValue(6)), |
|
ZOrigin = Convert.ToDouble(objReader.GetValue(7)), |
|
ZScale = Convert.ToDouble(objReader.GetValue(8)), |
|
MOrigin = Convert.ToDouble(objReader.GetValue(9)), |
|
MScale = Convert.ToDouble(objReader.GetValue(10)), |
|
XYTolerance = Convert.ToDouble(objReader.GetValue(11)), |
|
ZTolerance = Convert.ToDouble(objReader.GetValue(12)), |
|
MTolerance = Convert.ToDouble(objReader.GetValue(13)), |
|
HighPrecision = Convert.ToBoolean(Convert.ToInt32(objReader.GetValue(14))) |
|
}; |
|
spatialReference.WKT = string.Format("{0},AUTHORITY[\"EPSG\",{1}]]", spatialReference.WKT.Remove(spatialReference.WKT.Length - 1), spatialReference.WKID); |
|
int nSrid = Convert.ToInt32(objReader.GetValue(0)); |
|
objReader.Close(); |
|
return nSrid; |
|
} |
|
else |
|
{ |
|
objReader.Close(); |
|
int currentsrid = GetSeqIDInSqlite_Sequence(conn, "st_aux_spatial_reference_systems"); |
|
string selectSql = string.Format("SELECT * FROM st_spatial_reference_systems where auth_srid={0}", srid); |
|
cmd.CommandText = selectSql; |
|
SQLiteDataReader reader = cmd.ExecuteReader(); |
|
reader.Read(); |
|
spatialReference = new XSDClass.ProjectedCoordinateSystem() |
|
{ |
|
WKID = Convert.ToInt32(reader.GetValue(2)), |
|
LatestWKID = Convert.ToInt32(reader.GetValue(2)), |
|
WKT = reader.GetValue(3).ToString(), |
|
XOrigin = Convert.ToDouble(reader.GetValue(4)), |
|
YOrigin = Convert.ToDouble(reader.GetValue(5)), |
|
XYScale = Convert.ToDouble(reader.GetValue(6)), |
|
ZOrigin = Convert.ToDouble(reader.GetValue(7)), |
|
ZScale = Convert.ToDouble(reader.GetValue(8)), |
|
MOrigin = Convert.ToDouble(reader.GetValue(9)), |
|
MScale = Convert.ToDouble(reader.GetValue(10)), |
|
XYTolerance = Convert.ToDouble(reader.GetValue(11)), |
|
ZTolerance = Convert.ToDouble(reader.GetValue(12)), |
|
MTolerance = Convert.ToDouble(reader.GetValue(13)), |
|
HighPrecision = Convert.ToBoolean(Convert.ToInt32(reader.GetValue(14))) |
|
}; |
|
spatialReference.WKT = string.Format("{0},AUTHORITY[\"EPSG\",{1}]]", spatialReference.WKT.Remove(spatialReference.WKT.Length - 1), spatialReference.WKID); |
|
//插入数据 |
|
string insertSql = string.Format("insert into st_aux_spatial_reference_systems values({0},'{1}',{2},'{3}',{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14})", |
|
currentsrid, |
|
reader.GetValue(1), |
|
reader.GetValue(2), |
|
reader.GetValue(3), |
|
reader.GetValue(4), |
|
reader.GetValue(5), |
|
reader.GetValue(6), |
|
reader.GetValue(7), |
|
reader.GetValue(8), |
|
reader.GetValue(9), |
|
reader.GetValue(10), |
|
reader.GetValue(11), |
|
reader.GetValue(12), |
|
reader.GetValue(13), |
|
reader.GetValue(14) |
|
); |
|
reader.Close(); |
|
reader.Dispose(); |
|
cmd.CommandText = insertSql; |
|
int resturt = cmd.ExecuteNonQuery(); |
|
return currentsrid; |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
OnReport(string.Format("插入空间参考失败!原因:{0}", ex), -1); |
|
spatialReference = null; |
|
return -1; |
|
} |
|
} |
|
|
|
private int GetSeqIDInSqlite_Sequence(SQLiteConnection conn, string tablename) |
|
{ |
|
SQLiteCommand cmd = new SQLiteCommand(conn); |
|
//sqlite_sequence |
|
string selectseq = string.Format(@"SELECT seq FROM sqlite_sequence where name='{0}'", tablename); |
|
cmd.CommandText = selectseq; |
|
int currentseq = Convert.ToInt32(cmd.ExecuteScalar()); |
|
currentseq++; |
|
string updateseq = string.Format(@"update sqlite_sequence set seq ={0} where name='{1}'", currentseq, tablename); |
|
cmd.CommandText = updateseq; |
|
int resturt = cmd.ExecuteNonQuery(); |
|
return currentseq; |
|
} |
|
#endregion |
|
|
|
#region 修改GDB_Items的Definition字段 |
|
private void UpdateGDB_Items_Definition(SQLiteConnection conn, XSDClass.ProjectedCoordinateSystem spatialReference) |
|
{ |
|
//Definition |
|
SQLiteCommand cmd = new SQLiteCommand(conn); |
|
string sql1 = string.Format("select Definition from GDB_Items where name='main.{0}'", FeatureLayerName); |
|
cmd.CommandText = sql1; |
|
object obj1 = cmd.ExecuteScalar(); |
|
XSDClass.DEFeatureClassInfo pDEFeatureClassInfo = XSDClass.DEFeatureClassInfo.FromXml(obj1.ToString()); |
|
//增加字段 |
|
foreach (var item in this.FieldInfos) |
|
{ |
|
if (item.FieldName.ToLower() == "objectid") |
|
continue; |
|
pDEFeatureClassInfo.GPFieldInfoExs.Add(new XSDClass.GPFieldInfoEx() |
|
{ |
|
Name = item.FieldName, |
|
ModelName = item.FieldName, |
|
FieldType = XSDClass.CommonMethod.ConvertFromShpFieldType(item), |
|
IsNullable = true, |
|
AliasName = item.FieldAlias |
|
}); |
|
} |
|
pDEFeatureClassInfo.SpatialReference = spatialReference; |
|
pDEFeatureClassInfo.Extent.XMax = SpatialDataSource.Extent.XMax; |
|
pDEFeatureClassInfo.Extent.XMin = SpatialDataSource.Extent.XMin; |
|
pDEFeatureClassInfo.Extent.YMax = SpatialDataSource.Extent.YMax; |
|
pDEFeatureClassInfo.Extent.YMin = SpatialDataSource.Extent.YMin; |
|
pDEFeatureClassInfo.Extent.SpatialReference = spatialReference; |
|
string xml1 = pDEFeatureClassInfo.ToXml(); |
|
sql1 = string.Format("update GDB_Items set Definition='{0}' where name='main.{1}'", xml1, FeatureLayerName); |
|
cmd.CommandText = sql1; |
|
int resturt = cmd.ExecuteNonQuery(); |
|
} |
|
#endregion |
|
|
|
#region 添加字段 |
|
private void AddFields(SQLiteConnection conn) |
|
{ |
|
try |
|
{ |
|
SQLiteCommand cmd = new SQLiteCommand(conn); |
|
StringBuilder sqlAlterTable = new StringBuilder(); |
|
foreach (var item in FieldInfos) |
|
{ |
|
if (item.FieldName.ToLower() == "objectid") |
|
continue; |
|
sqlAlterTable.AppendFormat("ALTER TABLE KOFDT ADD COLUMN {0} {1};", item.FieldName, XSDClass.CommonMethod.GetCreateSqlTypeFormShapeField(item)); |
|
int sdeType = 5; |
|
string column_size = "0"; |
|
int object_flags = 0; |
|
//string object_id = ""; |
|
string decimal_digits = ""; |
|
switch (item.FieldType) |
|
{ |
|
case eFieldType.shpBoolean: |
|
sdeType = 1; |
|
break; |
|
case eFieldType.shpInteger: |
|
sdeType = 2; |
|
break; |
|
case eFieldType.shpLong: |
|
sdeType = 11; |
|
break; |
|
case eFieldType.shpDate: |
|
sdeType = 7; |
|
object_flags = 4; |
|
break; |
|
case eFieldType.shpNumeric: |
|
if (item.FieldDecimal == 0) |
|
{ |
|
sdeType = 2; |
|
} |
|
else |
|
{ |
|
sdeType = 4; |
|
decimal_digits = "0"; |
|
} |
|
break; |
|
case eFieldType.shpSingle: |
|
case eFieldType.shpFloat: |
|
case eFieldType.shpDouble: |
|
sdeType = 4; |
|
decimal_digits = "0"; |
|
break; |
|
case eFieldType.shpText: |
|
sdeType = 5; |
|
column_size = item.FieldSize.ToString(); |
|
break; |
|
case eFieldType.shpMemo: |
|
sdeType = 13; |
|
break; |
|
default: |
|
sdeType = 5; |
|
column_size = item.FieldSize.ToString(); |
|
break; |
|
} |
|
sqlAlterTable.AppendFormat("insert into GDB_ColumnRegistry values('{0}','{1}',{2},'{3}','{4}','',{5},'');", |
|
FeatureLayerName, |
|
item.FieldName, |
|
sdeType, |
|
column_size, |
|
decimal_digits, |
|
object_flags |
|
); |
|
} |
|
cmd.CommandText = sqlAlterTable.ToString(); |
|
int result = cmd.ExecuteNonQuery(); |
|
} |
|
catch (Exception ex) |
|
{ |
|
OnReport(string.Format("字段添加失败!原因:{0}", ex), -1); |
|
} |
|
|
|
} |
|
#endregion |
|
|
|
#region 修改GDB_ServiceItems的ItemInfo字段 |
|
private void UpdateGDB_ServiceItems_ItemInfo(SQLiteConnection conn, int srid) |
|
{ |
|
try |
|
{ |
|
SQLiteCommand cmd = new SQLiteCommand(conn); |
|
string sql2 = string.Format("select ItemInfo from GDB_ServiceItems where datasetname='{0}'", FeatureLayerName); |
|
cmd.CommandText = sql2; |
|
object obj2 = cmd.ExecuteScalar(); |
|
XSDClass.ItemInfo pItemInfo = XSDClass.ItemInfo.FromJson(obj2.ToString()); |
|
foreach (ShapeFieldInfo item in FieldInfos) |
|
{ |
|
if (item.FieldName.ToLower() == "objectid") |
|
continue; |
|
XSDClass.esriFieldType fieldType = XSDClass.CommonMethod.ConvertFromShpFieldType(item); |
|
Field field = null; |
|
if (fieldType == XSDClass.esriFieldType.esriFieldTypeString) |
|
{ |
|
field = new XSDClass.FieldWithLength() |
|
{ |
|
Length = item.FieldSize |
|
}; |
|
} |
|
else |
|
{ |
|
field = new XSDClass.FieldNomal(); |
|
} |
|
field.Alias = item.FieldAlias; |
|
field.Editable = true; |
|
field.Name = item.FieldName; |
|
field.Nullable = true; |
|
field.Type = fieldType; |
|
if (pItemInfo.Fields.Find(p => p.Name == item.FieldName) == null) |
|
pItemInfo.Fields.Add(field); |
|
} |
|
pItemInfo.Extent.Xmax = this.SpatialDataSource.Extent.XMax; |
|
pItemInfo.Extent.Xmin = this.SpatialDataSource.Extent.XMin; |
|
pItemInfo.Extent.Ymax = this.SpatialDataSource.Extent.YMax; |
|
pItemInfo.Extent.Ymin = this.SpatialDataSource.Extent.YMin; |
|
pItemInfo.Extent.SpatialReference = new XSDClass.SpatialReference() |
|
{ |
|
LatestWkid = srid, |
|
Wkid = srid, |
|
wkt = pItemInfo.Extent.SpatialReference.wkt |
|
}; |
|
pItemInfo.MaxScale = this.MaxScale; |
|
pItemInfo.MinScale = this.MinScale; |
|
if (LabelingInfo != null) |
|
{ |
|
pItemInfo.DrawingInfo.LabelingInfo = new List<XSDClass.LabelingInfo>(); |
|
pItemInfo.DrawingInfo.LabelingInfo.Add(LabelingInfo); |
|
} |
|
//设置显示字段 |
|
pItemInfo.DisplayField = this.DisplayField; |
|
//pItemInfo.DrawingInfo |
|
//如果是唯一值渲染 则还要赋值ItemInfo中的types字段和typeIdField字段 |
|
if (this.Render is XSDClass.UniqueValueRenderer) |
|
{ |
|
XSDClass.UniqueValueRenderer pUniqueValueRenderer = this.Render as XSDClass.UniqueValueRenderer; |
|
pItemInfo.TypeIdField = pUniqueValueRenderer.Field1; |
|
if (this.EncryptData) |
|
{ |
|
//加密唯一值填充的的code值,如果当前字段是字符串 |
|
//pUniqueValueRenderer.Field1 |
|
cmd.CommandText = string.Format("select {0} from {1} where 1=0", pItemInfo.TypeIdField, this.FeatureLayerName); |
|
using (SQLiteDataAdapter ada = new SQLiteDataAdapter(cmd)) |
|
{ |
|
DataSet ds = new DataSet(); |
|
ada.Fill(ds); |
|
if (ds != null && ds.Tables.Count > 0) |
|
{ |
|
if (ds.Tables[0].Columns[0].DataType.Equals(typeof(string))) |
|
{ |
|
//加密字符串 |
|
foreach (var item in pUniqueValueRenderer.UniqueValueInfos) |
|
{ |
|
item.Value = this.EnCode(item.Value); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
//设置iteminfo的DrawingInfo样式 |
|
pItemInfo.DrawingInfo.Renderer = this.ItemInfoRender; |
|
XSDClass.CommonMethod.BuildItemInfoTypes(pItemInfo); |
|
string json2 = pItemInfo.ToJson(); |
|
sql2 = string.Format("update GDB_ServiceItems set ItemInfo='{0}' where datasetname='{1}'", json2, FeatureLayerName); |
|
cmd.CommandText = sql2; |
|
int result = cmd.ExecuteNonQuery(); |
|
} |
|
catch (Exception ex) |
|
{ |
|
OnReport(string.Format("修改GDB_ServiceItems的ItemInfo字段失败!原因:{0}", ex), -1); |
|
} |
|
} |
|
#endregion |
|
|
|
|
|
#region 修改GDB_ServiceItems的AdvancedDrawingInfo字段 |
|
private void UpdateGDB_ServiceItems_AdvancedDrawingInfo(SQLiteConnection conn) |
|
{ |
|
try |
|
{ |
|
SQLiteCommand cmd = new SQLiteCommand(conn); |
|
AdvancedDrawimgInfoManager ad = new AdvancedDrawimgInfoManager(); |
|
string json3 = ad.GetRenderer(this.Render, LabelingInfo); |
|
string sql3 = string.Format("update GDB_ServiceItems set AdvancedDrawingInfo='{0}' where datasetname='{1}'", json3, FeatureLayerName); |
|
cmd.CommandText = sql3; |
|
int resturt = cmd.ExecuteNonQuery(); |
|
} |
|
catch (Exception ex) |
|
{ |
|
OnReport(string.Format("修改GDB_ServiceItems的AdvancedDrawingInfo字段失败!原因:{0}", ex), -1); |
|
} |
|
} |
|
#endregion |
|
|
|
#region 插入KO数据 |
|
|
|
private void InsertDataRecords(SQLiteConnection conn, int srid) |
|
{ |
|
SQLiteCommand cmd = new SQLiteCommand(conn); |
|
SpatialDataSource.Open(); |
|
try |
|
{ |
|
List<ShapeFieldInfo> fieldInfoList = FieldInfos.FindAll(x => x.FieldName == "TBMJ"); |
|
if (fieldInfoList == null || fieldInfoList.Count == 0) |
|
{ |
|
string sql = string.Format("ALTER TABLE KOFDT ADD COLUMN {0} {1};", "TBMJ", "float64"); |
|
cmd.CommandText = sql; |
|
int result= cmd.ExecuteNonQuery(); |
|
|
|
ShapeFieldInfo pShapeFieldInfo = new ShapeFieldInfo(); |
|
pShapeFieldInfo.FieldName = "TBMJ"; |
|
pShapeFieldInfo.FieldAlias = "图斑面积"; |
|
pShapeFieldInfo.FieldType = eFieldType.shpDouble; |
|
FieldInfos.Add(pShapeFieldInfo); |
|
} |
|
|
|
int objectid = 1; |
|
while (!SpatialDataSource.EOF) |
|
{ |
|
try |
|
{ |
|
InsertOneRecord(conn, cmd, objectid, srid); |
|
if (objectid % 10 == 0) |
|
OnReport(string.Format("已处理{0}条记录!", objectid), objectid); |
|
objectid++; |
|
Thread.Sleep(1000); |
|
} |
|
catch (Exception ex) |
|
{ |
|
Console.WriteLine(ex.Message); |
|
} |
|
} |
|
} |
|
finally |
|
{ |
|
SpatialDataSource.Close(); |
|
} |
|
} |
|
|
|
private void InsertOneRecord(SQLiteConnection conn, SQLiteCommand cmd, int objectid, int srid) |
|
{ |
|
DataRow dr = SpatialDataSource.GetNextRecord(); |
|
if (dr == null) return; |
|
cmd.CommandText = CreateInsertSql(objectid, srid, dr); |
|
int resturt = cmd.ExecuteNonQuery(); |
|
} |
|
|
|
private string CreateInsertSql(int objectid, int srid, DataRow dr) |
|
{ |
|
StringBuilder sbField = new StringBuilder(); |
|
sbField.AppendFormat("insert into {0} (objectid,KReserved,shape", FeatureLayerName); |
|
StringBuilder sbValue = new StringBuilder(); |
|
if (FeatureSymbol is XSDClass.FSymbol)//面 |
|
{ |
|
if (dr["shape"].ToString().StartsWith("POLYGON")) |
|
{ |
|
sbValue.AppendFormat(" values({0},0,st_polygon ('{1}',{2})", objectid, dr["shape"], srid); |
|
} |
|
else |
|
{ |
|
sbValue.AppendFormat(" values({0},0,st_multipolygon ('{1}',{2})", objectid, dr["shape"], srid); |
|
} |
|
} |
|
else if (FeatureSymbol is XSDClass.MSymbol)//点 |
|
{ |
|
sbValue.AppendFormat(" values({0},0,st_point ('{1}',{2})", objectid, dr["shape"], srid); |
|
} |
|
else if (FeatureSymbol is XSDClass.LSymbol) |
|
{ |
|
sbValue.AppendFormat(" values({0},0,st_multilinestring ('{1}',{2})", objectid, dr["shape"], srid); |
|
} |
|
foreach (ShapeFieldInfo item in FieldInfos) |
|
{ |
|
if (item.FieldName.ToLower() == "objectid" |
|
|| item.FieldName.ToLower() == "shape") |
|
continue; |
|
sbField.AppendFormat(",{0}", item.FieldName); |
|
object value = null; |
|
if (dr.Table.Columns.Contains(item.FieldName)) |
|
value = dr[item.FieldName]; |
|
if (item.FieldName == "TBMJ" && value != null && string.IsNullOrEmpty(value.ToString())) |
|
{ |
|
try |
|
{ |
|
OSGeo.OGR.Geometry geo = OSGeo.OGR.Geometry.CreateFromWkt(dr["Shape"].ToString()); |
|
value = geo.GetArea(); |
|
} |
|
catch |
|
{ } |
|
} |
|
|
|
if (value != null && value.ToString().Trim() != string.Empty) |
|
{ |
|
if (item.FieldType == eFieldType.shpDate) |
|
{ |
|
sbValue.AppendFormat(",{0}", ((DateTime)value).Subtract(new DateTime(1900, 1, 1)).TotalDays + 2415020.5); |
|
} |
|
else |
|
{ |
|
|
|
sbValue.AppendFormat(",'{0}'", value); |
|
} |
|
} |
|
else |
|
{ |
|
sbValue.Append(",null"); |
|
} |
|
} |
|
sbField.Append(")"); |
|
sbValue.Append(")"); |
|
return sbField.Append(sbValue).ToString(); |
|
} |
|
#endregion |
|
|
|
|
|
private void OnReport(string msg, int count = 0) |
|
{ |
|
if (Report != null) |
|
Report(msg, count); |
|
} |
|
|
|
private void EncryptTable() |
|
{ |
|
string sConnStringSQLite = string.Format("Data Source={0}", OutputFileName, FeatureLayerName); |
|
string item = FeatureLayerName; |
|
try |
|
{ |
|
OnReport("开始加密表:" + item); |
|
string sql = string.Format("select * from {0} where 1=0", item); |
|
DataSet ds = SQLiteHelper.ExecuteDataSet(sConnStringSQLite, sql, CommandType.Text); |
|
if (ds != null && ds.Tables.Count > 0) |
|
{ |
|
DataTable dt = ds.Tables[0]; |
|
StringBuilder sqlSelect = new StringBuilder("select rowid"); |
|
StringBuilder sqlUpdata = new StringBuilder(); |
|
sqlUpdata.AppendFormat("update {0} set ", item); |
|
int nOtherFields = 0; |
|
foreach (DataColumn dc in dt.Columns) |
|
{ |
|
if (dc.DataType.Equals(typeof(string))) |
|
{ |
|
sqlSelect.AppendFormat(",{0}", dc.ColumnName); |
|
sqlUpdata.AppendFormat("{0}=?,", dc.ColumnName); |
|
nOtherFields++; |
|
} |
|
} |
|
if (nOtherFields == 0) |
|
return; |
|
sqlSelect.AppendFormat(" from {0}", item); |
|
sqlUpdata.Replace(",", " where rowid=?", sqlUpdata.Length - 1, 1); |
|
//获取所有数据集 |
|
DataSet dsRecords = SQLiteHelper.ExecuteDataSet(sConnStringSQLite, sqlSelect.ToString(), CommandType.Text); |
|
if (ds == null || ds.Tables.Count == 0 || dsRecords.Tables[0].Rows.Count == 0) |
|
{ |
|
OnReport("表中无记录!", -1); |
|
return; |
|
} |
|
DataTable dtRecords = dsRecords.Tables[0]; |
|
OnReport(string.Format("共{0}条记录需要加密", dtRecords.Rows.Count)); |
|
SQLiteConnection liteConn = new SQLiteConnection(sConnStringSQLite); |
|
liteConn.Open(); |
|
try |
|
{ |
|
SQLiteTransaction trans = liteConn.BeginTransaction(); |
|
try |
|
{ |
|
SQLiteCommand cmdlite = new SQLiteCommand(sqlUpdata.ToString(), liteConn); |
|
List<SQLiteParameter> parameters = new List<SQLiteParameter>(); |
|
for (int i = 0; i <= nOtherFields; i++) |
|
{ |
|
parameters.Add(cmdlite.CreateParameter()); |
|
} |
|
cmdlite.Parameters.AddRange(parameters.ToArray()); |
|
for (int i = 0; i < dtRecords.Rows.Count; i++) |
|
{ |
|
DataRow dr = dtRecords.Rows[i]; |
|
foreach (DataColumn dc in dtRecords.Columns) |
|
{ |
|
|
|
if (dc.Ordinal == 0) |
|
{ |
|
parameters[parameters.Count - 1].Value = dr[dc]; |
|
} |
|
else if (dc.ColumnName.ToUpper() == this.LayerLabelName.ToUpper()) |
|
{ |
|
parameters[dc.Ordinal - 1].Value = dr[dc]; |
|
} |
|
else |
|
{ |
|
parameters[dc.Ordinal - 1].Value = EnCode(dr[dc]); |
|
} |
|
} |
|
int resturt = cmdlite.ExecuteNonQuery(); |
|
if (i % 10000 == 0) |
|
{ |
|
OnReport(string.Format("已加密{0}/{1}", i + 1, dtRecords.Rows.Count)); |
|
} |
|
} |
|
trans.Commit(); |
|
OnReport("数据已全部加密成功!", -1); |
|
} |
|
catch (Exception ex) |
|
{ |
|
trans.Rollback(); |
|
throw; |
|
} |
|
finally |
|
{ |
|
trans.Dispose(); |
|
} |
|
} |
|
finally |
|
{ |
|
liteConn.Close(); |
|
liteConn.Dispose(); |
|
GC.Collect(); |
|
} |
|
} |
|
OnReport(string.Format("加密表:{0}结束。", item), -1); |
|
} |
|
catch (Exception ex) |
|
{ |
|
OnReport(string.Format("加密表:{0}出现异常,异常信息为:{1}。", item, ex), -1); |
|
} |
|
} |
|
|
|
private string EnCode(object value) |
|
{ |
|
if (value == null) |
|
return ""; |
|
string str = value.ToString(); |
|
if (string.IsNullOrEmpty(str)) |
|
return ""; |
|
string outStr = ""; |
|
foreach (char item in str) |
|
{ |
|
outStr += (char)(item + 22); |
|
} |
|
return outStr; |
|
} |
|
|
|
public void Dispose() |
|
{ |
|
|
|
try |
|
{ |
|
if (this.SpatialDataSource != null) |
|
{ |
|
this.SpatialDataSource.Close(); |
|
} |
|
if (this.doWorkThread != null) |
|
{ |
|
try |
|
{ |
|
this.doWorkThread.Abort(); |
|
} |
|
catch { } |
|
finally |
|
{ |
|
this.doWorkThread = null; |
|
} |
|
} |
|
} |
|
catch |
|
{ |
|
|
|
} |
|
finally |
|
{ |
|
GC.Collect(); |
|
} |
|
|
|
|
|
|
|
} |
|
} |
|
}
|
|
|