Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3. import getpass
  4. import logging
  5. import sys
  6.  
  7. import geocoder
  8. import numpy as np
  9. from github import Github
  10. from PIL import Image
  11. from pyproj import Proj
  12.  
  13. from pycoast import ContourWriter, ContourWriterAGG
  14.  
  15. LOG = logging.getLogger(__name__)
  16.  
  17.  
  18. def get_all_pytroll_contributors(user):
  19. g = Github(user, getpass.getpass('GitHub Password: '))
  20. pytroll_org = g.get_organization('pytroll')
  21. LOG.info("Getting all PyTroll repositories...")
  22. pytroll_repos = [x for x in pytroll_org.get_repos() if x.name != u'aggdraw']
  23. LOG.info("Getting all PyTroll contributors...")
  24. all_pytroll_contributors = [
  25. u for r in pytroll_repos for u in r.get_contributors()]
  26. set_pytroll_contributors = {u.login: u for u in all_pytroll_contributors}
  27. return set_pytroll_contributors
  28.  
  29. def main():
  30. import argparse
  31. parser = argparse.ArgumentParser(
  32. description="Create a world map image of PyTroll contributor locations")
  33. parser.add_argument('--map-proj', default="+proj=moll",
  34. help='PROJ.4 map projection string to use for the output image')
  35. parser.add_argument('--output-size', default=(800, 600), nargs=2, type=int,
  36. help='\'width height\' of output image')
  37. parser.add_argument('--contributor-color', default=(255, 127, 127),
  38. help='Color of dots for each contributor')
  39. parser.add_argument('--bg-color', default=None, nargs=3, type=int,
  40. help='Background color of image \'R G B\' 0-255 (default: transparent)')
  41. parser.add_argument('-o', '--output', default='contributors_map.png',
  42. help='Output filename to save the image to')
  43. parser.add_argument('-u', '--github-user', required=True,
  44. help='github username')
  45. parser.add_argument('gshhg_dir',
  46. help='root directory for GHSSG shape files')
  47. args = parser.parse_args()
  48.  
  49. logging.basicConfig(level=logging.INFO)
  50.  
  51. # data_array = np.zeros((args.output_size[1], args.output_size[0], 4), dtype=np.uint8)
  52. # if args.bg_color:
  53. # data_array[:, :, 0] = args.bg_color[0]
  54. # data_array[:, :, 1] = args.bg_color[1]
  55. # data_array[:, :, 2] = args.bg_color[2]
  56. # data_array[:, :, 3] = 255
  57. im = Image.new('RGBA', args.output_size, color=tuple(args.bg_color))
  58. cw = ContourWriterAGG(args.gshhg_dir)
  59. p = Proj(args.map_proj)
  60. a, trash = p(-180, 0)
  61. trash, b = p(0, -90)
  62. c, trash = p(180, 0)
  63. trash, d = p(0, 90)
  64.  
  65. extents = (a, b, c, d)
  66. area_def = (args.map_proj, extents)
  67.  
  68. LOG.info("Getting PyTroll contributors...")
  69. contributors = get_all_pytroll_contributors(args.github_user)
  70. LOG.info("Getting all PyTroll contributor locations...")
  71. all_pytroll_locations = set([u.location for u in contributors.values()])
  72. all_lon_lats = set([tuple(geocoder.location(l).latlng[::-1])
  73. for l in all_pytroll_locations if l is not None])
  74. # all_x_y = [p(*lonlat[::-1]) for lonlat in all_lon_lats]
  75.  
  76. cw.add_coastlines(im, area_def, resolution='c', level=1, fill=(
  77. 127, 127, 127), fill_opacity=127, outline=(127, 127, 127))
  78. LOG.info("Applying contributor locations to map image...")
  79. for lon, lat in all_lon_lats:
  80. # make a polygon to represent the developer
  81. box_size = 1
  82.  
  83. loc_box = (
  84. (lon - box_size, lat - box_size),
  85. (lon - box_size, lat + box_size),
  86. (lon + box_size, lat + box_size),
  87. (lon + box_size, lat - box_size),
  88. )
  89. cw.add_polygon(im, area_def, loc_box,
  90. outline=args.contributor_color, fill=args.contributor_color, fill_opacity=127)
  91. # cw.add_grid(im, area_def, (180, 90), (180, 90),
  92. # write_text=False, fill=(127, 127, 127))
  93. im.save(args.output, dpi=(300, 300))
  94.  
  95.  
  96. if __name__ == "__main__":
  97. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement