예제 #1
0
    def ready_data_resource(self, **kwargs):
        print "ready data resource"
        r = self.region
        s_srs =  self.proj
        print s_srs
        print s_srs.ExportToProj4()

        e3857 = osr.SpatialReference()
        e3857.ImportFromEPSG(3857)
        crx = osr.CoordinateTransformation(s_srs, e3857)

        x0, y0, x1, y1 = r['w'], r['s'], r['e'], r['n']
        self.resource.spatial_metadata.native_srs = s_srs.ExportToProj4()

        xx0, yy0, _ = crx.TransformPoint(x0, y0)
        xx1, yy1, _ = crx.TransformPoint(x1, y1)

        raster = kwargs['RASTER'] if 'RASTER' in kwargs else self.env.default_raster
        cached_basename = os.path.join(self.cache_path, raster)
        cached_tiff = cached_basename + '.tif'
        output = cached_basename + '.native.tif'
        output_prj = cached_basename+ '.native.prj'

        print cached_tiff
        if not os.path.exists(cached_tiff):
            print "generating tiff"
            # Remove GRASS mask if present
            self.g.run_command('r.mask', flags='r')

            # TODO: Dynamically set name of mask layer
            # Mask layer must be 0|1 raster with 0 representing areas to
            #      exclude
            mask = 'basin_dr5'
            # TODO: Make export layer contain session ID, etc. to support multi
            #       user
            export = 'export'
            if mask:
                # Use mask to properly set alpha channel of exported TIFF
                self.g.write_command('r.mapcalc', stdin="{export}=if({mask},{raster},{mask})".format(export=export, mask=mask, raster=raster) )
                self.g.run_command('r.out.gdal', flags='f', type='UInt16', input=export, output=output)
            else:
                self.g.run_command('r.out.gdal', flags='f', type='UInt16', input=raster, output=output)

            with open(output_prj, 'w') as prj:
                prj.write(s_srs.ExportToWkt())

            sh.gdalwarp("-s_srs", output_prj, "-t_srs", "EPSG:3857", output, cached_tiff, '-srcnodata', '0', '-dstalpha')

            if export:
                # Clean up temporary export raster
                self.g.run_command('g.remove', rast=export)

        return self.cache_path, (
            self.resource.slug,
            e3857.ExportToProj4(),
            {
                "type" : "gdal",
                "file" : cached_tiff,
            }
        )