说明:首先要将sde要素类发布成对应的要素服务,通过对要素服务的操作,实现数据在sde要素类中的增删
//向服务器发出请求
public string getPostData(string postString, string url)
{
string result = "";
try
{
byte[] postData = Encoding.UTF8.GetBytes(postString);
WebClient webClient = new WebClient();
string method = "POST";
//POST Header
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
//webClient.Headers.Add("Cache-Control", "no-cache");
//同步提交请求,并返回数据
byte[] responseData = webClient.UploadData(url, method, postData);//得到返回字符流
result = Encoding.UTF8.GetString(responseData);//解码
}
catch
{
return result;
}
return result;
}
#region 用c#直接读取shapfile的过程类
class Point//点类
{
public double X;
public double Y;
}
class Polyline//线类
{
public double[] Box = new double[4];
public int NumParts;
public int NumPoints;
public ArrayList Parts; //在部分中第一个点的索引
public ArrayList Points; //所有部分的点
}
class Polygon : Polyline//面类
{ }
#endregion 用c#直接读取shapfile的过程类
//从服务器上读取shp文件流
public Stream GetShpStream(string url)
{
var client = new WebClient();
client.Encoding = Encoding.UTF8;
Uri uri = new Uri(url);
byte[] bytes = client.DownloadData(uri);
Stream pstream = new MemoryStream(bytes);
return pstream;
}
//将读取的shp流拼成符合sde要素类的图形
public static string GetGeometryFromShp(Stream stream, string czah)
{
//int wkid = 2362;//西安80
StringBuilder sb = new StringBuilder("[{");
sb.Append(@"""attributes""" + ":{"CZAH":"" + czah + ""},");//添加属性信息
sb.Append(@"""geometry""" + ":{");
ArrayList polygons = new ArrayList();//面集合
ArrayList polylines = new ArrayList();//线集合
ArrayList points = new ArrayList();//点集合
int ShapeType;//shp文件类型
double xmin, ymin, xmax, ymax;
//FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);//File.Open(filepath, FileMode.Open)
using (BinaryReader br = new BinaryReader(stream))
{
br.ReadBytes(24);
int FileLength = br.ReadInt32();//<0代表数据长度未知
int FileBanben = br.ReadInt32();
ShapeType = br.ReadInt32();
xmin = br.ReadDouble();
ymin = br.ReadDouble();
xmax = br.ReadDouble();
ymax = br.ReadDouble();
br.ReadBytes(32);
if (IsXian80(xmin, ymin, xmax, ymax))
{
if (ShapeType == 5)
{
polygons.Clear();
while (br.PeekChar() != -1)
{
Polygon polygon = new Polygon();
polygon.Parts = new ArrayList();
polygon.Points = new ArrayList();
uint RecordNum = br.ReadUInt32();
int DataLength = br.ReadInt32();
//读取第i个记录
int m = br.ReadInt32();
for (int i = 0; i < 4; i++)
{
polygon.Box[i] = br.ReadDouble();
}
polygon.NumParts = br.ReadInt32();
polygon.NumPoints = br.ReadInt32();
for (int j = 0; j < polygon.NumParts; j++)
{
int parts = new int();
parts = br.ReadInt32();
polygon.Parts.Add(parts);
}
for (int j = 0; j < polygon.NumPoints; j++)
{
Point pointtemp = new Point();
pointtemp.X = br.ReadDouble();
pointtemp.Y = br.ReadDouble();
polygon.Points.Add(pointtemp);
}
polygons.Add(polygon);
}
foreach (Polygon p in polygons)
{
sb.Append(@"""rings"":[");
for (int i = 0; i < p.NumParts; i++)
{
int startpoint;
int endpoint;
if (i == p.NumParts - 1)
{
startpoint = (int)p.Parts[i];
endpoint = p.NumPoints;
}
else
{
startpoint = (int)p.Parts[i];
endpoint = (int)p.Parts[i + 1];
}
sb.Append("[");
for (int k = 0, j = startpoint; j < endpoint; j++, k++)
{
Point ps = (Point)p.Points[j];
sb.Append("[" + ps.X + "," + ps.Y + "],");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("],");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("]");
}
}
}
else
{
return "noxian";
}
br.Close();
br.Dispose();
}
sb.Append("}");
sb.Append("}]");
return sb.ToString();
}
//添加要素到sde要素类中(url为发布的要素服务地址)
public bool addFeatureToFeatureClass(string feature, string url)
{
bool isSuccess = false;
url += "/addFeatures";
string postString = "features=" + feature + "";
postString += "&" + "f=json";
string result = getPostData(postString, url);
var result1 = JsonConvert.DeserializeObject<dynamic>(result);
var presult = result1.addResults[0].success as IEnumerable<dynamic>;
string strresult = presult.ToString();
if (string.Equals(strresult, "True"))
{
isSuccess = true;
}
return isSuccess;
}