import json

import geojson
from geojson import Polygon, Feature, FeatureCollection

# # Local code for compact printing of geojson
from utils import CompactFeature, CompactFeatureCollection

"""
Explanation: Repository AOIs
This notebook gathers all the aois used in this repo and saves them as a geojson file.
To grab a single aoi geojson string to add to your notebook, just uncomment the line that prints the aoi. Be sure to add your notebook to the list of notebooks used by that aoi!
To specify a new aoi or grab an aoi and indicate indicate that the aoi is used by an additional notebook, jump to Specify AOIs.
NOTE: Be sure to run the entire notebook so the aois get printed to geojson at the end
Setup
End of explanation
"""


class Aoi(CompactFeature):
    def __init__(self, name, used_by, coordinates, id=None):
        pg = Polygon(coordinates)
        prop = {'name': name, 'used by': used_by}
        if id:
            prop['id']: id
        super(CompactFeature, self).__init__(geometry=pg, properties=prop)
        self["type"] = 'Feature'

class Aois(CompactFeatureCollection):
    def __init__(self, features=None, **extras):
        if features is not None:
            for f in features:
                self._check_aoi(f)
        else:
            features = []
        super(CompactFeatureCollection, self).__init__(features, **extras)
        self.type = 'FeatureCollection'

    @staticmethod
    def _check_aoi(aoi):
        if not isinstance(aoi, Aoi):
            raise(Exception('expected instance of Aoi, got {}'.format(type(aoi).__name__)))

    def append(self, aoi):
        self._check_aoi(aoi)
        self.features.append(aoi)
    
    def write(self, filename):
        with open(filename, 'w') as dest:
            dest.write(self.__str__())

"""
Explanation: AOI management
Some classes that make it a little easier to manage aois and create compact geojson string representations of the aois.
End of explanation
"""


aoi_filename = 'aois.geojson'

aois = Aois()

"""
Explanation: AOIs
Setup
End of explanation
"""


iowa_crops = Aoi(
    'iowa_crops',
    ['crop_classification/classify_cart_l8_ps',
     'coverage/calculate_coverage_wgs84'],
    [[
        [-93.2991294841129, 42.6995987669915],
        [-93.2996742314127, 42.8127566482941],
        [-93.2884356831875, 42.8619208871588],
        [-93.2653319466575, 42.9248165306276],
        [-92.9938730936993, 42.9251238519476],
        [-92.9938880477425, 42.7736373428868],
        [-92.9983961055212, 42.7545290276869],
        [-93.0191535706845, 42.6999877495273],
        [-93.2991294841129, 42.6995987669915]
    ]])
aois.append(iowa_crops)
# iowa_crops

sacramento_crops = Aoi('sacramento_crops',
    ['crop_classification/datasets_identify-1'],
    [[
        [-121.58460974693298, 38.29170496647727],
        [-121.58460974693298, 38.32726528409606],
        [-121.5248715877533, 38.32726528409606],
        [-121.5248715877533, 38.29170496647727],
        [-121.58460974693298, 38.29170496647727]
    ]])
aois.append(sacramento_crops)
# sacramento_crops

sacramento_crops_2 = Aoi(
    'sacramento_crops_2',
    ['coverage/calculage_coverage_wgs84',
     'crossovers/ps_l8_crossovers',
     'landsat-ps-comparison/landsat-ps-comparison',
     'crop_classification/datasets_identify-2'],
    [[
        [-121.3113248348236, 38.28911976564886],
        [-121.3113248348236, 38.34622533958],
        [-121.2344205379486, 38.34622533958],
        [-121.2344205379486, 38.28911976564886],
        [-121.3113248348236, 38.28911976564886]
    ]])
aois.append(sacramento_crops_2)
# sacramento_crops_2

golden_gate_park = Aoi('golden_gate_park',
    ['data-api-tutorials/clip_and_ship_introduction'],
    [[
        [-122.51103401184083, 37.771596736802074],
        [-122.51060485839844, 37.763997637045456],
        [-122.45902061462401, 37.76603318676243],
        [-122.45773315429689, 37.7654903789825],
        [-122.45275497436523, 37.76637243960179],
        [-122.45455741882324, 37.775124624817906],
        [-122.46597290039062, 37.7738356083287],
        [-122.51103401184083, 37.771596736802074]
    ]])
aois.append(golden_gate_park)
# golden_gate_park

san_francisco_city = Aoi('san_francisco_city',
    ['data-api-tutorials/planet_cli_introduction'],
    [[
        [-122.47455596923828, 37.810326435534755],
        [-122.49172210693358, 37.795406713958236],
        [-122.52056121826172, 37.784282779035216],
        [-122.51953124999999, 37.6971326434885],
        [-122.38941192626953, 37.69441603823106],
        [-122.38872528076173, 37.705010235842614],
        [-122.36228942871092, 37.70935613533687],
        [-122.34992980957031, 37.727280276860036],
        [-122.37773895263672, 37.76230130281876],
        [-122.38494873046875, 37.794592824285104],
        [-122.40554809570311, 37.813310018173155],
        [-122.46150970458983, 37.805715207044685],
        [-122.47455596923828, 37.810326435534755]
    ]])
aois.append(san_francisco_city)
# san_francisco_city

vancouver_island_s = Aoi(
    'Vancouver Island South',
    ['data-api-tutorials/planet_data_api_introduction'],
    [[
        [-125.29632568359376, 48.37084770238366],
        [-125.29632568359376, 49.335861591104106],
        [-123.2391357421875, 49.335861591104106],
        [-123.2391357421875, 48.37084770238366],
        [-125.29632568359376, 48.37084770238366]
    ]])
aois.append(vancouver_island_s)
# vancouver_island_s

# also ndvi-from-sr/ndvi_planetscope_sr and ndvi/ndvi_planetscope
west_stockton = Aoi('West of Stockton',
    ['data-api-tutorials/search_and_download_quickstart',
     'ndvi-from-sr/ndvi_planetscope_sr',
     'ndvi/ndvi_planetscope'
    ],
    [[
        [-121.59290313720705, 37.93444993515032],
        [-121.27017974853516, 37.93444993515032],
        [-121.27017974853516, 38.065932950547484],
        [-121.59290313720705, 38.065932950547484],
        [-121.59290313720705, 37.93444993515032]
    ]])
aois.append(west_stockton)
# west_stockton

congo_forest = Aoi('congo_forest',
    ['forest-monitoring/drc_roads_download'],
    [[
        [25.42429478260258,1.0255377823058893],
        [25.592960813580472,1.0255377823058893],
        [25.592960813580472,1.1196578801254304],
        [25.42429478260258,1.1196578801254304],
        [25.42429478260258,1.0255377823058893]
    ]])
aois.append(congo_forest)
# congo_forest

# also used in mosaicing/basic_compositing_demo
mt_dana = Aoi('Mt Dana',
    ['in-class-exercises/mosaicing-and-masking/mosaicing-and-masking-key',
     'mosaicing/basic_compositing_demo'
    ],
    [[
        [-119.16183471679688,37.82903964181452],
        [-119.14947509765626,37.83663205340172],
        [-119.13745880126953,37.846392577323286],
        [-119.13574218750001,37.856422880849514],
        [-119.13883209228514,37.86645181975611],
        [-119.12406921386719,37.86916210952103],
        [-119.12200927734376,37.875937397778614],
        [-119.1212688230194,37.90572368618133],
        [-119.13740499245301,37.930641295117404],
        [-119.16595458984376,37.92659678938742],
        [-119.18243408203126,37.9447389942697],
        [-119.2088161252655,37.95257263611974],
        [-119.25516469704283,37.92522514171301],
        [-119.2630611203827,37.88215253011582],
        [-119.25104482399598,37.84474832157969],
        [-119.18203695046083,37.82576791597315],
        [-119.16183471679688,37.82903964181452]
    ]])
aois.append(mt_dana)
# mt_dana

hanoi_s = Aoi('S Hanoi',
    ['label-data/label_maker_pl_geotiff'],
    [[
        [105.81775409169494, 20.84015810005586],
        [105.9111433289945, 20.84015810005586],
        [105.9111433289945, 20.925748489914824],
        [105.81775409169494, 20.925748489914824],
        [105.81775409169494, 20.84015810005586]
    ]])
aois.append(hanoi_s)
# hanoi_s

myanmar_s = Aoi('S Myanmar',
    ['orders/ordering_and_delivery',
     'orders/tools_and_toolchains'
    ],
    [[
        [94.25142652167966,16.942922591218252],
        [95.95431374929511,16.587048751480086],
        [95.55802198999191,14.851751617790999],
        [93.87002080638986,15.209870864141054],
        [94.25142652167966,16.942922591218252]
    ]])
aois.append(myanmar_s)
# myanmar_s

merced_n = Aoi('North of Merced',
    ['toar/toar_planetscope'],
    [[
        [-120.53282682046516,37.387200839539496],
        [-120.52354973008043,37.420706184624756],
        [-120.23858050023456,37.37089230084231],
        [-120.24140251133315,37.36077146074112],
        [-120.240470649891,37.36060856263429],
        [-120.253098881104,37.31418359933723],
        [-120.25781268370172,37.29734056539194],
        [-120.54356183694901,37.347297317827675],
        [-120.53282682046516,37.387200839539496]
    ]])
aois.append(merced_n)
# merced_n

iowa_crops_2 = Aoi('iowa_crops_2',
    ['udm/udm', 'udm/udm2'],
    [[
        [-93.29905768168668,42.69958733505418],
        [-93.29965849650593,42.81289914666694],
        [-93.28841467631707,42.862022561801815],
        [-93.2653691364643,42.924746756580326],
        [-92.99388666885065,42.92512385194759],
        [-92.99388666885065,42.77359750030287],
        [-92.99839277999504,42.75450452618375],
        [-93.01916380660347,42.699965805770056],
        [-93.29905768168668,42.69958733505418]
    ]])
aois.append(iowa_crops_2)
# iowa_crops_2

"""
Explanation: Specify AOIs
Each AOI is specified by instantiating the Aoi class with the aoi name, notebooks that use the aoi, and aoi geometry coordinates, in that order. Once an AOI is created, append it to the Aois object. Be sure to run this through once to generate the geojson file at the end of the notebook.
NOTE: Be careful about appending an Aoi to Aois multiple times. This will result in repeat Features in the resulting geojson.
End of explanation
"""


aois.write(aoi_filename)

"""
Explanation: Write to File
End of explanation
"""


