Skip to content

Commit

Permalink
Add SegmentString default methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts committed Jan 19, 2024
1 parent 13135e8 commit 08dc89e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 08dc89e

Please sign in to comment.