Skip to content

Commit 7dd953e

Browse files
authored
Fix STAsBinary returning null for a single point (#1074)
* populate wkb for point * fix stasbinary issue * change variable name * apply same variable name change
1 parent 40fb4fc commit 7dd953e

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSpatialDatatype.java

+39-19
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,31 @@ abstract class SQLServerSpatialDatatype {
9494
/**
9595
* Serializes the Geogemetry/Geography instance to WKB.
9696
*
97-
* @param noZM
98-
* flag to indicate if Z and M coordinates should be included
97+
* @param excludeZMFromWKB
98+
* flag to indicate if Z and M coordinates should be excluded from the WKB representation
9999
* @param type
100100
* Type of Spatial Datatype (Geometry/Geography)
101101
*/
102-
protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
103-
ByteBuffer buf = ByteBuffer.allocate(determineWkbCapacity());
102+
protected void serializeToWkb(boolean excludeZMFromWKB, SQLServerSpatialDatatype type) {
103+
ByteBuffer buf = ByteBuffer.allocate(determineWkbCapacity(excludeZMFromWKB));
104104
createSerializationProperties();
105105

106106
buf.order(ByteOrder.LITTLE_ENDIAN);
107107
buf.putInt(srid);
108108
buf.put(version);
109-
buf.put(serializationProperties);
109+
if (excludeZMFromWKB) {
110+
byte serializationPropertiesNoZM = serializationProperties;
111+
if (hasZvalues) {
112+
serializationPropertiesNoZM -= hasZvaluesMask;
113+
}
114+
115+
if (hasMvalues) {
116+
serializationPropertiesNoZM -= hasMvaluesMask;
117+
}
118+
buf.put(serializationPropertiesNoZM);
119+
} else {
120+
buf.put(serializationProperties);
121+
}
110122

111123
if (!isSinglePoint && !isSingleLineSegment) {
112124
buf.putInt(numberOfPoints);
@@ -124,7 +136,7 @@ protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
124136
}
125137
}
126138

127-
if (!noZM) {
139+
if (!excludeZMFromWKB) {
128140
if (hasZvalues) {
129141
for (int i = 0; i < numberOfPoints; i++) {
130142
buf.putDouble(zValues[i]);
@@ -139,7 +151,11 @@ protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
139151
}
140152

141153
if (isSinglePoint || isSingleLineSegment) {
142-
wkb = buf.array();
154+
if (excludeZMFromWKB) {
155+
wkbNoZM = buf.array();
156+
} else {
157+
wkb = buf.array();
158+
}
143159
return;
144160
}
145161

@@ -163,7 +179,7 @@ protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
163179
}
164180
}
165181

166-
if (noZM) {
182+
if (excludeZMFromWKB) {
167183
wkbNoZM = buf.array();
168184
} else {
169185
wkb = buf.array();
@@ -1282,32 +1298,36 @@ protected void createSerializationProperties() {
12821298
}
12831299
}
12841300

1285-
protected int determineWkbCapacity() {
1301+
protected int determineWkbCapacity(boolean excludeZMFromWKB) {
12861302
int totalSize = 0;
12871303

12881304
totalSize += 6; // SRID + version + SerializationPropertiesByte
12891305

12901306
if (isSinglePoint || isSingleLineSegment) {
12911307
totalSize += 16 * numberOfPoints;
12921308

1293-
if (hasZvalues) {
1294-
totalSize += 8 * numberOfPoints;
1295-
}
1309+
if (!excludeZMFromWKB) {
1310+
if (hasZvalues) {
1311+
totalSize += 8 * numberOfPoints;
1312+
}
12961313

1297-
if (hasMvalues) {
1298-
totalSize += 8 * numberOfPoints;
1314+
if (hasMvalues) {
1315+
totalSize += 8 * numberOfPoints;
1316+
}
12991317
}
13001318

13011319
return totalSize;
13021320
}
13031321

13041322
int pointSize = 16;
1305-
if (hasZvalues) {
1306-
pointSize += 8;
1307-
}
1323+
if (!excludeZMFromWKB) {
1324+
if (hasZvalues) {
1325+
pointSize += 8;
1326+
}
13081327

1309-
if (hasMvalues) {
1310-
pointSize += 8;
1328+
if (hasMvalues) {
1329+
pointSize += 8;
1330+
}
13111331
}
13121332

13131333
totalSize += 12; // 4 bytes for 3 ints, each representing the number of points, shapes and figures

src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.microsoft.sqlserver.jdbc.datatypes;
66

77
import static org.junit.Assert.fail;
8+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
89
import static org.junit.jupiter.api.Assertions.assertEquals;
910

1011
import java.io.IOException;
@@ -957,8 +958,8 @@ public void testSTAsBinary() throws SQLException {
957958
byte[] geomWKB2 = geomWKT2.STAsBinary();
958959
byte[] geogWKB2 = geogWKT2.STAsBinary();
959960

960-
assertEquals(geomWKB, geomWKB2);
961-
assertEquals(geogWKB, geogWKB2);
961+
assertArrayEquals(geomWKB, geomWKB2);
962+
assertArrayEquals(geogWKB, geogWKB2);
962963
}
963964

964965
@Test

0 commit comments

Comments
 (0)