0

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..

  • Have you read https://stackoverflow.com/a/14797013/34092 ? Which specific parts of your question does that not address? – mjwills Oct 06 '20 at 22:32
  • I'd like to deploy the application and all of its requirements together, requiring users to download an outdated (2008, or 2012) version of CLR Types for SQL Server seems a little strange, and increases the total size of the installation package for a work-around. – Michael Di Felice Oct 06 '20 at 22:36
  • `Microsoft.SqlServer.Types` currently ships with `SqlServerSpatial140.dll` (last I checked). It also instructs you to ensure that either the Microsoft SQL CLR types are installed, or to manually load `SqlServerSpatial*.dll` from the `bin` directory, as this answer suggests from the same question - https://stackoverflow.com/a/34876194/2942725 In the latest version of `Microsoft.SqlServer.Types` installed, using the `SqlServerSpatial*.dll` it ships with, there is an ADDITIONAL requirement of installing a legacy version of the CLR types to get it working, which is counter-intuitive to me. – Michael Di Felice Oct 06 '20 at 22:38
  • I am not utterly clear what your question is though. You _know_ what it needs. You need to give it what it needs. If your question is "why does it need that?" the answer is likely "because that is how they coded it". – mjwills Oct 06 '20 at 23:17
  • `I'd like to deploy the application and all of its requirements together,` Can you include the CLR Types install package in your application install? – mjwills Oct 06 '20 at 23:18
  • What version of SQL Server do you plan to use? I would use the latest version of SQLServerSoatial140.dll that is earlier than the version of SQL Server that you are using. You can download from : https://www.nuget.org/packages/SqlServerSpatial.Toolkit/ It looks like your link is for a IIS Server which is Windows 2012. If you are using a new version of windows I wouldn't use an old version of the dll. – jdweng Oct 06 '20 at 23:25
  • The application can potentially connect to different versions of MSSQL, making it somewhat more complex to deploy the correct version of SqlServerSpatial*.dll along with it. @mjwills may be right, and the answer may simply be that the differing requirements are HOW DbGeography had been developed. I'm just a little confused as to how two seemingly similar methods (PolygonFromText, and FromText) could yield such different results, and have such different requirements – Michael Di Felice Oct 06 '20 at 23:36

0 Answers0