Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # encoding: utf-8
- import getpass
- import logging
- import sys
- import geocoder
- import numpy as np
- from github import Github
- from PIL import Image
- from pyproj import Proj
- from pycoast import ContourWriter, ContourWriterAGG
- LOG = logging.getLogger(__name__)
- def get_all_pytroll_contributors(user):
- g = Github(user, getpass.getpass('GitHub Password: '))
- pytroll_org = g.get_organization('pytroll')
- LOG.info("Getting all PyTroll repositories...")
- pytroll_repos = [x for x in pytroll_org.get_repos() if x.name != u'aggdraw']
- LOG.info("Getting all PyTroll contributors...")
- all_pytroll_contributors = [
- u for r in pytroll_repos for u in r.get_contributors()]
- set_pytroll_contributors = {u.login: u for u in all_pytroll_contributors}
- return set_pytroll_contributors
- def main():
- import argparse
- parser = argparse.ArgumentParser(
- description="Create a world map image of PyTroll contributor locations")
- parser.add_argument('--map-proj', default="+proj=moll",
- help='PROJ.4 map projection string to use for the output image')
- parser.add_argument('--output-size', default=(800, 600), nargs=2, type=int,
- help='\'width height\' of output image')
- parser.add_argument('--contributor-color', default=(255, 127, 127),
- help='Color of dots for each contributor')
- parser.add_argument('--bg-color', default=None, nargs=3, type=int,
- help='Background color of image \'R G B\' 0-255 (default: transparent)')
- parser.add_argument('-o', '--output', default='contributors_map.png',
- help='Output filename to save the image to')
- parser.add_argument('-u', '--github-user', required=True,
- help='github username')
- parser.add_argument('gshhg_dir',
- help='root directory for GHSSG shape files')
- args = parser.parse_args()
- logging.basicConfig(level=logging.INFO)
- # data_array = np.zeros((args.output_size[1], args.output_size[0], 4), dtype=np.uint8)
- # if args.bg_color:
- # data_array[:, :, 0] = args.bg_color[0]
- # data_array[:, :, 1] = args.bg_color[1]
- # data_array[:, :, 2] = args.bg_color[2]
- # data_array[:, :, 3] = 255
- im = Image.new('RGBA', args.output_size, color=tuple(args.bg_color))
- cw = ContourWriterAGG(args.gshhg_dir)
- p = Proj(args.map_proj)
- a, trash = p(-180, 0)
- trash, b = p(0, -90)
- c, trash = p(180, 0)
- trash, d = p(0, 90)
- extents = (a, b, c, d)
- area_def = (args.map_proj, extents)
- LOG.info("Getting PyTroll contributors...")
- contributors = get_all_pytroll_contributors(args.github_user)
- LOG.info("Getting all PyTroll contributor locations...")
- all_pytroll_locations = set([u.location for u in contributors.values()])
- all_lon_lats = set([tuple(geocoder.location(l).latlng[::-1])
- for l in all_pytroll_locations if l is not None])
- # all_x_y = [p(*lonlat[::-1]) for lonlat in all_lon_lats]
- cw.add_coastlines(im, area_def, resolution='c', level=1, fill=(
- 127, 127, 127), fill_opacity=127, outline=(127, 127, 127))
- LOG.info("Applying contributor locations to map image...")
- for lon, lat in all_lon_lats:
- # make a polygon to represent the developer
- box_size = 1
- loc_box = (
- (lon - box_size, lat - box_size),
- (lon - box_size, lat + box_size),
- (lon + box_size, lat + box_size),
- (lon + box_size, lat - box_size),
- )
- cw.add_polygon(im, area_def, loc_box,
- outline=args.contributor_color, fill=args.contributor_color, fill_opacity=127)
- # cw.add_grid(im, area_def, (180, 90), (180, 90),
- # write_text=False, fill=(127, 127, 127))
- im.save(args.output, dpi=(300, 300))
- if __name__ == "__main__":
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement