Skip to content

Commit c90ed8c

Browse files
committed
Allow minus sign in point wkt
Very small coordinates that contain three or more zeros after the dot (like 0.000123) have a string representation of 1.23E-4. The readPointWkt did not account for this and set the end pos to the E, thus the BigDecimal tried to parse "1.23E". This resulted in a NumberFormatException. This fixes the issue by also allowing the minus sign when determining the end of the number.
1 parent 76e8115 commit c90ed8c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,8 @@ void readPointWkt() throws SQLServerException {
14841484

14851485
while (currentWktPos < wkt.length()
14861486
&& (Character.isDigit(wkt.charAt(currentWktPos)) || wkt.charAt(currentWktPos) == '.'
1487-
|| wkt.charAt(currentWktPos) == 'E' || wkt.charAt(currentWktPos) == 'e')) {
1487+
|| wkt.charAt(currentWktPos) == 'E' || wkt.charAt(currentWktPos) == 'e'
1488+
|| wkt.charAt(currentWktPos) == '-')) {
14881489
currentWktPos++;
14891490
}
14901491

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,18 @@ public void testLargeCases() throws SQLException {
21222122
}
21232123
}
21242124

2125+
/**
2126+
* Tests Geography almost zero coordinates like 0.0001234. The string representation is "1.234E-4", which
2127+
* caused a bug when creating a Geography object.
2128+
*/
2129+
@Test
2130+
public void testGeographySmallCoordinates() throws SQLException {
2131+
Geography g = Geography.point(0.0001234, 1.234, 4326);
2132+
2133+
assertEquals(0.0001234, g.getLatitude());
2134+
assertEquals(1.234, g.getLongitude());
2135+
}
2136+
21252137
private void beforeEachSetup() throws SQLException {
21262138
try (Statement stmt = connection.createStatement()) {
21272139
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(geomTableName), stmt);

0 commit comments

Comments
 (0)