/
GeometryUtils.java
200 lines (172 loc) · 9.07 KB
/
GeometryUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package org.opentripplanner.common.geometry;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.linearref.LengthLocationMap;
import com.vividsolutions.jts.linearref.LinearLocation;
import com.vividsolutions.jts.linearref.LocationIndexedLine;
import org.geojson.GeoJsonObject;
import org.geojson.LngLatAlt;
import org.geotools.referencing.CRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opentripplanner.analyst.UnsupportedGeometryException;
import org.opentripplanner.common.model.P2;
import java.util.List;
public class GeometryUtils {
private static CoordinateSequenceFactory csf = new Serializable2DPackedCoordinateSequenceFactory();
private static GeometryFactory gf = new GeometryFactory(csf);
/** A shared copy of the WGS84 CRS with longitude-first axis order. */
public static final CoordinateReferenceSystem WGS84_XY;
static {
try {
WGS84_XY = CRS.getAuthorityFactory(true).createCoordinateReferenceSystem("EPSG:4326");
} catch (Exception ex) {
throw new RuntimeException("Could not create longitude-first WGS84 coordinate reference system.");
}
}
public static LineString makeLineString(double... coords) {
GeometryFactory factory = getGeometryFactory();
Coordinate [] coordinates = new Coordinate[coords.length / 2];
for (int i = 0; i < coords.length; i+=2) {
coordinates[i / 2] = new Coordinate(coords[i], coords[i+1]);
}
return factory.createLineString(coordinates);
}
public static GeometryFactory getGeometryFactory() {
return gf;
}
/**
* Splits the input geometry into two LineStrings at the given point.
*/
public static P2<LineString> splitGeometryAtPoint(Geometry geometry, Coordinate nearestPoint) {
// An index in JTS can actually refer to any point along the line. It is NOT an array index.
LocationIndexedLine line = new LocationIndexedLine(geometry);
LinearLocation l = line.indexOf(nearestPoint);
LineString beginning = (LineString) line.extractLine(line.getStartIndex(), l);
LineString ending = (LineString) line.extractLine(l, line.getEndIndex());
return new P2<LineString>(beginning, ending);
}
/**
* Splits the input geometry into two LineStrings at a fraction of the distance covered.
*/
public static P2<LineString> splitGeometryAtFraction(Geometry geometry, double fraction) {
LineString empty = new LineString(null, gf);
Coordinate[] coordinates = geometry.getCoordinates();
CoordinateSequence sequence = gf.getCoordinateSequenceFactory().create(coordinates);
LineString total = new LineString(sequence, gf);
if (coordinates.length < 2) return new P2<LineString>(empty, empty);
if (fraction <= 0) return new P2<LineString>(empty, total);
if (fraction >= 1) return new P2<LineString>(total, empty);
double totalDistance = total.getLength();
double requestedDistance = totalDistance * fraction;
// An index in JTS can actually refer to any point along the line. It is NOT an array index.
LocationIndexedLine line = new LocationIndexedLine(geometry);
LinearLocation l = LengthLocationMap.getLocation(geometry, requestedDistance);
LineString beginning = (LineString) line.extractLine(line.getStartIndex(), l);
LineString ending = (LineString) line.extractLine(l, line.getEndIndex());
return new P2<LineString>(beginning, ending);
}
/**
* Returns the chunk of the given geometry between the two given coordinates.
*
* Assumes that "second" is after "first" along the input geometry.
*/
public static LineString getInteriorSegment(Geometry geomerty, Coordinate first,
Coordinate second) {
P2<LineString> splitGeom = GeometryUtils.splitGeometryAtPoint(geomerty, first);
splitGeom = GeometryUtils.splitGeometryAtPoint(splitGeom.second, second);
return splitGeom.first;
}
/**
* Adapted from com.vividsolutions.jts.geom.LineSegment
* Combines segmentFraction and projectionFactor methods.
*/
public static double segmentFraction(double x0, double y0, double x1, double y1,
double xp, double yp, double xscale) {
// Use comp.graphics.algorithms Frequently Asked Questions method
double dx = (x1 - x0) * xscale;
double dy = y1 - y0;
double len2 = dx * dx + dy * dy;
// this fixes a (reported) divide by zero bug in JTS when line segment has 0 length
if (len2 == 0)
return 0;
double r = ( (xp - x0) * xscale * dx + (yp - y0) * dy ) / len2;
if (r < 0.0)
return 0.0;
else if (r > 1.0)
return 1.0;
return r;
}
/**
* Convert a org.geojson.Xxxx geometry to a JTS geometry.
* Only support Point, Polygon, MultiPolygon, LineString and MultiLineString for now.
* @param geoJsonGeom
* @return The equivalent JTS geometry.
* @throws UnsupportedGeometryException
*/
public static Geometry convertGeoJsonToJtsGeometry(GeoJsonObject geoJsonGeom)
throws UnsupportedGeometryException {
if (geoJsonGeom instanceof org.geojson.Point) {
org.geojson.Point geoJsonPoint = (org.geojson.Point) geoJsonGeom;
return gf.createPoint(new Coordinate(geoJsonPoint.getCoordinates().getLongitude(), geoJsonPoint
.getCoordinates().getLatitude()));
} else if (geoJsonGeom instanceof org.geojson.Polygon) {
org.geojson.Polygon geoJsonPolygon = (org.geojson.Polygon) geoJsonGeom;
LinearRing shell = gf.createLinearRing(convertPath(geoJsonPolygon.getExteriorRing()));
LinearRing[] holes = new LinearRing[geoJsonPolygon.getInteriorRings().size()];
int i = 0;
for (List<LngLatAlt> hole : geoJsonPolygon.getInteriorRings()) {
holes[i++] = gf.createLinearRing(convertPath(hole));
}
return gf.createPolygon(shell, holes);
} else if (geoJsonGeom instanceof org.geojson.MultiPolygon) {
org.geojson.MultiPolygon geoJsonMultiPolygon = (org.geojson.MultiPolygon) geoJsonGeom;
Polygon[] jtsPolygons = new Polygon[geoJsonMultiPolygon.getCoordinates().size()];
int i = 0;
for (List<List<LngLatAlt>> geoJsonRings : geoJsonMultiPolygon.getCoordinates()) {
org.geojson.Polygon geoJsonPoly = new org.geojson.Polygon();
for (List<LngLatAlt> geoJsonRing : geoJsonRings)
geoJsonPoly.add(geoJsonRing);
jtsPolygons[i++] = (Polygon) convertGeoJsonToJtsGeometry(geoJsonPoly);
}
return gf.createMultiPolygon(jtsPolygons);
} else if (geoJsonGeom instanceof org.geojson.LineString) {
org.geojson.LineString geoJsonLineString = (org.geojson.LineString) geoJsonGeom;
return gf.createLineString(convertPath(geoJsonLineString.getCoordinates()));
} else if (geoJsonGeom instanceof org.geojson.MultiLineString) {
org.geojson.MultiLineString geoJsonMultiLineString = (org.geojson.MultiLineString) geoJsonGeom;
LineString[] jtsLineStrings = new LineString[geoJsonMultiLineString.getCoordinates().size()];
int i = 0;
for (List<LngLatAlt> geoJsonPath : geoJsonMultiLineString.getCoordinates()) {
org.geojson.LineString geoJsonLineString = new org.geojson.LineString(
geoJsonPath.toArray(new LngLatAlt[geoJsonPath.size()]));
jtsLineStrings[i++] = (LineString) convertGeoJsonToJtsGeometry(geoJsonLineString);
}
return gf.createMultiLineString(jtsLineStrings);
}
throw new UnsupportedGeometryException(geoJsonGeom.getClass().toString());
}
private static Coordinate[] convertPath(List<LngLatAlt> path) {
Coordinate[] coords = new Coordinate[path.size()];
int i = 0;
for (LngLatAlt p : path) {
coords[i++] = new Coordinate(p.getLatitude(), p.getLongitude());
}
return coords;
}
}