// Map levels section
seek(treFatBlock.getSubfileOffset() + treHeader.getMapLevelsSectionOffset());
int numberOfLevels = treHeader.getMapLevelsSectionSize() / 4; // fixme, porque 4?
for (int i = 0; i < numberOfLevels; i++) {
MapLevel mapLevel = new MapLevel();
mapLevel.setNumber(numberOfLevels - i - 1);
// Inherited
nextByte();
mapLevel.setInherited((currentByte & 128) == 128);
// Zoom level
mapLevel.setZoomLevel(currentByte & 15);
// Bits per coord
mapLevel.setBitsPerCoordinate(nextByte());
// Subdivisions
mapLevel.setQuantityOfSubdivisions(getLittleEndianWord(2));
subfile.getMapLevels().add(mapLevel);
}
// 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);