Example #1
0
    def connect(self, coverage, data_items, layer, options):
        path = join("/vsimem", uuid4().hex)
        range_type = coverage.range_type
        num_bands = len(coverage.range_type)

        vrt_builder = vrt.VRTBuilder(coverage.size_x, coverage.size_y, vrt_filename=path)

        bands_re = re.compile(r"bands\[(\d+)(,\d+)?\]")

        for data_item in sorted(data_items, key=lambda d: d.semantic):
            start, end = bands_re.match(data_item.semantic).groups()
            start = int(start)
            end = int(end) if end is not None else None
            if end is None:
                dst_band_indices = range(start + 1, start + 2)
                src_band_indices = range(1, 2)
            else:
                dst_band_indices = range(start + 1, end + 2)
                src_band_indices = range(1, end - start + 1)

            for src_index, dst_index in zip(src_band_indices, dst_band_indices):
                vrt_builder.add_band(range_type[dst_index - 1].data_type)
                vrt_builder.add_simple_source(
                    dst_index,
                    # gdal.OpenShared(data_item.location),
                    data_item.location,
                    src_index,
                )

        print data_items[0].location
        print gdal.OpenShared(data_items[0].location).GetGCPs()
        if isinstance(coverage, models.ReferenceableDataset):
            vrt_builder.copy_gcps(gdal.OpenShared(data_items[0].location))
            layer.setMetaData("eoxs_ref_data", path)

        layer.data = path

        # with vsi.open(path, "w+") as f:
        #    print type(vrt_builder.build())
        #    f.write(vrt_builder.build())

        del vrt_builder
        with vsi.open(path) as f:
            print f.read(100000)

        # layer.clearProcessing()
        # layer.addProcessing("SCALE_1=1,4")
        # layer.addProcessing("BANDS=2")
        # layer.offsite = mapserver.colorObj(0,0,0)

        if isinstance(coverage, models.ReferenceableDataset):
            vrt_path = join("/vsimem", uuid4().hex)
            reftools.create_rectified_vrt(path, vrt_path)
            layer.data = vrt_path
            layer.setMetaData("eoxs_ref_data", path)

            with vsi.open(vrt_path) as f:
                print f.read(100000)

        """
    def connect(self, coverage, data_items, layer):
        filtered = filter(lambda d: d.semantic.startswith("bands"), data_items)
        data = connect(filtered[0])

        if isinstance(coverage, models.ReferenceableDataset):
            vrt_path = join("/vsimem", uuid4().hex)
            reftools.create_rectified_vrt(data, vrt_path)
            data = vrt_path
            layer.setMetaData("eoxs_ref_data", data)

        if not layer.metadata.get("eoxs_wrap_dateline") == "true":
            layer.data = data
        else:
            e = wrap_extent_around_dateline(coverage.extent, coverage.srid)

            vrt_path = join("/vsimem", uuid4().hex)
            ds = gdal.Open(data)
            vrt_ds = create_simple_vrt(ds, vrt_path)
            size_x = ds.RasterXSize
            size_y = ds.RasterYSize
            
            dx = abs(e[0] - e[2]) / size_x
            dy = abs(e[1] - e[3]) / size_y 
            
            vrt_ds.SetGeoTransform([e[0], dx, 0, e[3], 0, -dy])
            vrt_ds = None
            
            layer.data = vrt_path