空间参考是GIS的基础,失去了空间参考信息,地理空间内所有的信息也就失去了存在的意义,因为它们是不准确的或是错误的。看看ArcGIS9.2对于Spatial Reference的描述及如何编程实现。
文章中代码使用C#描述,AE92 for Java SDK的com.esri.arcgis.geometry中各种接口、实现方式和C#基本一致,可以使用相同的思路来解决AE Java开发中关于Spatial Reference的问题。
作者:Flyingis
空间参考是GIS的基础,失去了空间参考信息,地理空间内所有的信息也就失去了存在的意义,因为它们是不准确的或是错误的。关于ArcGIS坐标系统文件,可以看看这篇文章——《ArcGIS 坐标系统文件》。
刻画Spatial Reference的精度
首先,我们主要讨论的是ArcGIS中Spatial Reference的各种精度,看到resolution、tolerance、domain、scale factor、precision,是否很熟悉?为了保证表达的准确性,所有这些关键字使用英文表述,不再译为中文。
Resolution和domain范围决定了Geometry坐标的存储方式,Resolution和关联的坐标系使用相同的数量单位,如当空间参考使用以米为单位的投影参考时,XY resolution单位为米,默认情况下resolution=0.0001m,不管怎么样resolution值至少应该小于数据精度的1/10。当定位某一坐标到坐标格网时,我们依据如下公式:
Persisted coordinate = Round((map coordinate - minimum domain extent) / resolution)
在ArcGIS 9.2之前,resolution=1/precision,ArcGIS 9.2认为resolution和precision几乎相同,在9.2之前坐标的存储精度是31位,9.2中为53位,对于上面的公式而言,当resolution很小时,坐标系统表达数据会更精确,但在9.2之前的ArcGIS中persisted coordinate会受到限制。例如在ArcGIS 9.2之前,当minimum domain value=0,resolution=1时,maximum domain value=231-2,resolution=0.0001时,maximum domain value=(231-2)*0.0001=214748.3647;在ArcGIS 9.2中,当minimum domain value=0,resolution=1时,maximum domain value=253-2,resolution=0.0001时,maximum domain value=(253-2)*0.0001,很显然,ArcGIS 9.1中maximum domain value=214748.3647已经不能满足UTM、State Plane等投影坐标系的要求,ArcGIS 9.2存储的数据可以拥有更高精度的空间参考。
默认情况下,ArcGIS 9.2为整数坐标采用53位空间存储,当然在编辑没有升级空间参考的空间数据库中的数据时,也可以保持向下兼容。新的COM接口已经可以用来判断数据采用的是低精度的各种空间参考,还是高精度的,同时有新的接口可以在低精度空间参考和高精度空间参考之间转换。
还是默认情况下,Tolerance=10*resolution,minimum tolerance=2*resolution=2.0/scale factor,tolerance决定在relational和topological操作中,两个坐标之间的最小距离,当小于该距离时,认为这两个坐标为相同的坐标。relational和topological操作采用不同的tolerance会得到不同的处理结果。9.2之前tolerance值受resolution的影响,9.2中必须要明确指定tolerance的值,在IRelationalOperator、ITopologicalOperator等对两个几何对象进行Geometry操作的接口中,使用第一个对象的tolerance来判断两几何体点点之间的关系,如果空间参考是未定义,或没有空间参考和几何体关联,将采用该格网所允许的最小tolerance取值。
如何编程?
使用SpatialReferenceEnvironment
private void PrintPreDefinedProjections()
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
{
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
ISet projectionSet = spatialReferenceFactory.CreatePredefinedProjections();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
System.Windows.Forms.MessageBox.Show("Number of predefined Projections = " + projectionSet.Count);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
projectionSet.Reset();
for (int i = 0; i < projectionSet.Count; i++)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IProjection projection = projectionSet.Next() as IProjection;
System.Windows.Forms.MessageBox.Show(projection.Name);
}
}
CreatePredefinedProjections返回AE中所有预定义的投影坐标,一共59个。ISpatialReferenceFactory的CreateESRISpatialReferenceFromPRJFile方法可以从已定义的PRJ文件获取坐标。
private IProjectedCoordinateSystem LoadProjectedCoordinateSystem()
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
{
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接口实现了创建垂直坐标系、构建高精度坐标系和低精度坐标系的系列方法。
private void ConstructCoordinateSystem(bool highPrecision)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
{
ISpatialReferenceFactory3 spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
ISpatialReference3 spatialReference = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile("D:\\ArcGIS\\Coordinate Systems\\Geographic Coordinate Systems\\World\\WGS 1984.prj") as ISpatialReference3;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
IControlPrecision2 controlPrecision = spatialReference as IControlPrecision2;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Determines whether you are constructing a high or low.
controlPrecision.IsHighPrecision = highPrecision;
ISpatialReferenceResolution spatialReferenceResolution = spatialReference as ISpatialReferenceResolution;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//These three methods are the keys, construct horizon, then set the default x,y resolution and tolerance.
spatialReferenceResolution.ConstructFromHorizon();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Set the default x,y resolution value.
spatialReferenceResolution.SetDefaultXYResolution();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Set the default x,y tolerance value.
ISpatialReferenceTolerance spatialReferenceTolerance = spatialReference as ISpatialReferenceTolerance;
spatialReferenceTolerance.SetDefaultXYTolerance();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
double xMin;
double xMax;
double yMin;
double yMax;
spatialReference.GetDomain(out xMin, out xMax, out yMin, out yMax);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
System.Windows.Forms.MessageBox.Show("Domain : " + xMin + ", " + xMax + ", " + yMin + ", " + yMax);
}
IControlPrecision2.IsHighPrecision用来判断是否对数据采用高精度坐标,后面的设置空间参考的方法将根据这个判断来决定各种参数的精确程度。highPrecision等于true或false时,返回的Domain分别是:
highPrecision=true -400 9006799.25474099 -400 9006799.25474099
highPrecision=false -400 793.04646944444426 -400 793.04646944444426
可以看出两者之间精度差别的大小。
我们可以创建一个新的投影坐标系并保存为prj文件,同时可以利用这个prj生成一个新的投影坐标系。
private void ImportExportSR_Example()
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
{
//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;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
spatialReferenceResolution.ConstructFromHorizon();
spatialReferenceTolerance.SetDefaultXYTolerance();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Export the PCS to a .prj file.
String fileName = "c:\\temp\\utm10.prj";
spatialReferenceFactory.ExportESRISpatialReferenceToPRJFile(fileName, projectedCoordinateSystem);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Rehydrate it as a new spatial reference object.
ISpatialReference projectedCoordinateSystem2 = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(fileName);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//See if they are equal.
IClone comparison = projectedCoordinateSystem as IClone;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Should be true, but coordinate grid information has not been checked.
System.Windows.Forms.MessageBox.Show((comparison.IsEqual(projectedCoordinateSystem2 as IClone)).ToString());
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
ISpatialReference2 comparePrecisions = projectedCoordinateSystem as ISpatialReference2;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Should be false, PRJ files do not persist coordinate grid information.
System.Windows.Forms.MessageBox.Show((comparePrecisions.IsXYPrecisionEqual(projectedCoordinateSystem2)).ToString());
}
两个空间参考是否相等不能简单通过一个方法来确定,IClone.isEqual只能比较坐标系相同,而不能确定坐标格网信息是否相同,因此最后一行代码比较的结果是false(IsXYPrecisionEqual是ISpatialReference2接口中的方法)。最后,看看如何自定义一个完整的投影坐标参考系,地理坐标系和垂直坐标系定义方法类似。
private void SetProjectionParameters()
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
{
//Create a factory.
ISpatialReferenceFactory2 spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//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;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Get the default parameters from the projection.
IParameter[] parameters = projection.GetDefaultParameters();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Iterate through the parameters and print out their name and value.
for (int i = 0; i < parameters.Length; i++)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IParameter currentParameter = parameters[i];
if (currentParameter != null)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Windows.Forms.MessageBox.Show(currentParameter.Name + ", " + currentParameter.Index + ", " + currentParameter.Value);
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Reset one of the parameter values.
IParameter parameter = parameters[2];
parameter.Value = 45;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//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;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
projectedCoordinateSystemEdit.Define(ref name,
ref alias,
ref abbreviation,
ref remarks,
ref usage,
ref geographicCoordinateSystemObject,
ref unitObject,
ref projectionObject,
ref parametersObject);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
IProjectedCoordinateSystem4GEN projectedCoordinateSystem = projectedCoordinateSystemEdit as IProjectedCoordinateSystem4GEN;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//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];
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Get the parameters.
projectedCoordinateSystem.GetParameters(ref newParameters);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
//Iterate through the parameters and print out their name and value.
for (int i = 0; i < newParameters.Length; i++)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IParameter currentNewParameter = newParameters[i];
if (currentNewParameter != null)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Windows.Forms.MessageBox.Show(currentNewParameter.Name + ", " + currentNewParameter.Index + ", " + currentNewParameter.Value);
}
}
}
具体的可以设置每一个参数,精确定义空间参考。
可以看到,关于Spatial Reference编程与实现无非就是在几个接口之间实现,了解了这几个接口及它们之间的联系(参考GeometryObjectModel.pdf文档),也就了解了Spatial Reference编程的方法,当然基础是地理空间参考的基本理论知识。