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

Fix Geometry.getCoordinate for collections with empty first element #987

Merged
merged 1 commit into from
Jun 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,14 @@ public PrecisionModel getPrecisionModel() {
}

/**
* Returns a vertex of this <code>Geometry</code>
* (usually, but not necessarily, the first one).
* Returns a vertex of this geometry
* (usually, but not necessarily, the first one),
* or <code>null</code> if the geometry is empty.
* The returned coordinate should not be assumed
* to be an actual Coordinate object used in
* to be an actual <code>Coordinate</code> object used in
* the internal representation.
*
*@return a {@link Coordinate} which is a vertex of this <code>Geometry</code>.
*@return a coordinate which is a vertex of this <code>Geometry</code>.
*@return null if this Geometry is empty
*/
public abstract Coordinate getCoordinate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ public GeometryCollection(Geometry[] geometries, GeometryFactory factory) {
}

public Coordinate getCoordinate() {
if (isEmpty()) return null;
return geometries[0].getCoordinate();
for (int i = 0; i < geometries.length; i++) {
if (! geometries[i].isEmpty()) {
return geometries[i].getCoordinate();
}
}
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2023 Martin Davis.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.locationtech.jts.geom;

import test.jts.GeometryTestCase;

public class GeometryCoordinateTest extends GeometryTestCase {

public static void main(String[] args) throws Exception {
junit.textui.TestRunner.run(GeometryCoordinateTest.class);
}

public GeometryCoordinateTest(String name) {
super(name);
}

public void testPoint() {
checkCoordinate( "POINT (1 1)", 1, 1);
}

public void testLineString() {
checkCoordinate( "LINESTRING (1 1, 2 2)", 1, 1);
}

public void testPolygon() {
checkCoordinate( "POLYGON ((1 1, 1 2, 2 1, 1 1))", 1, 1);
}

public void testEmptyElementsAll() {
checkCoordinate( "GEOMETRYCOLLECTION ( LINESTRING EMPTY, POINT EMPTY )");
}

public void testEmptyFirstElementPolygonal() {
checkCoordinate( "MULTIPOLYGON ( EMPTY, ((1 1, 1 2, 2 1, 1 1)) )", 1, 1);
}

public void testEmptyFirstElement() {
checkCoordinate( "GEOMETRYCOLLECTION ( LINESTRING EMPTY, POINT(1 1) )", 1, 1);
}

public void testEmptySecondElement() {
checkCoordinate( "GEOMETRYCOLLECTION ( POINT(1 1), LINESTRING EMPTY )", 1, 1);
}

private void checkCoordinate(String wkt, int x, int y) {
checkCoordinate(read(wkt), new Coordinate(x, y));
}

private void checkCoordinate(final Geometry g, Coordinate expected) {
Coordinate actual = g.getCoordinate();
checkEqualXY( expected, actual );
}

private void checkCoordinate(String wkt) {
Geometry g = read(wkt);
Coordinate actual = g.getCoordinate();
assertNull(actual);
}
}