Python装包

188 阅读1分钟

Python装包 --笔记

一、Pyhton安装

1、python的依赖包位置

E:\Python\Lib\site-packages

2、pycharm装包

#每个项目会有自己的依赖包,新建项目后,扩展里面搜索就行

#装包有问题的,可以尝试自行下载这个包,然后将这个包存放到在对应项目下的依赖包所在位置
项目文件夹 》 venv 》 lib 》 site-packages 》 粘贴位置

3、使用demo

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'SmallTiger'

from osgeo import gdal

def createEmptyTif(source,outFile):
    dataSource = gdal.Open(source)  # 打开数据源
    band = dataSource.GetRasterBand(1)  # 读取第一波段
    in_data = band.ReadAsArray(0, 0, dataSource.RasterXSize, dataSource.RasterYSize)  # 读取数组
    for i in range(len(in_data)):
        for j in range(len(in_data[0])):
            in_data[i][j] = 0
    gtiffDriver = gdal.GetDriverByName('GTiff')  # 驱动
    outPutImage = gtiffDriver.Create(outFile, \
                                     band.XSize, band.YSize, 1, band.DataType)
    outPutImage.SetProjection(dataSource.GetProjection())  # 设置坐标系
    outPutImage.SetGeoTransform(dataSource.GetGeoTransform())  # 设置六参数
    outPutImage.GetRasterBand(1).WriteArray(in_data)

def retuArr(source):
    dataSource = gdal.Open(source)
    band = dataSource.GetRasterBand(1)
    bandArray = band.ReadAsArray()
    return bandArray

def calculate(source):
    res1 = retuArr(sourPath + sourceName[0])
    res2 = retuArr(sourPath + sourceName[1])
    res3 = retuArr(sourPath + sourceName[2])
    res4 = retuArr(sourPath + sourceName[3])
    res5 = retuArr(sourPath + sourceName[4])
    res6 = retuArr(sourPath + sourceName[5])
    res7 = retuArr(sourPath + sourceName[6])
    res8 = retuArr(sourPath + tempTif)
    resArr = res1 + res2 + res3 + res4 + res5 + res6 + res7 + res8   #这里无法实现,因为范围不一致operands could not be broadcast together with shapes (2096,2304) (2816,3024)
    dataSource = gdal.Open(source)
    band = dataSource.GetRasterBand(1)
    gtiffDriver = gdal.GetDriverByName('GTiff')
    outPutImage = gtiffDriver.Create(sourPath + resTif, \
                                     band.XSize, band.YSize, 1, band.DataType)
    outPutImage.SetProjection(dataSource.GetProjection())  # 设置坐标系
    outPutImage.SetGeoTransform(dataSource.GetGeoTransform())  # 设置六参数
    outPutImage.GetRasterBand(1).WriteArray(resArr)

if __name__ == "__main__":
    sourPath = 'E:/ArcpyGIS/seconeTerm/result/source/'
    sourceName = ['reclass_school1.tif','reclass_school2.tif','reclass_school3.tif',\
                  'reclass_school4.tif','reclass_enter1.tif','reclass_enter2.tif',\
                  'reclass_enter3.tif']
    tempTif = 'temp.tif'
    resTif = 'result.tif'
    #createEmptyTif(sourPath + sourceName[6],sourPath + tempTif)
    calculate(sourPath + sourceName[6])