from base import *
from render import output_proj

def output(request, type, output, code=None, slug=None):
    if slug: 
        srs = get_object_or_404(SRS, type=type, slug=slug)
    else:
        srs = get_object_or_404(SRS, type=type, srs_id=code)
    try:
        ref = osr.SpatialReference(str(srs.wkt))
    except Exception, E:
        error = {'title': 'Invalid Projection', 'text': "OGR Failed to load the requested projection. %s" %srs.wkt, 'exception': E} 
        response = render_to_response("references/404.html", error)
        response.status_code = 404
        return response
    try:    
        return output_proj(request, ref, output, srs, srs.srs_id, type)
    except FormatError: 
        return HttpResponseRedirect('/ref/%s/%s/' % (type,code))
