/** Write a Coordinatesequence, part of LinearRing and Linestring, but not MultiPoint! */ private void writeCoordinates(CoordinateSequence seq, int dims, ValueSetter dest) { for (int i = 0; i < seq.size(); i++) { for (int d = 0; d < dims; d++) { dest.setDouble(seq.getOrdinate(i, d)); } } }
public void writeCoordinates(CoordinateSequence c, ContentHandler output) throws SAXException { output.startElement("", "coordinates", "coordinates", atts); final int coordCount = c.size(); // used to check whether the coordseq handles a third dimension or not final int coordSeqDimension = c.getDimension(); double x, y, z; // write down a coordinate at a time for (int i = 0, n = coordCount; i < n; i++) { x = c.getOrdinate(i, 0); y = c.getOrdinate(i, 1); // clear the buffer coordBuff.setLength(0); // format x into buffer and append delimiter formatDecimal(x, coordBuff); coordBuff.append(coordinateDelimiter); // format y into buffer formatDecimal(y, coordBuff); if (coordSeqDimension > 2 && !Double.isNaN(c.getOrdinate(i, 2))) { coordBuff.append(coordinateDelimiter); formatDecimal(c.getOrdinate(i, 2), coordBuff); } // if there is another coordinate, tack on a tuple delimiter if (i + 1 < coordCount) { coordBuff.append(tupleDelimiter); } // make sure our character buffer is big enough if (coordBuff.length() > buff.length) { buff = new char[coordBuff.length()]; } // copy the characters coordBuff.getChars(0, coordBuff.length(), buff, 0); // finally, output output.characters(buff, 0, coordBuff.length()); } output.endElement(null, "coordinates", "coordinates"); }
static DirectPosition[] positions(LineString line) { CoordinateSequence coordinates = line.getCoordinateSequence(); DirectPosition[] dps = new DirectPosition[coordinates.size()]; double x; double y; for (int i = 0; i < dps.length; i++) { x = coordinates.getOrdinate(i, 0); y = coordinates.getOrdinate(i, 1); dps[i] = new DirectPosition2D(x, y); } return dps; }
public static final int getCoordSequenceDim(CoordinateSequence coords) { if (coords == null || coords.size() == 0) return 0; // JTS has a really strange way to handle dimensions! // Just have a look at PackedCoordinateSequence and // CoordinateArraySequence int dimensions = coords.getDimension(); if (dimensions == 3) { // CoordinateArraySequence will always return 3, so we have to // check, if // the third ordinate contains NaN, then the geom is actually // 2-dimensional return Double.isNaN(coords.getOrdinate(0, CoordinateSequence.Z)) ? 2 : 3; } else { return dimensions; } }
/** * Write the coordinates of a geometry * * @param coords The coordinates to write * @return this * @throws JSONException */ private JSONBuilder writeCoordinates(CoordinateSequence coords) throws JSONException { this.array(); // guess the dimension of the coordinate sequence int dim = CoordinateSequences.coordinateDimension(coords); final int coordCount = coords.size(); for (int i = 0; i < coordCount; i++) { if (dim > 2) { writeCoordinate(coords.getX(i), coords.getY(i), coords.getOrdinate(i, 2)); } else { writeCoordinate(coords.getX(i), coords.getY(i)); } } return this.endArray(); }