From 08dc89e7dd791d750e19b6a150ef686e506f1641 Mon Sep 17 00:00:00 2001 From: Martin Davis Date: Fri, 19 Jan 2024 10:58:11 -0800 Subject: [PATCH] Add SegmentString default methods --- .../jts/noding/SegmentString.java | 58 +++++++++++++++++++ .../jts/noding/SegmentStringTest.java | 52 +++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 modules/core/src/test/java/org/locationtech/jts/noding/SegmentStringTest.java diff --git a/modules/core/src/main/java/org/locationtech/jts/noding/SegmentString.java b/modules/core/src/main/java/org/locationtech/jts/noding/SegmentString.java index 8860ab59b7..88d866dad0 100644 --- a/modules/core/src/main/java/org/locationtech/jts/noding/SegmentString.java +++ b/modules/core/src/main/java/org/locationtech/jts/noding/SegmentString.java @@ -36,8 +36,66 @@ public interface SegmentString */ public void setData(Object data); + /** + * Gets the number of coordinates in this segment string. + * + * @return the number of coordinates + */ public int size(); + + /** + * Gets the segment string coordinate at a given index. + * + * @param i the coordinate index + * @return the coordinate at the index + */ public Coordinate getCoordinate(int i); + + /** + * Gets the coordinates in this segment string. + * + * @return the coordinates as an array + */ public Coordinate[] getCoordinates(); + + /** + * Tests if a segment string is a closed ring. + * + * @return true if the segment string is closed + */ public boolean isClosed(); + + /** + * Gets the previous vertex in a ring from a vertex index. + * + * @param ringSS a segment string forming a ring + * @param index the vertex index + * @return the previous vertex in the ring + * + * @see #isClosed + */ + public default Coordinate prevInRing(int index) { + int prevIndex = index - 1; + if (prevIndex < 0) { + prevIndex = size() - 2; + } + return getCoordinate( prevIndex ); + } + + /** + * Gets the next vertex in a ring from a vertex index. + * + * @param ringSS a segment string forming a ring + * @param index the vertex index + * @return the next vertex in the ring + * + * @see #isClosed + */ + public default Coordinate nextInRing(int index) { + int nextIndex = index + 1; + if (nextIndex > size() - 1) { + nextIndex = 1; + } + return getCoordinate( nextIndex ); + } } diff --git a/modules/core/src/test/java/org/locationtech/jts/noding/SegmentStringTest.java b/modules/core/src/test/java/org/locationtech/jts/noding/SegmentStringTest.java new file mode 100644 index 0000000000..04df1d98ad --- /dev/null +++ b/modules/core/src/test/java/org/locationtech/jts/noding/SegmentStringTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2024 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.noding; + +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Geometry; + +import test.jts.GeometryTestCase; + +public class SegmentStringTest extends GeometryTestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run(SegmentStringTest.class); + } + + public SegmentStringTest(String name) { + super(name); + } + + public void testNextInRing() { + SegmentString ss = create("LINESTRING(0 0, 1 2, 3 1, 0 0)"); + assertTrue(ss.isClosed()); + checkEqualXY(ss.nextInRing(0), new Coordinate(1, 2)); + checkEqualXY(ss.nextInRing(1), new Coordinate(3, 1)); + checkEqualXY(ss.nextInRing(2), new Coordinate(0, 0)); + checkEqualXY(ss.nextInRing(3), new Coordinate(1, 2)); + } + + public void testPrevInRing() { + SegmentString ss = create("LINESTRING(0 0, 1 2, 3 1, 0 0)"); + assertTrue(ss.isClosed()); + checkEqualXY(ss.prevInRing(0), new Coordinate(3, 1)); + checkEqualXY(ss.prevInRing(1), new Coordinate(0, 0)); + checkEqualXY(ss.prevInRing(2), new Coordinate(1, 2)); + checkEqualXY(ss.prevInRing(3), new Coordinate(3, 1)); + } + + private SegmentString create(String wkt) { + Geometry geom = read(wkt); + return new BasicSegmentString(geom.getCoordinates(), null); + } + +}