📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
namespace AddSDE
{
public partial class Form1 : Form
{
private ICommandHost m_CommandHost = new CommandHostClass();//字符集转换
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//SdeConn("cqc-gis", "5151/TCP", "ora9", "sde", "zhongdi");
}
//--------------------------------------------建立SDE的连接------------------------------------------------
private IPropertySet SdeConn(string Server, string Instance, string DataBase, string user, string password)
{
try
{
IPropertySet pProSet = new PropertySetClass();
//-----------------------SDE连接属性的设置------------------------
pProSet.SetProperty("Server", Server);
pProSet.SetProperty("Instance", Instance);
pProSet.SetProperty("Database", DataBase);
pProSet.SetProperty("user", user);
pProSet.SetProperty("password", password);
pProSet.SetProperty("version", "SDE.DEFAULT");
return pProSet;
}
catch
{
return null;
}
}
//-------------------------向空间库添加要素----------------------------
private void Add_Fea(double[] X,double[] Y,string[] FeaName,string TableName)
{
IPropertySet pProSet = new PropertySetClass();
IWorkspaceFactory pWorkSpFac = new SdeWorkspaceFactoryClass();
IFeatureWorkspace pFeaWorkSp = null;
IFeatureClass FeaCls = null;
IFeatureBuffer FeaBuffer = null;
IFeatureCursor FeaCursor = null;
try
{
pProSet = SdeConn("cqc-gis", "5151/TCP", "ora9", "sde", "zhongdi");//建立空间数据库的连接
if (pProSet != null)
{
pFeaWorkSp = (IFeatureWorkspace)(pWorkSpFac.Open(pProSet, 0));//打开要素空间
FeaCls = pFeaWorkSp.OpenFeatureClass(TableName);//取得要素集
FeaCursor = FeaCls.Insert(true);
FeaBuffer = FeaCls.CreateFeatureBuffer();
IField Fld = new FieldClass();
IFields Flds = new FieldsClass();
if (FeaCls.ShapeType == esriGeometryType.esriGeometryPoint)//判断选择的要素集为点要素集
{
//------通过循环将点要素添加进指定的要素集中去------
for (int index = 0; index < X.Length; index++)
{
IPoint pPoint = new PointClass();
IGeometry Geotry = null;
double xx = 0, yy = 0, longitude = 0, latitude = 0;
xx = X[index];
yy = Y[index];
GaussProjInvCal(3, yy, xx, ref longitude, ref latitude);//高斯坐标反算经纬度
pPoint.PutCoords(longitude, latitude);//创建点要素
Geotry = pPoint;
Flds = FeaCls.Fields;
for (int i = 1; i < Flds.FieldCount - 1; i++)
{
Fld = Flds.get_Field(i);
if (Fld.Name == "NAME")
{
FeaBuffer.set_Value(i, FeaName[index]);
}
}
FeaBuffer.Shape = Geotry;
FeaCursor.InsertFeature(FeaBuffer);
MessageBox.Show("要素添加成功!");
}
}
else if (FeaCls.ShapeType == esriGeometryType.esriGeometryPolyline)//判断选择的要素集为线要素集
{
for (int index = 0; index < X.Length - 1; index++)
{
ILine pLine = new LineClass();
IPolyline pPolyLine = new PolylineClass();
IGeometry Geotry;
IPoint Fpoint = new PointClass();
IPoint Tpoint = new PointClass();
double Fxx = 0, Fyy = 0, Txx = 0, Tyy = 0, longitude = 0, latitude = 0;
Fxx = X[index];
Fyy = Y[index];
Txx = X[index + 1];
Tyy = Y[index + 1];
GaussProjInvCal(3, Fyy, Fxx, ref longitude, ref latitude);
Fpoint.PutCoords(longitude, latitude);
GaussProjInvCal(3, Tyy, Txx, ref longitude, ref latitude);
Tpoint.PutCoords(longitude, latitude);
pPolyLine.Envelope.PutCoords(Fpoint.X, Fpoint.Y, Tpoint.X, Tpoint.Y);
Geotry = pPolyLine;
Flds = FeaCls.Fields;
for (int i = 1; i < Flds.FieldCount - 1; i++)
{
Fld = Flds.get_Field(i);
if (Fld.Name == "NAME")
{
FeaBuffer.set_Value(i, FeaName[0]);
}
}
FeaBuffer.Shape = Geotry;
FeaCursor.InsertFeature(FeaBuffer);
}
MessageBox.Show("要素添加成功!");
}
}
}
catch
{
MessageBox.Show("添加要素出错!");
}
//---释放对空间数据库的连接---
pFeaWorkSp = null;
pProSet = null;
}
private void button1_Click(object sender, EventArgs e)
{
//SdeConn("cqc-gis", "5151/TCP", "ora9", "sde", "zhongdi");
double[] x ={ 4181330.66061736, 4181330.659999, 4181330.659999 };
double[] y ={ 37569634.2437095, 37569634.250000, 37569634.2437095 };
string[] name ={ "TEST1", "TEST2","测试" };
Add_Fea(x, y, name,"SDE.JING");
}
//高斯投影由大地坐标(Unit:Metres)反算经纬度(Unit:DD)
private void GaussProjInvCal(int ZoneWidth,double X, double Y, ref double longitude, ref double latitude)
{
int ProjNo; int ZoneWide; ////带宽
double longitude1, latitude1, longitude0,X0, Y0, xval, yval;
double e1, e2, f, a, ee, NN, T, C, M, D, R, u, fai, iPI;
iPI = 0.0174532925199433; ////3.1415926535898/180.0;
//a = 6378245.0; f = 1.0 / 298.3; //54年北京坐标系参数
a=6378140.0; f=1/298.257; //80年西安坐标系参数
//ZoneWide = 6; ////6度带宽
ZoneWide = ZoneWidth;//指定分带数
ProjNo = (int)(X / 1000000L); //查找带号
longitude0 = (ProjNo - 1) * ZoneWide + ZoneWide / 2;
longitude0 = longitude0 * iPI; //中央经线
X0 = ProjNo * 1000000L + 500000L;
Y0 = 0;
xval = X - X0; yval = Y - Y0; //带内大地坐标
e2 = 2 * f - f * f;
e1 = (1.0 - Math.Sqrt(1 - e2)) / (1.0 + Math.Sqrt(1 - e2));
ee = e2 / (1 - e2);
M = yval;
u = M / (a * (1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256));
fai = u + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * Math.Sin(2 * u) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * Math.Sin(4 * u) + (151 * e1 * e1 * e1 / 96) * Math.Sin(6 * u) + (1097 * e1 * e1 * e1 * e1 / 512) * Math.Sin(8 * u);
C = ee * Math.Cos(fai) * Math.Cos(fai);
T = Math.Tan(fai) * Math.Tan(fai);
NN = a / Math.Sqrt(1.0 - e2 * Math.Sin(fai) * Math.Sin(fai));
R = a * (1 - e2) / Math.Sqrt((1 - e2 * Math.Sin(fai) * Math.Sin(fai)) * (1 - e2 * Math.Sin(fai) * Math.Sin(fai)) * (1 - e2 * Math.Sin(fai) * Math.Sin(fai)));
D = xval / NN;
//计算经度(Longitude) 纬度(Latitude)
longitude1 = longitude0 + (D - (1 + 2 * T + C) * D * D * D / 6 + (5 - 2 * C + 28 * T - 3 * C * C + 8 * ee + 24 * T * T) * D * D * D * D * D / 120) / Math.Cos(fai);
latitude1 = fai - (NN * Math.Tan(fai) / R) * (D * D / 2 - (5 + 3 * T + 10 * C - 4 * C * C - 9 * ee) * D * D * D * D / 24 + (61 + 90 * T + 298 * C + 45 * T * T - 256 * ee - 3 * C * C) * D * D * D * D * D * D / 720);
//转换为度 DD
longitude = longitude1 / iPI;
latitude = latitude1 / iPI;
}
private void button2_Click(object sender, EventArgs e)
{
double longitude=0, latitude=0;
GaussProjInvCal(3,37569634.2437095, 4181330.66061736, ref longitude, ref latitude);
MessageBox.Show(longitude.ToString() + "---" + latitude.ToString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -