Exemplo n.º 1
0
    def setUp_files(self):
        # create a temporary storage directory, copy the reference test data
        # into it, and point the control.ingest.storage_dir to this location
        self.temp_storage_dir = tempfile.mktemp() # create a temp dir
        
        config = get_ngeo_config()
        section = "control.ingest"

        self.config_filename = tempfile.NamedTemporaryFile(delete=False).name
        environ["NGEO_CONFIG_FILE"] = self.config_filename

        shutil.copytree(join(settings.PROJECT_DIR, self.storage_dir), self.temp_storage_dir)
        config.set(section, "storage_dir", self.temp_storage_dir)
        
        # create a temporary optimized files directory, empty. point the 
        # control.ingest.optimized_files_dir to it
        self.temp_optimized_files_dir = tempfile.mkdtemp()
        config.set(section, "optimized_files_dir", self.temp_optimized_files_dir)
        
        self.temp_success_dir = tempfile.mkdtemp()
        config.set(section, "success_dir", self.temp_success_dir)
        
        self.temp_failure_dir = tempfile.mkdtemp()
        config.set(section, "failure_dir", self.temp_failure_dir)
        
        # copy files to optimized dir
        for filename_src, filename_dst in self.copy_to_optimized:
            filename_src = join(settings.PROJECT_DIR, "data", filename_src)
            filename_dst = join(self.temp_optimized_files_dir, filename_dst)
            safe_makedirs(dirname(filename_dst))
            shutil.copy(filename_src, filename_dst)
        
        # setup mapcache config/files as retrieved from template
        self.temp_mapcache_dir = tempfile.mkdtemp() + "/"
        db_file = settings.DATABASES["mapcache"]["TEST_NAME"]
        mapcache_config_file = join(self.temp_mapcache_dir, "mapcache.xml")
        self.mapcache_config_file = mapcache_config_file
        
        with open(mapcache_config_file, "w+") as f:
            f.write(render_to_string("test_control/mapcache.xml",
                                     {"mapcache_dir": self.temp_mapcache_dir,
                                      "mapcache_test_db": db_file,
                                      "browse_layers": models.BrowseLayer.objects.all(),
                                      "base_url": getattr(self, "live_server_url",
                                                          "http://localhost/browse")}))

        config.set(SEED_SECTION, "config_file", mapcache_config_file)
        config.set("mapcache", "tileset_root", self.temp_mapcache_dir)
        
        # setup mapcache dummy seed command
        seed_command_file = tempfile.NamedTemporaryFile(delete=False)
        seed_command_file.write("#!/bin/sh\nexit 0")
        self.seed_command = seed_command_file.name
        seed_command_file.close()
        st = stat(self.seed_command)
        chmod(self.seed_command, st.st_mode | S_IEXEC)
        
        config.set(SEED_SECTION, "seed_command", self.seed_command)

        self.temp_status_config = join(tempfile.gettempdir(), "status.conf")
Exemplo n.º 2
0
 def test_expected_optimized_files(self):
     """ Check that the expected optimized files are created. """
     
     # check that all optimized files are beeing created
     files = self.get_file_list(self.temp_optimized_files_dir)
     
     if self.save_optimized_files:
         save_dir = join(settings.PROJECT_DIR, "results/ingest/")
         safe_makedirs(dirname(save_dir))
         for path, _, filenames in walk(self.temp_optimized_files_dir):
             for file_to_save in filenames:
                 shutil.copy(join(path, file_to_save), save_dir) 
     
     # normalize files i.e. remove/add UUID
     for i in range(len(files)):
         if self.expected_optimized_files[i] != files[i]:
             files[i] = files[i][33:]
     
     self.assertItemsEqual(self.expected_optimized_files, files)
Exemplo n.º 3
0
    def open_raster(self, dir_name=None, raster_file=None):
        """ Convenience function to open a GDAL dataset. """
        
        dir_name = dir_name or self.temp_optimized_files_dir
        raster_file = raster_file or self.raster_file
        
        filename = join(dir_name, raster_file)

        # check filename and adjust if necessary i.e. add UUID
        if not isfile(filename):
            for file in listdir(dirname(filename)):
                if file.find(basename(raster_file)):
                    filename = join(dirname(filename), file)
        
        if self.save_to_file:
            save_filename = join(settings.PROJECT_DIR, self.save_to_file)
            safe_makedirs(dirname(save_filename))
            shutil.copy(filename, save_filename)
        
        try:
            return gdal.Open(filename)
        except RuntimeError:
            self.fail("Raster file '%s' is not present." % filename)
Exemplo n.º 4
0
 def open_raster(self):
     # dispatch wms request
     response = self.client.get(self.wms_request)
     
     if self.save_to_file:
         save_filename = join(settings.PROJECT_DIR, self.save_to_file)
         safe_makedirs(dirname(save_filename))
         with open(save_filename, "w+") as f:
             f.write(response.content)
     
     if response.status_code != 200:
         self.fail("WMS received response with status '%d'"
                   % response.status_code) 
     
     filename = '/vsimem/wms_temp'
     
     try:
         gdal.FileFromMemBuffer(filename, response.content)
         ds = gdal.Open(filename, gdal.GA_ReadOnly)
     
     finally:
         gdal.Unlink(filename)
     
     return ds