|
|
@@ -23,7 +23,10 @@ def check_filled(block, lat_int, lon_int, grid_spacing):
|
|
|
print("Bad size {0} of {1}".format(len(block), IO_BLOCK_SIZE))
|
|
|
return False
|
|
|
(bitmap, lat, lon, crc, version, spacing) = struct.unpack("<QiiHHH", block[:22])
|
|
|
- if (version != TERRAIN_GRID_FORMAT_VERSION):
|
|
|
+ if(lat == 0 and lon == 0 and crc == 0 and version == 0 and spacing == 0):
|
|
|
+ #print("Empty block")
|
|
|
+ return True
|
|
|
+ if (str(version) != str(TERRAIN_GRID_FORMAT_VERSION)):
|
|
|
print("Bad version: " + str(version))
|
|
|
return False
|
|
|
if abs(lat_int - (lat/1E7)) > 2 or abs(lon_int - (lon/1E7)) > 2:
|
|
|
@@ -41,7 +44,9 @@ def check_filled(block, lat_int, lon_int, grid_spacing):
|
|
|
if crc2 != crc:
|
|
|
print("Bad CRC")
|
|
|
return False
|
|
|
- return True
|
|
|
+
|
|
|
+ # all is good, return lon/lat of block
|
|
|
+ return (lat, lon)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
# Create the parser
|
|
|
@@ -63,6 +68,7 @@ if __name__ == '__main__':
|
|
|
if file.endswith("DAT.gz") or file.endswith("DAT"):
|
|
|
# It's a compressed tile
|
|
|
# 1. Check it's a valid gzip
|
|
|
+ print(file)
|
|
|
tile = None
|
|
|
try:
|
|
|
lat_int = int(os.path.basename(file)[1:3])
|
|
|
@@ -82,17 +88,43 @@ if __name__ == '__main__':
|
|
|
print(e)
|
|
|
# 2. Is it a valid dat file?
|
|
|
if (tile):
|
|
|
- total_blocks = east_blocks(lat_int*1e7, lon_int*1e7, grid_spacing) * TERRAIN_GRID_BLOCK_SIZE_Y
|
|
|
- # 2a. Are the correct number of blocks present?
|
|
|
- # There is an extra 1821 bytes at the end on the file (2048-1821 = 227), as the
|
|
|
+
|
|
|
+ # 2a. Are the correct number (integer) of blocks present?
|
|
|
+ # It will be a multiple of 2048 bytes (block size)
|
|
|
+ # There is an missing 227 bytes at the end on the file (2048-1821 = 227), as the
|
|
|
# terrain blocks only take up 1821 bytes.
|
|
|
- if (len(tile)+227) != (total_blocks * IO_BLOCK_SIZE):
|
|
|
- print("Bad number of blocks: {0}, {1} vs {2}".format(file, len(tile), total_blocks * IO_BLOCK_SIZE))
|
|
|
+ # APM actually adds the padding on the end, so extra 227 is not needed sometimes
|
|
|
+ total_blocks = 0
|
|
|
+ if (len(tile)+227) % IO_BLOCK_SIZE == 0:
|
|
|
+ total_blocks = int((len(tile)+227) / IO_BLOCK_SIZE)
|
|
|
+ elif len(tile) % IO_BLOCK_SIZE == 0:
|
|
|
+ total_blocks = int(len(tile) / IO_BLOCK_SIZE)
|
|
|
+ else:
|
|
|
+ print("Bad file size: {0}. {1} extra bytes at end".format(file, len(tile), len(tile) % IO_BLOCK_SIZE))
|
|
|
+ print("Has {0} blocks".format(total_blocks))
|
|
|
# 2b. Does each block have the correct CRC and fields?
|
|
|
- for blocknum in range(total_blocks):
|
|
|
- block = tile[(blocknum * IO_BLOCK_SIZE):((blocknum + 1)* IO_BLOCK_SIZE)-227]
|
|
|
- if not check_filled(block, lat_int, lon_int, 100):
|
|
|
- print("Bad data in block {0} of {1}".format(blocknum, total_blocks))
|
|
|
+ if total_blocks != 0:
|
|
|
+ lat_min = 90 * 1.0e7
|
|
|
+ lat_max = -90 * 1.0e7
|
|
|
+ lon_min = 180 * 1.0e7
|
|
|
+ lon_max = -180 * 1.0e7
|
|
|
+ for blocknum in range(total_blocks):
|
|
|
+ block = tile[(blocknum * IO_BLOCK_SIZE):((blocknum + 1)* IO_BLOCK_SIZE)-227]
|
|
|
+ ret = check_filled(block, lat_int, lon_int, 100)
|
|
|
+ if not ret:
|
|
|
+ print("Bad data in block {0} of {1}".format(blocknum, total_blocks))
|
|
|
+ else:
|
|
|
+ (lat, lon) = ret
|
|
|
+ lat_min = min(lat_min, lat)
|
|
|
+ lat_max = max(lat_max, lat)
|
|
|
+ lon_min = min(lon_min, lon)
|
|
|
+ lon_max = max(lon_max, lon)
|
|
|
+ lat_min *= 1.0e-7
|
|
|
+ lat_max *= 1.0e-7
|
|
|
+ lon_min *= 1.0e-7
|
|
|
+ lon_max *= 1.0e-7
|
|
|
+ print("Tile covers ({0},{1}) to ({2},{3})".format(lat_min, lon_min, lat_max, lon_max))
|
|
|
+ print("Tile size is ({0:.4f}, {1:.4f}) degrees".format(lat_max-lat_min, lon_max-lon_min))
|
|
|
else:
|
|
|
print("Bad tile: " + file)
|
|
|
print("Done!")
|