def main(): """Main method for script.""" parser = argparse.ArgumentParser() parser.add_argument('shapegrid_filename', type=str, help='File location of the shapegrid shapefile') parser.add_argument('pam_filename', type=str, help='File location of the PAM matrix for statistics') parser.add_argument('tree_filename', type=str, help='File location of the tree to use for statistics') parser.add_argument('tree_schema', choices=['newick', 'nexus'], help='The tree schema') parser.add_argument('out_geojson_filename', type=str, help='File location to write the output GeoJSON') parser.add_argument('--layer', nargs=2, action='append', help='File location of a layer followed by a label') args = parser.parse_args() # Load data pam = Matrix.load(args.pam_filename) tree = TreeWrapper.get(path=args.tree_filename, schema=args.tree_schema) # Encode layers encoded_layers = encode_environment_layers(args.shapegrid_filename, args.layer) # Calculate PAM statistics stats_mtx = calculate_tree_site_statistics(pam, tree) # Join encoded layers and PAM statistics mtx = join_encoded_layers_and_pam_stats(encoded_layers, stats_mtx) # Generate GeoJSON geojson_data = create_geojson(args.shapegrid_filename, mtx) # Write GeoJSON with open(args.out_geojson_filename, 'w') as out_file: json.dump(geojson_data, out_file)
def main(): """Main method for script.""" parser = argparse.ArgumentParser() parser.add_argument('--out_stats_matrix_filename', type=str, help='Location to write statistics matrix.') parser.add_argument('shapegrid_filename', type=str, help='File location of the shapegrid shapefile') parser.add_argument('pam_filename', type=str, help='File location of the PAM matrix for statistics') parser.add_argument('tree_filename', type=str, help='File location of the tree to use for statistics') parser.add_argument('tree_schema', choices=['newick', 'nexus'], help='The tree schema') parser.add_argument('out_geojson_filename', type=str, help='File location to write the output GeoJSON') parser.add_argument('out_csv_filename', type=str, help='File location to write the output CSV') parser.add_argument('out_matrix_filename', type=str, help='File location to write the output matrix') parser.add_argument('--layer', nargs=2, action='append', help='File location of a layer followed by a label') args = parser.parse_args() # Load data pam = Matrix.load(args.pam_filename) tree = TreeWrapper.get(path=args.tree_filename, schema=args.tree_schema) # Encode layers encoded_layers = encode_environment_layers(args.shapegrid_filename, args.layer) # Calculate PAM statistics stats_mtx = calculate_tree_site_statistics(pam, tree) if args.out_stats_matrix_filename: stats_mtx.write(args.out_stats_matrix_filename) # Join encoded layers and PAM statistics mtx = join_encoded_layers_and_pam_stats(encoded_layers, stats_mtx) # Generate GeoJSON geojson_data = create_geojson(args.shapegrid_filename, mtx) # Write GeoJSON with open(args.out_geojson_filename, 'w') as out_file: json.dump(geojson_data, out_file, indent=4) # Write matrix data new_rh = [] res = 0.5 for _, x, y in mtx.get_row_headers(): min_x = x - res max_x = x + res min_y = y - res max_y = y + res new_rh.append('"POLYGON (({} {},{} {},{} {},{} {},{} {}))"'.format( min_x, max_y, max_x, max_y, max_x, min_y, min_x, min_y, min_x, max_y)) mtx.write(args.out_matrix_filename) mtx.set_row_headers(new_rh) with open(args.out_csv_filename, 'w') as out_file: mtx.write_csv(out_file)