JorgeDeJesus

Untitled

Oct 21st, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. """
  2. GDAL Image Conversion process
  3.  
  4. Author: Richard Reinicke ([email protected])
  5. Example:
  6. http://localhost/wps/pywps.cgi?request=Execute&
  7. service=wps&version=1.0.0&
  8. identifier=imageconvert&status=true&storeExecuteResponse=true&
  9. datainputs=[geotiff=http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif]&
  10. responsedocument=jpeg=@asreference=true
  11.  
  12.  
  13. http://localhost/wps/pywps.cgi?request=Execute&
  14. service=wps&version=1.0.0&identifier=imageconvert&
  15. datainputs=[geotiff=http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif]&
  16. responsedocument=jpeg=@asreference=true
  17.  
  18. """
  19. from pywps.Process import WPSProcess
  20. from pywps.Exceptions import *
  21. from osgeo import gdal
  22. from osgeo.gdalconst import *
  23. class Process(WPSProcess):
  24.      def __init__(self):
  25.           # init process
  26.          WPSProcess.__init__(self,
  27.               identifier = "imageconvert", # must be same, as filename
  28.               title="Image conversion GeoTIFF to JPEG with GDAL",
  29.               version = "0.1",
  30.               storeSupported = "true",
  31.               statusSupported = "true",
  32.               metadata=[{'title':'geotiff2jpeg' ,'href':"http://foo/bar"}],
  33.               abstract="Process demonstrating the conversion of GeoTiff image into JPEG using GDAL library")
  34.              
  35.          self.GeoTIFF = self.addComplexInput(identifier = "geotiff",
  36.                                             title = "Input GeoTIFF image",
  37.                                             formats = [{"mimeType": "image/tiff"}],
  38.                                             maxmegabites=400)
  39.  
  40.          self.JPEG = self.addComplexOutput(identifier="jpeg",
  41.                                            title="Output JPEG image",
  42.                                            formats=[{"mimeType":"image/jpeg"}])
  43.  
  44.      def execute(self):
  45.             import tempfile
  46.             import os
  47.             #better to use PyWPS debug and send everything to pywps.log file
  48.             #notice that status and logging will be dumpt to the same file
  49.             import logging
  50.             logging.debug("Kick start the process")
  51.  
  52.             #better use the default system /tmp
  53.             tmpfile = tempfile.NamedTemporaryFile("w+b", suffix=".jpg", delete=False)
  54.  
  55.             self.status.set("Define input and output", 0)
  56.             #using a Tiff from natural earth since the blue marble is down, using image from NASA
  57.             #http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif
  58.             #sh ./pywps.cgi "request=Execute&service=WPS&version=1.0.0&identifier=imageconvert&datainputs=[geotiff=http://photojournal.jpl.nasa.gov/tiff/PIA17427.tif]"
  59.             inRaster = gdal.Open(self.GeoTIFF.getValue())
  60.            
  61.  
  62.             self.status.set("Initialize JPEG Driver", 25)
  63.             jpegDriver = gdal.GetDriverByName("JPEG")
  64.  
  65.             self.status.set("Calculating output raster", 75)    
  66.             output = jpegDriver.CreateCopy(tmpfile.name, inRaster, 0)
  67.            
  68.             # Once we're done, close properly the dataset
  69.             inRaster = None
  70.             output = None
  71.            
  72.            
  73.             #tmppath, tmpbasename = os.path.split(tmpfile.name)
  74.             #tmpsuffix = tmpbasename.split(".")[0]
  75.             #logfile.write(tmppath + "\n" + tmpbasename + "\n" + tmpsuffix)
  76.            
  77.             self.JPEG.setValue(tmpfile.name)
  78.  
  79.  
  80.             return
Advertisement
Add Comment
Please, Sign In to add comment