def encode(file, features_by_layer, bounds, size=4096):
    """ Encode a dict of layername: (shape, props, id) features into a
        TopoJSON stream.

        If no id is available, pass in None

        Geometries in the features list are assumed to be unprojected
        lon, lats.  Bounds are given in geographic coordinates as
        (xmin, ymin, xmax, ymax).

        Size is the number of integer coordinates which span the extent
        of the tile.
    """
    transform, forward = get_transform(bounds, size=size)
    arcs = []

    geometries_by_layer = {}

    for layer, features in features_by_layer.iteritems():
        geometries = []
        for shape, props, fid in features:
            if shape.type == 'GeometryCollection':
                continue

            geometry = dict(properties=props)

            if fid is not None:
                geometry['id'] = fid

            elif shape.type == 'Point':
                geometry.update(dict(
                    type='Point',
                    coordinates=forward(shape.x, shape.y)))

            elif shape.type == 'LineString':
                geometry.update(dict(type='LineString', arcs=[len(arcs)]))
                arcs.append(diff_encode(shape, forward))

            elif shape.type == 'Polygon':
                geometry.update(dict(type='Polygon', arcs=[]))

                rings = [shape.exterior] + list(shape.interiors)

                for ring in rings:
                    geometry['arcs'].append([len(arcs)])
                    arcs.append(diff_encode(ring, forward))

            elif shape.type == 'MultiPoint':
                geometry.update(dict(type='MultiPoint', coordinates=[]))

                for point in shape.geoms:
                    geometry['coordinates'].append(forward(point.x, point.y))

            elif shape.type == 'MultiLineString':
                geometry.update(dict(type='MultiLineString', arcs=[]))

                for line in shape.geoms:
                    geometry['arcs'].append([len(arcs)])
                    arcs.append(diff_encode(line, forward))

            elif shape.type == 'MultiPolygon':
                geometry.update(dict(type='MultiPolygon', arcs=[]))

                for polygon in shape.geoms:
                    rings = [polygon.exterior] + list(polygon.interiors)
                    polygon_arcs = []

                    for ring in rings:
                        polygon_arcs.append([len(arcs)])
                        arcs.append(diff_encode(ring, forward))

                    geometry['arcs'].append(polygon_arcs)

            else:
                raise NotImplementedError("Can't do %s geometries" %
                                          shape.type)

            geometries.append(geometry)

        geometries_by_layer[layer] = dict(
            type='GeometryCollection',
            geometries=geometries,
        )

    result = dict(
        type='Topology',
        transform=transform,
        objects=geometries_by_layer,
        arcs=arcs,
    )

    json.dump(result, file)
def encode ( file , features_by_layer , bounds , size = 4096 ) : transform , forward = get_transform ( bounds , size = size ) arcs = [ ] geometries_by_layer = { } for layer , features in features_by_layer . iteritems ( ) : geometries = [ ] for shape , props , fid in features : if shape . type == 'GeometryCollection' : continue geometry = dict ( properties = props ) if fid is not None : geometry [ 'id' ] = fid elif shape . type == 'Point' : geometry . update ( dict ( type = 'Point' , coordinates = forward ( shape . x , shape . y ) ) ) elif shape . type == 'LineString' : geometry . update ( dict ( type = 'LineString' , arcs = [ len ( arcs ) ] ) ) arcs . append ( diff_encode ( shape , forward ) ) elif shape . type == 'Polygon' : geometry . update ( dict ( type = 'Polygon' , arcs = [ ] ) ) rings = [ shape . exterior ] + list ( shape . interiors ) for ring in rings : geometry [ 'arcs' ] . append ( [ len ( arcs ) ] ) arcs . append ( diff_encode ( ring , forward ) ) elif shape . type == 'MultiPoint' : geometry . update ( dict ( type = 'MultiPoint' , coordinates = [ ] ) ) for point in shape . geoms : geometry [ 'coordinates' ] . append ( forward ( point . x , point . y ) ) elif shape . type == 'MultiLineString' : geometry . update ( dict ( type = 'MultiLineString' , arcs = [ ] ) ) for line in shape . geoms : geometry [ 'arcs' ] . append ( [ len ( arcs ) ] ) arcs . append ( diff_encode ( line , forward ) ) elif shape . type == 'MultiPolygon' : geometry . update ( dict ( type = 'MultiPolygon' , arcs = [ ] ) ) for polygon in shape . geoms : rings = [ polygon . exterior ] + list ( polygon . interiors ) polygon_arcs = [ ] for ring in rings : polygon_arcs . append ( [ len ( arcs ) ] ) arcs . append ( diff_encode ( ring , forward ) ) geometry [ 'arcs' ] . append ( polygon_arcs ) else : raise NotImplementedError ( "Can't do %s geometries" % shape . type ) geometries . append ( geometry ) geometries_by_layer [ layer ] = dict ( type = 'GeometryCollection' , geometries = geometries , ) result = dict ( type = 'Topology' , transform = transform , objects = geometries_by_layer , arcs = arcs , ) json . dump ( result , file )
Encode a dict of layername: (shape, props, id) features into a
        TopoJSON stream.

        If no id is available, pass in None

        Geometries in the features list are assumed to be unprojected
        lon, lats.  Bounds are given in geographic coordinates as
        (xmin, ymin, xmax, ymax).

        Size is the number of integer coordinates which span the extent
        of the tile.