It would appear as though DbGeography.PolygonFromText
and DbGeoGraphy.FromText
are leveraging two different sources of SQL Server spatial types when attempting to form polygons from well known text. I was wondering if anyone had encountered the same issue before or was aware of whether this was intended design or not.
I first encountered this bug when working through some deployment issues. Attempting to construct a DbGeography
from DbGeography.PolygonFromText
requires SQL Server Spatial Types for SQL Server 2012 (I've tried newer versions of Sql Server Spatial CLR types, none of which worked). Alternatively, DbGeography.FromText
appears to be able to generate DbGeography
objects from well known text without this requirement.
I've isolated the problem in the following application. Include the latest Microsoft.SqlServer.Types
package and run the following, which will generate the output listed below -
using System;
using System.Data.Spatial;
namespace DbGeographyTester
{
class Program
{
private static DbGeography WrapDbGeographyCreation(Func<DbGeography> geographyFactory)
{
try
{
return geographyFactory();
}
catch(Exception exc)
{
Console.WriteLine($"Caught Exception: [{exc}]");
}
return null;
}
public static void Main (string[] _)
{
var polyText = "POLYGON ((80.0 -30.0, 81.0 -31.0, 82.0 -32.0, 80.0 -30.0))";
Console.WriteLine($"Attempting DbGeography.PolygonFromText(\"{polyText}\", 4326)");
Console.WriteLine(WrapDbGeographyCreation(() => DbGeography.PolygonFromText(polyText, 4326)));
Console.WriteLine($"Attempting DbGeography.FromText(\"{polyText}\", 4326)");
Console.WriteLine(WrapDbGeographyCreation(() => DbGeography.FromText(polyText, 4326)));
}
}
}
/* Will output -
Attempting DbGeography.PolygonFromText("POLYGON ((80.0 -30.0, 81.0 -31.0, 82.0 -32.0, 80.0 -30.0))", 4326)
Caught Exception: [System.NotImplementedException: The method or operation is not implemented.
at System.Data.Spatial.DefaultSpatialServices.GeographyPolygonFromText(String geographyText, Int32 spatialReferenceSystemId)
at System.Data.Spatial.DbGeography.PolygonFromText(String polygonWellKnownText, Int32 coordinateSystemId)
at DbGeographyTester.Program.<>c__DisplayClass1_0.<Main>b__0() in D:\db-geography-creator\DbGeographyTester\DbGeographyTester\Program.cs:line 26
at DbGeographyTester.Program.WrapDbGeographyCreation(Func`1 geographyFactory) in D:\db-geography-creator\DbGeographyTester\DbGeographyTester\Program.cs:line 12]
Attempting DbGeography.FromText("POLYGON ((80.0 -30.0, 81.0 -31.0, 82.0 -32.0, 80.0 -30.0))", 4326)
SRID=4326;POLYGON ((80.0 -30.0, 81.0 -31.0, 82.0 -32.0, 80.0 -30.0))
*/
Then, after installing 'Microsoft System CLR Types for SQL Server 2012' from the SQL Server 2012 Feature Pack (SQLSysClrTypes.msi) available here https://www.microsoft.com/en-ca/download/details.aspx?id=43339, we get the following output -
/*
Attempting DbGeography.PolygonFromText("POLYGON ((80.0 -30.0, 81.0 -31.0, 82.0 -32.0, 80.0 -30.0))", 4326)
SRID=4326;POLYGON ((80 -30, 81 -31, 82 -32, 80 -30))
Attempting DbGeography.FromText("POLYGON ((80.0 -30.0, 81.0 -31.0, 82.0 -32.0, 80.0 -30.0))", 4326)
SRID=4326;POLYGON ((80 -30, 81 -31, 82 -32, 80 -30))
*/
Why would System.Data.Spatial.DbGeography
rely on two separate parsers for generating DbGeography
polygons from well known text? And furthermore, why would it rely on such an old version of SQL Server CLR types (SqlSpatial110.dll), and not any newer types?? Very confusing..