# 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
    
    
@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)
        os.system(s)
        return json.dumps({'status': 'Done'})


@dajaxice_register(method='GET')
def reproject_geotiff(request, selected_geotiff, geotif_re_project_epsg, projected_geotiff_name):
    if projected_geotiff_name.split(".")[-1] != "tif" or projected_geotiff_name.split(".")[-1] != "tiff":
        projected_geotiff_name = "{0}.tif".format(projected_geotiff_name)
    if os.path.isfile('{0}{1}{2}'.format(MEDIA_ROOT, MEDIA_URL, projected_geotiff_name)):
        return json.dumps({'status': 'File already exists.'})
    else:
        s = "gdalwarp -t_srs 'epsg:{2}' {0} {1}".format(MEDIA_ROOT+MEDIA_URL+selected_geotiff, MEDIA_ROOT+MEDIA_URL+projected_geotiff_name, geotif_re_project_epsg)
        os.system(s)
        return json.dumps({'status': 'Done'})


@dajaxice_register(method='GET')
def extract_shp_table_text(request, selected_vector, text_name):
    if text_name.split(".")[-1] != "txt":
        text_name = "{0}.txt".format(text_name)

# 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

    
    
@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)
        os.system(s)
        return json.dumps({'status': 'Done'})


@dajaxice_register(method='GET')
def reproject_geotiff(request, selected_geotiff, geotif_re_project_epsg, projected_geotiff_name):
    if projected_geotiff_name.split(".")[-1] != "tif" or projected_geotiff_name.split(".")[-1] != "tiff":
        projected_geotiff_name = "{0}.tif".format(projected_geotiff_name)
    if os.path.isfile('{0}{1}{2}'.format(MEDIA_ROOT, MEDIA_URL, projected_geotiff_name)):
        return json.dumps({'status': 'File already exists.'})
    else:
        s = "gdalwarp -t_srs 'epsg:{2}' {0} {1}".format(MEDIA_ROOT+MEDIA_URL+selected_geotiff, MEDIA_ROOT+MEDIA_URL+projected_geotiff_name, geotif_re_project_epsg)
        os.system(s)
        return json.dumps({'status': 'Done'})


@dajaxice_register(method='GET')
def extract_shp_table_text(request, selected_vector, text_name):
    if text_name.split(".")[-1] != "txt":
        text_name = "{0}.txt".format(text_name)

    if os.path.isfile('{0}{1}{2}'.format(MEDIA_ROOT, MEDIA_URL, text_name)):