Python CGDAL类——支持栅格数据的栅格计算/线性增强/滤波增强

该博客介绍了如何使用 Python 的 GDAL 库创建一个名为 CGDAL 的类,该类用于加载栅格图像并进行基本处理,如线性增强和空间滤波增强。CGDAL 类提供了读取像素、转换坐标等功能,并能根据 numpy 数组生成栅格图像。通过这个类,开发者可以方便地进行地理空间数据的预处理操作。
摘要由CSDN通过智能技术生成
# -*- coding: UTF-8 -*-

'''
python version: 2.7.11
numpy  ver=1.11.1
gdal   ver=2.0.3
Author: Liuph
Date: 2016/9/9
Description: This is a GDAL Class adapted from the Python GDAL_OGR Cookbook documentation
(http://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html). The CGDAL Class can used to
load Image and obtain description information of the image file. Usually, basic image processing
is included in the class and a linear enhancement or a spatial filtering operation can well performed
via CGDAL Class. Moreover, this class offers some functions to generate a raster image via a numpy array.
However,it' does not process perfection in exception handling, for instance, while a image with "None"
as nodata value might lead to some puzzles, or, it wont't happen.It need to be confirmed.
 Addition, as all know, codes in python formats is very concise and distinct. However, the ratio of running speed of python
 codes and C++ codes is about first in thirty ,which is a serious and longstanding problem, or shortcut.
 May python be better!
'''



from osgeo import gdal, gdalnumeric, ogr, osr
from PIL import Image, ImageDraw
import os, sys
from gdalconst import *
import struct
import numpy as np
import re
gdal.UseExceptions()


class CGDAL:
    #数据部分
    mpoDataset = None
    __mpData = None
    mpArray = np.array([])
    mgDataType = GDT_Byte
    mnRows = mnCols = mnBands = -1
    mnDatalength = -1
    mpGeoTransfor = []
    msProjectionRef = ""
    msFilename = ""
    mdInvalidValue = 0.0
    mnPerPixSize = 1

    srcSR = None
    latLongSR = None
    poTransform = None
    poTransformT = None

    #函数部分
    def __init__(self):
        pass

    def __del__(self):
        self.mpoDataset = None
        self.__mpData = None
        self.mpArray = np.array([])
        self.mgDataType = GDT_Byte
        self.mnRows = self.mnCols = self.mnBands = -1
        self.mnDatalength = -1
        self.mpGeoTransform = []
        self.msProjectionRef = ""
        self.msFilename = ""
        self.mdInvalidValue = 0.0
        self.mnPerPixSize = 1

        self.srcSR = None
        self.latLongSR = None
        self.poTransform = None
        self.poTransformT = None


    def read(self, band, row, col):
        return self.mpArray[band, row, col]

    def printimg(self):
        print self.mpArray

    def isValid(self):
        if self.__mpData == None or self.mpoDataset == None:
            return False
        return True

    def world2Pixel(self, lat, lon):
        if self.poTransformT is not None:
            CST = osr.CoordinateTransformation(self.poTransformT)
            CST.TransformPoint(lon, lat)

            adfInverseGeoTransform = []
            x = y = 0.0
            gdal.InvGeoTransform(self.mpGeoTransform, adfInverseGeoTransform)
            gdal.ApplyGeoTransform(adfInverseGeoTransform, lon, lat, x, y)
        return {'x': x, 'y': y}

    def pixel2World(self, x, y):
        if self.poTransform is not None:
            self.poTransform = None
            self.poTransform = osr.CoordinateTransformation(self.latLongSR, self.srcSR)

        lon = lat = 0.0
        gdal.ApplyGeoTransform(self.mpGeoTransform, x, y, lon, lat)
        if self.poTransform is not None:
            CST = osr.CoordinateTransformation(self.poTransform)
            CST.TransformPoint(lon, lat)
        return {'lon': lon, 'lat': lat}

    def pixel2Ground(self, x, y):
        pX = pY = 0.0
        gdal.ApplyGeoTransform(self.mpGeoTransform, x, y, pX, pY)
        return {'pX': pX, 'pY': pY}

    def ground2Pixel(self, pX, pY):
        x = y = 0.0
        adfInverseGeoTransform = []
        gdal.InvGeoTransform(self.mpGeoTransform, adfInverseGeoTransform)
        gdal.ApplyGeoTransform(adfInverseGeoTransform, pX, pY, x, y)
        return {'x': x, 'y': y}

    def loadFrom(self,filename):
        #close for
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值