Skip to content

Commit a25856f

Browse files
committed
Updates in preparation for adding support for SqlGeography to SharpMap.SqlServerSpatialObjects
1 parent 8959f91 commit a25856f

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

SharpMap/Data/Providers/SqlServer2008.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ public class SqlServer2008 : BaseProvider
103103
private const string SharpMapWkb = "sharpmapwkb";
104104

105105
// required for restricting extents of WKT (eg bbox) used to query SqlGeography
106-
private static readonly Envelope GeogMaxExtents = new Envelope(-179.999999999, 179.999999999, -89.999999999, 89.999999999);
106+
protected static readonly Envelope GeogMaxExtents = new Envelope(-179.999999999, 179.999999999, -89.999999999, 89.999999999);
107107

108108
// SqlGeography : polygon interior defined by left hand/foot rule (anti-clockwise orientation)
109109
// SqlGeometry : orientation is irrelevant
110110
// GeometryToWKT returns Envelope with clockwise ring, so need to call .ReorientObject() for WKT used to query SqlGeography
111-
private readonly string _reorientObject;
111+
protected readonly string _reorientObject;
112112

113113
// used for static spatial methods in SQL string
114-
private readonly string _spatialTypeString;
114+
protected readonly string _spatialTypeString;
115115

116116
private SqlServer2008ExtentsMode _extentsMode;
117117

@@ -156,7 +156,7 @@ public class SqlServer2008 : BaseProvider
156156
/// Ignored for <see cref="SqlServerSpatialObjectType"/>.Geomtry
157157
/// when <see cref="ForceSeekHint"/> or <see cref="ForceIndex"/> is enabled due to SQL Server query execution plan.
158158
/// </summary>
159-
public Boolean ValidateGeometries { get; set; }
159+
public virtual Boolean ValidateGeometries { get; set; }
160160

161161
/// <summary>
162162
/// When <code>true</code>, uses the FORCESEEK table hint, possibly over-riding <see cref="ValidateGeometries"/>.
@@ -400,7 +400,7 @@ protected void ParseTablename(string tablename)
400400
Table = sb.ToString();
401401
}
402402

403-
private string GetAttributeColumnNames()
403+
protected string GetAttributeColumnNames()
404404
{
405405
if (string.IsNullOrEmpty(_attributeColumnNames))
406406
{
@@ -523,7 +523,7 @@ public override IGeometry GetGeometryByID(uint oid)
523523
using (var conn = new SqlConnection(ConnectionString))
524524
{
525525
string strSql = $"SELECT {GeometryColumn}{GetMakeValidString()}.STAsBinary() FROM {QualifiedTable} " +
526-
$"WHERE {ObjectIdColumn}={oid}";
526+
$"WHERE {ObjectIdColumn} = {oid}";
527527

528528
if (_logger.IsDebugEnabled) _logger.DebugFormat("GetGeometryByID {0}", strSql);
529529

@@ -635,7 +635,11 @@ protected override void OnExecuteIntersectionQuery(IGeometry geom, FeatureDataSe
635635
if (!String.IsNullOrEmpty(DefinitionQuery))
636636
sb.Append($"{DefinitionQuery} AND ");
637637

638-
sb.Append($"{GeometryColumn}.STIntersects({_spatialTypeString}::STGeomFromText('{geom.AsText()}', {SRID}){_reorientObject})=1 {GetExtraOptions()}");
638+
// .MakeValid() in WHERE clause is not compatible certain BuildHints, resulting in error:
639+
// The query processor could not produce a query plan for a query with a spatial index hint. Reason: Could not find required binary spatial method in a condition. Try removing the index hints or removing SET FORCEPLAN.
640+
var makeValid = (ForceSeekHint || !string.IsNullOrEmpty(ForceIndex)) ? "" : GetMakeValidString(); //".MakeValid()"
641+
642+
sb.Append($"{GeometryColumn}{makeValid}.STIntersects({_spatialTypeString}::STGeomFromText('{geom.AsText()}', {SRID}){_reorientObject})=1 {GetExtraOptions()}");
639643

640644
if (_logger.IsDebugEnabled) _logger.DebugFormat("OnExecuteIntersectionQuery {0}", sb.ToString());
641645

@@ -714,8 +718,11 @@ public override FeatureDataRow GetFeature(uint rowId)
714718
if (col.ColumnName != GeometryColumn && col.ColumnName != SharpMapWkb)
715719
fdr[col.ColumnName] = dr[col];
716720

717-
var wkbReader = new NetTopologySuite.IO.WKBReader(GeometryServiceProvider.Instance);
718-
fdr.Geometry = wkbReader.Read((byte[])dr[SharpMapWkb]);
721+
if (dr[SharpMapWkb] != null && dr[SharpMapWkb] != DBNull.Value)
722+
{
723+
var wkbReader = new NetTopologySuite.IO.WKBReader(GeometryServiceProvider.Instance);
724+
fdr.Geometry = wkbReader.Read((byte[])dr[SharpMapWkb]);
725+
}
719726

720727
return fdr;
721728
}
@@ -877,7 +884,7 @@ public override void ExecuteIntersectionQuery(Envelope bbox, FeatureDataSet fds)
877884

878885
#endregion
879886

880-
private void ExecuteIntersectionQuery(string sql, FeatureDataSet fds)
887+
protected virtual void ExecuteIntersectionQuery(string sql, FeatureDataSet fds)
881888
{
882889
using (var conn = new SqlConnection(ConnectionString))
883890
{

0 commit comments

Comments
 (0)