# Path: giscube/config.py
# MEDIA_ROOT = '{0}/giscube_app/static/'.format(current_dir)
# 
# MEDIA_URL = 'uploaded_files/'


import os, shutil
import numpy as np
import json
from netCDF4 import Dataset
from dajaxice.decorators import dajaxice_register
from giscube.config import MEDIA_ROOT, MEDIA_URL
from scripts.extract_shp_table import extract_shp_table
from scripts.metadata import get_nc_data
from scripts.conversion import nc_to_gtif, nc_to_geojson, shp_to_kml, convert_geotiff_to_kml, shp_to_tif, shp_to_json, geotiff_to_point_shp, geotiff_to_point_json, convert_coord_to_point_shp
from scripts.spatial_analysis import buffer_shapefile, find_point_inside_feature
from scripts.clip_geotiff_by_shp import clip_geotiff_by_shp
from scripts.data_management import change_geotiff_resolution, color_table_on_geotiff
from scripts.opendap import load as load_opendap
from scripts.opendap import opendap_metadata
        return json.dumps({'status': '<div class="alert alert-danger" role="alert" >File already exists.</div>'})
    else:
        load_opendap(opendap_url, frm, opendap_out_name)
        return json.dumps({'status': '<div class="alert alert-success" role="alert" >Successfully retrieved data. Please refresh the page.</div>'})


@dajaxice_register(method='GET')
def opendap_getmetadata(request, opendap_url):
    metadata = opendap_metadata(opendap_url)
    return json.dumps({'metadata': metadata.splitlines()})


@dajaxice_register(method='GET')
def remove_loaded_file(request, param):
    Uploaded_file = MEDIA_ROOT + MEDIA_URL + param
    os.remove(Uploaded_file)
    #and remove the associated folder for a file is exists
    try:
        shutil.rmtree('{0}'.format(Uploaded_file[:-4]))
    except:
        pass
    
    
@dajaxice_register(method='GET')
def reproject_shapefile(request, selected_shapefile, shapefile_re_project_epsg, projected_shapefile_name):
    if projected_shapefile_name.split(".")[-1] != "shp":
        projected_shapefile_name = "{0}.shp".format(projected_shapefile_name)
    if os.path.isfile('{0}{1}{2}'.format(MEDIA_ROOT, MEDIA_URL, projected_shapefile_name)):
        return json.dumps({'status': 'File already exists.'})
    else:

# Path: giscube/config.py
# MEDIA_ROOT = '{0}/giscube_app/static/'.format(current_dir)
# 
# MEDIA_URL = 'uploaded_files/'


# Path: giscube_app/ajax.py
import os, shutil
import numpy as np
import json
from netCDF4 import Dataset
from dajaxice.decorators import dajaxice_register
from giscube.config import MEDIA_ROOT, MEDIA_URL
from scripts.extract_shp_table import extract_shp_table
from scripts.metadata import get_nc_data
from scripts.conversion import nc_to_gtif, nc_to_geojson, shp_to_kml, convert_geotiff_to_kml, shp_to_tif, shp_to_json, geotiff_to_point_shp, geotiff_to_point_json, convert_coord_to_point_shp
from scripts.spatial_analysis import buffer_shapefile, find_point_inside_feature
from scripts.clip_geotiff_by_shp import clip_geotiff_by_shp
from scripts.data_management import change_geotiff_resolution, color_table_on_geotiff
from scripts.opendap import load as load_opendap
from scripts.opendap import opendap_metadata

        return json.dumps({'status': '<div class="alert alert-danger" role="alert" >File already exists.</div>'})
    else:
        load_opendap(opendap_url, frm, opendap_out_name)
        return json.dumps({'status': '<div class="alert alert-success" role="alert" >Successfully retrieved data. Please refresh the page.</div>'})


@dajaxice_register(method='GET')
def opendap_getmetadata(request, opendap_url):
    metadata = opendap_metadata(opendap_url)
    return json.dumps({'metadata': metadata.splitlines()})


@dajaxice_register(method='GET')
def remove_loaded_file(request, param):
    Uploaded_file = MEDIA_ROOT + MEDIA_URL + param
    os.remove(Uploaded_file)
    #and remove the associated folder for a file is exists
    try:
        shutil.rmtree('{0}'.format(Uploaded_file[:-4]))
    except:
        pass
    
    
@dajaxice_register(method='GET')
def reproject_shapefile(request, selected_shapefile, shapefile_re_project_epsg, projected_shapefile_name):
    if projected_shapefile_name.split(".")[-1] != "shp":
        projected_shapefile_name = "{0}.shp".format(projected_shapefile_name)
    if os.path.isfile('{0}{1}{2}'.format(MEDIA_ROOT, MEDIA_URL, projected_shapefile_name)):
        return json.dumps({'status': 'File already exists.'})
    else:

        s = 'ogr2ogr -f "ESRI Shapefile" -t_srs EPSG:{2} {0} {1}'.format(MEDIA_ROOT+MEDIA_URL+projected_shapefile_name, MEDIA_ROOT+MEDIA_URL+selected_shapefile+'.shp', shapefile_re_project_epsg)