Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import numpy as np
  3.  
  4. from methods import sampler
  5.  
  6. maps = [
  7. '{}_slope.tif'.format(continent) for continent in ['au', 'eu']
  8. ]
  9.  
  10. def create_tiles(size):
  11. for ulLat in range(90, -91+size, -size):
  12. for ulLon in range(-180, 181-size, size):
  13. yield ulLon, ulLat, ulLon + size, ulLat - size
  14.  
  15. def sample_point_xy(px, py, rb, cols, rows, win_size=1, func=None):
  16. if (px >= 0) & (px <= cols) & (py >= 0) & (py <= rows): # check if within map extent
  17. if win_size > 1:
  18. intval = rb.ReadAsArray(
  19. int(px - win_size / 2.), int(py - win_size / 2.), win_size, win_size)
  20. else:
  21. intval = rb.ReadAsArray(
  22. int(px), int(py), win_size, win_size)
  23. if func is not None:
  24. value = func(intval)
  25. else:
  26. value = intval
  27. else:
  28. value = np.nan
  29. return value
  30.  
  31. def get_samples():
  32. size = 4
  33. for m in maps:
  34. rb, gt, cols, rows = sampler.get_raster_info(m)
  35. for tile in create_tiles(size):
  36. ulLon, ulLat, lrLon, lrLat = tile
  37. ulx, uly = sampler.MapToPixel(ulLon, ulLat, gt)
  38. lrx, lry = sampler.MapToPixel(lrLon, lrLat, gt)
  39. win_size = int(max(lrx-ulx, uly-lry))
  40. px, py = (ulx + lrx) / 2, (uly + lry) / 2
  41. sample = sample_point_xy(px, py, rb, cols, rows, win_size=win_size)
  42. yield sample
  43.  
  44. for sample in get_samples():
  45. if sample is not None and not np.isnan(sample):
  46. print(sample)
  47. print(sum(sum(sample)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement