1 /*
2  * Copyright (c) 2016 Vivid Solutions.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
8  * and the Eclipse Distribution License is available at
9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 
13 package org.locationtech.jtstest.function;
14 
15 
16 import org.locationtech.jts.geom.*;
17 import org.locationtech.jts.linearref.LengthIndexedLine;
18 import org.locationtech.jtstest.geomfunction.Metadata;
19 
20 public class LinearReferencingFunctions
21 {
extractPoint(Geometry g, double index)22   public static Geometry extractPoint(Geometry g, double index)
23   {
24     LengthIndexedLine ll = new LengthIndexedLine(g);
25     Coordinate p = ll.extractPoint(index);
26     return g.getFactory().createPoint(p);
27   }
extractLine(Geometry g, @Metadata(title=R) double start, @Metadata(title=R) double end)28   public static Geometry extractLine(Geometry g,
29       @Metadata(title="Start length")
30       double start,
31       @Metadata(title="End length")
32       double end)
33   {
34     LengthIndexedLine ll = new LengthIndexedLine(g);
35     return ll.extractLine(start, end);
36   }
project(Geometry g, Geometry g2)37   public static Geometry project(Geometry g, Geometry g2)
38   {
39     LengthIndexedLine ll = new LengthIndexedLine(g);
40     if (g2.getDimension() == 1) {
41       LineString line = (LineString) g2.getGeometryN(0);
42       Coordinate pStart = line.getCoordinateN(0);
43       Coordinate pEnd = line.getCoordinateN(line.getNumPoints() - 1);
44       double indexStart = ll.project(pStart);
45       double indexEnd = ll.project(pEnd);
46       Geometry lineProj = ll.extractLine(indexStart, indexEnd);
47       return lineProj;
48     }
49     else {
50       double index = ll.project(g2.getCoordinate());
51       Coordinate p = ll.extractPoint(index);
52       return g.getFactory().createPoint(p);
53     }
54   }
projectIndex(Geometry g, Geometry g2)55   public static double projectIndex(Geometry g, Geometry g2)
56   {
57     LengthIndexedLine ll = new LengthIndexedLine(g);
58     return ll.project(g2.getCoordinate());
59   }
60 
61 }
62