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

API and JavaDoc changes for Spatial Datatypes #752

Merged
merged 28 commits into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
73edf05
Added more exceptions for fuzz testing, and added some more javadocs
peterbae Jul 18, 2018
a09c247
fixing merge break
peterbae Jul 18, 2018
3f694ff
removing log files
peterbae Jul 18, 2018
243f294
javadoc changes
peterbae Jul 19, 2018
537959f
gets -> returns
peterbae Jul 19, 2018
862d71d
add test
peterbae Jul 20, 2018
d86b237
split points into two
peterbae Jul 20, 2018
eac074f
move error message around
peterbae Jul 20, 2018
79c7e59
check neg size for all number of sizes
peterbae Jul 20, 2018
81a065f
reflect comments + make internal spatial datatype file protected
peterbae Jul 23, 2018
7ec6fcc
more javadoc changes
peterbae Jul 23, 2018
272a729
switch lat/long
peterbae Jul 23, 2018
54c5297
refactor out duplicate code
peterbae Jul 23, 2018
a3eb552
add null checking + change int to long
peterbae Jul 23, 2018
0de6184
handle m and z value being null case and add test
peterbae Jul 23, 2018
b4fca56
reuse function
peterbae Jul 23, 2018
3135521
move this part to other PR
peterbae Jul 24, 2018
2dfb95b
reflect comment
peterbae Jul 24, 2018
6f4af3d
bit more javadoc
peterbae Jul 24, 2018
8aba9e6
remove StringIndexOutOfBoundsException
peterbae Jul 25, 2018
589c1c0
check wkt length
peterbae Jul 25, 2018
a61a58b
Merge branch 'dev' into Fuzz_And_Javadoc_fix
peterbae Jul 25, 2018
1d7800d
remove catching runtime exception
peterbae Jul 26, 2018
1f3e437
Merge branch 'Fuzz_And_Javadoc_fix' of https://github.com/peterbae/ms…
peterbae Jul 26, 2018
35d99a8
dont need those imports
peterbae Jul 26, 2018
e156263
use try blocks they're the best
peterbae Jul 26, 2018
5ca2a83
refactor throwing illegal WKT position into a method
peterbae Jul 26, 2018
2f886a0
apparently 0 and null are different, so fix that
peterbae Jul 26, 2018
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
179 changes: 45 additions & 134 deletions src/main/java/com/microsoft/sqlserver/jdbc/Geography.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

package com.microsoft.sqlserver.jdbc;

import java.nio.BufferUnderflowException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need to import this?

import java.nio.ByteBuffer;
import java.nio.ByteOrder;


/**
* Geography datatype represents data in a round-earth coordinate system.
*/

public class Geography extends SQLServerSpatialDatatype {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs class description


/**
* Private constructor used for creating a Geography object from WKT and srid.
* Private constructor used for creating a Geography object from WKT and Spatial Reference Identifier.
*
* @param WellKnownText
* Well-Known Text (WKT) provided by the user.
Expand All @@ -22,17 +27,20 @@ public class Geography extends SQLServerSpatialDatatype {
* if an exception occurs
*/
private Geography(String WellKnownText, int srid) throws SQLServerException {
if (null == WellKnownText || WellKnownText.length() <= 0) {
throwIllegalWKT();
}

this.wkt = WellKnownText;
this.srid = srid;

try {
parseWKTForSerialization(this, currentWktPos, -1, false);
} catch (StringIndexOutOfBoundsException e) {
String strError = SQLServerException.getErrString("R_illegalWKT");
throw new SQLServerException(strError, null, 0, null);
throwIllegalWKT();
}

serializeToWkb(false);
serializeToWkb(false, this);
isNull = false;
}

Expand All @@ -45,11 +53,19 @@ private Geography(String WellKnownText, int srid) throws SQLServerException {
* if an exception occurs
*/
private Geography(byte[] wkb) throws SQLServerException {
if (null == wkb || wkb.length <= 0) {
throwIllegalWKB();
}

this.wkb = wkb;
buffer = ByteBuffer.wrap(wkb);
buffer.order(ByteOrder.LITTLE_ENDIAN);

parseWkb();
try {
parseWkb(this);
} catch (BufferUnderflowException e) {
throwIllegalWKB();
}

WKTsb = new StringBuffer();
WKTsbNoZM = new StringBuffer();
Expand All @@ -62,8 +78,8 @@ private Geography(byte[] wkb) throws SQLServerException {
}

/**
* Returns a Geography instance from an Open Geospatial Consortium (OGC) Well-Known Text (WKT) representation
* augmented with any Z (elevation) and M (measure) values carried by the instance.
* Constructor for a Geography instance from an Open Geospatial Consortium (OGC) Well-Known Text (WKT)
* representation augmented with any Z (elevation) and M (measure) values carried by the instance.
*
* @param wkt
* Well-Known Text (WKT) provided by the user.
Expand All @@ -78,7 +94,8 @@ public static Geography STGeomFromText(String wkt, int srid) throws SQLServerExc
}

/**
* Returns a Geography instance from an Open Geospatial Consortium (OGC) Well-Known Binary (WKB) representation.
* Constructor for a Geography instance from an Open Geospatial Consortium (OGC) Well-Known Binary (WKB)
* representation.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
Expand All @@ -91,7 +108,7 @@ public static Geography STGeomFromWKB(byte[] wkb) throws SQLServerException {
}

/**
* Returns a constructed Geography from an internal SQL Server format for spatial data.
* Constructor for a Geography instance from an internal SQL Server format for spatial data.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
Expand All @@ -104,8 +121,8 @@ public static Geography deserialize(byte[] wkb) throws SQLServerException {
}

/**
* Returns a Geography instance from an Open Geospatial Consortium (OGC) Well-Known Text (WKT) representation. SRID
* is defaulted to 4326.
* Constructor for a Geography instance from an Open Geospatial Consortium (OGC) Well-Known Text (WKT)
* representation. Spatial Reference Identifier is defaulted to 4326.
*
* @param wkt
* Well-Known Text (WKT) provided by the user.
Expand All @@ -118,14 +135,15 @@ public static Geography parse(String wkt) throws SQLServerException {
}

/**
* Constructs a Geography instance that represents a Point instance from its X and Y values and an SRID.
* Constructor for a Geography instance that represents a Point instance from its latitude and longitude values and
* a Spatial Reference Identifier.
*
* @param x
* x coordinate
* latitude
* @param y
* y coordinate
* longitude
* @param srid
* SRID
* Spatial Reference Identifier value
* @return Geography Geography instance
* @throws SQLServerException
* if an exception occurs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you missed out on the method params: x and y should be changed to latitude/longitude respectively.
Refer: MS Docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Expand All @@ -147,7 +165,7 @@ public String STAsText() throws SQLServerException {
buffer = ByteBuffer.wrap(wkb);
buffer.order(ByteOrder.LITTLE_ENDIAN);

parseWkb();
parseWkb(this);

WKTsb = new StringBuffer();
WKTsbNoZM = new StringBuffer();
Expand All @@ -165,7 +183,7 @@ public String STAsText() throws SQLServerException {
*/
public byte[] STAsBinary() {
if (null == wkbNoZM) {
serializeToWkb(true);
serializeToWkb(true, this);
}
return wkbNoZM;
}
Expand Down Expand Up @@ -198,25 +216,25 @@ public boolean hasZ() {
}

/**
* Returns the X coordinate value.
* Returns the latitude value.
*
* @return double value that represents the X coordinate.
* @return double value that represents the latitude.
*/
public Double getX() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && points.length == 2) {
return points[0];
public Double getLatitude() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && xValues.length == 1) {
return xValues[0];
}
return null;
}

/**
* Returns the Y coordinate value.
* Returns the longitude value.
*
* @return double value that represents the Y coordinate.
* @return double value that represents the longitude.
*/
public Double getY() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && points.length == 2) {
return points[1];
public Double getLongitude() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && yValues.length == 1) {
return yValues[0];
}
return null;
}
Expand Down Expand Up @@ -302,111 +320,4 @@ public String asTextZM() {
public String toString() {
return wkt;
}

protected void serializeToWkb(boolean noZM) {
ByteBuffer buf = ByteBuffer.allocate(determineWkbCapacity());
createSerializationProperties();

buf.order(ByteOrder.LITTLE_ENDIAN);
buf.putInt(srid);
buf.put(version);
buf.put(serializationProperties);

if (!isSinglePoint && !isSingleLineSegment) {
buf.putInt(numberOfPoints);
}

for (int i = 0; i < numberOfPoints; i++) {
buf.putDouble(points[2 * i + 1]);
buf.putDouble(points[2 * i]);
}

if (!noZM) {
if (hasZvalues) {
for (int i = 0; i < numberOfPoints; i++) {
buf.putDouble(zValues[i]);
}
}
if (hasMvalues) {
for (int i = 0; i < numberOfPoints; i++) {
buf.putDouble(mValues[i]);
}
}
}

if (isSinglePoint || isSingleLineSegment) {
wkb = buf.array();
return;
}

buf.putInt(numberOfFigures);
for (int i = 0; i < numberOfFigures; i++) {
buf.put(figures[i].getFiguresAttribute());
buf.putInt(figures[i].getPointOffset());
}

buf.putInt(numberOfShapes);
for (int i = 0; i < numberOfShapes; i++) {
buf.putInt(shapes[i].getParentOffset());
buf.putInt(shapes[i].getFigureOffset());
buf.put(shapes[i].getOpenGISType());
}

if (version == 2 && null != segments) {
buf.putInt(numberOfSegments);
for (int i = 0; i < numberOfSegments; i++) {
buf.put(segments[i].getSegmentType());
}
}

if (noZM) {
wkbNoZM = buf.array();
} else {
wkb = buf.array();

}
return;
}

protected void parseWkb() {
srid = buffer.getInt();
version = buffer.get();
serializationProperties = buffer.get();

interpretSerializationPropBytes();
readNumberOfPoints();
readPoints();

if (hasZvalues) {
readZvalues();
}

if (hasMvalues) {
readMvalues();
}

if (!(isSinglePoint || isSingleLineSegment)) {
readNumberOfFigures();
readFigures();
readNumberOfShapes();
readShapes();
}

determineInternalType();

if (buffer.hasRemaining()) {
if (version == 2 && internalType.getTypeCode() != 8 && internalType.getTypeCode() != 11) {
readNumberOfSegments();
readSegments();
}
}
}

private void readPoints() {
points = new double[2 * numberOfPoints];
for (int i = 0; i < numberOfPoints; i++) {
points[2 * i + 1] = buffer.getDouble();
points[2 * i] = buffer.getDouble();
}
}
}
Loading