Esempio n. 1
0
def watershed_marxan(request, instance):
    from arp.models import PlanningUnit, WatershedPrioritization
    viewable, response = instance.is_viewable(request.user)
    if not viewable:
        return response

    if not isinstance(instance, WatershedPrioritization):
        return HttpResponse(
            "Shapefile export for watershed prioritizations only", status=500)

    from madrona.common.utils import KMZUtil
    zu = KMZUtil()
    filename = os.path.join(
        tempfile.gettempdir(), '%s_%s.zip' %
        (slugify(instance.name), slugify(instance.date_modified)))
    directory = instance.outdir
    zu.toZip(directory, filename)

    fh = open(filename, 'rb')
    zip_stream = fh.read()
    fh.close()
    response = HttpResponse()
    response[
        'Content-Disposition'] = 'attachment; filename=Marxan_%s.zip' % slugify(
            instance.name)
    response['Content-length'] = str(len(zip_stream))
    response['Content-Type'] = 'application/zip'
    response.write(zip_stream)
    return response
Esempio n. 2
0
def watershed_marxan(request, instance):
    from seak.models import Scenario
    viewable, response = instance.is_viewable(request.user)
    if not viewable:
        return response

    if not isinstance(instance, Scenario):
        return HttpResponse("Shapefile export for watershed prioritizations only", status=500)

    from madrona.common.utils import KMZUtil
    zu = KMZUtil()
    filename = os.path.join(tempfile.gettempdir(), 
            '%s_%s.zip' % (slugify(instance.name),slugify(instance.date_modified)))
    directory = instance.outdir 
    zu.toZip(directory, filename)

    fh = open(filename,'rb')
    zip_stream = fh.read() 
    fh.close()
    response = HttpResponse()
    response['Content-Disposition'] = 'attachment; filename=Marxan_%s.zip' % slugify(instance.name)
    response['Content-length'] = str(len(zip_stream))
    response['Content-Type'] = 'application/zip'
    response.write(zip_stream)
    return response
Esempio n. 3
0
def overlap_kmz_response(request, collection_uids):
    import cStringIO
    import shutil
    import tempfile

    buff = cStringIO.StringIO()
    temp_geotiff = overlap_geotiff(collection_uids, request.user)
    if isinstance(temp_geotiff, HttpResponse):
        return response

    tempdir = os.path.join(tempfile.gettempdir(),str(collection_uids.__hash__()))
    if os.path.exists(tempdir):
        shutil.rmtree(tempdir)
    os.mkdir(tempdir)
    outrgb = os.path.join(tempdir, "rgb.png")
    outvrt = os.path.join(tempdir, "rgb.vrt")
    outkmldir = os.path.join(tempdir, "kmlfiles")
    if os.path.exists(outkmldir):
        shutil.rmtree(outkmldir)
    os.mkdir(outkmldir)
    outkmz = os.path.join(tempdir, "array_overlap.kmz")

    # Convert to RGB 
    cmd = "pct2rgb.py -of PNG %s %s" % (temp_geotiff, outrgb)  
    print cmd
    print os.popen(cmd).read()

    # Assign a nodata value
    cmd = "gdal_translate -of vrt -a_nodata 0 %s %s" % (outrgb, outvrt)
    print cmd
    print os.popen(cmd).read()

    # Split into wgs84 tiles
    cmd = "gdal2tiles.py -k -z 6-10 -p geodetic %s %s" % (outvrt, outkmldir)
    print cmd
    print os.popen(cmd).read()

    # Zip up directory into a kmz
    print "Creating", outkmz
    from madrona.common.utils import KMZUtil
    zu = KMZUtil()
    filename = outkmz
    os.chdir(outkmldir)
    directory = ""
    zu.toZip(directory, filename)

    if not os.path.exists(outkmz):
        return HttpResponse("KMZ creation error", status=500)
    rfile = open(outkmz,'rb')
    buff.write(rfile.read())
    buff.flush()
    stream = buff.getvalue()
    response = HttpResponse()
    response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(outkmz)
    response['Content-length'] = str(len(stream))
    response['Content-Type'] = mimetypes.KMZ
    response.write(stream)
    return response
Esempio n. 4
0
def overlap_kmz_response(request, collection_uids):
    import cStringIO
    import shutil
    import tempfile

    buff = cStringIO.StringIO()
    temp_geotiff = overlap_geotiff(collection_uids, request.user)
    if isinstance(temp_geotiff, HttpResponse):
        return response

    tempdir = os.path.join(tempfile.gettempdir(),
                           str(collection_uids.__hash__()))
    if os.path.exists(tempdir):
        shutil.rmtree(tempdir)
    os.mkdir(tempdir)
    outrgb = os.path.join(tempdir, "rgb.png")
    outvrt = os.path.join(tempdir, "rgb.vrt")
    outkmldir = os.path.join(tempdir, "kmlfiles")
    if os.path.exists(outkmldir):
        shutil.rmtree(outkmldir)
    os.mkdir(outkmldir)
    outkmz = os.path.join(tempdir, "array_overlap.kmz")

    # Convert to RGB
    cmd = "pct2rgb.py -of PNG %s %s" % (temp_geotiff, outrgb)
    print cmd
    print os.popen(cmd).read()

    # Assign a nodata value
    cmd = "gdal_translate -of vrt -a_nodata 0 %s %s" % (outrgb, outvrt)
    print cmd
    print os.popen(cmd).read()

    # Split into wgs84 tiles
    cmd = "gdal2tiles.py -k -z 6-10 -p geodetic %s %s" % (outvrt, outkmldir)
    print cmd
    print os.popen(cmd).read()

    # Zip up directory into a kmz
    print "Creating", outkmz
    from madrona.common.utils import KMZUtil
    zu = KMZUtil()
    filename = outkmz
    os.chdir(outkmldir)
    directory = ""
    zu.toZip(directory, filename)

    if not os.path.exists(outkmz):
        return HttpResponse("KMZ creation error", status=500)
    rfile = open(outkmz, 'rb')
    buff.write(rfile.read())
    buff.flush()
    stream = buff.getvalue()
    response = HttpResponse()
    response[
        'Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(
            outkmz)
    response['Content-length'] = str(len(stream))
    response['Content-Type'] = mimetypes.KMZ
    response.write(stream)
    return response