Esempio n. 1
0
 def convert(self, format=None, geom=None):
     imgpath = self.image.path
     # Handle format as .tif, tif, or tif.zip
     ext = format or os.path.splitext(imgpath)[-1][1:]
     ext = os.path.splitext(ext)[0]
     # No conversion is needed if the original format without clipping
     # is requested.
     if not geom and imgpath.endswith(ext):
         return
     driver = greenwich.driver_for_path(ext, _imgdrivers)
     # Allow overriding of default driver settings.
     settings = self.driver_settings.get(ext)
     if settings:
         driver.settings = settings
     memio = MemFileIO()
     if geom:
         with self.raster() as r, r.clip(geom) as clipped:
             clipped.save(memio, driver)
     else:
         driver.copy(imgpath, memio.name)
     self.pk = None
     imgfield = self.image
     name = os.extsep.join((os.path.splitext(imgfield.name)[0], ext))
     name = imgfield.storage.get_available_name(name)
     imgfield.name = os.path.basename(name)
     imgfield.file = memio
Esempio n. 2
0
 def assert_member_formats(self, rend):
     ext = rend.format
     if rend.format.endswith('.zip'):
         ext = os.path.splitext(rend.format)[0]
     pat = 'tmin_.+(?<!\.{0})\.{0}$'.format(ext)
     driver = driver_for_path(ext, ImageDriver.filter_copyable())
     qs = self.qs.warp(format=driver.ext)
     rs = RasterStoreSerializer(qs.zipfiles(), many=True)
     fp = rend.render(rs.data)
     with zipfile.ZipFile(fp) as zf:
         for name in zf.namelist():
             self.assertRegexpMatches(name, pat)
             self.assert_format(zf.read(name), driver.format)
         filecount = len(zf.filelist)
     self.assertEqual(filecount, len(qs))
     fp.close()
Esempio n. 3
0
 def convert(self, format=None, geom=None):
     imgpath = self.image.path
     # Handle format as .tif, tif, or tif.zip
     ext = format or os.path.splitext(imgpath)[-1][1:]
     ext = os.path.splitext(ext)[0]
     driver = greenwich.driver_for_path('base.%s' % ext)
     # No conversion is needed if the original format without clipping
     # is requested.
     if not geom and imgpath.endswith(ext):
         return
     memio = MemFileIO()
     if geom:
         with greenwich.Raster(imgpath) as r:
             with r.clip(geom) as clipped:
                 clipped.save(memio, driver)
     else:
         driver.copy(imgpath, memio.name)
     self.image.file = memio
 def assert_member_formats(self, rend, geom=None):
     ext = rend.format
     if rend.format.endswith('.zip'):
         ext = os.path.splitext(rend.format)[0]
     pat = 'tmin_.+(?<!\.{0})\.{0}$'.format(ext)
     driver = driver_for_path(ext, ImageDriver.filter_copyable())
     qs = self.qs.warp(format=driver.ext, geom=geom)
     lst = [obj.raster() for obj in qs]
     rs = RasterStoreSerializer(qs.zipfiles(), many=True)
     fp = rend.render(rs.data)
     with zipfile.ZipFile(fp) as zf:
         for r, name in zip(lst, zf.namelist()):
             self.assertRegexpMatches(name, pat)
             imgdata = zf.read(name)
             self.assert_format(imgdata, driver.format)
             memio = MemFileIO()
             memio.write(imgdata)
             r2 = Raster(memio)
             self.assertTrue((r.array() == r2.array()).all())
             r2.close()
         filecount = len(zf.filelist)
     self.assertEqual(filecount, len(qs))
     fp.close()
Esempio n. 5
0
 def convert(self, format=None, geom=None):
     imgpath = self.image.path
     # Handle format as .tif, tif, or tif.zip
     ext = format or os.path.splitext(imgpath)[-1][1:]
     ext = os.path.splitext(ext)[0]
     # No conversion is needed if the original format without clipping
     # is requested.
     if not geom and imgpath.endswith(ext):
         return
     driver = greenwich.driver_for_path(ext, _imgdrivers)
     # Allow overriding of default driver settings.
     settings = self.driver_settings.get(ext)
     if settings:
         driver.settings = settings
     memio = MemFileIO()
     if geom:
         with greenwich.Raster(imgpath) as r, r.clip(geom) as clipped:
             clipped.save(memio, driver)
     else:
         driver.copy(imgpath, memio.name)
     self.image.name = os.extsep.join(
         (os.path.splitext(os.path.basename(imgpath))[0], ext))
     self.image.file = memio