protected void visitLiteralGeometry(Literal expression) throws IOException { // evaluate the literal and store it for later currentGeometry = (Geometry) evaluateLiteral(expression, Geometry.class); if ( currentGeometry instanceof LinearRing ) { // convert LinearRing to LineString final GeometryFactory factory = currentGeometry.getFactory(); final LinearRing linearRing = (LinearRing) currentGeometry; final CoordinateSequence coordinates; coordinates = linearRing.getCoordinateSequence(); currentGeometry = factory.createLineString(coordinates); } final String geoJson = new GeometryJSON().toString(currentGeometry); currentShapeBuilder = mapReader.readValue(geoJson); }
private void applyZValues(CoordinateSequence cs, int idx, CoordinateSequence csOrig, int origIdx) { double lx1 = cs.getOrdinate(idx, 0); double ly1 = cs.getOrdinate(idx, 1); double lz1; double ox1 = csOrig.getOrdinate(origIdx, 0); double oy1 = csOrig.getOrdinate(origIdx, 1); double oz1 = csOrig.getOrdinate(origIdx, 2); double ox2 = csOrig.getOrdinate(origIdx + 1, 0); double oy2 = csOrig.getOrdinate(origIdx + 1, 1); double oz2 = csOrig.getOrdinate(origIdx + 1, 2); if(lx1 == ox1 && ly1 == oy1) { lz1 = oz1; } else { double d1 = distance(ox1, oy1, lx1, ly1); double d = distance(ox1, oy1, ox2, oy2); lz1 = oz1 + (oz2 - oz1) * (d1 / d); } cs.setOrdinate(idx, 2, lz1); }
private static int checkRepeatedPoints2d(LineString lineString) { int repeatedPoints = 0; final CoordinateSequence coordSeq = lineString.getCoordinateSequence(); Coordinate nextCoord = null; Coordinate prevCoord; for (int i = 0; i < coordSeq.size(); ++i) { prevCoord = nextCoord; nextCoord = coordSeq.getCoordinate(i); if (nextCoord.equals(prevCoord)) { ++repeatedPoints; } } return repeatedPoints; }
public static JSONArray toJSON(LineString line, boolean inverseSeq) { // "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] int size = line.getNumPoints(); JSONArray arrCoords = new JSONArray(size); CoordinateSequence seq = line.getCoordinateSequence(); Coordinate coord = null; for (int i = 0; i < size; ++i) { coord = seq.getCoordinate(inverseSeq ? size - i - 1: i); arrCoords.put(toJSON(coord)); } return arrCoords; }
private CoordinateSequence toCoordinateSequence(OsmEntityProvider resolver) throws EntityNotFoundException { CoordinateSequenceFactory csf = factory.getCoordinateSequenceFactory(); int len = this.getLength(); CoordinateSequence points = csf.create(len, 2); int n = 0; for (int i = 0; i < this.segments.size(); i++) { WaySegment segment = this.segments.get(i); OsmWay way = segment.getWay(); for (int k = 0; k < way.getNumberOfNodes(); k++) { if (k > 0 || i == 0) { OsmNode node = resolver.getNode(segment.getNodeId(k)); points.setOrdinate(n, 0, node.getLongitude()); points.setOrdinate(n, 1, node.getLatitude()); n++; } } } return points; }
private static LinearRing ringFromSegments(List<LineSegment> segments) { GeometryFactory factory = new GeometryFactory(); int nSegs = segments.size(); if (nSegs < 3) { return null; } int len = segments.size() + 1; CoordinateSequence seq = factory.getCoordinateSequenceFactory().create( len, 2); int i = 0; for (LineSegment line : segments) { seq.setOrdinate(i, 0, line.p0.x); seq.setOrdinate(i, 1, line.p0.y); i++; } seq.setOrdinate(i, 0, segments.get(0).p0.x); seq.setOrdinate(i, 1, segments.get(0).p0.y); return factory.createLinearRing(seq); }
@Override public void filter(CoordinateSequence seq, int index) { if (index == 0) { return; } Coordinate p0 = seq.getCoordinate(index - 1); Coordinate p1 = seq.getCoordinate(index); Coordinate midPt = new Coordinate( (p0.x + p1.x) / 2, (p0.y + p1.y) / 2); this.minPtDist.initialize(); DistanceToPointFinder.computeDistance(this.geom, midPt, this.minPtDist); this.maxPtDist.setMaximum(this.minPtDist); }
/** * Creates facet sequences * * @param g * @return List<GeometryFacetSequence> */ private static List computeFacetSequences(Geometry g) { final List sections = new ArrayList(); g.apply((GeometryComponentFilter) geom -> { CoordinateSequence seq = null; if (geom instanceof LineString) { seq = ((LineString) geom).getCoordinateSequence(); addFacetSequences(seq, sections); } else if (geom instanceof Point) { seq = ((Point) geom).getCoordinateSequence(); addFacetSequences(seq, sections); } }); return sections; }
/** * Computes an average normal vector from a list of polygon coordinates. * Uses Newell's method, which is based * on the fact that the vector with components * equal to the areas of the projection of the polygon onto * the Cartesian axis planes is normal. * * @param seq the sequence of coordinates for the polygon * @return a normal vector */ private Vector3D averageNormal(CoordinateSequence seq) { int n = seq.size(); Coordinate sum = new Coordinate(0, 0, 0); Coordinate p1 = new Coordinate(0, 0, 0); Coordinate p2 = new Coordinate(0, 0, 0); for (int i = 0; i < n - 1; i++) { seq.getCoordinate(i, p1); seq.getCoordinate(i + 1, p2); sum.x += (p1.y - p2.y) * (p1.z + p2.z); sum.y += (p1.z - p2.z) * (p1.x + p2.x); sum.z += (p1.x - p2.x) * (p1.y + p2.y); } sum.x /= n; sum.y /= n; sum.z /= n; Vector3D norm = Vector3D.create(sum).normalize(); return norm; }
/** * Determines the {@link Location} of a point in a ring. * * @param p the point to test * @param ring a coordinate sequence forming a ring * @return the location of the point in the ring */ public static int locatePointInRing(Coordinate p, CoordinateSequence ring) { RayCrossingCounter counter = new RayCrossingCounter(p); Coordinate p1 = new Coordinate(); Coordinate p2 = new Coordinate(); for (int i = 1; i < ring.size(); i++) { ring.getCoordinate(i, p1); ring.getCoordinate(i - 1, p2); counter.countSegment(p1, p2); if (counter.isOnSegment()) { return counter.getLocation(); } } return counter.getLocation(); }
/** * @see com.vividsolutions.jts.geom.CoordinateSequence#setOrdinate(int, int, double) */ @Override public void setOrdinate(int index, int ordinateIndex, double value) { switch (ordinateIndex) { case CoordinateSequence.X: this.coordinates[index].x = value; break; case CoordinateSequence.Y: this.coordinates[index].y = value; break; case CoordinateSequence.Z: this.coordinates[index].z = value; break; default: throw new IllegalArgumentException("invalid ordinateIndex"); } }
private void writeCoordinate(CoordinateSequence seq, int index, OutStream os) throws IOException { ByteOrderValues.putDouble(seq.getX(index), this.buf, this.byteOrder); os.write(this.buf, 8); ByteOrderValues.putDouble(seq.getY(index), this.buf, this.byteOrder); os.write(this.buf, 8); // only write 3rd dim if caller has requested it for this writer if (this.outputDimension >= 3) { // if 3rd dim is requested, only write it if the CoordinateSequence provides it double ordVal = Coordinate.NULL_ORDINATE; if (seq.getDimension() >= 3) { ordVal = seq.getOrdinate(index, 2); } ByteOrderValues.putDouble(ordVal, this.buf, this.byteOrder); os.write(this.buf, 8); } }
/** * Generates the WKT for a <tt>LINESTRING</tt> * specified by a {@link CoordinateSequence}. * * @param seq the sequence to write * @return the WKT string */ public static String toLineString(CoordinateSequence seq) { StringBuffer buf = new StringBuffer(); buf.append("LINESTRING "); if (seq.size() == 0) { buf.append(" EMPTY"); } else { buf.append("("); for (int i = 0; i < seq.size(); i++) { if (i > 0) { buf.append(", "); } buf.append(seq.getX(i)).append(" ").append(seq.getY(i)); } buf.append(")"); } return buf.toString(); }
/** * Converts a <code>LineString</code> to <LineString Text> format, then * appends it to the writer. * * @param lineString the <code>LineString</code> to process * @param writer the output writer to append to */ private void appendSequenceText(CoordinateSequence seq, int level, boolean doIndent, Writer writer) throws IOException { if (seq.size() == 0) { writer.write("EMPTY"); } else { if (doIndent) { this.indent(level, writer); } writer.write("("); for (int i = 0; i < seq.size(); i++) { if (i > 0) { writer.write(", "); if (this.coordsPerLine > 0 && i % this.coordsPerLine == 0) { this.indent(level + 1, writer); } } this.appendCoordinate(seq, i, writer); } writer.write(")"); } }
/** * Does what it says, reverses the order of the Coordinates in the ring. * <p> * This is different then lr.reverses() in that a copy is produced using a * new coordinate sequence. * </p> * * @param lr The ring to reverse. * @return A new ring with the reversed Coordinates. */ public static final LinearRing reverseRing(LinearRing lr) { GeometryFactory gf = lr.getFactory(); CoordinateSequenceFactory csf = gf.getCoordinateSequenceFactory(); CoordinateSequence csOrig = lr.getCoordinateSequence(); int numPoints = csOrig.size(); int dimensions = csOrig.getDimension(); CoordinateSequence csNew = csf.create(numPoints, dimensions); for (int i = 0; i < numPoints; i++) { for (int j = 0; j < dimensions; j++) { csNew.setOrdinate(numPoints - 1 - i, j, csOrig.getOrdinate(i, j)); } } return gf.createLinearRing(csNew); }
public void addLineStringToPath2D(LineString lineString, Path2D path) { CoordinateSequence coordinateSequence = lineString .getCoordinateSequence(); if (coordinateSequence.size() == 0) { // sometimes JTS gives an empty LineString return; } // add the first coord to the path Coordinate coord = coordinateSequence.getCoordinate(0); path.moveTo(coord.x, coord.y); // The loop stops at the second-last coord since the last coord will be // the same as the start coord. for (int i = 1; i < coordinateSequence.size() - 1; i++) { coord = coordinateSequence.getCoordinate(i); path.lineTo(coord.x, coord.y); } path.closePath(); }
public static IShape buildRectangle(final double width, final double height, final ILocation location) { final Coordinate[] points = new Coordinate[5]; final double x = location == null ? 0 : location.getX(); final double y = location == null ? 0 : location.getY(); final double z = location == null ? 0 : location.getZ(); points[4] = new GamaPoint(x - width / 2.0, y + height / 2.0, z); points[3] = new GamaPoint(x + width / 2.0, y + height / 2.0, z); points[2] = new GamaPoint(x + width / 2.0, y - height / 2.0, z); points[1] = new GamaPoint(x - width / 2.0, y - height / 2.0, z); points[0] = new GamaPoint(x - width / 2.0, y + height / 2.0, z); final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY; final CoordinateSequence cs = fact.create(points); final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs); final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null); return new GamaShape(p); }
/** * abstract a Polyline shape. * * @param reader the reader * @return the geometry * @throws IOException Signals that an I/O exception has occurred. */ @Override public Geometry parseShape(ShapeReader reader) { reader.skip(4 * DOUBLE_LENGTH); int numParts = reader.readInt(); int numPoints = reader.readInt(); int[] offsets = readOffsets(reader, numParts, numPoints); LineString[] lines = new LineString[numParts]; for(int i = 0; i < numParts; ++i){ int readScale = offsets[i+1] - offsets[i]; CoordinateSequence csString = readCoordinates(reader, readScale); lines[i] = geometryFactory.createLineString(csString); } if (numParts == 1) { return lines[0]; } return geometryFactory.createMultiLineString(lines); }
public static Polygon createPolygonWithHoles(Polygon aPolygon, Geometry[] aHoles) { CoordinateSequence polygonSeq = csFactory.create(getPolygonShell(aPolygon).getCoordinates()); LinearRing shell = new LinearRing(polygonSeq, gFactory); List<LinearRing> holes = new ArrayList<>(); if (aHoles != null) { for (Geometry aHole : aHoles) { Coordinate[] coord = aHole.getCoordinates(); if (coord.length > 1) { CoordinateSequence holeSeq = csFactory.create(coord); holes.add(new LinearRing(holeSeq, gFactory)); } } } LinearRing[] arHoles = new LinearRing[0]; return gFactory.createPolygon(shell, holes.toArray(arHoles)); }
/** * @see com.vividsolutions.jts.geom.CoordinateSequence#setOrdinate(int, int, double) */ public void setOrdinate(int index, int ordinateIndex, double value) { switch (ordinateIndex) { case CoordinateSequence.X: coordinates[index].x = value; break; case CoordinateSequence.Y: coordinates[index].y = value; break; case CoordinateSequence.Z: coordinates[index].z = value; break; case CoordinateSequence.M: coordinates[index].setM(value); break; } }
/** * @see com.vividsolutions.jts.geom.CoordinateSequence#setOrdinate(int, int, double) */ public void setOrdinate(int index, int ordinateIndex, double value) { switch (ordinateIndex) { case CoordinateSequence.X: coordinates[index].x = value; break; case CoordinateSequence.Y: coordinates[index].y = value; break; case CoordinateSequence.Z: coordinates[index].z = value; break; default: throw new IllegalArgumentException("invalid ordinateIndex"); } }
protected Geometry transformLinearRing(LinearRing geom, Geometry parent) { CoordinateSequence seq = transformCoordinates(geom.getCoordinateSequence(), geom); int seqSize = seq.size(); if ((seqSize > 0) && (seqSize < 4)) { return factory.createLineString(seq); } else if ((seqSize > 2) && (!seq.getCoordinate(0).equals2D(seq.getCoordinate(seqSize - 1)))) { // sometimes, dropping out points above creates a linear // ring that's no longer closed, which makes JTS very sad Coordinate[] newArray = new Coordinate[seqSize+1]; for (int i = 0; i < seqSize; i += 1) { newArray[i] = seq.getCoordinate(i); } newArray[seqSize] = seq.getCoordinate(0); seq = createCoordinateSequence(newArray); } return factory.createLinearRing(seq); }
private static Points getAsPoints( CoordinateSequence seq, ICRS crs, boolean swapAxis ) { int dim = seq.getDimension(); double[] coordinates = new double[ seq.size() * dim ]; int idx = 0; for(int i = 0; i < seq.size(); i++) { for(int j = 0; j < dim; j++) { if(swapAxis) { if(j == 0) { coordinates[idx++] = seq.getOrdinate(i, 1); } else if( j == 1) { coordinates[idx++] = seq.getOrdinate(i, 0); } else { coordinates[idx++] = seq.getOrdinate(i, j); } } else { coordinates[idx++] = seq.getOrdinate(i, j); } } } PackedCoordinateSequenceFactory factory = new PackedCoordinateSequenceFactory(); seq = factory.create(coordinates, dim); return new JTSPoints( crs, seq ); }
/** * Creates facet sequences * * @param g * @return List<GeometryFacetSequence> */ private static List computeFacetSequences(Geometry g) { final List sections = new ArrayList(); g.apply(new GeometryComponentFilter() { public void filter(Geometry geom) { CoordinateSequence seq = null; if (geom instanceof LineString) { seq = ((LineString) geom).getCoordinateSequence(); addFacetSequences(seq, sections); } else if (geom instanceof Point) { seq = ((Point) geom).getCoordinateSequence(); addFacetSequences(seq, sections); } } }); return sections; }
/** * Converts geometry from lat/lon (EPSG:4326)) to Spherical Mercator * (EPSG:3857) * * @param geometry the geometry to convert * @return the geometry transformed to EPSG:3857 */ public Geometry lngLatToMeters(Geometry geometry) { GeometryTransformer transformer = new GeometryTransformer() { @Override protected CoordinateSequence transformCoordinates(CoordinateSequence coords, Geometry parent) { Coordinate[] newCoords = new Coordinate[coords.size()]; for (int i = 0; i < coords.size(); ++i) { Coordinate coord = coords.getCoordinate(i); newCoords[i] = lngLatToMeters(coord); } return new CoordinateArraySequence(newCoords); } }; Geometry result = transformer.transform(geometry); return result; }
/** * Converts geometry from Spherical Mercator * (EPSG:3857) to lat/lon (EPSG:4326)) * * @param geometry the geometry to convert * @return the geometry transformed to EPSG:4326 */ public Geometry metersToLngLat(Geometry geometry) { GeometryTransformer transformer = new GeometryTransformer() { @Override protected CoordinateSequence transformCoordinates(CoordinateSequence coords, Geometry parent) { Coordinate[] newCoords = new Coordinate[coords.size()]; for (int i = 0; i < coords.size(); ++i) { Coordinate coord = coords.getCoordinate(i); newCoords[i] = metersToLngLat(coord); } return new CoordinateArraySequence(newCoords); } }; Geometry result = transformer.transform(geometry); return result; }
/** * Computes an average normal vector from a list of polygon coordinates. * Uses Newell's method, which is based * on the fact that the vector with components * equal to the areas of the projection of the polygon onto * the Cartesian axis planes is normal. * * @param seq the sequence of coordinates for the polygon * @return a normal vector */ private Vector3D averageNormal(CoordinateSequence seq) { int n = seq.size(); Coordinate sum = new Coordinate(0,0,0); Coordinate p1 = new Coordinate(0,0,0); Coordinate p2 = new Coordinate(0,0,0); for (int i = 0; i < n - 1; i++) { seq.getCoordinate(i, p1); seq.getCoordinate(i+1, p2); sum.x += (p1.y - p2.y)*(p1.z + p2.z); sum.y += (p1.z - p2.z)*(p1.x + p2.x); sum.z += (p1.x - p2.x)*(p1.y + p2.y); } sum.x /= n; sum.y /= n; sum.z /= n; Vector3D norm = Vector3D.create(sum).normalize(); return norm; }
public double distanceAlong() { CoordinateSequence cs = (this.edge.getGeometry()).getCoordinateSequence(); double dist = 0; double x0 = cs.getX(0); double y0 = cs.getY(0); for (int s = 1; s < this.seg; s++) { double x1 = cs.getX(s); double y1 = cs.getY(s); dist += distanceLibrary.fastDistance(y0, x0, y1, x1); x0 = x1; y0 = y1; } dist += distanceLibrary.fastDistance(y0, x0, this.y, this.x); // dist along partial // segment return dist; }
public double distanceToEnd() { CoordinateSequence cs = (this.edge.getGeometry()).getCoordinateSequence(); int s = this.seg + 1; double x0 = cs.getX(s); double y0 = cs.getY(s); double dist = distanceLibrary.fastDistance(y0, x0, this.y, this.x); // dist along // partial segment int nc = cs.size(); for (; s < nc; s++) { double x1 = cs.getX(s); double y1 = cs.getY(s); dist += distanceLibrary.fastDistance(y0, x0, y1, x1); x0 = x1; y0 = y1; } return dist; }
public static double getLengthMultiplierFromElevation(CoordinateSequence elev) { double trueLength = 0; double flatLength = 0; double lastX = elev.getX(0); double lastY = elev.getY(0); for (int i = 1; i < elev.size(); ++i) { Coordinate c = elev.getCoordinate(i); double x = c.x - lastX; double y = c.y - lastY; trueLength += Math.sqrt((x * x) + (y * y)); flatLength += x; lastX = c.x; lastY = c.y; } if (flatLength == 0) { return 0; } return trueLength / flatLength; }
/** * 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); }
public void mouseDragged(final Point initPosition, final Point imagePosition, int button,Object graphContext) { if (selectedGeometry == null | !this.edit | this.move | this.add | this.delete) { return; } if (!selectedGeometry.contains(gf.createPoint(new Coordinate(initPosition.x, initPosition.y)))) { selectedGeometry = null; return; } if (type.equals(GeometryImage.POLYGON) || type.equals(GeometryImage.LINESTRING)) { selectedGeometry.apply(new CoordinateSequenceFilter() { public void filter(CoordinateSequence seq, int i) { if (i == seq.size() - 1) { if (seq.getCoordinate(i) == seq.getCoordinate(0)) { return; } } seq.getCoordinate(i).x += imagePosition.x - initPosition.x; seq.getCoordinate(i).y += imagePosition.y - initPosition.y; } public boolean isDone() { return false; } public boolean isGeometryChanged() { return true; } }); } }
/** * @see com.vividsolutions.jts.geom.CoordinateSequence#getOrdinate(int, int) */ public double getOrdinate(int index, int ordinateIndex) { switch (ordinateIndex) { case CoordinateSequence.X: return coordinates[index].x; case CoordinateSequence.Y: return coordinates[index].y; case CoordinateSequence.Z: return coordinates[index].z; } return Double.NaN; }
/** * @see com.vividsolutions.jts.geom.CoordinateSequence#setOrdinate(int, int, double) */ public void setOrdinate(int index, int ordinateIndex, double value) { switch (ordinateIndex) { case CoordinateSequence.X: coordinates[index].x = value; case CoordinateSequence.Y: coordinates[index].y = value; case CoordinateSequence.Z: coordinates[index].z = value; } }
private static double getSignedArea(CoordinateSequence ring) { int n = ring.size(); if (n < 3) return 0.0; /** * Based on the Shoelace formula. * http://en.wikipedia.org/wiki/Shoelace_formula */ Coordinate p0 = new Coordinate(); Coordinate p1 = new Coordinate(); Coordinate p2 = new Coordinate(); getMercatorCoordinate(ring, 0, p1); getMercatorCoordinate(ring, 1, p2); double x0 = p1.x; p2.x -= x0; double sum = 0.0; for (int i = 1; i < n - 1; i++) { p0.y = p1.y; p1.x = p2.x; p1.y = p2.y; getMercatorCoordinate(ring, i + 1, p2); p2.x -= x0; sum += p1.x * (p0.y - p2.y); } return sum / 2.0; }
public LineString toLineString(OsmEntityProvider resolver) throws EntityNotFoundException { CoordinateSequence points = this.toCoordinateSequence(resolver); LineString string = new LineString(points, factory); return string; }
public LinearRing toLinearRing(OsmEntityProvider resolver) throws EntityNotFoundException { int len = this.getLength(); if (len < 4) { return new LinearRing(null, factory); } CoordinateSequence points = this.toCoordinateSequence(resolver); LinearRing shell = new LinearRing(points, factory); return shell; }
private void createLine(WayBuilderResult result, CoordinateSequence cs, boolean close) { if (close && cs.size() > 3) { result.setLinearRing(this.factory.createLinearRing(cs)); } else { result.getLineStrings().add(this.factory.createLineString(cs)); } }
/** * Tests if a linestring is completely contained in the boundary of the target rectangle. * * @param line the linestring to test * @return true if the linestring is contained in the boundary */ private boolean isLineStringContainedInBoundary(LineString line) { CoordinateSequence seq = line.getCoordinateSequence(); Coordinate p0 = new Coordinate(); Coordinate p1 = new Coordinate(); for (int i = 0; i < seq.size() - 1; i++) { seq.getCoordinate(i, p0); seq.getCoordinate(i + 1, p1); if (!this.isLineSegmentContainedInBoundary(p0, p1)) { return false; } } return true; }