/** * Compute the (approximated) length of a polyline, with known cos(lat). * @param lineString The polyline in (longitude, latitude degrees). * @return The (approximated) length, in meters, of the linestring. */ public static final double fastLength(LineString lineString, double cosLat) { return equirectangularProject(lineString, cosLat).getLength() * RADIUS_OF_EARTH_IN_M; }
/** * Creates a circle shape, using the JTS buffer algorithm. The method is used when there is no street found within the given traveltime, e.g. when * the pointer is placed on a field or in the woods.<br> * TODO: Note it is actually not correct to do buffer calculation in Euclidian 2D, since the resulting shape will be elliptical when projected. * * @param dropPoint the location given by the user * @param pathToStreet the path from the dropPoint to the street, used to retrieve the buffer distance * @return a Circle */ private Geometry createCirle(Coordinate dropPoint, LineString pathToStreet) { double length = pathToStreet.getLength(); GeometryFactory gf = new GeometryFactory(); Point dp = gf.createPoint(dropPoint); Geometry buffer = dp.buffer(length); return buffer; }
/** * Extraction of a sub-LineString from an existing line, starting from 0; * * @param ls the line from which we extract the sub LineString () * @param fraction [0..1], the length until where we want the substring to go * @return the sub-LineString */ LineString getSubLineString(LineString ls, double fraction) { if (fraction >= 1) return ls; LengthIndexedLine linRefLine = new LengthIndexedLine(ls); LineString subLine = (LineString) linRefLine.extractLine(0, fraction * ls.getLength()); return subLine; }
/** * Compute the (approximated) length of a polyline * @param lineString The polyline in (longitude, latitude degrees). * @return The (approximated) length, in meters, of the linestring. */ public static final double fastLength(LineString lineString) { // Warn: do not use LineString.getCentroid() as it is broken // for degenerated geometry (same first/last point). Coordinate[] coordinates = lineString.getCoordinates(); double middleY = (coordinates[0].y + coordinates[coordinates.length - 1].y) / 2.0; double cosLat = FastMath.cos(Math.toRadians(middleY)); return equirectangularProject(lineString, cosLat).getLength() * RADIUS_OF_EARTH_IN_M; }
/** * Creates a PartialStreetEdge along the input StreetEdge iff its direction makes this possible. */ private void makePartialEdgeAlong(StreetEdge streetEdge, TemporaryStreetLocation from, TemporaryStreetLocation to) { LineString parent = streetEdge.getGeometry(); LineString head = GeometryUtils.getInteriorSegment(parent, streetEdge.getFromVertex().getCoordinate(), from.getCoordinate()); LineString tail = GeometryUtils.getInteriorSegment(parent, to.getCoordinate(), streetEdge.getToVertex().getCoordinate()); if (parent.getLength() > head.getLength() + tail.getLength()) { LineString partial = GeometryUtils.getInteriorSegment(parent, from.getCoordinate(), to.getCoordinate()); double lengthRatio = partial.getLength() / parent.getLength(); double length = streetEdge.getDistance() * lengthRatio; //TODO: localize this String name = from.getLabel() + " to " + to.getLabel(); new TemporaryPartialStreetEdge(streetEdge, from, to, partial, new NonLocalizedString(name), length); } }
private static void createHalfLocation(TemporaryStreetLocation base, I18NString name, Coordinate nearestPoint, StreetEdge street, boolean endVertex) { StreetVertex tov = (StreetVertex) street.getToVertex(); StreetVertex fromv = (StreetVertex) street.getFromVertex(); LineString geometry = street.getGeometry(); P2<LineString> geometries = getGeometry(street, nearestPoint); double totalGeomLength = geometry.getLength(); double lengthRatioIn = geometries.first.getLength() / totalGeomLength; double lengthIn = street.getDistance() * lengthRatioIn; double lengthOut = street.getDistance() * (1 - lengthRatioIn); if (endVertex) { TemporaryPartialStreetEdge temporaryPartialStreetEdge = new TemporaryPartialStreetEdge( street, fromv, base, geometries.first, name, lengthIn); temporaryPartialStreetEdge.setElevationProfile(ElevationUtils .getPartialElevationProfile(street.getElevationProfile(), 0, lengthIn), false); temporaryPartialStreetEdge.setNoThruTraffic(street.isNoThruTraffic()); temporaryPartialStreetEdge.setStreetClass(street.getStreetClass()); } else { TemporaryPartialStreetEdge temporaryPartialStreetEdge = new TemporaryPartialStreetEdge( street, base, tov, geometries.second, name, lengthOut); temporaryPartialStreetEdge.setElevationProfile(ElevationUtils .getPartialElevationProfile(street.getElevationProfile(), lengthIn, lengthIn + lengthOut), false); temporaryPartialStreetEdge.setStreetClass(street.getStreetClass()); temporaryPartialStreetEdge.setNoThruTraffic(street.isNoThruTraffic()); } }
/** * 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); }
|| segment.getBoundary().contains(startPoint)) { found = true; if (segment.getLength() > 0.000001) { edgeCoordinate = segment.getEndPoint().getCoordinate(); break;
public void findClosestPoint(String wktA, String wktB) { System.out.println("-------------------------------------"); try { Geometry A = wktRdr.read(wktA); Geometry B = wktRdr.read(wktB); System.out.println("Geometry A: " + A); System.out.println("Geometry B: " + B); DistanceOp distOp = new DistanceOp(A, B); double distance = distOp.distance(); System.out.println("Distance = " + distance); Coordinate[] closestPt = distOp.nearestPoints(); LineString closestPtLine = fact.createLineString(closestPt); System.out.println("Closest points: " + closestPtLine + " (distance = " + closestPtLine.getLength() + ")"); } catch (Exception ex) { ex.printStackTrace(); } }
public double getLength() { return lineString.getLength(); }
public LineString getLongest(ArrayList al) { if (al.size() == 1) return (LineString) (al.get(0)); double maxLength = -1; LineString result = null; final int size = al.size(); LineString l; for (int t = 0; t < size; t++) { l = (LineString) al.get(t); if (l.getLength() > maxLength) { result = l; maxLength = l.getLength(); } } return result; }
private static LineString interpolateLineString(LineString geom, String linear) { if (geom.getLength() > interpolatedSegmentLength) { if (linear.equals(GeometryWithInterpolation.LINEAR)) { return interpolateRhumbLine(geom); } else { return interpolateGreatCircle(geom); } } return geom; }
@Override public Measure getLength( Unit requestedUnit ) { // TODO respect requested unit double length = ( (com.vividsolutions.jts.geom.LineString) getJTSGeometry() ).getLength(); return new Measure( Double.toString( length ), null ); }
@Override public Measure getLength( Unit requestedUnit ) { // TODO respect requested unit double length = ( (com.vividsolutions.jts.geom.LineString) getJTSGeometry() ).getLength(); return new Measure( Double.toString( length ), null ); }
private LineString validateRing(LineString ring) { if (ring == null || ring.isEmpty() || ring.getLength() == 0) { return null; } GeometryFactory factory = ring.getFactory(); if (!ring.isClosed()) { List<Coordinate> sources = Arrays.asList(ring.getCoordinates()); List<Coordinate> coordList = new ArrayList<Coordinate>(sources); coordList.add(coordList.get(0)); ring = factory.createLineString(coordList.toArray(new Coordinate[0])); } return factory.createLinearRing(ring.getCoordinateSequence()); }
/** * Compute the perimeter * @param geometry * @return */ private static double computePerimeter(Geometry geometry) { double sum = 0; for (int i = 0; i < geometry.getNumGeometries(); i++) { Geometry subGeom = geometry.getGeometryN(i); if (subGeom instanceof Polygon) { sum += ((Polygon) subGeom).getExteriorRing().getLength(); } } return sum; } }
public static MultiLineString linearZInterpolation(MultiLineString multiLineString) { int nbGeom = multiLineString.getNumGeometries(); LineString[] lines = new LineString[nbGeom]; for (int i = 0; i < nbGeom; i++) { LineString subGeom = (LineString) multiLineString.getGeometryN(i); double startz = subGeom.getStartPoint().getCoordinates()[0].z; double endz = subGeom.getEndPoint().getCoordinates()[0].z; double length = subGeom.getLength(); subGeom.apply(new LinearZInterpolationFilter(startz, endz, length)); lines[i] = subGeom; } return FACTORY.createMultiLineString(lines); }