作者:Flyingis
空間參考是GIS的基礎(chǔ),失去了空間參考信息,地理空間內(nèi)所有的信息也就失去了存在的意義,因?yàn)樗鼈兪遣粶?zhǔn)確的或是錯(cuò)誤的。關(guān)于ArcGIS坐標(biāo)系統(tǒng)文件,可以看看這篇文章——《ArcGIS 坐標(biāo)系統(tǒng)文件》。
刻畫Spatial Reference的精度
首先,我們主要討論的是ArcGIS中Spatial Reference的各種精度,看到resolution、tolerance、domain、scale factor、precision,是否很熟悉?為了保證表達(dá)的準(zhǔn)確性,所有這些關(guān)鍵字使用英文表述,不再譯為中文。
Resolution和domain范圍決定了Geometry坐標(biāo)的存儲(chǔ)方式,Resolution和關(guān)聯(lián)的坐標(biāo)系使用相同的數(shù)量單位,如當(dāng)空間參考使用以米為單位的投影參考時(shí),XY resolution單位為米,默認(rèn)情況下resolution=0.0001m,不管怎么樣resolution值至少應(yīng)該小于數(shù)據(jù)精度的1/10。當(dāng)定位某一坐標(biāo)到坐標(biāo)格網(wǎng)時(shí),我們依據(jù)如下公式:
Persisted coordinate = Round((map coordinate - minimum domain extent) / resolution)
在ArcGIS 9.2之前,resolution=1/precision,ArcGIS 9.2認(rèn)為resolution和precision幾乎相同,在9.2之前坐標(biāo)的存儲(chǔ)精度是31位,9.2中為53位,對(duì)于上面的公式而言,當(dāng)resolution很小時(shí),坐標(biāo)系統(tǒng)表達(dá)數(shù)據(jù)會(huì)更精確,但在9.2之前的ArcGIS中persisted coordinate會(huì)受到限制。例如在ArcGIS 9.2之前,當(dāng)minimum domain value=0,resolution=1時(shí),maximum domain value=231-2,resolution=0.0001時(shí),maximum domain value=(231-2)*0.0001=214748.3647;在ArcGIS 9.2中,當(dāng)minimum domain value=0,resolution=1時(shí),maximum domain value=253-2,resolution=0.0001時(shí),maximum domain value=(253-2)*0.0001,很顯然,ArcGIS 9.1中maximum domain value=214748.3647已經(jīng)不能滿足UTM、State Plane等投影坐標(biāo)系的要求,ArcGIS 9.2存儲(chǔ)的數(shù)據(jù)可以擁有更高精度的空間參考。
默認(rèn)情況下,ArcGIS 9.2為整數(shù)坐標(biāo)采用53位空間存儲(chǔ),當(dāng)然在編輯沒有升級(jí)空間參考的空間數(shù)據(jù)庫中的數(shù)據(jù)時(shí),也可以保持向下兼容。新的COM接口已經(jīng)可以用來判斷數(shù)據(jù)采用的是低精度的各種空間參考,還是高精度的,同時(shí)有新的接口可以在低精度空間參考和高精度空間參考之間轉(zhuǎn)換。
還是默認(rèn)情況下,Tolerance=10*resolution,minimum tolerance=2*resolution=2.0/scale factor,tolerance決定在relational和topological操作中,兩個(gè)坐標(biāo)之間的最小距離,當(dāng)小于該距離時(shí),認(rèn)為這兩個(gè)坐標(biāo)為相同的坐標(biāo)。relational和topological操作采用不同的tolerance會(huì)得到不同的處理結(jié)果。9.2之前tolerance值受resolution的影響,9.2中必須要明確指定tolerance的值,在IRelationalOperator、ITopologicalOperator等對(duì)兩個(gè)幾何對(duì)象進(jìn)行Geometry操作的接口中,使用第一個(gè)對(duì)象的tolerance來判斷兩幾何體點(diǎn)點(diǎn)之間的關(guān)系,如果空間參考是未定義,或沒有空間參考和幾何體關(guān)聯(lián),將采用該格網(wǎng)所允許的最小tolerance取值。
如何編程?
使用SpatialReferenceEnvironment
private void PrintPreDefinedProjections()


{
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
ISet projectionSet = spatialReferenceFactory.CreatePredefinedProjections();

System.Windows.Forms.MessageBox.Show("Number of predefined Projections = " + projectionSet.Count);

projectionSet.Reset();
for (int i = 0; i < projectionSet.Count; i++)

{
IProjection projection = projectionSet.Next() as IProjection;
System.Windows.Forms.MessageBox.Show(projection.Name);
}
}
CreatePredefinedProjections返回AE中所有預(yù)定義的投影坐標(biāo),一共59個(gè)。ISpatialReferenceFactory的CreateESRISpatialReferenceFromPRJFile方法可以從已定義的PRJ文件獲取坐標(biāo)。
private IProjectedCoordinateSystem LoadProjectedCoordinateSystem()


{
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
IProjectedCoordinateSystem projectedCoordinateSystem = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile("C:\\Program Files\\ArcGIS\\Coordinate Systems\\Projected Coordinate Systems\\World\\Mollweide (world).prj") as IProjectedCoordinateSystem;
return projectedCoordinateSystem;
}
除了ISpatialReferenceFactory接口,AE還提供ISpatialReferenceFactory3接口實(shí)現(xiàn)了創(chuàng)建垂直坐標(biāo)系、構(gòu)建高精度坐標(biāo)系和低精度坐標(biāo)系的系列方法。
private void ConstructCoordinateSystem(bool highPrecision)


{
ISpatialReferenceFactory3 spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
ISpatialReference3 spatialReference = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile("D:\\ArcGIS\\Coordinate Systems\\Geographic Coordinate Systems\\World\\WGS 1984.prj") as ISpatialReference3;

IControlPrecision2 controlPrecision = spatialReference as IControlPrecision2;

//Determines whether you are constructing a high or low.
controlPrecision.IsHighPrecision = highPrecision;
ISpatialReferenceResolution spatialReferenceResolution = spatialReference as ISpatialReferenceResolution;

//These three methods are the keys, construct horizon, then set the default x,y resolution and tolerance.
spatialReferenceResolution.ConstructFromHorizon();

//Set the default x,y resolution value.
spatialReferenceResolution.SetDefaultXYResolution();

//Set the default x,y tolerance value.
ISpatialReferenceTolerance spatialReferenceTolerance = spatialReference as ISpatialReferenceTolerance;
spatialReferenceTolerance.SetDefaultXYTolerance();

double xMin;
double xMax;
double yMin;
double yMax;
spatialReference.GetDomain(out xMin, out xMax, out yMin, out yMax);

System.Windows.Forms.MessageBox.Show("Domain : " + xMin + ", " + xMax + ", " + yMin + ", " + yMax);
}
IControlPrecision2.IsHighPrecision用來判斷是否對(duì)數(shù)據(jù)采用高精度坐標(biāo),后面的設(shè)置空間參考的方法將根據(jù)這個(gè)判斷來決定各種參數(shù)的精確程度。highPrecision等于true或false時(shí),返回的Domain分別是:
highPrecision=true -400 9006799.25474099 -400 9006799.25474099
highPrecision=false -400 793.04646944444426 -400 793.04646944444426
可以看出兩者之間精度差別的大小。
我們可以創(chuàng)建一個(gè)新的投影坐標(biāo)系并保存為prj文件,同時(shí)可以利用這個(gè)prj生成一個(gè)新的投影坐標(biāo)系。
private void ImportExportSR_Example()


{
//Instantiate a predefined spatial reference and set its coordinate grid information.
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
IProjectedCoordinateSystem projectedCoordinateSystem = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_10N);
ISpatialReferenceResolution spatialReferenceResolution = projectedCoordinateSystem as ISpatialReferenceResolution;
ISpatialReferenceTolerance spatialReferenceTolerance = projectedCoordinateSystem as ISpatialReferenceTolerance;

spatialReferenceResolution.ConstructFromHorizon();
spatialReferenceTolerance.SetDefaultXYTolerance();

//Export the PCS to a .prj file.
String fileName = "c:\\temp\\utm10.prj";
spatialReferenceFactory.ExportESRISpatialReferenceToPRJFile(fileName, projectedCoordinateSystem);

//Rehydrate it as a new spatial reference object.
ISpatialReference projectedCoordinateSystem2 = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(fileName);

//See if they are equal.
IClone comparison = projectedCoordinateSystem as IClone;

//Should be true, but coordinate grid information has not been checked.
System.Windows.Forms.MessageBox.Show((comparison.IsEqual(projectedCoordinateSystem2 as IClone)).ToString());

ISpatialReference2 comparePrecisions = projectedCoordinateSystem as ISpatialReference2;

//Should be false, PRJ files do not persist coordinate grid information.
System.Windows.Forms.MessageBox.Show((comparePrecisions.IsXYPrecisionEqual(projectedCoordinateSystem2)).ToString());
}
兩個(gè)空間參考是否相等不能簡單通過一個(gè)方法來確定,IClone.isEqual只能比較坐標(biāo)系相同,而不能確定坐標(biāo)格網(wǎng)信息是否相同,因此最后一行代碼比較的結(jié)果是false(IsXYPrecisionEqual是ISpatialReference2接口中的方法)。最后,看看如何自定義一個(gè)完整的投影坐標(biāo)參考系,地理坐標(biāo)系和垂直坐標(biāo)系定義方法類似。
private void SetProjectionParameters()


{
//Create a factory.
ISpatialReferenceFactory2 spatialReferenceFactory = new SpatialReferenceEnvironmentClass();

//Create a projection, GeographicCoordinateSystem, and unit using the factory.
IProjectionGEN projection = spatialReferenceFactory.CreateProjection((int)esriSRProjectionType.esriSRProjection_Sinusoidal) as IProjectionGEN;
IGeographicCoordinateSystem geographicCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)
esriSRGeoCSType.esriSRGeoCS_WGS1984);
ILinearUnit unit = spatialReferenceFactory.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter) as ILinearUnit;

//Get the default parameters from the projection.
IParameter[] parameters = projection.GetDefaultParameters();

//Iterate through the parameters and print out their name and value.
for (int i = 0; i < parameters.Length; i++)

{
IParameter currentParameter = parameters[i];
if (currentParameter != null)

{
System.Windows.Forms.MessageBox.Show(currentParameter.Name + ", " + currentParameter.Index + ", " + currentParameter.Value);
}
}

//Reset one of the parameter values.
IParameter parameter = parameters[2];
parameter.Value = 45;

//Create a projected coordinate system using the Define method.
IProjectedCoordinateSystemEdit projectedCoordinateSystemEdit = new ProjectedCoordinateSystemClass();
object name = "Newfoundland";
object alias = "NF_LAB";
object abbreviation = "NF";
object remarks = "Most Eastern Province in Canada";
object usage = "When making maps of Newfoundland";
object geographicCoordinateSystemObject = geographicCoordinateSystem as object;
object unitObject = unit as object;
object projectionObject = projection as object;
object parametersObject = parameters as object;

projectedCoordinateSystemEdit.Define(ref name,
ref alias,
ref abbreviation,
ref remarks,
ref usage,
ref geographicCoordinateSystemObject,
ref unitObject,
ref projectionObject,
ref parametersObject);

IProjectedCoordinateSystem4GEN projectedCoordinateSystem = projectedCoordinateSystemEdit as IProjectedCoordinateSystem4GEN;

//Get the parameters from your new projected coordinate system and verify
//that the parameter value was changed.
//Create an array of IParameters with 16 elements.
IParameter[] newParameters = new IParameter[16];

//Get the parameters.
projectedCoordinateSystem.GetParameters(ref newParameters);

//Iterate through the parameters and print out their name and value.
for (int i = 0; i < newParameters.Length; i++)

{
IParameter currentNewParameter = newParameters[i];
if (currentNewParameter != null)

{
System.Windows.Forms.MessageBox.Show(currentNewParameter.Name + ", " + currentNewParameter.Index + ", " + currentNewParameter.Value);
}
}
}
具體的可以設(shè)置每一個(gè)參數(shù),精確定義空間參考。
可以看到,關(guān)于Spatial Reference編程與實(shí)現(xiàn)無非就是在幾個(gè)接口之間實(shí)現(xiàn),了解了這幾個(gè)接口及它們之間的聯(lián)系(參考GeometryObjectModel.pdf文檔),也就了解了Spatial Reference編程的方法,當(dāng)然基礎(chǔ)是地理空間參考的基本理論知識(shí)。