def generate_viewsheds(iso3, sampling_areas, sampling_points):
    '\n\n    '
    sampling_areas = sampling_areas.to_crs('epsg:3857')
    folder_out_viewsheds = os.path.join(DATA_INTERMEDIATE, iso3, 'viewsheds')
    if (not os.path.exists(folder_out_viewsheds)):
        os.makedirs(folder_out_viewsheds)
    for (idx, sampling_area) in tqdm(sampling_areas.iterrows(), total=sampling_areas.shape[0]):
        output = []
        lon = sampling_area['geometry'].representative_point().coords[0][0]
        lat = sampling_area['geometry'].representative_point().coords[0][1]
        area_filename = '{}-{}'.format(lon, lat)
        print('--Working on {}'.format(area_filename))
        directory = os.path.join(DATA_INTERMEDIATE, iso3, 'sampling_points')
        points = gpd.read_file(os.path.join(directory, (area_filename + '.shp')))
        sampling_area_df = gpd.GeoDataFrame({'geometry': sampling_area['geometry']}, crs='epsg:3857', index=[0])
        sampling_area_df = sampling_area_df.to_crs('epsg:4326')
        for (idx, item) in sampling_area_df.iterrows():
            path_input = find_tile(item['geometry'].bounds, tile_lookup)
        for (idx, point) in tqdm(points.iterrows(), total=points.shape[0]):
            results = []
            lon = point['geometry'].representative_point().coords[0][0]
            lat = point['geometry'].representative_point().coords[0][1]
            filename2 = '{}-{}'.format(lon, lat)
            path_output = os.path.join(folder_out_viewsheds, filename2)
            file_path = os.path.join(path_output, 'location', 'PERMANENT', 'viewsheds', (filename2 + '.tif'))
            x = point['geometry'].coords[0][0]
            y = point['geometry'].coords[0][1]
            if (not os.path.exists(file_path)):
                try:
                    viewshed((x, y), path_input, path_output, filename2, 45000, 'epsg:4326')
                except:
                    print('--Viewshed already exists')
            seen = set()
            for (idx, node) in tqdm(points.iterrows(), total=points.shape[0]):
                x2 = node['geometry'].coords[0][0]
                y2 = node['geometry'].coords[0][1]
                link = '{}_{}_{}_{}'.format(x, y, x2, y2)
                if (link in seen):
                    continue
                dist = find_distance((x, y), (x2, y2))
                if (dist < 10):
                    continue
                los = check_los(file_path, (x2, y2))
                results.append({'sampling_area': area_filename, 'point_id': filename2, 'node_id': '{}_{}'.format(x2, y2), 'distance': dist, 'id_range_m': sampling_area['id_range_m'], 'decile': sampling_area['decile'], 'los': los})
                seen.add('{}_{}_{}_{}'.format(x, y, x2, y2))
                seen.add('{}_{}_{}_{}'.format(x2, y2, x, y))
            output = (output + results)
        output = pd.DataFrame(output)
        folder = os.path.join(DATA_INTERMEDIATE, iso3, 'los_results')
        if (not os.path.exists(folder)):
            os.makedirs(folder)
        output.to_csv(os.path.join(folder, (area_filename + '.csv')), index=False)
def generate_viewsheds(iso3, sampling_areas, sampling_points):
    '\n\n    '
    sampling_areas = sampling_areas.to_crs('epsg:3857')
    folder_out_viewsheds = os.path.join(DATA_INTERMEDIATE, iso3, 'viewsheds')
    if (not os.path.exists(folder_out_viewsheds)):
        os.makedirs(folder_out_viewsheds)
    for (idx, sampling_area) in tqdm(sampling_areas.iterrows(), total=sampling_areas.shape[0]):
        output = []
        lon = sampling_area['geometry'].representative_point().coords[0][0]
        lat = sampling_area['geometry'].representative_point().coords[0][1]
        area_filename = '{}-{}'.format(lon, lat)
        print('--Working on {}'.format(area_filename))
        directory = os.path.join(DATA_INTERMEDIATE, iso3, 'sampling_points')
        points = gpd.read_file(os.path.join(directory, (area_filename + '.shp')))
        sampling_area_df = gpd.GeoDataFrame({'geometry': sampling_area['geometry']}, crs='epsg:3857', index=[0])
        sampling_area_df = sampling_area_df.to_crs('epsg:4326')
        for (idx, item) in sampling_area_df.iterrows():
            path_input = find_tile(item['geometry'].bounds, tile_lookup)
        for (idx, point) in tqdm(points.iterrows(), total=points.shape[0]):
            results = []
            lon = point['geometry'].representative_point().coords[0][0]
            lat = point['geometry'].representative_point().coords[0][1]
            filename2 = '{}-{}'.format(lon, lat)
            path_output = os.path.join(folder_out_viewsheds, filename2)
            file_path = os.path.join(path_output, 'location', 'PERMANENT', 'viewsheds', (filename2 + '.tif'))
            x = point['geometry'].coords[0][0]
            y = point['geometry'].coords[0][1]
            if (not os.path.exists(file_path)):
                try:
                    viewshed((x, y), path_input, path_output, filename2, 45000, 'epsg:4326')
                except:
                    print('--Viewshed already exists')
            seen = set()
            for (idx, node) in tqdm(points.iterrows(), total=points.shape[0]):
                x2 = node['geometry'].coords[0][0]
                y2 = node['geometry'].coords[0][1]
                link = '{}_{}_{}_{}'.format(x, y, x2, y2)
                if (link in seen):
                    continue
                dist = find_distance((x, y), (x2, y2))
                if (dist < 10):
                    continue
                los = check_los(file_path, (x2, y2))
                results.append({'sampling_area': area_filename, 'point_id': filename2, 'node_id': '{}_{}'.format(x2, y2), 'distance': dist, 'id_range_m': sampling_area['id_range_m'], 'decile': sampling_area['decile'], 'los': los})
                seen.add('{}_{}_{}_{}'.format(x, y, x2, y2))
                seen.add('{}_{}_{}_{}'.format(x2, y2, x, y))
            output = (output + results)
        output = pd.DataFrame(output)
        folder = os.path.join(DATA_INTERMEDIATE, iso3, 'los_results')
        if (not os.path.exists(folder)):
            os.makedirs(folder)
        output.to_csv(os.path.join(folder, (area_filename + '.csv')), index=False)