Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue with Geography.point method having coordinates reversed #853

Merged
merged 6 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,14 @@ static final Object convertStreamToObject(BaseInputStream stream, TypeInfo typeI
if (JDBCType.GUID == jdbcType) {
return Util.readGUID(byteValue);
} else if (JDBCType.GEOMETRY == jdbcType) {
if (!typeInfo.getSSTypeName().equalsIgnoreCase(jdbcType.toString())) {
DataTypes.throwConversionError(typeInfo.getSSTypeName().toUpperCase(), jdbcType.toString());
}
return Geometry.STGeomFromWKB(byteValue);
} else if (JDBCType.GEOGRAPHY == jdbcType) {
if (!typeInfo.getSSTypeName().equalsIgnoreCase(jdbcType.toString())) {
DataTypes.throwConversionError(typeInfo.getSSTypeName().toUpperCase(), jdbcType.toString());
}
return Geography.STGeomFromWKB(byteValue);
} else {
String hexString = Util.bytesToHexString(byteValue, byteValue.length);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/microsoft/sqlserver/jdbc/Geography.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static Geography parse(String wkt) throws SQLServerException {
* if an exception occurs
*/
public static Geography point(double lat, double lon, int srid) throws SQLServerException {
return new Geography("POINT (" + lat + " " + lon + ")", srid);
return new Geography("POINT (" + lon + " " + lat + ")", srid);
}

/**
Expand Down Expand Up @@ -212,8 +212,8 @@ public boolean hasZ() {
* @return double value that represents the latitude.
*/
public Double getLatitude() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && xValues.length == 1) {
return xValues[0];
if (null != internalType && internalType == InternalSpatialDatatype.POINT && yValues.length == 1) {
return yValues[0];
}
return null;
}
Expand All @@ -224,8 +224,8 @@ public Double getLatitude() {
* @return double value that represents the longitude.
*/
public Double getLongitude() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && yValues.length == 1) {
return yValues[0];
if (null != internalType && internalType == InternalSpatialDatatype.POINT && xValues.length == 1) {
return xValues[0];
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package com.microsoft.sqlserver.jdbc.datatypes;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
Expand Down Expand Up @@ -812,7 +813,7 @@ public void testPoint() throws SQLException {
String geoWKT = "POINT(1 2)";

Geometry geomWKT = Geometry.point(1, 2, 0);
Geography geogWKT = Geography.point(1, 2, 4326);
Geography geogWKT = Geography.point(2, 1, 4326);

try (Connection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
Statement stmt = con.createStatement()) {
Expand Down Expand Up @@ -968,8 +969,8 @@ public void testGetXGetY() throws SQLException {

x = geog.getLatitude();
y = geog.getLongitude();
assertEquals(x, 1);
assertEquals(y, 2);
assertEquals(x, 2);
assertEquals(y, 1);
}

@Test
Expand Down Expand Up @@ -1041,6 +1042,48 @@ public void testNull() throws SQLException {
}
}
}

@Test
public void testWrongtype() throws SQLException {
beforeEachSetup();

Geometry geomWKT = Geometry.point(1, 2, 0);
Geography geogWKT = Geography.point(2, 1, 4326);

try (Connection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
Statement stmt = con.createStatement()) {

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement(
"insert into " + AbstractSQLGenerator.escapeIdentifier(geomTableName) + " values (?)");) {
pstmt.setGeometry(1, geomWKT);
pstmt.execute();

try {
SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(geomTableName));
rs.next();
rs.getGeography(1); // should fail
fail();
} catch (SQLServerException e) {
assertEquals(e.getMessage(), "The conversion from GEOMETRY to GEOGRAPHY is unsupported.");
}
}

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement(
"insert into " + AbstractSQLGenerator.escapeIdentifier(geogTableName) + " values (?)");) {
pstmt.setGeography(1, geogWKT);
pstmt.execute();

try {
SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(geogTableName));
rs.next();
rs.getGeometry(1); // should fail
fail();
} catch (SQLServerException e) {
assertEquals(e.getMessage(), "The conversion from GEOGRAPHY to GEOMETRY is unsupported.");
}
}
}
}

private void beforeEachSetup() throws SQLException {
try (Connection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
Expand Down