Esempio n. 1
0
def ogr_info(ds):
    layers = [attrdict(name=defn.GetName(),
                       geom_type=defn.GetGeomType(),
                       srs=srclayer.GetSpatialRef().ExportToProj4(),
                       fields=[attrdict(name=field.GetName(),
                                        type=field.GetType(),
                                        width=field.GetWidth(),
                                        precision=field.GetPrecision())
                               for field in (defn.GetFieldDefn(i)
                                             for i in range(defn.GetFieldCount()))])
              for defn,srclayer in ((l.GetLayerDefn(), l)
                                    for l in map(ds.GetLayer, range(ds.GetLayerCount())))]

    return attrdict(layers=layers)
Esempio n. 2
0
    def _copy_layer(self, srclayer, layer_info, dstlayer):
        """Copy an OGR layer.
        """

        for feat in features(srclayer):
            f = attrdict(feat.items())
            # grab geometry too?

            f = self.edit_feature(f, layer_info)
            if f is None:
                continue

            outf = ogr.Feature(feature_def=dstlayer.GetLayerDefn())
            for k, v in f.items():
                outf.SetField(k, v)
            outf.SetGeometry(feat.GetGeometryRef())
            #outf.SetFrom(feat)
            dstlayer.CreateFeature(outf)
            outf.Destroy()
Esempio n. 3
0
def load_raster(input):
    """open a GDAL-readable raster file
    returns a pair of (metadata,array)
    """
    dem = gdal.Open(input)

    nodata = []
    layers = []
    for i in range(1, dem.RasterCount+1):
        band = dem.GetRasterBand(i)
        data = band.ReadAsArray(0, 0, dem.RasterXSize, dem.RasterYSize)
        layers.append(data)
        nodata.append(band.GetNoDataValue())

    if len(layers) > 1:
        layers = N.dstack(layers) 

    info = attrdict(
        metadata=dem.GetMetadata_Dict(),
        grid=Grid.from_gdal(dem),
        nodata=nodata)

    return (info,layers)
Esempio n. 4
0
File: vpx.py Progetto: nix/pyvpx
    def iterpackets(self):
        """iterate through packets generated by the encoder so far
        """
        iter = vpx_codec_iter_t()
        while 1:
            pktp = vpx_codec_get_cx_data(self.codec, iter)
            if not pktp:
                break
            pkt = pktp.contents

            # save statistics packets for the next pass
            if pkt.kind == VPX_CODEC_STATS_PKT:
                pktstats = pkt.data.twopass_stats
                buf = bytes_from_ptr(pktstats.buf, pktstats.sz)
                self.stats.append(buf)
                continue

            if pkt.kind == VPX_CODEC_PSNR_PKT:
                psnr = pkt.data.psnr.psnr[0]
                print 'snr', psnr
                continue

            if pkt.kind == VPX_CODEC_PSNR_PKT:
                buf = bytes_from_ptr(pkt.data.raw.buf, pkt.data.raw.sz)
                # custom packets are not handled
                assert 0

                continue

            fr = pkt.data.frame

            pktd = attrdict(_pkt=pkt)
            pktd.keyframe = bool(fr.flags & VPX_FRAME_IS_KEY)

            pktd.buf = bytes_from_ptr(fr.buf, fr.sz)

            yield pktd
Esempio n. 5
0
File: render.py Progetto: nix/mapsaw
def render_layer(workdir, layername, grid, sourcenames):
    layers = []
    for name in sourcenames:
        source = dict(mapconfig.datasources[name])
        if 'table' in source:
            source.update(mapconfig.db)
        if 'file' in source:
            source['type'] = 'shape'
            source['file'] = os.path.join(workdir, source['file'])

        layer = attrdict(name=name,
                         datasource_params=source)
        if 'srs' in source:
            layer.srs = source['srs']
        layers.append(layer)

    """
    if isinstance(source, basestring):
        source = dict(type='shape', file=source)
    else:
        table,multi = source
        source = dict(type='postgis',
                      table=table,
                      multiple_geometries=multi,
                      #estimate_extent='false',
                      #extent=','.join([str(x) for x in grid.extent()]),
                      user='******',
                      dbname='osmdb')
    lines = attrdict(name='lines',
                     datasource_params=source,
                     srs=data_srs)
    """

    sy,sx = grid.shape
    srs = grid.srs()

    base = None
    styletxt = get_mss(layername)
    styles = do_mss(layers, styletxt, base, srs)

    map = mapnik.Map(sx, sy, srs)
    styles.to_mapnik(map)

    x0,y0,x1,y1 = grid.extent()

    daspect = (x1-x0) / (y1-y0) * sy / sx
    if abs(daspect - 1) > 1e-5:
        raise Exception('aspect ratios must match to avoid mapnik bug? (grid %s vs map %s)'
                        % ((x1-x0) / (y1-y0), sy / sx))

    map.zoom_to_box(mapnik.Envelope(*grid.extent()))

    if 1:
        # XXX should use a tempfile here
        mapfile = 'foo.xml'
        mapnik.save_map(map, mapfile)
        print 'MAPXML'
        print open(mapfile).read()

    img = mapnik.Image(map.width, map.height)
    mapnik.render(map, img)

    imgdata = N.frombuffer(img.tostring(), dtype=N.uint8).reshape((sy, sx, 4))
    return N.array(imgdata, N.float32) / 255.0
Esempio n. 6
0
File: vpx.py Progetto: nix/pyvpx
            info = dict()

            # XXX destroy img?

            yield info,arrs


    

if __name__ == '__main__':
    from glob import glob
    #for fn in glob('/xtra/run/download/2011-03-24T12Z/gfsmap/*/1/0/0.png'):
    fns = list(sorted(glob('/x/pyvpx/satanim/*.png')))

    cfgd = attrdict(shape=(256,256),
                    tbase_den=20,
                    tbase_num=1)

    enc = VpxEncodeStream(**cfgd)
    #while 1:
    #    enc.encgrey()

    enc.open('vp8out')

    enc.decoder = VpxDecodeStream()
    enc.decoder.outdir = 'pngs'

    enc.npasses = 1

    for passi in range(enc.npasses):
        enc.start_pass(passi)