DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlGeometry.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 
4 using Deveel.Data.Sql.Objects;
5 
6 using GeoAPI;
7 using GeoAPI.Geometries;
8 
9 using NetTopologySuite;
10 using NetTopologySuite.Geometries;
11 using NetTopologySuite.IO;
12 
13 namespace Deveel.Data.Spatial {
14  public class SqlGeometry : ISqlObject {
15  public static SqlGeometry Null = new SqlGeometry(null, true);
16 
17  // TODO: Make these ones configurable and in context...
18  private static readonly IGeometryServices DefaultGeometryServices = new NtsGeometryServices();
19  private static readonly IGeometryFactory DefaultGeometryFactory = new GeometryFactory();
20 
21 
22  private SqlGeometry(IGeometry geometry, bool isNull) {
23  Geometry = geometry;
24  IsNull = isNull;
25  }
26 
27  private SqlGeometry(IGeometry geometry)
28  : this(geometry, false) {
29  if (geometry == null)
30  throw new ArgumentNullException("geometry");
31  }
32 
33  private IGeometry Geometry { get; set; }
34 
35  int IComparable.CompareTo(object obj) {
36  if (!(obj is SqlGeometry))
37  throw new ArgumentException();
38 
39  return CompareTo((SqlGeometry) obj);
40  }
41 
42  int IComparable<ISqlObject>.CompareTo(ISqlObject other) {
43  if (!(other is SqlGeometry))
44  throw new ArgumentException();
45 
46  return CompareTo((SqlGeometry)other);
47  }
48 
49  public int CompareTo(SqlGeometry other) {
50  return Geometry.CompareTo(other.Geometry);
51  }
52 
53  public bool IsNull { get; private set; }
54 
55  public SqlString GeometryType {
56  get {
57  if (IsNull)
58  return SqlString.Null;
59 
60  return new SqlString(Geometry.GeometryType);
61  }
62  }
63 
64  public SqlNumber Area {
65  get {
66  if (IsNull)
67  return SqlNumber.Null;
68 
69  return new SqlNumber(Geometry.Area);
70  }
71  }
72 
73  public SqlNumber Length {
74  get {
75  if (IsNull)
76  return SqlNumber.Null;
77 
78  return new SqlNumber(Geometry.Length);
79  }
80  }
81 
82  public SqlGeometry Boundary {
83  get {
84  if (IsNull)
85  return Null;
86 
87  return new SqlGeometry(Geometry.Boundary);
88  }
89  }
90 
91  public SqlGeometry Envelope {
92  get {
93  if (IsNull)
94  return Null;
95 
96  return new SqlGeometry(Geometry.Envelope);
97  }
98  }
99 
101  if (!(other is SqlGeometry))
102  return false;
103 
104  return IsComparableTo((SqlGeometry)other);
105  }
106 
107  public bool IsComparableTo(SqlGeometry other) {
108  if (IsNull && (other == null || other.IsNull))
109  return true;
110  if (!IsNull && (other != null && !other.IsNull))
111  return false;
112 
113  return true;
114  }
115 
116  public SqlGeometry Buffer(SqlNumber distance) {
117  if (distance.IsNull)
118  return Null;
119 
120  return Buffer(distance.ToDouble());
121  }
122 
123  public SqlGeometry Buffer(double distance) {
124  var result = Geometry.Buffer(distance);
125  return new SqlGeometry(result);
126  }
127 
128  public SqlNumber Distance(SqlGeometry geometry) {
129  if (IsNull || (geometry == null || geometry.IsNull))
130  return SqlNumber.Null;
131 
132  var result = Geometry.Distance(geometry.Geometry);
133  return new SqlNumber(result);
134  }
135 
137  if (IsNull || (other == null || other.IsNull))
138  return SqlBoolean.Null;
139 
140  return Geometry.Contains(other.Geometry);;
141  }
142 
144  if (IsNull)
145  return SqlBinary.Null;
146 
147  var bytes = Geometry.AsBinary();
148  return new SqlBinary(bytes);
149  }
150 
151  public override string ToString() {
152  if (IsNull)
153  return String.Empty;
154 
155  return ToWellKnownText().ToString();
156  }
157 
159  if (IsNull)
160  return SqlString.Null;
161 
162  var text = Geometry.AsText();
163  return new SqlString(text);
164  }
165 
166  private static bool TryParse(string text, out SqlGeometry geometry, out Exception error) {
167  if (String.IsNullOrEmpty(text)) {
168  geometry = Null;
169  error = new ArgumentNullException("text");
170  return false;
171  }
172 
173  try {
174  var reader = new WKTReader(DefaultGeometryFactory);
175 
176  IGeometry g;
177  using (var textReader = new StringReader(text)) {
178  g = reader.Read(textReader);
179  }
180 
181  geometry = new SqlGeometry(g);
182  error = null;
183  return true;
184  } catch (Exception ex) {
185  geometry = Null;
186  error = ex;
187  return false;
188  }
189  }
190 
191  public static bool TryParse(string text, out SqlGeometry geometry) {
192  Exception error;
193  return TryParse(text, out geometry, out error);
194  }
195 
196  public static SqlGeometry Parse(string text) {
197  Exception error;
198  SqlGeometry geometry;
199  if (!TryParse(text, out geometry, out error))
200  throw new FormatException(String.Format("Could not parse the input string '{0}' to a valid GEOMETRY.", text), error);
201 
202  return geometry;
203  }
204 
205  public static bool TryParse(byte[] bytes, out SqlGeometry geometry) {
206  Exception error;
207  return TryParse(bytes, out geometry, out error);
208  }
209 
210  private static bool TryParse(byte[] bytes, out SqlGeometry geometry, out Exception error) {
211  if (bytes == null) {
212  geometry = Null;
213  error = new ArgumentNullException("bytes");
214  return false;
215  }
216 
217  IGeometry g;
218 
219  try {
220  var reader = new WKBReader(DefaultGeometryServices);
221  using (var stream = new MemoryStream(bytes)) {
222  g = reader.Read(stream);
223  }
224 
225  geometry = new SqlGeometry(g);
226  error = null;
227  return true;
228  } catch (Exception ex) {
229  error = ex;
230  geometry = Null;
231  return false;
232  }
233  }
234 
235  public static SqlGeometry Parse(byte[] bytes) {
236  Exception error;
237  SqlGeometry geometry;
238  if (!TryParse(bytes, out geometry, out error))
239  throw new FormatException("Could not parse the input bytes to a valid GEOMETRY.", error);
240 
241  return geometry;
242  }
243  }
244 }
bool IsComparableTo(SqlGeometry other)
Definition: SqlGeometry.cs:107
static bool TryParse(string text, out SqlGeometry geometry)
Definition: SqlGeometry.cs:191
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
SqlGeometry Buffer(double distance)
Definition: SqlGeometry.cs:123
SqlNumber Distance(SqlGeometry geometry)
Definition: SqlGeometry.cs:128
static bool TryParse(byte[] bytes, out SqlGeometry geometry, out Exception error)
Definition: SqlGeometry.cs:210
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
static SqlGeometry Parse(string text)
Definition: SqlGeometry.cs:196
double IConvertible. ToDouble(IFormatProvider provider)
Definition: SqlNumber.cs:317
SqlGeometry(IGeometry geometry)
Definition: SqlGeometry.cs:27
static readonly SqlNumber Null
Definition: SqlNumber.cs:31
static SqlGeometry Parse(byte[] bytes)
Definition: SqlGeometry.cs:235
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
static bool TryParse(byte[] bytes, out SqlGeometry geometry)
Definition: SqlGeometry.cs:205
SqlGeometry Buffer(SqlNumber distance)
Definition: SqlGeometry.cs:116
SqlGeometry(IGeometry geometry, bool isNull)
Definition: SqlGeometry.cs:22
int CompareTo(SqlGeometry other)
Definition: SqlGeometry.cs:49
Deveel.Data.Sql.Objects.SqlString SqlString
Definition: DataObject.cs:27
static readonly SqlBinary Null
Definition: SqlBinary.cs:29