private Envelope<P> envelopeOf(String mortoncode, int index, Envelope<P> extent) {
assert (extent != null);
if (index >= mortoncode.length()) {
return extent;
}
char c = mortoncode.charAt(index);
double minX = extent.lowerLeft().getCoordinate(0);
double minY = extent.lowerLeft().getCoordinate(1);
double w = (extent.extentAlongDimension(0)) / 2.0;
double h = (extent.extentAlongDimension(1)) / 2.0;
CoordinateReferenceSystem<P> crs = extent.getCoordinateReferenceSystem();
switch (c) {
case '0':
return envelopeOf(mortoncode, ++index, new Envelope<P>(minX, minY, minX + w, minY + h, crs));
case '1':
return envelopeOf(mortoncode, ++index, new Envelope<P>(minX, minY + h, minX + w, minY + 2 * h, crs));
case '2':
return envelopeOf(mortoncode, ++index, new Envelope<P>(minX + w, minY, minX + 2 * w, minY + h, crs));
case '3':
return envelopeOf(mortoncode, ++index, new Envelope<P>(minX + w, minY + h, minX + 2 * w, minY + 2 * h, crs));
default:
throw new IllegalStateException("Received a mortoncode element that is not 0, 1, 2 or 3.");
}
}