楼主: ReneeBK
2114 10

PyRaster:Python Tools for Processing Geospatial Raster Data [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
PyRaster is a collection of Python tools for processing geospatial raster data as NumPy arrays.

本帖隐藏的内容

PyRaster-master.zip (79.26 KB)



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Processing processI Process Spatial python collection

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-9-12 08:06:56 |只看作者 |坛友微信交流群
  1. #!/usr/bin/env python
  2. # Import standard modules and rasterIO
  3. import sys, os, string, rasterIO

  4. # Get a list of files in current directory
  5. # Create a loop for all files in current directory

  6. for file in flist:
  7.         # Check file type (in this case Geotiff)   
  8.         if file.endswith('.tif'):
  9.         
  10.         # Open a pointer to the file
  11.         pointer = rasterIO.opengdalraster(file)
  12.         
  13.         # Read the raster metadata for the new output file
  14.         driver, XSize, YSize, proj_wkt, geo_t_params = rasterIO.readrastermeta(pointer)
  15.         
  16.         # Read the first band to a matrix called band_1
  17.         band_1 =rasterIO.readrasterband(pointer, 1)
  18.         
  19.         # Read the second band to a matrix called band_2
  20.         band_2 = rasterIO.readrasterband(pointer, 2)
  21.         
  22.         # Perform the NDVI calculation and put the results into a new matrix
  23.         new_ndvi_band = ((band_2 - band_1) / (band_2 + band_1))
  24.         # Get the input file filename without extension and create a new file name
  25.         parts = string.split(file)
  26.         newname ='./'+parts[0]+'_ndvi.tif' # ./filename_ndvi.tif
  27.         # Get the EPSG code from well known text projection
  28.         epsg =rasterIO.wkt2epsg(proj_wkt)
  29.         
  30.         # Write the NDVI matrix to a new raster file
  31.         rasterIO.writerasterband(new_ndvi_band, newname, XSize, YSize, geo_t_params, epsg)
  32.    
  33. # loop will now go to next file in input list
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-9-12 08:08:07 |只看作者 |坛友微信交流群
  1. #!/usr/bin/env python
  2. # Script to plot raster image histogram
  3. import sys, os, string, rasterIO
  4. import numpy.ma as ma
  5. import matplotlib.pyplot as plt

  6. # Create a histogram function
  7. def rasterHistogram(raster_matrix):
  8.     '''Accepts matrix and generates histogram'''
  9.      
  10.     # Line above is function docstring
  11.     # Flatten 2d-matrix
  12.     flat_raster = ma.compressed(raster_matrix)
  13.      
  14.     # Setup the plot (see matplotlib.sourceforge.net)
  15.     fig = plt.figure(figsize=(8,11))
  16.     ax = fig.add_subplot(1,1,1)
  17.      
  18.     # Plot histogram
  19.     ax.hist(flat_raster, 10, normed=0, histtype='bar',
  20.             align=mid)
  21.     # Show the plot on screen
  22.     plt.show()
  23.      
  24. # Open a raster using rasterIO
  25. #...
  26. # Pass matrix representation of raster to function
  27. rasterHistogram(band)
  28. # Shows histogram on screen
复制代码

使用道具

板凳
ReneeBK 发表于 2016-9-12 08:08:40 |只看作者 |坛友微信交流群
  1. #!/usr/bin/env python
  2. # Batch processing script with multiprocessing enabled

  3. import sys, os, string, rasterIO
  4. from multiprocessing import Process, Queue

  5. # Create a function for the process

  6. def processfunction(file_list):
  7.             
  8.         # Create a loop for all files in current directory

  9.             
  10.         for file in file_list:
  11.             
  12.                 # Check file type (in this case Geotiff)
  13.                 if file.endswith('.tif'):
  14.                             
  15.                 # Open a pointer to the file
  16.                 pointer = rasterIO.opengdalraster(file)
  17.                             
  18.                 # Read the raster metadata for the new output file
  19.                 driver, XSize, YSize, proj_wkt, geo_t_params = rasterIO.readrastermeta(pointer)
  20.                             
  21.                 # Read the first band to a matrix called band_1
  22.                 band_1 = rasterIO.readrasterband(pointer, 1)
  23.                             
  24.                 # Read the second band to a matrix called band_2

  25.                 band_2 =rasterIO.readrasterband(pointer, 2)
  26.                             
  27.                 # Perform the NDVI calculation and put the results into a new matrix
  28.                 new_ndvi_band = ((band_2 - band_1) / (band_2 + band_1))
  29.                             
  30.                 # Get the input file filename without extension and create a new file name
  31.                 parts =string.split(file)
  32.                 newname = './'+parts[0]+'_ndvi.tif' # ./filename_ndvi.tif

  33.         # Get the EPSG code from well known text projection
  34.         epsg = rasterIO.wkt2epsg(proj_wkt)
  35.        
  36.         # Write the NDVI matrix to a new raster file
  37.         rasterIO.writerasterband(new_ndvi_band, newname, XSize, YSize, geo_t_params, epsg)
  38.    
  39. # loop will now go to next file in input list
  40. # Create a run function to accept jobs from queue
  41. def processRun(q, filename):
  42.         q.put(processfunc(filename))

  43. # Create the main function
  44. def main(arg=sys.argv):
  45.         # Get a list fo files in the current directory (assumed to contain raster data)
  46.         file_list = os.listdir(os.getcwd())
  47.             
  48.         # Get the length of the file list
  49.         len_flist = len(file_list)
  50.         half_len = len_flist / 2

  51.         # Create a queue object
  52.         q = Queue()
  53.             
  54.         # Create two processes (add more depending on processor availability)
  55.         p1 = Process(target=processRun, args=(q, flist[:half_len]))
  56.         p2 = Process(target=processRun, args=(q, flist[half_len:]))

  57.         # Start processes
  58.         p1.start()
  59.         p2.start()

  60. # Standard Python script execution/exit handling

  61. if __name__=='__main__':
  62.         sys.exit(main())
复制代码

使用道具

报纸
ReneeBK 发表于 2016-9-12 08:09:48 |只看作者 |坛友微信交流群
  1. #The numerical Python package Numpy contains several functions to optimise matrix
  2. #operations. Using the numpy.vectorize() function it is possible to create an
  3. #optimised matrix function from a standard function.

  4. # Test each item ('pixel') in a 2d-matrix
  5. #...
  6. # first define a standard non-matrix function...
  7. def value_test(a, b):
  8.     if a <= b:
  9.             return 1
  10.         else:
  11.             return 0
  12. # now vectorize the function
  13. value_test_vect = numpy.vectorize(value_test)
  14. # now pass the new vectorized function the matrix
  15. new_matrix = value_test_vect(matrix, some_value)
  16. #...
复制代码

使用道具

地板
ReneeBK 发表于 2016-9-12 08:10:32 |只看作者 |坛友微信交流群
  1. #Read a raster file

  2. # Import the rasterIO module
  3. import rasterIO

  4. # Open a gdal pointer to a file on disk
  5. file_pointer = rasterIO.opengdalraster(
  6.     '/path/surface_temperature_kelvin.tif')

  7. # Get the spatial reference information from the image
  8. drive, XSize, YSize, proj_wkt, geo_t_params =
  9.     rasterIO.readrastermeta(pointer)

  10. # Read a band from the file to a new matrix in memory,
  11.     #the number indicates which band to read.
  12. kelvin_band = rasterIO.readrasterband(file_pointer, 1)
复制代码

使用道具

7
colongkong 发表于 2016-9-12 08:34:45 |只看作者 |坛友微信交流群
谢谢楼主,看看是什么!

使用道具

8
jinyizhe282 发表于 2016-9-12 11:02:14 |只看作者 |坛友微信交流群
hhahhaaaaaaaaaaaaaaa

使用道具

9
shakespare 发表于 2016-9-18 10:22:09 |只看作者 |坛友微信交流群
谢谢!

使用道具

10
神戶猿·風 发表于 2016-9-19 00:30:14 |只看作者 |坛友微信交流群
GIS for Statistic

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 05:51