Ejemplo n.º 1
0
 def test_statistics(self):
     if not self.expected_statistics:
         self.skipTest("No expected statistics given.")
     
     # Use in-memory dataset here to not create a statistics metadata file on
     # the disc.
     ds = create_mem_copy(self.open_raster())
     self.assertEqual(len(self.expected_statistics), ds.RasterCount)
     
     statistics = []
     for index in range(1, ds.RasterCount + 1):
         names = ("min", "max", "mean", "stddev", "checksum")
         
         band = ds.GetRasterBand(index)
         stats = band.ComputeStatistics(False) + [band.Checksum()]
         statistics.append(dict(zip(names, stats)))
         
     self.assertEqual(self.expected_statistics, statistics)
Ejemplo n.º 2
0
 def process(self, input_filename, output_filename, 
             geo_reference=None, generate_metadata=True):
     
     # open the dataset and create an In-Memory Dataset as copy
     # to perform optimizations
     ds = create_mem_copy(gdal.Open(input_filename))
     
     gt = ds.GetGeoTransform()
     footprint_wkt = None
     
     if not geo_reference:
         if gt == (0.0, 1.0, 0.0, 0.0, 0.0, 1.0): # TODO: maybe use a better check
             raise ValueError("No geospatial reference for unreferenced "
                              "dataset given.")
     else:
         logger.debug("Applying geo reference '%s'."
                      % type(geo_reference).__name__)
         ds, footprint_wkt = geo_reference.apply(ds)
     
     # apply optimizations
     for optimization in self.get_optimizations(ds):
         logger.debug("Applying optimization '%s'."
                      % type(optimization).__name__)
         new_ds = optimization(ds)
         ds = None
         ds = new_ds
         
     # generate the footprint from the dataset
     if not footprint_wkt:
         logger.debug("Generating footprint.")
         footprint_wkt = self._generate_footprint_wkt(ds)
     
     
     if self.footprint_alpha:
         logger.debug("Applying optimization 'AlphaBandOptimization'.")
         opt = AlphaBandOptimization()
         opt(ds, footprint_wkt)
     
     
     output_filename = self.generate_filename(output_filename)
     
     logger.debug("Writing file to disc using options: %s."
                  % ", ".join(self.format_selection.creation_options))
     
     logger.debug("Metadata tags to be written: %s"
                  % ", ".join(ds.GetMetadata_List("") or []))
     
     # save the file to the disc
     driver = gdal.GetDriverByName(self.format_selection.driver_name)
     ds = driver.CreateCopy(output_filename, ds,
                            options=self.format_selection.creation_options)
     
     for optimization in self.get_post_optimizations(ds):
         logger.debug("Applying post-optimization '%s'."
                      % type(optimization).__name__)
         optimization(ds)
     
     # generate metadata if requested
     footprint = None
     if generate_metadata:
         normalized_space = Polygon.from_bbox((-180, -90, 180, 90))
         non_normalized_space = Polygon.from_bbox((180, -90, 360, 90))
         
         footprint = GEOSGeometry(footprint_wkt)
         #.intersection(normalized_space)
         outer = non_normalized_space.intersection(footprint)
         
         if len(outer):
             footprint = MultiPolygon(
                 *map(lambda p: 
                     Polygon(*map(lambda ls:
                         LinearRing(*map(lambda point: 
                             (point[0] - 360, point[1]), ls.coords
                         )), tuple(p)
                     )), (outer,)
                 )
             ).union(normalized_space.intersection(footprint))
         else:
             if isinstance(footprint, Polygon):
                 footprint = MultiPolygon(footprint)
             
         
         
         logger.info("Calculated Footprint: '%s'" % footprint.wkt)
         
         
         
         # use the provided footprint
         #geom = OGRGeometry(footprint_wkt)
         #exterior = []
         #for x, y in geom.exterior_ring.tuple:
         #    exterior.append(y); exterior.append(x)
         
         #polygon = [exterior]
     
     num_bands = ds.RasterCount
     
     # close the dataset and write it to the disc
     ds = None
     
     return PreProcessResult(output_filename, footprint, num_bands)
Ejemplo n.º 3
0
    def process(self, input_filename, output_filename,
                geo_reference=None, generate_metadata=True):

        # open the dataset and create an In-Memory Dataset as copy
        # to perform optimizations
        ds = create_mem_copy(gdal.Open(input_filename))

        gt = ds.GetGeoTransform()
        footprint_wkt = None

        if not geo_reference:
            if gt == (0.0, 1.0, 0.0, 0.0, 0.0, 1.0):
                # TODO: maybe use a better check
                raise ValueError("No geospatial reference for unreferenced "
                                 "dataset given.")
        else:
            logger.debug("Applying geo reference '%s'."
                         % type(geo_reference).__name__)
            ds, footprint_wkt = geo_reference.apply(ds)

        # apply optimizations
        for optimization in self.get_optimizations(ds):
            logger.debug("Applying optimization '%s'."
                         % type(optimization).__name__)

            try:
                new_ds = optimization(ds)

                if new_ds is not ds:
                    # cleanup afterwards
                    cleanup_temp(ds)
                    ds = new_ds
            except:
                cleanup_temp(ds)
                raise

        # generate the footprint from the dataset
        if not footprint_wkt:
            logger.debug("Generating footprint.")
            footprint_wkt = self._generate_footprint_wkt(ds)
        # check that footprint is inside of extent of generated image
        # regenerate otherwise
        else:
            tmp_extent = getExtentFromRectifiedDS(ds)
            tmp_bbox = Polygon.from_bbox((tmp_extent[0], tmp_extent[1],
                                          tmp_extent[2], tmp_extent[3]))
            tmp_footprint = GEOSGeometry(footprint_wkt)
            if not tmp_bbox.contains(tmp_footprint):
                footprint_wkt = tmp_footprint.intersection(tmp_bbox).wkt

        if self.footprint_alpha:
            logger.debug("Applying optimization 'AlphaBandOptimization'.")
            opt = AlphaBandOptimization()
            opt(ds, footprint_wkt)

        output_filename = self.generate_filename(output_filename)

        logger.debug("Writing file to disc using options: %s."
                     % ", ".join(self.format_selection.creation_options))

        logger.debug("Metadata tags to be written: %s"
                     % ", ".join(ds.GetMetadata_List("") or []))

        # save the file to the disc
        driver = gdal.GetDriverByName(self.format_selection.driver_name)
        ds = driver.CreateCopy(output_filename, ds,
                               options=self.format_selection.creation_options)

        for optimization in self.get_post_optimizations(ds):
            logger.debug("Applying post-optimization '%s'."
                         % type(optimization).__name__)
            optimization(ds)

        # generate metadata if requested
        footprint = None
        if generate_metadata:
            normalized_space = Polygon.from_bbox((-180, -90, 180, 90))
            non_normalized_space = Polygon.from_bbox((180, -90, 360, 90))

            footprint = GEOSGeometry(footprint_wkt)
            #.intersection(normalized_space)
            outer = non_normalized_space.intersection(footprint)

            if len(outer):
                footprint = MultiPolygon(
                    *map(lambda p:
                        Polygon(*map(lambda ls:
                            LinearRing(*map(lambda point:
                                (point[0] - 360, point[1]), ls.coords
                            )), tuple(p)
                        )), (outer,)
                    )
                ).union(normalized_space.intersection(footprint))
            else:
                if isinstance(footprint, Polygon):
                    footprint = MultiPolygon(footprint)

            logger.info("Calculated Footprint: '%s'" % footprint.wkt)

            # use the provided footprint
            #geom = OGRGeometry(footprint_wkt)
            #exterior = []
            #for x, y in geom.exterior_ring.tuple:
            #    exterior.append(y); exterior.append(x)

            #polygon = [exterior]
        num_bands = ds.RasterCount

        # finally close the dataset and write it to the disc
        ds = None

        return PreProcessResult(output_filename, footprint, num_bands)
Ejemplo n.º 4
0
    def process(self,
                input_filename,
                output_filename,
                geo_reference=None,
                generate_metadata=True,
                merge_with=None,
                original_footprint=None):

        # open the dataset and create an In-Memory Dataset as copy
        # to perform optimizations
        ds = create_mem_copy(gdal.Open(input_filename))

        gt = ds.GetGeoTransform()
        footprint_wkt = None

        if not geo_reference:
            if gt == (0.0, 1.0, 0.0, 0.0, 0.0, 1.0):
                if ds.GetGCPCount() > 0:
                    geo_reference = InternalGCPs()
                else:
                    raise ValueError("No geospatial reference for "
                                     "unreferenced dataset given.")

        if geo_reference:
            logger.debug("Applying geo reference '%s'." %
                         type(geo_reference).__name__)
            # footprint is always in EPSG:4326
            ds, footprint_wkt = geo_reference.apply(ds)

        # apply optimizations
        for optimization in self.get_optimizations(ds):
            logger.debug("Applying optimization '%s'." %
                         type(optimization).__name__)

            try:
                new_ds = optimization(ds)

                if new_ds is not ds:
                    # cleanup afterwards
                    cleanup_temp(ds)
                    ds = new_ds
            except:
                cleanup_temp(ds)
                raise

        # generate the footprint from the dataset
        if not footprint_wkt:
            logger.debug("Generating footprint.")
            footprint_wkt = self._generate_footprint_wkt(ds)
        # check that footprint is inside of extent of generated image
        # regenerate otherwise
        else:
            tmp_extent = getExtentFromRectifiedDS(ds)
            tmp_bbox = Polygon.from_bbox(
                (tmp_extent[0], tmp_extent[1], tmp_extent[2], tmp_extent[3]))
            # transform image bbox to EPSG:4326 if necessary
            proj = ds.GetProjection()
            srs = osr.SpatialReference()
            try:
                srs.ImportFromWkt(proj)
                srs.AutoIdentifyEPSG()
                ptype = "PROJCS" if srs.IsProjected() else "GEOGCS"
                srid = int(srs.GetAuthorityCode(ptype))
                if srid != '4326':
                    out_srs = osr.SpatialReference()
                    out_srs.ImportFromEPSG(4326)
                    transform = osr.CoordinateTransformation(srs, out_srs)
                    tmp_bbox2 = ogr.CreateGeometryFromWkt(tmp_bbox.wkt)
                    tmp_bbox2.Transform(transform)
                    tmp_bbox = GEOSGeometry(tmp_bbox2.ExportToWkt())
            except (RuntimeError, TypeError), e:
                logger.warn("Projection: %s" % proj)
                logger.warn("Failed to identify projection's EPSG code."
                            "%s: %s" % (type(e).__name__, str(e)))

            tmp_footprint = GEOSGeometry(footprint_wkt)
            if not tmp_bbox.contains(tmp_footprint):
                logger.debug("Re-generating footprint because not inside of "
                             "generated image.")
                footprint_wkt = tmp_footprint.intersection(tmp_bbox).wkt
Ejemplo n.º 5
0
    def process(self, input_filename, output_filename,
                geo_reference=None, generate_metadata=True,
                merge_with=None, original_footprint=None):

        # open the dataset and create an In-Memory Dataset as copy
        # to perform optimizations
        ds = create_mem_copy(gdal.Open(input_filename))

        gt = ds.GetGeoTransform()
        footprint_wkt = None

        if not geo_reference:
            if gt == (0.0, 1.0, 0.0, 0.0, 0.0, 1.0):
                if ds.GetGCPCount() > 0:
                    geo_reference = InternalGCPs()
                else:
                    raise ValueError("No geospatial reference for "
                                     "unreferenced dataset given.")

        if geo_reference:
            logger.debug("Applying geo reference '%s'."
                         % type(geo_reference).__name__)
            # footprint is always in EPSG:4326
            ds, footprint_wkt = geo_reference.apply(ds)

        # apply optimizations
        for optimization in self.get_optimizations(ds):
            logger.debug("Applying optimization '%s'."
                         % type(optimization).__name__)

            try:
                new_ds = optimization(ds)

                if new_ds is not ds:
                    # cleanup afterwards
                    cleanup_temp(ds)
                    ds = new_ds
            except:
                cleanup_temp(ds)
                raise

        # generate the footprint from the dataset
        if not footprint_wkt:
            logger.debug("Generating footprint.")
            footprint_wkt = self._generate_footprint_wkt(ds)
        # check that footprint is inside of extent of generated image
        # regenerate otherwise
        else:
            tmp_extent = getExtentFromRectifiedDS(ds)
            tmp_bbox = Polygon.from_bbox((tmp_extent[0], tmp_extent[1],
                                          tmp_extent[2], tmp_extent[3]))

            # transform image bbox to EPSG:4326 if necessary
            proj = ds.GetProjection()
            srs = osr.SpatialReference()
            try:
                srs.ImportFromWkt(proj)
                srs.AutoIdentifyEPSG()
                ptype = "PROJCS" if srs.IsProjected() else "GEOGCS"
                srid = int(srs.GetAuthorityCode(ptype))
                if srid != '4326':
                    out_srs = osr.SpatialReference()
                    out_srs.ImportFromEPSG(4326)
                    transform = osr.CoordinateTransformation(srs, out_srs)
                    tmp_bbox2 = ogr.CreateGeometryFromWkt(tmp_bbox.wkt)
                    tmp_bbox2.Transform(transform)
                    tmp_bbox = GEOSGeometry(tmp_bbox2.ExportToWkt())
            except (RuntimeError, TypeError), e:
                logger.warn("Projection: %s" % proj)
                logger.warn("Failed to identify projection's EPSG code."
                    "%s: %s" % ( type(e).__name__ , str(e) ) )

            tmp_footprint = GEOSGeometry(footprint_wkt)
            if not tmp_bbox.contains(tmp_footprint):
                footprint_wkt = tmp_footprint.intersection(tmp_bbox).wkt