public LinearRing createLinearRing(CoordinateSequence cs) {
    if (cs.getCoordinate(0).equals(cs.getCoordinate(cs.size() - 1)))
      return super.createLinearRing(cs);

    // add a new coordinate to close the ring
    CoordinateSequenceFactory csFact = getCoordinateSequenceFactory();
    CoordinateSequence csNew = csFact.create(cs.size() + 1, cs.getDimension());
    CoordinateSequences.copy(cs, 0, csNew, 0, cs.size());
    CoordinateSequences.copyCoord(csNew, 0, csNew, csNew.size() - 1);
    return super.createLinearRing(csNew);
  }
 private CoordinateSequence readCoordinateSequence(int size) throws IOException {
   CoordinateSequence seq = csFactory.create(size, inputDimension);
   int targetDim = seq.getDimension();
   if (targetDim > inputDimension) targetDim = inputDimension;
   for (int i = 0; i < size; i++) {
     readCoordinate();
     for (int j = 0; j < targetDim; j++) {
       seq.setOrdinate(i, j, ordValues[j]);
     }
   }
   return seq;
 }
 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;
   }
 }
  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");
  }