当前位置:   article > 正文

GEE|colab下python版云盘连接与获取云量低于50%日期的小应用示例_google colab 中的 python代码 访问google云盘

google colab 中的 python代码 访问google云盘

一、与GEE云盘连接

  1. import ee
  2. ee.Authenticate()

点击连接后,登录GEE账户,并复制验证码到小框里。

二、连接谷歌云盘

点击链接后连接到云盘根目录

  1. from google.colab import drive
  2. drive.mount('/content/drive')

点击链接后连接到云盘根目录。

  1. %cd /content/drive/My Drive
  2. !ls

检查是否连接成功,如果成功会出现你的云盘文件名字

三、获取50%云量以下影像日期并制作excel

  1. import datetime
  2. import dateutil.parser
  3. import ee
  4. import ipywidgets
  5. import IPython.display
  6. import numpy as np
  7. import pprint
  8. import pandas as pd
  9. import traitlets
  10. from xlwt import *
  11. import xlrd
  12. import pandas as pd
  1. def jisuan(shp,startt,end,cloud,SR):
  2. #下面是一个包含point的shp文件路径,我计算的是把一个影像,按照格网切分后,面转点,并获取包含点的影像日期
  3. shpfilee = ee.FeatureCollection('users/wenqikou/'+shp+"point").sort("ORIG_FID")
  4. timetable=[]
  5. sx=[]
  6. def composition(shpfile):
  7. image = ee.ImageCollection('COPERNICUS/S2'+SR).filterDate(startt,end).filterBounds(shpfile.geometry()).filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',cloud))
  8. return image
  9. y2020 = shpfilee.map(composition)
  10. sizze=y2020.size()
  11. icc=y2020.toList(sizze)
  12. dianshu=ee.Number(sizze).getInfo()
  13. print("十米格网数:",dianshu)
  14. for i in range(dianshu):
  15. time=[]
  16. imagec=ee.ImageCollection(icc.get(i))
  17. sizeee=imagec.size()
  18. image11=imagec.toList(sizeee)
  19. for j in range(ee.Number(sizeee).getInfo()):
  20. epoch=ee.Image(image11.get(j)).get("system:time_start")
  21. readableDate = ee.Date(epoch)
  22. stringDate = ee.String(readableDate.format("YYYY-MM-dd"))
  23. rq=stringDate.getInfo()
  24. time.append(rq)
  25. time=set(time)
  26. print(i,len(time))
  27. timetable.append(sorted(list(time)))
  28. sx.append(len(time))
  29. # total=0
  30. # for ele in range(0,len(sx)):
  31. # total = total + sx[ele]
  32. # print("总时相数与格网数",total,dianshu)
  33. bb=list(range(1,dianshu+1))
  34. erw=list(zip(bb,sx))
  35. # print("各点对应时相数",erw)
  36. def deal1():
  37. company_name_list = erw
  38. df = pd.DataFrame(company_name_list, columns=['xuhao', 'geshu'])
  39. df.to_excel("cjccy/"+shp+"/"+startt+SR+".xlsx", index=False)
  40. data={}
  41. file = Workbook(encoding = 'utf-8')
  42. table = file.add_sheet('data')
  43. for i in range(len(timetable)):
  44. data[i]= timetable[i]
  45. ldata = []
  46. num = [aa for aa in data]
  47. num.sort()
  48. for x in num:
  49. t = [int(x)]
  50. for aa in data[x]:
  51. t.append(aa)
  52. ldata.append(t)
  53. for i,p in enumerate(ldata):
  54. for j,q in enumerate(p):
  55. # print i,j,q
  56. table.write(i,j,q)
  57. file.save('cjccy/'+shp+"/"+startt+SR+'dataqs.xlsx')
  58. datapp=[]
  59. data = xlrd.open_workbook('cjccy/'+shp+"/"+startt+SR+'dataqs.xlsx')
  60. table_index=data.sheet_by_name("data")
  61. for i in range(19):
  62. cols_values=table_index.row_values(i)
  63. datapp.append(cols_values)
  64. b=[]
  65. xt = datapp[0]
  66. for i in range(len(datapp)):
  67. axt = [x for x in xt if x in datapp[i]]
  68. xt = axt
  69. print(len(xt))
  70. print(xt)
  71. def deal():
  72. # 列表
  73. company_name_list = xt
  74. # list转dataframe
  75. df = pd.DataFrame(company_name_list, columns=['shixu'])
  76. # 保存到本地excel
  77. df.to_excel('cjccy/'+shp+"/"+startt+SR+'dataqss.xlsx', index=False)
  78. if __name__ == '__main__':
  79. deal1()
  80. deal()
  1. ee.Initialize()
  2. jisuan("qixia","2020-03-01","2020-10-30",20,"_SR")

函数使用,但感觉没人会用到这个函数hhhh,生成3个excel,分别是每个点的日期,每个点的日期个数,所有点共同包含的日期哦~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/1010694
推荐阅读
相关标签
  

闽ICP备14008679号