# Path: mapchete/index.py
# def zoom_index_gen(
#     mp=None,
#     out_dir=None,
#     zoom=None,
#     tile=None,
#     geojson=False,
#     gpkg=False,
#     shapefile=False,
#     txt=False,
#     vrt=False,
#     fieldname="location",
#     basepath=None,
#     for_gdal=True,
#     threading=False,
#     **kwargs,
# ):
#     """
#     Generate indexes for given zoom level.
# 
#     Parameters
#     ----------
#     mp : Mapchete object
#         process output to be indexed
#     out_dir : path
#         optionally override process output directory
#     zoom : int
#         zoom level to be processed
#     geojson : bool
#         generate GeoJSON index (default: False)
#     gpkg : bool
#         generate GeoPackage index (default: False)
#     shapefile : bool
#         generate Shapefile index (default: False)
#     txt : bool
#         generate tile path list textfile (default: False)
#     vrt : bool
#         GDAL-style VRT file (default: False)
#     fieldname : str
#         field name which contains paths of tiles (default: "location")
#     basepath : str
#         if set, use custom base path instead of output path
#     for_gdal : bool
#         use GDAL compatible remote paths, i.e. add "/vsicurl/" before path
#         (default: True)
#     """
#     if tile and zoom:  # pragma: no cover
#         raise ValueError("tile and zoom cannot be used at the same time")
#     zoom = tile[0] if tile else zoom
#     for zoom in get_zoom_levels(process_zoom_levels=zoom):
#         with ExitStack() as es:
#             # get index writers for all enabled formats
#             index_writers = []
#             if geojson:
#                 index_writers.append(
#                     es.enter_context(
#                         VectorFileWriter(
#                             driver="GeoJSON",
#                             out_path=_index_file_path(out_dir, zoom, "geojson"),
#                             crs=mp.config.output_pyramid.crs,
#                             fieldname=fieldname,
#                         )
#                     )
#                 )
#             if gpkg:
#                 index_writers.append(
#                     es.enter_context(
#                         VectorFileWriter(
#                             driver="GPKG",
#                             out_path=_index_file_path(out_dir, zoom, "gpkg"),
#                             crs=mp.config.output_pyramid.crs,
#                             fieldname=fieldname,
#                         )
#                     )
#                 )
#             if shapefile:
#                 index_writers.append(
#                     es.enter_context(
#                         VectorFileWriter(
#                             driver="ESRI Shapefile",
#                             out_path=_index_file_path(out_dir, zoom, "shp"),
#                             crs=mp.config.output_pyramid.crs,
#                             fieldname=fieldname,
#                         )
#                     )
#                 )
#             if txt:
#                 index_writers.append(
#                     es.enter_context(
#                         TextFileWriter(out_path=_index_file_path(out_dir, zoom, "txt"))
#                     )
#                 )
#             if vrt:
#                 index_writers.append(
#                     es.enter_context(
#                         VRTFileWriter(
#                             out_path=_index_file_path(out_dir, zoom, "vrt"),
#                             output=mp.config.output,
#                             out_pyramid=mp.config.output_pyramid,
#                         )
#                     )
#                 )
# 
#             logger.debug("use the following index writers: %s", index_writers)
# 
#             if tile:
#                 output_tiles_batches = mp.config.output_pyramid.tiles_from_bounds(
#                     mp.config.process_pyramid.tile(*tile).bounds, zoom, batch_by="row"
#                 )
#             else:
#                 output_tiles_batches = mp.config.output_pyramid.tiles_from_geom(
#                     mp.config.area_at_zoom(zoom), zoom, batch_by="row"
#                 )
# 
#             for output_tile, exists in tiles_exist(
#                 mp.config, output_tiles_batches=output_tiles_batches
#             ):
# 
#                 tile_path = _tile_path(
#                     orig_path=mp.config.output.get_path(output_tile),
#                     basepath=basepath,
#                     for_gdal=for_gdal,
#                 )
#                 # get indexes where tile entry does not exist
#                 indexes = [
#                     index_writer
#                     for index_writer in index_writers
#                     if not index_writer.entry_exists(tile=output_tile, path=tile_path)
#                 ]
# 
#                 if indexes and exists:
#                     logger.debug("%s exists", tile_path)
#                     logger.debug("write to %s indexes", len(indexes))
#                     for index in indexes:
#                         index.write(output_tile, tile_path)
# 
#                 # yield tile for progress information
#                 yield output_tile
# 
# Path: mapchete/io/_misc.py
# def get_boto3_bucket(bucket_name):
#     """Return boto3.Bucket object from bucket name."""
#     import boto3
# 
#     url = os.environ.get("AWS_S3_ENDPOINT")
#     return boto3.resource(
#         "s3",
#         endpoint_url=(
#             "https://" + url
#             if url and not url.startswith(("http://", "https://"))
#             else url
#         ),
#     ).Bucket(bucket_name)


import fiona
import numpy as np
import os
import pytest
import rasterio
import mapchete
from mapchete.index import zoom_index_gen
from mapchete.io import get_boto3_bucket



@pytest.mark.remote
def test_remote_indexes(gtiff_s3):

    zoom = 7
    gtiff_s3.dict.update(zoom_levels=zoom)

    def gen_indexes_and_check():
        # generate indexes
        list(
            zoom_index_gen(
                mp=mp,
                zoom=zoom,
                out_dir=mp.config.output.path,
                geojson=True,
                txt=True,
                vrt=True,
            )
        )

        # assert GeoJSON exists
        with fiona.open(
            os.path.join(mp.config.output.path, "%s.geojson" % zoom)
        ) as src:
            assert len(src) == 2

        # assert TXT exists
        txt_index = os.path.join(mp.config.output.path, "%s.txt" % zoom)

# Path: mapchete/index.py
# def zoom_index_gen(
#     mp=None,
#     out_dir=None,
#     zoom=None,
#     tile=None,
#     geojson=False,
#     gpkg=False,
#     shapefile=False,
#     txt=False,
#     vrt=False,
#     fieldname="location",
#     basepath=None,
#     for_gdal=True,
#     threading=False,
#     **kwargs,
# ):
#     """
#     Generate indexes for given zoom level.
# 
#     Parameters
#     ----------
#     mp : Mapchete object
#         process output to be indexed
#     out_dir : path
#         optionally override process output directory
#     zoom : int
#         zoom level to be processed
#     geojson : bool
#         generate GeoJSON index (default: False)
#     gpkg : bool
#         generate GeoPackage index (default: False)
#     shapefile : bool
#         generate Shapefile index (default: False)
#     txt : bool
#         generate tile path list textfile (default: False)
#     vrt : bool
#         GDAL-style VRT file (default: False)
#     fieldname : str
#         field name which contains paths of tiles (default: "location")
#     basepath : str
#         if set, use custom base path instead of output path
#     for_gdal : bool
#         use GDAL compatible remote paths, i.e. add "/vsicurl/" before path
#         (default: True)
#     """
#     if tile and zoom:  # pragma: no cover
#         raise ValueError("tile and zoom cannot be used at the same time")
#     zoom = tile[0] if tile else zoom
#     for zoom in get_zoom_levels(process_zoom_levels=zoom):
#         with ExitStack() as es:
#             # get index writers for all enabled formats
#             index_writers = []
#             if geojson:
#                 index_writers.append(
#                     es.enter_context(
#                         VectorFileWriter(
#                             driver="GeoJSON",
#                             out_path=_index_file_path(out_dir, zoom, "geojson"),
#                             crs=mp.config.output_pyramid.crs,
#                             fieldname=fieldname,
#                         )
#                     )
#                 )
#             if gpkg:
#                 index_writers.append(
#                     es.enter_context(
#                         VectorFileWriter(
#                             driver="GPKG",
#                             out_path=_index_file_path(out_dir, zoom, "gpkg"),
#                             crs=mp.config.output_pyramid.crs,
#                             fieldname=fieldname,
#                         )
#                     )
#                 )
#             if shapefile:
#                 index_writers.append(
#                     es.enter_context(
#                         VectorFileWriter(
#                             driver="ESRI Shapefile",
#                             out_path=_index_file_path(out_dir, zoom, "shp"),
#                             crs=mp.config.output_pyramid.crs,
#                             fieldname=fieldname,
#                         )
#                     )
#                 )
#             if txt:
#                 index_writers.append(
#                     es.enter_context(
#                         TextFileWriter(out_path=_index_file_path(out_dir, zoom, "txt"))
#                     )
#                 )
#             if vrt:
#                 index_writers.append(
#                     es.enter_context(
#                         VRTFileWriter(
#                             out_path=_index_file_path(out_dir, zoom, "vrt"),
#                             output=mp.config.output,
#                             out_pyramid=mp.config.output_pyramid,
#                         )
#                     )
#                 )
# 
#             logger.debug("use the following index writers: %s", index_writers)
# 
#             if tile:
#                 output_tiles_batches = mp.config.output_pyramid.tiles_from_bounds(
#                     mp.config.process_pyramid.tile(*tile).bounds, zoom, batch_by="row"
#                 )
#             else:
#                 output_tiles_batches = mp.config.output_pyramid.tiles_from_geom(
#                     mp.config.area_at_zoom(zoom), zoom, batch_by="row"
#                 )
# 
#             for output_tile, exists in tiles_exist(
#                 mp.config, output_tiles_batches=output_tiles_batches
#             ):
# 
#                 tile_path = _tile_path(
#                     orig_path=mp.config.output.get_path(output_tile),
#                     basepath=basepath,
#                     for_gdal=for_gdal,
#                 )
#                 # get indexes where tile entry does not exist
#                 indexes = [
#                     index_writer
#                     for index_writer in index_writers
#                     if not index_writer.entry_exists(tile=output_tile, path=tile_path)
#                 ]
# 
#                 if indexes and exists:
#                     logger.debug("%s exists", tile_path)
#                     logger.debug("write to %s indexes", len(indexes))
#                     for index in indexes:
#                         index.write(output_tile, tile_path)
# 
#                 # yield tile for progress information
#                 yield output_tile
# 
# Path: mapchete/io/_misc.py
# def get_boto3_bucket(bucket_name):
#     """Return boto3.Bucket object from bucket name."""
#     import boto3
# 
#     url = os.environ.get("AWS_S3_ENDPOINT")
#     return boto3.resource(
#         "s3",
#         endpoint_url=(
#             "https://" + url
#             if url and not url.startswith(("http://", "https://"))
#             else url
#         ),
#     ).Bucket(bucket_name)


# Path: test/test_index.py
import fiona
import numpy as np
import os
import pytest
import rasterio
import mapchete
from mapchete.index import zoom_index_gen
from mapchete.io import get_boto3_bucket




@pytest.mark.remote
def test_remote_indexes(gtiff_s3):

    zoom = 7
    gtiff_s3.dict.update(zoom_levels=zoom)

    def gen_indexes_and_check():
        # generate indexes
        list(
            zoom_index_gen(
                mp=mp,
                zoom=zoom,
                out_dir=mp.config.output.path,
                geojson=True,
                txt=True,
                vrt=True,
            )
        )

        # assert GeoJSON exists
        with fiona.open(
            os.path.join(mp.config.output.path, "%s.geojson" % zoom)
        ) as src:
            assert len(src) == 2

        # assert TXT exists
        txt_index = os.path.join(mp.config.output.path, "%s.txt" % zoom)

        bucket = get_boto3_bucket(txt_index.split("/")[2])