/**
* Copyright (c) 2009-2011, chunquedong(YangJiandong)
*
* This file is part of ChunMap project
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
*
* History:
* 2010-05-05 Jed Young Creation
*/
package chunmap.model.geom;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import chunmap.model.algorithm.LinearReference;
import chunmap.model.coord.CoordSeqEditor;
import chunmap.model.coord.Coordinate2D;
import chunmap.model.coord.CoordinateSeq;
import chunmap.model.coord.CPoint;
import chunmap.model.elem.PointLineBag;
/**
* @author chunquedong
*
*/
public class LineStringTest {
private GeometryFactory factory=new GeometryFactory();
@Test
public void testToString() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(10, 20);
CPoint p2 = new Coordinate2D(30, 40);
points.add(p1);
points.add(p2);
LineString ls = new LineString(points);
String exp = "LINESTRING(10.0 20.0,30.0 40.0)";
assertTrue(exp.equals(ls.toString()));
}
@Test
public void testInsertPoint() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(10, 20);
CPoint p2 = new Coordinate2D(30, 60);
CPoint p3 = new Coordinate2D(20, 40);
CPoint p4 = new Coordinate2D(40, 80);
points.add(p1);
points.add(p2);
points.add(p4);
LineString ls = new LineString(points);
CoordSeqEditor les=new CoordSeqEditor(ls.getPoints());
les.tryInsertPoint(p3);
LineString ls2 = new LineString(les.toCoordinateSeq());
CoordSeqEditor les2=new CoordSeqEditor(ls.getPoints());
les.tryInsertPoint(p2);
LineString ls3 =new LineString(les2.toCoordinateSeq());
String exp2 = "LINESTRING(10.0 20.0,20.0 40.0,30.0 60.0,40.0 80.0)";
assertTrue(exp2.equals(ls2.toString()));
String exp3 = "LINESTRING(10.0 20.0,30.0 60.0,40.0 80.0)";
assertTrue(exp3.equals(ls3.toString()));
}
@Test
public void testBreakLine() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(10, 20);
CPoint p2 = new Coordinate2D(30, 60);
CPoint p3 = new Coordinate2D(20, 40);
CPoint p4 = new Coordinate2D(40, 80);
CPoint p5 = new Coordinate2D(4, 80);
points.add(p1);
points.add(p2);
points.add(p4);
LineString ls = new LineString(points);
List<CoordinateSeq> ls2 = LinearReference.splitLine(ls.getPoints(), p3);
List<CoordinateSeq> ls3 = LinearReference.splitLine(ls.getPoints(),p2);
List<CoordinateSeq> ls4 = LinearReference.splitLine(ls.getPoints(),p1);
List<CoordinateSeq> ls5 = LinearReference.splitLine(ls.getPoints(),p5);
String s = "LINESTRING(10.0 20.0,30.0 60.0,40.0 80.0)";
String s2 = "MULTILINESTRING((10.0 20.0,20.0 40.0),(20.0 40.0,30.0 60.0,40.0 80.0))";
String s3 = "MULTILINESTRING((10.0 20.0,30.0 60.0),(30.0 60.0,40.0 80.0))";
assertTrue(s.equals(ls.toString()));
assertTrue(s2.equals(factory.createMultiLineString(ls2).toString()));
assertTrue(s3.equals(factory.createMultiLineString(ls3).toString()));
assertTrue(s.equals(factory.createMultiLineString(ls4).getGeometry(0).toString()));
assertTrue(ls5.size() == 0);
}
@Test
public void testContainLineString() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(10, 20);
CPoint p2 = new Coordinate2D(20, 40);
CPoint p3 = new Coordinate2D(30, 60);
CPoint p4 = new Coordinate2D(40, 80);
CPoint p5 = new Coordinate2D(50, 100);
points.add(p1);
points.add(p3);
points.add(p5);
LineString ls = new LineString(points);
List<CPoint> points2 = new ArrayList<CPoint>();
points2.add(p2);
points2.add(p4);
LineString ls2 = new LineString(points2);
assertTrue(ls.containLineString(ls2));
}
@Test
public void testSubLineString() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(1, 1);
CPoint p2 = new Coordinate2D(1, 2);
CPoint p3 = new Coordinate2D(4, 2);
points.add(p1);
points.add(p2);
points.add(p3);
LineString ls = new LineString(points);
LineString sub = new LineString(
LinearReference.subLineString(ls.getPoints(),2, 3));
String s = "LINESTRING(2.0 2.0,3.0 2.0)";
assertTrue(s.equals(sub.toString()));
}
@Test
public void testGetLength() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(1, 1);
CPoint p2 = new Coordinate2D(1, 2);
CPoint p3 = new Coordinate2D(4, 2);
points.add(p1);
points.add(p2);
points.add(p3);
LineString ls = new LineString(points);
assertTrue(ls.getLength() == 4d);
}
@Test
public void testIsSimple() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(10, 20);
CPoint p3 = new Coordinate2D(30, 60);
CPoint p5 = new Coordinate2D(50, 100);
points.add(p1);
points.add(p3);
points.add(p5);
LineString ls = new LineString(points);
assertTrue(ls.isValid());
}
@Test
public void testIsSimple2() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(10, 20);
CPoint p2 = new Coordinate2D(20, 40);
CPoint p3 = new Coordinate2D(30, 60);
CPoint p5 = new Coordinate2D(50, 100);
points.add(p1);
points.add(p3);
points.add(p5);
points.add(p2);
LineString ls = new LineString(points);
assertTrue(!ls.isValid());
}
@Test
public void testIsSimple3() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(10, 20);
CPoint p3 = new Coordinate2D(30, 60);
CPoint p5 = new Coordinate2D(50, 100);
points.add(p1);
points.add(p3);
points.add(p5);
points.add(p1);
LineString ls = new LineString(points);
assertTrue(!ls.isValid());
}
@Test
public void testIsSimple4() {
List<CPoint> points = new ArrayList<CPoint>();
CPoint p1 = new Coordinate2D(1, 1);
CPoint p2 = new Coordinate2D(1, 2);
CPoint p3 = new Coordinate2D(3, 2);
CPoint p4 = new Coordinate2D(4, 0);
points.add(p3);
points.add(p1);
points.add(p2);
points.add(p3);
points.add(p4);
LineString ls = new LineString(points);
assertTrue(ls.isValid());
}
@Test
public void testJoin() {
WktReader wkt = new WktReader();
Geometry g = wkt.read("LineString(334 23,1230.09 234)");
Geometry g2 = wkt.read("LineString(32 34,334 23)");
LineString l = (LineString) g;
LineString l2 = (LineString) g2;
CoordSeqEditor lineEditor = new CoordSeqEditor(l.getPoints());
lineEditor.join(l2.getPoints());
LineString rl2 = new LineString(lineEditor.toCoordinateSeq());
String s = "LINESTRING(32.0 34.0,334.0 23.0,1230.09 234.0)";
assertTrue(s.equals(rl2.toString()));
}
@Test
public void testChain() {
WktReader wkt = new WktReader();
Geometry g = wkt.read("LINESTRING(0 4,4 0)");
Geometry g2 = wkt.read("LINESTRING(1 1,2 1,2 3)");
LineString l = (LineString) g;
LineString l2 = (LineString) g2;
PointLineBag bag = l.intersection(l2);
assertTrue(!bag.isEmpty());
}
@Test
public void testChain2() {
WktReader wkt = new WktReader();
Geometry g = wkt
.read("LINESTRING(1.0 6.0,5.999998414659173 1.0039816335536662,1.0079632645824341 -3.999993658637697,-3.9999857319385903 0.9880551094385923,1.0 6.0)");
LineString ls = (LineString) g;
Geometry g2 = wkt
.read("LINESTRING(-2.639991350383829 -1.7999950537374092,-1.8399918576928131 -1.7999950537374092,-1.8399918576928131 -0.9999955610463935,-2.639991350383829 -0.9999955610463935,-2.639991350383829 -1.7999950537374092)");
LineString ls2 = (LineString) g2;
PointLineBag bag = ls.intersection(ls2);
assertTrue(!bag.isEmpty());
}
@Test
public void testChain3() {
WktReader wkt = new WktReader();
Geometry g = wkt
.read("LINESTRING(-4.125146582183989 2.162505569897099,-3.4010910293343892 2.162505569897099,-3.4010910293343892 2.8865611227466985,-4.125146582183989 2.8865611227466985,-4.125146582183989 2.162505569897099)");
LineString ls = (LineString) g;
Geometry g2 = wkt
.read("LINESTRING(1.0 6.0,5.75429730253235 2.5481140652851213,3.9440778098389764 -3.0413371363465505,-1.9311899958501355 -3.0506944106199967,-3.759204394078429 2.5329623398454943,1.0 6.0)");
LineString ls2 = (LineString) g2;
PointLineBag bag = ls.intersection(ls2);
assertTrue(!bag.isEmpty());
}
@Test
public void testChain4() {
WktReader wkt = new WktReader();
Geometry g = wkt
.read("LINESTRING(-3.9419812736325976 1.0200031579984215,-3.141981780941582 1.0200031579984215,-3.141981780941582 1.8200026506894371,-3.9419812736325976 1.8200026506894371,-3.9419812736325976 1.0200031579984215)");
LineString ls = (LineString) g;
Geometry g2 = wkt
.read("LINESTRING(1.0 6.0,5.328799197461722 3.502298445041029,5.332779000281329 -1.4954009967780992,1.0079632645824363 -3.999993658637697,-3.324808414448497 -1.5091895461115477,-3.3367478128123684 3.488497218818446,1.0 6.0)");
LineString ls2 = (LineString) g2;
PointLineBag bag = ls.intersection(ls2);
assertTrue(!bag.isEmpty());
}
@Test
public void testChain5() {
WktReader wkt = new WktReader();
Geometry g = wkt
.read("LINESTRING(1.0 6.0,5.999998414659173 1.0039816335536662,1.0079632645824341 -3.999993658637697,-3.9999857319385903 0.9880551094385923,1.0 6.0)");
LineString ls = (LineString) g;
Geometry g2 = wkt
.read("LINESTRING(3.3400048574815147 -2.1999948000829175,4.140004350172529 -2.1999948000829175,4.140004350172529 -1.399995307391901,3.3400048574815147 -1.399995307391901,3.3400048574815147 -2.1999948000829175)");
LineString ls2 = (LineString) g2;
PointLineBag bag = ls.intersection(ls2);
assertTrue(!bag.isEmpty());
}
}