// Subdivisions section
seek(treFatBlock.getSubfileOffset() + treHeader.getSubdivisionsSectionOffset());
boolean firstSubdivision = true;
int subdivisionCounter = 1;
Subdivision contiguousPreviousSubdivision = null;
Collection<Subdivision> subdivisionsTemp = new ArrayList<Subdivision>();
for (MapLevel mapLevel : subfile.getMapLevels()) {
for (int i = 0; i < mapLevel.getQuantityOfSubdivisions(); i++) {
Subdivision subdivision = new Subdivision();
subdivision.setLevel(mapLevel);
subdivision.setNumber(subdivisionCounter++);
// Offset in RGN subfile
subdivision.setOffsetInRGNSubfile(getLittleEndianWord(3));
// Object types
subdivision.setObjectTypes(getTRESubfileObjectTypes());
// Longitude center
subdivision.setLongitudeCenter(
Utils.convertMapUnitsToDegrees(getSignedInteger(3)));
// Latitude center
subdivision.setLatitudeCenter(
Utils.convertMapUnitsToDegrees(getSignedInteger(3)));
// Width
int value = getLittleEndianWord(2);
subdivision.setWidth(value & 32767);
subdivision.setTotalWidth((subdivision.getWidth() * 2) + 1);
subdivision.setTerminatingFlag((value & 32768) == 32768);
if (contiguousPreviousSubdivision != null) {
contiguousPreviousSubdivision.setContiguousSubdivision(subdivision);
}
if (subdivision.isTerminatingFlag()) {
contiguousPreviousSubdivision = null;
} else {
contiguousPreviousSubdivision = subdivision;
}
// Height
subdivision.setHeight(getLittleEndianWord(2));
subdivision.setTotalHeight((subdivision.getHeight() * 2) + 1);
if (mapLevel.getNumber() != 0) {
// Next level subdivision
int nextSubdivisionNumber = getLittleEndianWord(2);
if (nextSubdivisionNumber != 0) {
Subdivision subdivisionTemp = new Subdivision();
subdivisionTemp.setNumber(nextSubdivisionNumber);
subdivision.setNextLevelSubdivision(subdivisionTemp);
}
}
if (firstSubdivision) {
subfile.setFirstSubdivision(subdivision);
firstSubdivision = false;
}
for (Subdivision subdivisionTemp : subdivisionsTemp) {
if (subdivisionTemp.getNextLevelSubdivision() != null) {
if (subdivisionTemp.getNextLevelSubdivision().getNumber() == subdivision.getNumber()) {
subdivisionTemp.setNextLevelSubdivision(subdivision);
}
}
}
subdivisionsTemp.add(subdivision);
}