
import os.path
from datetime import datetime, timedelta
import json
import time

from osgeo import ogr, osr

OTP_ROUTER_EPSG = 4326
LOCATION_NAME_FIELD = "Name"
ID_FIELD = "id"
TIME_FIELD = "time"

def rasterName(loc_name, date, time, base_path=None, suffix=None):
    fname =  str.replace(loc_name, ' ', '_') \
        + '-' + str.replace(date, '-', '_') \
        + '-' + str.replace(time, ':', '_')
    if suffix and suffix != "":
        fname += '-' + suffix
    fname += '.tiff'
    if base_path is not None:
        path = os.path.join(os.path.expanduser(base_path), fname)
    else:
        path = fname
    return path

def vectorName(loc_name, date, time, iso, vec_type,
        base_path=None, suffix=None):
    fname = str.replace(loc_name, ' ', '_') \
        + '-' + str.replace(date, '-', '_') \
        + '-' + str.replace(time, ':', '_') \
        + '-' + str(iso) +"min" \
        + '-' + str.lower(vec_type)
    if suffix and suffix != "":
        fname += '-' + suffix
    fname += '.geojson'
    if base_path is not None:
        path = os.path.join(os.path.expanduser(base_path), fname)
    else:
        path = fname
    return path

def avgRasterName(loc_name, datestr, timestr, save_path, save_suffix,
        num_each_side):
    numavg = 1 + 2*num_each_side
    fname_base = rasterName(loc_name, datestr, timestr, save_path,
        save_suffix)
    return os.path.splitext(fname_base)[0] + "-avg%d.tiff" % numavg

def isoContoursName(loc_name, datestr, timestr, save_path, save_suffix,
        num_each_side):
    avg_fname = avgRasterName(loc_name, datestr, timestr, save_path,
        save_suffix, num_each_side)
    return os.path.splitext(avg_fname)[0] + "-isocontours.shp"

def isoBandsName(loc_name, datestr, timestr, save_path, save_suffix, 
        num_each_side):
    avg_fname = avgRasterName(loc_name, datestr, timestr, save_path,
        save_suffix, num_each_side)
    return os.path.splitext(avg_fname)[0] + "-isobands.shp"

def isoBandsAllName(loc_name, datestr, timestr, save_path, save_suffix, 
        num_each_side):
    isob_fname = isoBandsName(loc_name, datestr, timestr, save_path,
        save_suffix, num_each_side)
    return os.path.splitext(isob_fname)[0] + "-all.shp"

def polysIsoBandsName(loc_name, datestr, timestr, save_path, save_suffix,
        num_each_side, iso_level):
    isob_fname = isoBandsName(loc_name, datestr, timestr, save_path,
        save_suffix, num_each_side)
    return os.path.splitext(isob_fname)[0] + "-%d-polys.shp" % iso_level