Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- GDAL Image Conversion process
- Author: Richard Reinicke ([email protected])
- Example:
- http://localhost/wps/pywps.cgi?request=Execute&
- service=wps&version=1.0.0&
- identifier=imageconvert&status=true&storeExecuteResponse=true&
- datainputs=[geotiff=http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif]&
- responsedocument=jpeg=@asreference=true
- http://localhost/wps/pywps.cgi?request=Execute&
- service=wps&version=1.0.0&identifier=imageconvert&
- datainputs=[geotiff=http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif]&
- responsedocument=jpeg=@asreference=true
- """
- from pywps.Process import WPSProcess
- from pywps.Exceptions import *
- from osgeo import gdal
- from osgeo.gdalconst import *
- class Process(WPSProcess):
- def __init__(self):
- # init process
- WPSProcess.__init__(self,
- identifier = "imageconvert", # must be same, as filename
- title="Image conversion GeoTIFF to JPEG with GDAL",
- version = "0.1",
- storeSupported = "true",
- statusSupported = "true",
- metadata=[{'title':'geotiff2jpeg' ,'href':"http://foo/bar"}],
- abstract="Process demonstrating the conversion of GeoTiff image into JPEG using GDAL library")
- self.GeoTIFF = self.addComplexInput(identifier = "geotiff",
- title = "Input GeoTIFF image",
- formats = [{"mimeType": "image/tiff"}],
- maxmegabites=400)
- self.JPEG = self.addComplexOutput(identifier="jpeg",
- title="Output JPEG image",
- formats=[{"mimeType":"image/jpeg"}])
- def execute(self):
- import tempfile
- import os
- #better to use PyWPS debug and send everything to pywps.log file
- #notice that status and logging will be dumpt to the same file
- import logging
- logging.debug("Kick start the process")
- #better use the default system /tmp
- tmpfile = tempfile.NamedTemporaryFile("w+b", suffix=".jpg", delete=False)
- self.status.set("Define input and output", 0)
- #using a Tiff from natural earth since the blue marble is down, using image from NASA
- #http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif
- #sh ./pywps.cgi "request=Execute&service=WPS&version=1.0.0&identifier=imageconvert&datainputs=[geotiff=http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif]"
- inRaster = gdal.Open(self.GeoTIFF.getValue())
- self.status.set("Initialize JPEG Driver", 25)
- jpegDriver = gdal.GetDriverByName("JPEG")
- self.status.set("Calculating output raster", 75)
- output = jpegDriver.CreateCopy(tmpfile.name, inRaster, 0)
- # Once we're done, close properly the dataset
- inRaster = None
- output = None
- #tmppath, tmpbasename = os.path.split(tmpfile.name)
- #tmpsuffix = tmpbasename.split(".")[0]
- #logfile.write(tmppath + "\n" + tmpbasename + "\n" + tmpsuffix)
- self.JPEG.setValue(tmpfile.name)
- return
Advertisement
Add Comment
Please, Sign In to add comment