/**
  * Get the envelope flag indicator
  *
  * <p>1 for xy, 2 for xyz, 3 for xym, 4 for xyzm (null would be 0)
  *
  * @return
  */
 public static int getIndicator(GeometryEnvelope envelope) {
   int indicator = 1;
   if (envelope.hasZ()) {
     indicator++;
   }
   if (envelope.hasM()) {
     indicator += 2;
   }
   return indicator;
 }
  /**
   * Read the envelope based upon the indicator value
   *
   * @param envelopeIndicator
   * @param reader
   * @return
   */
  private GeometryEnvelope readEnvelope(int envelopeIndicator, ByteReader reader) {

    GeometryEnvelope envelope = null;

    if (envelopeIndicator > 0) {

      // Read x and y values and create envelope
      double minX = reader.readDouble();
      double maxX = reader.readDouble();
      double minY = reader.readDouble();
      double maxY = reader.readDouble();

      boolean hasZ = false;
      Double minZ = null;
      Double maxZ = null;

      boolean hasM = false;
      Double minM = null;
      Double maxM = null;

      // Read z values
      if (envelopeIndicator == 2 || envelopeIndicator == 4) {
        hasZ = true;
        minZ = reader.readDouble();
        maxZ = reader.readDouble();
      }

      // Read m values
      if (envelopeIndicator == 3 || envelopeIndicator == 4) {
        hasM = true;
        minM = reader.readDouble();
        maxM = reader.readDouble();
      }

      envelope = new GeometryEnvelope(hasZ, hasM);

      envelope.setMinX(minX);
      envelope.setMaxX(maxX);
      envelope.setMinY(minY);
      envelope.setMaxY(maxY);

      if (hasZ) {
        envelope.setMinZ(minZ);
        envelope.setMaxZ(maxZ);
      }

      if (hasM) {
        envelope.setMinM(minM);
        envelope.setMaxM(maxM);
      }
    }

    return envelope;
  }
  /**
   * Write the envelope bytes
   *
   * @param writer
   * @throws IOException
   */
  private void writeEnvelope(ByteWriter writer) throws IOException {

    if (envelope != null) {

      // Write x and y values
      writer.writeDouble(envelope.getMinX());
      writer.writeDouble(envelope.getMaxX());
      writer.writeDouble(envelope.getMinY());
      writer.writeDouble(envelope.getMaxY());

      // Write z values
      if (envelope.hasZ()) {
        writer.writeDouble(envelope.getMinZ());
        writer.writeDouble(envelope.getMaxZ());
      }

      // Write m values
      if (envelope.hasM()) {
        writer.writeDouble(envelope.getMinM());
        writer.writeDouble(envelope.getMaxM());
      }
    }
  }