# Path: TileStache/Goodies/VecTiles/wkb.py
# def approximate_wkb(wkb_in):
#     ''' Return an approximation of the input WKB with lower-precision geometry.
#     '''
#     input, output = BytesIO(wkb_in), BytesIO()
#     approx_geometry(input, output)
#     wkb_out = output.getvalue()
# 
#     assert len(wkb_in) == input.tell(), 'The whole WKB was not processed'
#     assert len(wkb_in) == len(wkb_out), 'The output WKB is the wrong length'
#     
#     return wkb_out


from io import BytesIO
from zlib import decompress as _decompress, compress as _compress
from struct import unpack as _unpack, pack as _pack
from .wkb import approximate_wkb
import json
    ''' Decode an MVT file into a list of (WKB, property dict) features.
    
        Result can be passed directly to mapnik.PythonDatasource.wkb_features().
    '''
    head = file.read(4)
    
    if head != '\x89MVT':
        raise Exception('Bad head: "%s"' % head)
    
    body = BytesIO(_decompress(file.read(_next_int(file))))
    features = []
    
    for i in range(_next_int(body)):
        wkb = body.read(_next_int(body))
        raw = body.read(_next_int(body))

        props = json.loads(raw)
        features.append((wkb, props))
    
    return features

def encode(file, features):
    ''' Encode a list of (WKB, property dict) features into an MVT stream.
    
        Geometries in the features list are assumed to be in spherical mercator.
        Floating point precision in the output is approximated to 26 bits.
    '''
    parts = []
    
    for feature in features:

# Path: TileStache/Goodies/VecTiles/wkb.py
# def approximate_wkb(wkb_in):
#     ''' Return an approximation of the input WKB with lower-precision geometry.
#     '''
#     input, output = BytesIO(wkb_in), BytesIO()
#     approx_geometry(input, output)
#     wkb_out = output.getvalue()
# 
#     assert len(wkb_in) == input.tell(), 'The whole WKB was not processed'
#     assert len(wkb_in) == len(wkb_out), 'The output WKB is the wrong length'
#     
#     return wkb_out


# Path: TileStache/Goodies/VecTiles/mvt.py
from io import BytesIO
from zlib import decompress as _decompress, compress as _compress
from struct import unpack as _unpack, pack as _pack
from .wkb import approximate_wkb
import json

    ''' Decode an MVT file into a list of (WKB, property dict) features.
    
        Result can be passed directly to mapnik.PythonDatasource.wkb_features().
    '''
    head = file.read(4)
    
    if head != '\x89MVT':
        raise Exception('Bad head: "%s"' % head)
    
    body = BytesIO(_decompress(file.read(_next_int(file))))
    features = []
    
    for i in range(_next_int(body)):
        wkb = body.read(_next_int(body))
        raw = body.read(_next_int(body))

        props = json.loads(raw)
        features.append((wkb, props))
    
    return features

def encode(file, features):
    ''' Encode a list of (WKB, property dict) features into an MVT stream.
    
        Geometries in the features list are assumed to be in spherical mercator.
        Floating point precision in the output is approximated to 26 bits.
    '''
    parts = []
    
    for feature in features:

        wkb = approximate_wkb(feature[0])