public void write(ByteBuffer buffer, Object geometry) { Point point = (Point) geometry; Coordinate c = point.getCoordinate(); buffer.putDouble(c.x); buffer.putDouble(c.y); if (shapeType == ShapeType.POINTZ) { if (Double.isNaN(c.z)) { // nan means not defined buffer.putDouble(0.0); } else { buffer.putDouble(c.z); } } if ((shapeType == ShapeType.POINTZ) || (shapeType == ShapeType.POINTM)) { double m = point.getCoordinateSequence().getM(0); buffer.putDouble(!Double.isNaN(m) ? m : 0.0); // 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)); }
for (int t = 0, tt = mp.getNumGeometries(); t < tt; t++) { Point point = (Point) mp.getGeometryN(t); mvalues.add(point.getCoordinateSequence().getM(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)); }
double m; for (int coordN = 0; coordN < seqSize; coordN++) { m = coords.getM(coordN); values.add(m);
CoordinateSequence seq = line.getCoordinateSequence(); for (int i = 0; i < seq.size(); i++) { mvalues.add(line.getCoordinateSequence().getM(i));
/** @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); } }
writer.write(writeNumber(seq.getM(i), formatter));
/** * 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].getM(),seq.getM(i))) return false;