📄 esrixmledit.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Geometry;
using System.IO;
namespace ChangeXMLPrj
{
/// <summary>
/// XML scheme 投影编辑
/// 需要设定两个值。
/// ISpatialReference
/// ESRTTypehead
/// </summary>
public class EsriXmlEdit
{
/// <summary>
/// esri
/// 字段
/// </summary>
private ISpatialReference psp = null;
public ISpatialReference ISpatialRef
{
get { return psp; }
set { psp = value; }
}
/// <summary>
/// 七参数
/// </summary>
private double Res = Math.Pow(2, 53) - 2;
public double dblxorign = 0;
public double dblyorign = 0;
public int dblxyscale = 0;
public double dblzorign = 0;
public int dblzscale = 0;
public double dblmorign = 0;
public int dblmscale = 0;
/// <summary>
/// 原scheme件流
/// </summary>
FileStream pfs = null;
/// <summary>
/// 读文件流
/// </summary>
StreamReader psr = null;
/// <summary>
/// 原好Spatialref节点行
/// </summary>
string strSP = "";
/// <summary>
/// 是否此行已经开始包含spatialref
/// </summary>
bool hasstart = false;
/// <summary>
/// 新建文件流
/// </summary>
FileStream pnew = null;
/// <summary>
/// 写文件流
/// </summary>
StreamWriter psw = null;
public EsriXmlEdit(string xmlfilename)
{
pfs = new FileStream(xmlfilename, FileMode.Open , FileAccess.Read );
psr = new StreamReader(pfs as Stream);
pnew=new FileStream("new"+xmlfilename, FileMode.Create , FileAccess.ReadWrite );
psw = new StreamWriter(pnew as Stream);
}
/// <summary>
/// 执行指定值
/// </summary>
public void Duck()
{
///解算投影中的参数
dblxorign = 0;
dblyorign = 0;
double dblxmax = 0;
double dblymax = 0;
psp.GetDomain(out dblxorign, out dblxmax, out dblyorign, out dblymax);
dblxyscale = Convert.ToInt32(Res / (dblymax - dblyorign));
double dblzmax = 0;
psp.GetZDomain(out dblzorign, out dblzmax);
dblzscale = Convert.ToInt32(Res / (dblzmax - dblzorign));
double dblmmax = 0;
psp.GetMDomain(out dblmorign, out dblmmax);
dblmscale = Convert.ToInt32(Res / (dblmmax - dblmorign));
string str = psr.ReadLine();
while (str != null)
{
if (str.Contains(All.stagSR))
{
strSP = "";
hasstart = true;
}
if (str.Contains(All.stagSR.Substring(0, 1) + "/" + All.stagSR.Substring(1)))
{
//
hasstart = false;
strSP += str;
WriteSR();
strSP = "";
str = psr.ReadLine();
continue;
}
if (hasstart)
{
strSP += str;
str = psr.ReadLine();
continue;
}
psw.WriteLine(str);
str = psr.ReadLine();
}
}
/// <summary>
/// 改写投影信息
/// </summary>
private void WriteSR()
{
string now = "" ;
string old = strSP;
string strall = "";
string[] list = new string[2];
list[0] = All.stagSR;
list[1] = All.stagSR.Substring(0, 1) + "/" + All.stagSR.Substring(1)+">";
string[] arr= old.Split(list ,1000,StringSplitOptions.None );
for (int i = 0; i < arr.Length; i++)
{
if (i % 2 == 0)
{
///不变的信息。此中不包含投影
strall += arr[i];
}
else
{
///需要重写的投影配置参数
strall += getSPString(arr[i]);
}
}
psw.WriteLine(strall);
strall = "";
strSP = "";
}
/// <summary>
/// 获取转换后的投影配置串
/// </summary>
/// <param name="oldtmp">原本信息</param>
/// <returns></returns>
private string getSPString(string oldtmp)
{
#region
string now = "";
string Spatial = "<SpatialReference xsi:type='" + this.ESRTTypehead + "'>";
now += Spatial;
string WKT = All.stagwkt + opera.getPrjText() + All.stagwkt.Substring(0, 1) + "/" + All.stagwkt.Substring(1);
if (ESRTTypehead.Contains("ProjectedCoordinateSystem"))
{
now += WKT;
}
string tmp = "";
if (oldtmp.Contains(All.stagXOrigin))
{
tmp = All.stagXOrigin + this.dblxorign.ToString() +
All.stagXOrigin.Substring(0, 1) + "/" + All.stagXOrigin.Substring(1);
now += tmp;
}
if (oldtmp.Contains(All.stagYOrigin))
{
tmp = All.stagYOrigin + this.dblyorign.ToString() +
All.stagYOrigin.Substring(0, 1) + "/" + All.stagYOrigin.Substring(1);
now += tmp;
}
if (oldtmp.Contains(All.stagXYScale))
{
tmp = All.stagXYScale + this.dblxyscale.ToString() +
All.stagXYScale.Substring(0, 1) + "/" + All.stagXYScale.Substring(1);
now += tmp;
}
if (oldtmp.Contains(All.stagZOrigin))
{
tmp = All.stagZOrigin + this.dblzorign.ToString() +
All.stagZOrigin.Substring(0, 1) + "/" + All.stagZOrigin.Substring(1);
now += tmp;
}
if (oldtmp.Contains(All.stagZScale))
{
tmp = All.stagZScale + this.dblzscale.ToString() +
All.stagZScale.Substring(0, 1) + "/" + All.stagZScale.Substring(1);
now += tmp;
}
if (oldtmp.Contains(All.stagMOrigin))
{
tmp = All.stagMOrigin + this.dblmorign.ToString() +
All.stagMOrigin.Substring(0, 1) + "/" + All.stagMOrigin.Substring(1);
now += tmp;
}
if (oldtmp.Contains(All.stagMScale))
{
tmp = All.stagMScale + this.dblmscale.ToString() +
All.stagMScale.Substring(0, 1) + "/" + All.stagMScale.Substring(1);
now += tmp;
}
int end = oldtmp.IndexOf("<XYTolerance>", 0);
if (end == -1)
{
throw new ArgumentException("daw");
}
tmp = oldtmp.Substring(end);
now += tmp;
now += "</SpatialReference>";
#endregion
return now;
}
/// <summary>
/// unk || PCS
/// esri type 节点属性标识。
/// </summary>
public string ESRTTypehead = "";
/// <summary>
/// 关闭文件流
/// </summary>
public void Close()
{
psr.Close();
pfs.Close();
psw.Close();
pnew.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -