Basin Storage Optimization Example
Basin Storage Optimization Example
Before (Legacy Format)
Each basin stored as individual objects with coordinate lists:
{
"basins": [
{
"id": "1#A",
"tiles": ["10,5", "11,5", "12,5", "10,6", "11,6", "12,6", "10,7", "11,7", "12,7"],
"volume": 150,
"level": 2,
"height": 1,
"outlets": ["2#A"]
},
{
"id": "2#A",
"tiles": ["15,8", "16,8", "17,8", "15,9", "16,9", "17,9"],
"volume": 80,
"level": 1,
"height": 2,
"outlets": []
}
]
}
Size: ~400 characters for 2 small basins
After (Optimized Format)
Basin data stored as 2D map + tree structure + metadata:
{
"basins": {
"format": "optimized_v1",
"basinIdMap": {
"format": "string_rows",
"data": "0|0|0|0|0|0|0|0|0|0|1#A|1#A|1#A|0|0|2#A|2#A|2#A\n0|0|0|0|0|0|0|0|0|0|1#A|1#A|1#A|0|0|2#A|2#A|2#A\n0|0|0|0|0|0|0|0|0|0|1#A|1#A|1#A|0|0|0|0|0"
},
"basinTree": {
"1#A": {"outlets": ["2#A"], "height": 1},
"2#A": {"outlets": [], "height": 2}
},
"basinMetadata": {
"1#A": {"volume": 150, "level": 2, "tileCount": 9},
"2#A": {"volume": 80, "level": 1, "tileCount": 6}
}
}
}
Size: ~350 characters for same data + full spatial information
Benefits
- Space Efficiency: 10-50% smaller depending on basin distribution
- Faster Reconstruction: Direct array mapping instead of coordinate parsing
- Spatial Queries: Easy to find basin at any coordinate
- Tree Structure: Explicit outlet relationships preserved
- Scalability: Performance doesn’t degrade with basin complexity
RLE Alternative
For maps with large uniform areas, run-length encoding can provide even better compression:
{
"basinIdMap": {
"format": "rle_basin_ids",
"data": [["0", 180], ["1#A", 9], ["0", 45], ["2#A", 6], ["0", 120]]
}
}
This would compress a sparse 20x18 map (360 tiles) with just 2 small basins to ~50 characters.