Ejemplo n.º 1
0
def get_kml_file(request, uid, session_key='0', input_username=None):
    load_session(request, session_key)
    user = request.user

    if input_username and user.username != input_username:
        return HttpResponse('Access denied', status=401)
    
    instance = get_object_for_viewing(request, uid)
    if isinstance(instance, HttpResponse):
        return instance

    full = instance.kml_full
    response = HttpResponse(full)
    if is_text(full):
        response['Content-Type'] = mimetypes.KML
    else:
        response['Content-Type'] = mimetypes.KMZ
    return response
Ejemplo n.º 2
0
def get_kml_file(request, uid, session_key='0', input_username=None):
    load_session(request, session_key)
    user = request.user

    if input_username and user.username != input_username:
        return HttpResponse('Access denied', status=401)

    instance = get_object_for_viewing(request, uid)
    if isinstance(instance, HttpResponse):
        return instance

    full = instance.kml_full
    response = HttpResponse(full)
    if is_text(full):
        response['Content-Type'] = mimetypes.KML
    else:
        response['Content-Type'] = mimetypes.KMZ
    return response
Ejemplo n.º 3
0
def kml_core(request, instances, kmz):
    """
    Generic view for KML representation of feature classes. 
    Can be overridden in options but this provided a default.
    """
    from lingcod.kmlapp.views import get_styles, create_kmz 
    from django.template.loader import get_template
    from lingcod.common import default_mimetypes as mimetypes
    from lingcod.features.models import FeatureCollection

    user = request.user
    try:
        session_key = request.COOKIES['sessionid']
    except:
        session_key = 0

    # Get features, collection from instances
    features = []
    collections = []

    # If there is only a single instance with a kml_full property,
    # just return the contents verbatim
    if len(instances) == 1:
        from lingcod.common.utils import is_text
        filename = slugify(instances[0].name)
        try:
            kml = instances[0].kml_full
            response = HttpResponse()
            if is_text(kml) and kmz:
                # kml_full is text, but they want as KMZ
                kmz = create_kmz(kml, 'mm/doc.kml')
                response['Content-Type'] = mimetypes.KMZ
                response['Content-Disposition'] = 'attachment; filename=%s.kmz' % filename
                response.write(kmz)
                return response
            elif is_text(kml) and not kmz:
                # kml_full is text, they just want kml
                response['Content-Type'] = mimetypes.KML
                response['Content-Disposition'] = 'attachment; filename=%s.kml' % filename
                response.write(kml)
                return response
            else:
                # If the kml_full returns binary, always return kmz
                # even if they asked for kml
                response['Content-Type'] = mimetypes.KMZ
                response['Content-Disposition'] = 'attachment; filename=%s.kmz' % filename
                response.write(kml) # actually its kmz but whatevs
                return response
        except AttributeError:
            pass

    for instance in instances:
        viewable, response = instance.is_viewable(user)
        if not viewable:
            return viewable, response
        
        if isinstance(instance, FeatureCollection):
            collections.append(instance)
        else:
            features.append(instance)

    styles = get_styles(features,collections,links=False)

    t = get_template('kmlapp/myshapes.kml')
    context = Context({
                'user': user, 
                'features': features, 
                'collections': collections,
                'use_network_links': False, 
                'request_path': request.path, 
                'styles': styles,
                'session_key': session_key,
                'shareuser': None,
                'sharegroup': None,
                'feature_id': None,
                })
    kml = t.render(context)
    response = HttpResponse()
    filename = '_'.join([slugify(i.name) for i in instances])

    if kmz:
        kmz = create_kmz(kml, 'mm/doc.kml')
        response['Content-Type'] = mimetypes.KMZ
        response['Content-Disposition'] = 'attachment; filename=%s.kmz' % filename
        response.write(kmz)
    else:
        response['Content-Type'] = mimetypes.KML
        response['Content-Disposition'] = 'attachment; filename=%s.kml' % filename
        response.write(kml)
        response.write('\n')
    return response