48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
#!/usr/bin/env python
|
|
# -*- encoding: utf-8 -*-
|
|
"""
|
|
Topic: 读写变长的二进制格式文件
|
|
Desc :
|
|
"""
|
|
import struct
|
|
import itertools
|
|
|
|
|
|
def write_polys(filename, polys):
|
|
# Determine bounding box
|
|
flattened = list(itertools.chain(*polys))
|
|
min_x = min(x for x, y in flattened)
|
|
max_x = max(x for x, y in flattened)
|
|
min_y = min(y for x, y in flattened)
|
|
max_y = max(y for x, y in flattened)
|
|
with open(filename, 'wb') as f:
|
|
f.write(struct.pack('<iddddi',
|
|
0x1234,
|
|
min_x, min_y,
|
|
max_x, max_y,
|
|
len(polys)))
|
|
for poly in polys:
|
|
size = len(poly) * struct.calcsize('<dd')
|
|
f.write(struct.pack('<i', size + 4))
|
|
for pt in poly:
|
|
f.write(struct.pack('<dd', *pt))
|
|
|
|
|
|
def read_polys(filename):
|
|
with open(filename, 'rb') as f:
|
|
# Read the header
|
|
header = f.read(40)
|
|
file_code, min_x, min_y, max_x, max_y, num_polys = \
|
|
struct.unpack('<iddddi', header)
|
|
polys = []
|
|
for n in range(num_polys):
|
|
pbytes, = struct.unpack('<i', f.read(4))
|
|
poly = []
|
|
for m in range(pbytes // 16):
|
|
pt = struct.unpack('<dd', f.read(16))
|
|
poly.append(pt)
|
|
polys.append(poly)
|
|
return polys
|
|
|
|
|