Demo entry 6236012

funzioni gidal

   

Submitted by anonymous on Oct 13, 2016 at 17:13
Language: Python. Code size: 3.6 kB.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import numpy as np
from osgeo import gdal, gdalconst, osr

class gdal_fs:
    def __init__(self):
        description = "supporting functions"
    
    def rasterToAsc(self,fnInput,fnOutput):
        orig = gdal.Open(fnInput, gdalconst.GA_ReadOnly)
        driver = gdal.GetDriverByName('AAIGrid') 
        dst = driver.CreateCopy(fnOutput, orig)
        del dst
    
    def rasterToTiff(self,fnInput,fnOutput):
        orig = gdal.Open(fnInput, gdalconst.GA_ReadOnly)
        driver = gdal.GetDriverByName('GTiff') 
        dst = driver.CreateCopy(fnOutput, orig)
        del dst
    
    def adatta_raster(self,src_filename,match_filename,dst_filename):
        src = gdal.Open(src_filename, gdalconst.GA_ReadOnly)
        src_proj = src.GetProjection()
        src_geotrans = src.GetGeoTransform()
        match_ds = gdal.Open(match_filename, gdalconst.GA_ReadOnly)
        match_proj = match_ds.GetProjection()
        match_geotrans = match_ds.GetGeoTransform()
        wide = match_ds.RasterXSize
        high = match_ds.RasterYSize
        dst = gdal.GetDriverByName('GTiff').Create(dst_filename, wide, high, 1, gdalconst.GDT_Float32)
        dst.SetGeoTransform( match_geotrans )
        dst.SetProjection( match_proj)
        gdal.ReprojectImage(src, dst, src_proj, match_proj, gdalconst.GRA_Bilinear)
        del dst
    
    def adatta_raster_asc(self,src_filename,match_filename,dst_filename):
        src = gdal.Open(src_filename, gdalconst.GA_ReadOnly)
        src_proj = src.GetProjection()
        src_geotrans = src.GetGeoTransform()
        match_ds = gdal.Open(match_filename, gdalconst.GA_ReadOnly)
        match_proj = match_ds.GetProjection()
        match_geotrans = match_ds.GetGeoTransform()
        wide = match_ds.RasterXSize
        high = match_ds.RasterYSize
        dstTemp = gdal.GetDriverByName('MEM').Create(dst_filename, wide, high, 1, gdalconst.GDT_Float32)
        dstTemp.SetGeoTransform( match_geotrans )
        dstTemp.SetProjection( match_proj)
        gdal.ReprojectImage(src, dstTemp, src_proj, match_proj, gdalconst.GRA_Bilinear)
        driver = gdal.GetDriverByName('AAIGrid') 
        dst = driver.CreateCopy(dst_filename, dstTemp)
        del dst
        del dstTemp
    
    def get_crs(self,src_filename):
        src = gdal.Open(src_filename, gdalconst.GA_ReadOnly)
        print src.GetProjection()
    
    def set_crs(self,src_filename,proj4_code):
        src = gdal.Open(src_filename, gdalconst.GF_Write)
        sr = osr.SpatialReference()
        sr.ImportFromProj4(proj4_code)
        src.SetProjection(sr.ExportToWkt())
        del src

    def prodotto(self,src1_filename,src2_filename,dst_filename):
        src1 = gdal.Open(src1_filename, gdalconst.GA_ReadOnly)
        src2 = gdal.Open(src2_filename, gdalconst.GA_ReadOnly)
        src1_band = src1.GetRasterBand(1)
        src2_band = src2.GetRasterBand(1)
        src1_np = np.array(src1_band.ReadAsArray())
        src2_np = np.array(src2_band.ReadAsArray())
        prod=np.multiply(src1_np,src2_np)
        wide = src1.RasterXSize
        high = src1.RasterYSize
        dst_geotrans = src1.GetGeoTransform()
        dst_proj = src1.GetProjection()
        dst = gdal.GetDriverByName('GTiff').Create(dst_filename, wide, high, 1, gdalconst.GDT_Float32)
        dst.SetGeoTransform(dst_geotrans)
        dst.SetProjection( dst_proj)
        dst.GetRasterBand(1).WriteArray(prod)
        dst.FlushCache() 

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).