15
15
// along with NHibernate.Spatial; if not, write to the Free Software
16
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
18
- using NetTopologySuite . Geometries ;
19
- using NetTopologySuite . IO ;
20
18
using System ;
21
19
using System . Data . Common ;
20
+ using NetTopologySuite . Geometries ;
22
21
using NHibernate . Engine ;
23
22
using NHibernate . SqlTypes ;
24
23
using NHibernate . Type ;
28
27
namespace NHibernate . Spatial . Type
29
28
{
30
29
[ Serializable ]
31
- public class PostGisGeometryType : GeometryTypeBase < byte [ ] >
30
+ public class PostGisGeometryType : GeometryTypeBase < Geometry >
32
31
{
33
32
private static readonly NullableType GeometryType = new CustomGeometryType ( ) ;
34
33
@@ -45,45 +44,17 @@ public PostGisGeometryType()
45
44
/// </summary>
46
45
/// <param name="value">The GeoAPI geometry value.</param>
47
46
/// <returns></returns>
48
- protected override byte [ ] FromGeometry ( object value )
47
+ protected override Geometry FromGeometry ( object value )
49
48
{
50
- Geometry geometry = value as Geometry ;
49
+ var geometry = value as Geometry ;
51
50
if ( geometry == null )
52
51
{
53
52
return null ;
54
53
}
55
- // PostGIS can't parse a WKB of any empty geometry other than GeomtryCollection
56
- // (throws the error: "geometry requires more points")
57
- // and parses WKT of empty geometries always as GeometryCollection
58
- // (ie. "select AsText(GeomFromText('LINESTRING EMPTY', -1)) = 'GEOMETRYCOLLECTION EMPTY'").
59
- // Force GeometryCollection.Empty to avoid the error.
60
- if ( ! ( geometry is GeometryCollection ) && geometry . IsEmpty )
61
- {
62
- geometry = GeometryCollection . Empty ;
63
- }
64
54
65
55
this . SetDefaultSRID ( geometry ) ;
66
56
67
- // Determine the ordinality of the geometry to ensure 3D and 4D geometries are
68
- // correctly serialized by PostGisWriter (see issue #66)
69
- // NOTE: Cannot use InteriorPoint here as that always returns a 2D point (see #120)
70
- // TODO: Is there a way of getting the ordinates directly from the geometry?
71
- var ordinates = Ordinates . XY ;
72
- var coordinate = geometry . Coordinate ;
73
- if ( coordinate != null && ! double . IsNaN ( coordinate . Z ) )
74
- {
75
- ordinates |= Ordinates . Z ;
76
- }
77
- if ( coordinate != null && ! double . IsNaN ( coordinate . M ) )
78
- {
79
- ordinates |= Ordinates . M ;
80
- }
81
-
82
- var postGisWriter = new PostGisWriter
83
- {
84
- HandleOrdinates = ordinates
85
- } ;
86
- return postGisWriter . Write ( geometry ) ;
57
+ return geometry ;
87
58
}
88
59
89
60
/// <summary>
@@ -93,15 +64,14 @@ protected override byte[] FromGeometry(object value)
93
64
/// <returns></returns>
94
65
protected override Geometry ToGeometry ( object value )
95
66
{
96
- var bytes = value as byte [ ] ;
97
- if ( bytes == null )
67
+ var geometry = value as Geometry ;
68
+ if ( geometry == null )
98
69
{
99
70
return null ;
100
71
}
101
72
102
- PostGisReader reader = new PostGisReader ( ) ;
103
- Geometry geometry = reader . Read ( bytes ) ;
104
73
this . SetDefaultSRID ( geometry ) ;
74
+
105
75
return geometry ;
106
76
}
107
77
@@ -114,16 +84,7 @@ internal CustomGeometryType() : base(new BinarySqlType())
114
84
115
85
public override object Get ( DbDataReader rs , int index , ISessionImplementor session )
116
86
{
117
- // Npgsql 3 from the received bytes creates his own PostGisGeometry type.
118
- // As we need to return a byte array that represents the geometry object,
119
- // we will retrive the bytes from the reader instead.
120
- var length = ( int ) rs . GetBytes ( index , 0 , null , 0 , 0 ) ;
121
- var buffer = new byte [ length ] ;
122
- if ( length > 0 )
123
- {
124
- rs . GetBytes ( index , 0 , buffer , 0 , length ) ;
125
- }
126
- return buffer ;
87
+ return ( Geometry ) rs . GetValue ( index ) ;
127
88
}
128
89
129
90
public override object Get ( DbDataReader rs , string name , ISessionImplementor session )
@@ -144,8 +105,8 @@ public override void Set(DbCommand cmd, object value, int index, ISessionImpleme
144
105
145
106
public override object DeepCopyNotNull ( object value )
146
107
{
147
- var arr = ( byte [ ] ) value ;
148
- return arr . Clone ( ) ;
108
+ var obj = ( Geometry ) value ;
109
+ return obj . Copy ( ) ;
149
110
}
150
111
}
151
112
}
0 commit comments