Skip to content

Commit

Permalink
Merge pull request #165 from MoeweX/master
Browse files Browse the repository at this point in the history
Add pointLatLon convenience method
  • Loading branch information
dsmiley authored Aug 28, 2018
2 parents fbc615b + 45f0b4b commit fd860ad
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<version>0.8.2</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public OnePointsBuilder pointXYZ(double x, double y, double z) {
return this;
}

@Override
public OnePointsBuilder pointLatLon(double latitude, double longitude) {
assert point == null;
point = shapeFactory.pointLatLon(latitude, longitude);
return this;
}

public Point getPoint() {
return point;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/locationtech/spatial4j/shape/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ public interface Point extends Shape {
/** The Y coordinate, or Latitude in geospatial contexts. */
public double getY();

/** Convenience method that usually maps on {@link org.locationtech.spatial4j.shape.Point#getY()} */
public double getLat();

/** Convenience method that usually maps on {@link org.locationtech.spatial4j.shape.Point#getX()} */
public double getLon();

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public interface ShapeFactory {
/** Construct a point. */
Point pointXY(double x, double y);

/** Construct a point of latitude, longitude coordinates */
Point pointLatLon(double latitude, double longitude);

/** Construct a point of 3 dimensions. The implementation might ignore unsupported
* dimensions like 'z' or throw an error. */
Point pointXYZ(double x, double y, double z);
Expand Down Expand Up @@ -129,6 +132,8 @@ interface PointsBuilder<T> {
T pointXY(double x, double y);
/** @see ShapeFactory#pointXYZ(double, double, double) */
T pointXYZ(double x, double y, double z);
/** @see ShapeFactory#pointLatLon(double, double) */
T pointLatLon(double latitude, double longitude);
}

/** @see #lineString() */
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/locationtech/spatial4j/shape/impl/PointImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public double getY() {
return y;
}

@Override
public double getLat() {
return getY();
}

@Override
public double getLon() {
return getX();
}

@Override
public Rectangle getBoundingBox() {
return ctx.makeRectangle(this, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public Point pointXY(double x, double y) {
return new PointImpl(x, y, ctx);
}

@Override
public Point pointLatLon(double latitude, double longitude) {
verifyX(longitude);
verifyY(latitude);
return new PointImpl(longitude, latitude, ctx);
}

@Override
public Point pointXYZ(double x, double y, double z) {
return pointXY(x, y); // or throw?
Expand Down Expand Up @@ -173,6 +180,12 @@ public LineStringBuilder pointXYZ(double x, double y, double z) {
return this;
}

@Override
public LineStringBuilder pointLatLon(double latitude, double longitude) {
points.add(ShapeFactoryImpl.this.pointLatLon(latitude, longitude));
return this;
}

@Override
public Shape build() {
return new BufferedLineString(points, bufferDistance, false, ctx);
Expand Down Expand Up @@ -232,6 +245,12 @@ public MultiPointBuilder pointXYZ(double x, double y, double z) {
return this;
}

@Override
public MultiPointBuilder pointLatLon(double latitude, double longitude) {
shapes.add(ShapeFactoryImpl.this.pointLatLon(latitude, longitude));
return this;
}

@Override
public LineStringBuilder lineString() {
return ShapeFactoryImpl.this.lineString();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/locationtech/spatial4j/shape/jts/JtsPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public double getY() {
return isEmpty() ? Double.NaN : pointGeom.getY();
}

@Override
public double getLat() {
return getY();
}

@Override
public double getLon() {
return getX();
}

@Override
public void reset(double x, double y) {
assert ! isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ public T pointXYZ(double x, double y, double z) {
return getThis();
}

public T pointLatLon(double latitude, double longitude) {
return pointXYZ(longitude, latitude, Coordinate.NULL_ORDINATE);
}
// TODO would be be useful to add other ways of providing points? e.g. point(Coordinate)?

// TODO consider wrapping the List<Coordinate> in a custom CoordinateSequence and then (conditionally) use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private void assertDistanceConversion(double dist) {
}

private Point pLL(double lat, double lon) {
return ctx.makePoint(lon,lat);
return ctx.getShapeFactory().pointLatLon(lat, lon);
}

@Test
Expand Down

0 comments on commit fd860ad

Please sign in to comment.