private void applyZValues( CoordinateSequence cs, int idx, CoordinateSequence csOrig, int origIdx) { if (!cs.hasZ()) return; double lx1 = cs.getOrdinate(idx, 0); double ly1 = cs.getOrdinate(idx, 1); double lz1; double ox1 = csOrig.getX(origIdx); double oy1 = csOrig.getY(origIdx); double oz1 = csOrig.getZ(origIdx); double ox2 = csOrig.getX(origIdx + 1); double oy2 = csOrig.getY(origIdx + 1); double oz2 = csOrig.getZ(origIdx + 1); 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); }
/** @see org.locationtech.jts.geom.CoordinateSequenceFilter#isGeometryChanged */ public void filter(CoordinateSequence seq, int i) { if (checkOrdinateFlags.contains(Ordinate.Z) && !outputOrdinates.contains(Ordinate.Z)) { if (!Double.isNaN(seq.getZ(i))) outputOrdinates.add(Ordinate.Z); } if (checkOrdinateFlags.contains(Ordinate.M) && !outputOrdinates.contains(Ordinate.M)) { if (!Double.isNaN(seq.getM(i))) outputOrdinates.add(Ordinate.M); } }
/** Checks that the measures of XYM geometries are not transformed. */ @Test public void testXymGeometriesMeasuresArePreserved() throws Exception { // build a XYM geometry and reproject it Geometry geometry = new WKTReader().read("LINESTRINGM(170 -40 2, 190 40 7)"); MathTransform transform = CRS.findMathTransform(WGS84, MERCATOR, true); Geometry transformed = JTS.transform(geometry, transform); // check that coordinates where transformed but measures preserved assertThat(transformed, instanceOf(LineString.class)); LineString line = (LineString) transformed; assertThat(line.getCoordinateSequence().getDimension(), is(3)); assertThat(line.getCoordinateSequence().getMeasures(), is(1)); // check the first coordinate assertThat(line.getCoordinateSequence().getX(0), closeTo(1.8924313434856504E7, EPS)); assertThat(line.getCoordinateSequence().getY(0), closeTo(-4838471.398061137, EPS)); assertThat(line.getCoordinateSequence().getZ(0), is(Double.NaN)); assertThat(line.getCoordinateSequence().getM(0), is(2.0)); // check the second coordinate assertThat(line.getCoordinateSequence().getX(1), closeTo(2.115070325072198E7, EPS)); assertThat(line.getCoordinateSequence().getY(1), closeTo(4838471.398061137, EPS)); assertThat(line.getCoordinateSequence().getZ(1), is(Double.NaN)); assertThat(line.getCoordinateSequence().getM(1), is(7.0)); }
/** Checks that the measures of XYZM geometries are not transformed. */ @Test public void testXyzmGeometriesMeasuresArePreserved() throws Exception { // build a XYM geometry and reproject it Geometry geometry = new WKTReader().read("LINESTRINGZM(170 -40 10 2, 190 40 15 7)"); MathTransform transform = CRS.findMathTransform(WGS84, MERCATOR, true); Geometry transformed = JTS.transform(geometry, transform); // check that coordinates where transformed but measures preserved assertThat(transformed, instanceOf(LineString.class)); LineString line = (LineString) transformed; assertThat(line.getCoordinateSequence().getDimension(), is(4)); assertThat(line.getCoordinateSequence().getMeasures(), is(1)); // check the first coordinate assertThat(line.getCoordinateSequence().getX(0), closeTo(1.8924313434856504E7, EPS)); assertThat(line.getCoordinateSequence().getY(0), closeTo(-4838471.398061137, EPS)); assertThat(line.getCoordinateSequence().getZ(0), is(10.0)); assertThat(line.getCoordinateSequence().getM(0), is(2.0)); // check the second coordinate assertThat(line.getCoordinateSequence().getX(1), closeTo(2.115070325072198E7, EPS)); assertThat(line.getCoordinateSequence().getY(1), closeTo(4838471.398061137, EPS)); assertThat(line.getCoordinateSequence().getZ(1), is(15.0)); assertThat(line.getCoordinateSequence().getM(1), is(7.0)); }
/** * Helper method that encodes a {@see Point} coordinate to the JSON output. This method will * respect the configured axis order. If activated, coordinates measures (M) will be encoded, * otherwise measures will be ignored. * * @param point the point whose coordinate will be encoded * @return the JSON builder instance, this allow chained calls */ private JSONBuilder writeCoordinate(Point point) throws JSONException { CoordinateSequence coordinates = point.getCoordinateSequence(); // let's see if we need to encode measures, NaN values will not be encoded double m = encodeMeasures ? coordinates.getM(0) : Double.NaN; return writeCoordinate(coordinates.getX(0), coordinates.getY(0), coordinates.getZ(0), m); }
/** * Helper method that encodes a sequence of coordinates to the JSON output as an array. This * method will respect the configured axis order. If activated, coordinates measures (M) will be * encoded, otherwise measures will be ignored. * * @param coordinates the coordinates sequence that will be encoded * @return the JSON builder instance, this allow chained calls */ private JSONBuilder writeCoordinates(CoordinateSequence coordinates) throws JSONException { // start encoding the JSON array of coordinates this.array(); // each coordinate will be encoded has an array of ordinates for (int i = 0; i < coordinates.size(); i++) { // let's see if we need to encode measures, NaN values will not be encoded double m = encodeMeasures ? coordinates.getM(i) : Double.NaN; // encode the coordinate ordinates to the JSON output writeCoordinate(coordinates.getX(i), coordinates.getY(i), coordinates.getZ(i), m); } // we are done with the array return this.endArray(); }
@SuppressWarnings("unchecked") public <P extends Position> PositionSequence<P> toPositionSequence(CoordinateSequence cs, Class<P> posType, CoordinateReferenceSystem<P> crs) { if (cs instanceof PositionSequence && ((PositionSequence) cs).getPositionClass().equals(posType)) { return (PositionSequence<P>) cs; } Coordinate c = new Coordinate(); double[] psc = new double[crs.getCoordinateDimension()]; Arrays.fill(psc, Double.NaN); PositionSequenceBuilder<P> builder = PositionSequenceBuilders.fixedSized(cs.size(), posType); for (int i = 0; i < cs.size(); i++) { psc[0] = cs.getX(i); psc[1] = cs.getY(i); if (hasVerticalAxis(crs)) { psc[2] = cs.getZ(i); } // transfer measure values to position if (hasMeasureAxis(crs)) { final int idxM = hasVerticalAxis(crs) ? 3 : 2; final double mOrdinate = cs.getM(i); psc[idxM] = mOrdinate; } builder.add(psc); } return builder.toPositionSequence(); }
boolean isAllCoordsEqual(CoordinateSequence seq, Coordinate coord) { for (int i = 0; i < seq.size(); i++) { if (!coord.equals(seq.getCoordinate(i))) return false; if (coord.x != seq.getOrdinate(i, CoordinateSequence.X)) return false; if (coord.y != seq.getOrdinate(i, CoordinateSequence.Y)) return false; if (seq.hasZ()) { if (coord.getZ() != seq.getZ(i)) return false; } if (seq.hasM()) { if (coord.getM() != seq.getM(i)) return false; } if (seq.getDimension() > 2) { if (coord.getOrdinate(2) != seq.getOrdinate(i, 2)) return false; } if (seq.getDimension() > 3) { if (coord.getOrdinate(3) != seq.getOrdinate(i, 3)) return false; } } return true; }
if (!isEqual(coords[i].y,seq.getY(i))) return false; if (seq.hasZ()) { if (!isEqual(coords[i].getZ(),seq.getZ(i))) return false;