DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SpatialSystemFunctions.cs
Go to the documentation of this file.
1 using System;
2 
3 using Deveel.Data.Routines;
4 using Deveel.Data.Sql.Objects;
5 using Deveel.Data.Types;
6 
7 namespace Deveel.Data.Spatial {
8  public static class SpatialSystemFunctions {
9  public static IRoutineResolver Resolver {
10  get { return new SpatialFunctionProvider(); }
11  }
12 
13  public static SqlGeometry FromWkb(SqlBinary source) {
14  if (source.IsNull)
15  return SqlGeometry.Null;
16 
17  SqlGeometry geometry;
18  if (!SqlGeometry.TryParse(source.ToByteArray(), out geometry))
19  return SqlGeometry.Null;
20 
21  return geometry;
22  }
23 
24  public static DataObject FromWkb(DataObject source) {
25  var input = (SqlBinary) source.Value;
26  var result = FromWkb(input);
27  return new DataObject(SpatialType.Geometry(), result);
28  }
29 
30  public static DataObject ToWkb(DataObject geometry) {
31  if (geometry.IsNull)
33 
34  var g = (SqlGeometry)geometry.Value;
35  return DataObject.Binary(ToWkb(g));
36  }
37 
38  public static SqlBinary ToWkb(SqlGeometry geometry) {
39  if (geometry == null || geometry.IsNull)
40  return SqlBinary.Null;
41 
42  return geometry.ToWellKnownBytes();
43  }
44 
45  public static DataObject FromWkt(IRequest context, DataObject source) {
46  var input = (SqlString) source.Value;
47  var result = FromWkt(input);
48  return new DataObject(SpatialType.Geometry(), result);
49  }
50 
51  public static SqlGeometry FromWkt(SqlString source) {
52  SqlGeometry geometry;
53  if (!SqlGeometry.TryParse(source.ToString(), out geometry))
54  return SqlGeometry.Null;
55 
56  return geometry;
57  }
58 
59  public static SqlString ToWkt(SqlGeometry geometry) {
60  if (geometry == null || geometry.IsNull)
61  return SqlString.Null;
62 
63  return geometry.ToWellKnownText();
64  }
65 
66  public static DataObject ToWkt(DataObject geometry) {
67  if (geometry.IsNull)
69 
70  var g = (SqlGeometry) geometry.Value;
71  return DataObject.String(ToWkt(g));
72  }
73 
74  public static DataObject Envelope(DataObject geometry) {
75  var input = (SqlGeometry) geometry.Value;
76  var envelope = Envelope(input);
77  return new DataObject(SpatialType.Geometry(), envelope);
78  }
79 
80  public static SqlGeometry Envelope(SqlGeometry geometry) {
81  if (geometry.IsNull)
82  return SqlGeometry.Null;
83 
84  return geometry.Envelope;
85  }
86 
87  public static DataObject Distance(DataObject geometry, DataObject other) {
88  var input = (SqlGeometry)geometry.Value;
89  var otherGeometry = (SqlGeometry) other.Value;
90  var result = Distance(input, otherGeometry);
91  return DataObject.Number(result);
92  }
93 
94  public static SqlNumber Distance(SqlGeometry geometry, SqlGeometry other) {
95  if (geometry == null || geometry.IsNull)
96  return SqlNumber.Null;
97 
98  return geometry.Distance(other);
99  }
100 
101  public static SqlBoolean Contains(SqlGeometry geometry, SqlGeometry other) {
102  if (geometry == null || geometry.IsNull)
103  return SqlBoolean.Null;
104 
105  return geometry.Contains(other);
106  }
107 
108  public static DataObject Contains(DataObject geometry, DataObject other) {
109  var g1 = (SqlGeometry) geometry.Value;
110  var g2 = (SqlGeometry) other.Value;
111  var result = Contains(g1, g2);
112  return DataObject.Boolean(result);
113  }
114 
115  public static DataObject Area(DataObject geometry) {
116  var input = (SqlGeometry) geometry.Value;
117  var result = Area(input);
118  return DataObject.Number(result);
119  }
120 
121  public static SqlNumber Area(SqlGeometry geometry) {
122  if (geometry == null || geometry.IsNull)
123  return SqlNumber.Null;
124 
125  return geometry.Area;
126  }
127 
128  public static DataObject Boundary(DataObject geometry) {
129  var input = (SqlGeometry) geometry.Value;
130  var result = input.Boundary;
131  return new DataObject(SpatialType.Geometry(), result);
132  }
133  }
134 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static DataObject FromWkt(IRequest context, DataObject source)
static SqlGeometry FromWkb(SqlBinary source)
The system uses instances of this interface to resolve routines given a user invocation.
static SqlBinary ToWkb(SqlGeometry geometry)
bool IsNull
Gets a value that indicates if this object is materialized as null.
Definition: DataObject.cs:91
static DataObject Binary(SqlBinary binary)
Definition: DataObject.cs:638
static SpatialType Geometry(int srid)
Definition: SpatialType.cs:31
static DataObject Contains(DataObject geometry, DataObject other)
static DataObject ToWkb(DataObject geometry)
static bool TryParse(string text, out SqlGeometry geometry, out Exception error)
Definition: SqlGeometry.cs:166
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
Definition: SqlBinary.cs:27
SqlBoolean Contains(SqlGeometry other)
Definition: SqlGeometry.cs:136
static DataObject Number(SqlNumber value)
Definition: DataObject.cs:552
static DataObject Null(SqlType type)
Definition: DataObject.cs:630
static DataObject FromWkb(DataObject source)
SqlNumber Distance(SqlGeometry geometry)
Definition: SqlGeometry.cs:128
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
static DataObject String(string s)
Definition: DataObject.cs:592
static DataObject Boolean(SqlBoolean value)
Definition: DataObject.cs:544
byte[] ToByteArray()
Returns an array of bytes representing the contents of the binary.
Definition: SqlBinary.cs:102
static DataObject Envelope(DataObject geometry)
static SqlGeometry Envelope(SqlGeometry geometry)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
static DataObject Boundary(DataObject geometry)
string ToString(Encoding encoding)
Definition: SqlString.cs:182
static readonly SqlNumber Null
Definition: SqlNumber.cs:31
static SqlNumber Distance(SqlGeometry geometry, SqlGeometry other)
static DataObject Distance(DataObject geometry, DataObject other)
static DataObject ToWkt(DataObject geometry)
static SqlBoolean Contains(SqlGeometry geometry, SqlGeometry other)
static DataObject Area(DataObject geometry)
static SqlString ToWkt(SqlGeometry geometry)
static SqlGeometry FromWkt(SqlString source)
Deveel.Data.Sql.Objects.SqlString SqlString
Definition: DataObject.cs:27
static SqlNumber Area(SqlGeometry geometry)
static readonly SqlBinary Null
Definition: SqlBinary.cs:29