ArcMap 10.8.1 using .NET ArcObjects
I have a geometry with a spatial reference. How do I get its projection WKT? I've been looking for hours and can't seem to find a simple way to do this.
Dim pGeom As IGeometry = CreateGeometry()
Dim wkt as String = pGeom.SpatialReference.ToWKT '<=== What API do I use for this conversion?
The output should be something like:
PROJCS["World_Azimuthal_Equidistant",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Azimuthal_Equidistant"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-86.02400831252139],PARAMETER["Latitude_Of_Origin",41.70912056625856],UNIT["Meter",1.0]]
Solved! Go to Solution.
Hi,
You can serialize ISpatialReference object to xml ant then read wkt from it. Code for serialization:
private static string GetSpatialReferenceAsXmlString(ISpatialReference spatialReference)
{
if (spatialReference == null) throw new ArgumentNullException("spatialReference");
var xmlStream = new XMLStreamClass();
var xmlWriter = new XMLWriterClass();
var xmlSerializer = new XMLSerializerClass();
xmlWriter.WriteTo(xmlStream);
xmlSerializer.WriteObject(xmlWriter, null, null, string.Empty, string.Empty, spatialReference);
return xmlStream.SaveToString();
}
You will get xml like this:
<ProjectedCoordinateSystem xsi:type='typens:ProjectedCoordinateSystem' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/10.8'>
<WKT>PROJCS["LKS_1994_Transverse_Mercator",GEOGCS["GCS_LKS_1994",DATUM["D_Lithuania_1994",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",24.0],PARAMETER["Scale_Factor",0.9998],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]</WKT>
<XOrigin>-22523120044.116333</XOrigin>
<YOrigin>-22527998119.405388</YOrigin>
<XYScale>18181.818166159173</XYScale>
<ZOrigin>-100000</ZOrigin>
<ZScale>10000</ZScale>
<MOrigin>-100000</MOrigin>
<MScale>10000</MScale>
<XYTolerance>0.001</XYTolerance>
<ZTolerance>0.001</ZTolerance>
<MTolerance>0.001</MTolerance>
<HighPrecision>true</HighPrecision>
</ProjectedCoordinateSystem>
Then read WKT node from it
Hi,
You can serialize ISpatialReference object to xml ant then read wkt from it. Code for serialization:
private static string GetSpatialReferenceAsXmlString(ISpatialReference spatialReference)
{
if (spatialReference == null) throw new ArgumentNullException("spatialReference");
var xmlStream = new XMLStreamClass();
var xmlWriter = new XMLWriterClass();
var xmlSerializer = new XMLSerializerClass();
xmlWriter.WriteTo(xmlStream);
xmlSerializer.WriteObject(xmlWriter, null, null, string.Empty, string.Empty, spatialReference);
return xmlStream.SaveToString();
}
You will get xml like this:
<ProjectedCoordinateSystem xsi:type='typens:ProjectedCoordinateSystem' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/10.8'>
<WKT>PROJCS["LKS_1994_Transverse_Mercator",GEOGCS["GCS_LKS_1994",DATUM["D_Lithuania_1994",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",24.0],PARAMETER["Scale_Factor",0.9998],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]</WKT>
<XOrigin>-22523120044.116333</XOrigin>
<YOrigin>-22527998119.405388</YOrigin>
<XYScale>18181.818166159173</XYScale>
<ZOrigin>-100000</ZOrigin>
<ZScale>10000</ZScale>
<MOrigin>-100000</MOrigin>
<MScale>10000</MScale>
<XYTolerance>0.001</XYTolerance>
<ZTolerance>0.001</ZTolerance>
<MTolerance>0.001</MTolerance>
<HighPrecision>true</HighPrecision>
</ProjectedCoordinateSystem>
Then read WKT node from it
Wow, I didnt know you could do that. Thanks!
There's also the ISpatialReferenceFactory.ExportESRISpatialReferenceToPRJFile Method
I like that one too except you have to write it to disk first.
As a final edit I took @GKmieliauskas code and made it an extension. Note I use full namespaces so as not to clash with the MS XML functions.
<Extension()>
Function SerializeEsri(Of T As Class)(ByVal value As T) As XDocument
Dim sr As New ESRI.ArcGIS.esriSystem.XMLStream
Dim ir As ESRI.ArcGIS.esriSystem.IStream = CType(sr, ESRI.ArcGIS.esriSystem.IStream)
Dim wr As New ESRI.ArcGIS.esriSystem.XMLWriter
Dim ser As New ESRI.ArcGIS.esriSystem.XMLSerializer
wr.WriteTo(ir)
ser.WriteObject(wr, Nothing, Nothing, String.Empty, String.Empty, value)
Dim xdoc As XDocument = XDocument.Parse(sr.SaveToString)
Return xdoc
End Function
Usage:
Private Function GetSpatialReferenceAsWkt(pSpatRef As ISpatialReference) As String
If pSpatRef Is Nothing Then Return Nothing
Dim xdoc As XDocument = pSpatRef.SerializeEsri
Dim xelem As XElement = xdoc.Root.Element("WKT")
Return xelem.Value
End Function