Beispiel #1
0
def bmm_shapefile(request, instances):
    from mybioregions.models import MyBioregion, Folder, BioregionShapefile
    bios = []
    for inst in instances:
        viewable, response = inst.is_viewable(request.user)
        if not viewable:
            return response

        if isinstance(inst, MyBioregion):
            inst.convert_to_shp()
            bios.append(inst)
        elif isinstance(inst, Folder):
            for bio in inst.feature_set(recurse=True,feature_classes=[MyBioregion]):
                bio.convert_to_shp()
                bios.append(bio)
        else:
            pass # ignore anything else

    filename = '_'.join([slugify(inst.name) for inst in instances])
    pks = [bio.pk for bio in bios]
    qs = BioregionShapefile.objects.filter(bio_id_num__in=pks)
    if len(qs) == 0:
        return HttpResponse(
            "Nothing in the query set; you don't want an empty shapefile", 
            status=404
        )
    shp_response = ShpResponder(qs)
    shp_response.file_name = slugify(filename[0:8])
    return shp_response()
Beispiel #2
0
def overlap_geotiff(collection_uids, user=None):
    collections = [get_feature_by_uid(x) for x in collection_uids.split(',')]
    collections = [
        x for x in collections if x.__class__ in get_collection_models()
    ]
    if len(collections) < 1:
        raise Http404

    filenames = []
    for collection in collections:
        viewable, response = collection.is_viewable(user)
        if user and not viewable:
            return response

        fs = collection.feature_set(recurse=True)
        poly_fs = [x for x in fs if isinstance(x, PolygonFeature)]
        unique_types = list(set([x.__class__ for x in poly_fs]))
        for model in unique_types:
            responder = ShpResponder(
                model.objects.filter(
                    pk__in=[x.pk for x in poly_fs if x.__class__ == model]))
            responder.geo_field = 'geometry_final'
            fn = responder('return_file_not_response')
            filenames.append(fn)

    temp_geotiff = create_heatmap(filenames)
    return temp_geotiff
Beispiel #3
0
def watershed_shapefile(request, instances):
    from arp.models import PlanningUnit, WatershedPrioritization, PlanningUnitShapes

    wshds = PlanningUnit.objects.all()
    stamp = int(time.time() * 1000.0)

    for instance in instances:
        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)

        ob = json.loads(instance.output_best)
        wids = [int(x.strip()) for x in ob['best']]
        puc = json.loads(instance.output_pu_count)

        for ws in wshds:
            # create custom model records
            pus, created = PlanningUnitShapes.objects.get_or_create(pu=ws, stamp=stamp)

            # Only do this on the first go 'round
            if created and not pus.geometry:
                pus.name = ws.name
                pus.fid = ws.fid
                p = ws.geometry.simplify(100)
                if p.geom_type == 'Polygon':
                    pus.geometry = MultiPolygon(p)
                elif p.geom_type == 'MultiPolygon':
                    pus.geometry = p

            # Calculate hits and best
            try:
                hits = puc[str(ws.pk)] 
            except:
                hits = 0
            best = ws.pk in wids
            pus.hits += hits
            if best:
                pus.bests += 1
            pus.save()

    readme = """Watershed Prioritization Analysis
contact: [email protected]

Includes scenarios:
    %s

Contains HUC8s from Oregon, Washington, Idaho
    'bests' contains the number of scenarios in which the subbasin was included in the best run
    'hits' contains the number of times the subbasin was included in any run, cumulative across scenarios.
    """ % ('\n    '.join([i.name for i in instances]), )

    allpus = PlanningUnitShapes.objects.filter(stamp=stamp)
    shp_response = ShpResponder(allpus, readme=readme)
    filename = '_'.join([slugify(i.pk) for i in instances])
    shp_response.file_name = slugify('wp_' + filename)
    return shp_response()
Beispiel #4
0
def watershed_shapefile(request, instances):
    from seak.models import PlanningUnitShapes, Scenario
    wshds = PlanningUnit.objects.all()
    wshd_fids = [x.fid for x in PlanningUnit.objects.all()]
    results = {}
    for fid in wshd_fids:
        w = wshds.get(fid=fid)
        p = w.geometry
        if p.geom_type == 'Polygon':
            p = MultiPolygon(p)
        results[fid] = {'pu': w, 'geometry': p, 'name': w.name, 'hits': 0, 'bests': 0} 

    stamp = int(time.time() * 1000.0)

    for instance in instances:
        viewable, response = instance.is_viewable(request.user)
        if not viewable:
            return response
        if not isinstance(instance, Scenario):
            return HttpResponse("Shapefile export for prioritization scenarios only", status=500)

        ob = json.loads(instance.output_best)
        wids = [int(x.strip()) for x in ob['best']]
        puc = json.loads(instance.output_pu_count)

        for fid in wshd_fids:
            # Calculate hits and best
            try:
                hits = puc[str(fid)] 
            except KeyError:
                hits = 0
            best = fid in wids
            results[fid]['hits'] += hits
            if best:
                results[fid]['bests'] += 1

    readme = """Prioritization Scenario Array
contact: [email protected]

Includes scenarios:
    %s

    'bests' contains the number of scenarios in which the subbasin was included in the best run
    'hits' contains the number of times the subbasin was included in any run, cumulative across scenarios.
    """ % ('\n    '.join([i.name for i in instances]), )

    for fid in results.keys():
        r = results[fid]
        PlanningUnitShapes.objects.create(stamp=stamp, fid=fid, name=r['name'], pu=r['pu'],
                                          geometry=r['geometry'], bests=r['bests'], hits=r['hits'])
    allpus = PlanningUnitShapes.objects.filter(stamp=stamp)
    shp_response = ShpResponder(allpus, readme=readme)
    filename = '_'.join([slugify(i.pk) for i in instances])
    shp_response.file_name = slugify('scenarios_' + filename)
    return shp_response()
Beispiel #5
0
def overlap_geotiff(collection_uids, user=None):
    collections = [get_feature_by_uid(x) for x in collection_uids.split(',')]
    collections = [x for x in collections if x.__class__ in get_collection_models()]
    if len(collections) < 1:
        raise Http404

    filenames = []
    for collection in collections:
        viewable, response = collection.is_viewable(user)
        if user and not viewable:
            return response

        fs = collection.feature_set(recurse=True)
        poly_fs = [x for x in fs if isinstance(x,PolygonFeature)]
        unique_types = list(set([x.__class__ for x in poly_fs]))
        for model in unique_types:
            responder = ShpResponder(model.objects.filter(pk__in=[x.pk for x in poly_fs if x.__class__ == model]))
            responder.geo_field = 'geometry_final'
            fn = responder('return_file_not_response')
            filenames.append(fn)

    temp_geotiff = create_heatmap(filenames)
    return temp_geotiff
Beispiel #6
0
def watershed_shapefile(request, instances):
    from arp.models import PlanningUnit, WatershedPrioritization, PlanningUnitShapes

    wshds = PlanningUnit.objects.all()
    stamp = int(time.time() * 1000.0)

    for instance in instances:
        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)

        ob = json.loads(instance.output_best)
        wids = [int(x.strip()) for x in ob['best']]
        puc = json.loads(instance.output_pu_count)

        for ws in wshds:
            # create custom model records
            pus, created = PlanningUnitShapes.objects.get_or_create(
                pu=ws, stamp=stamp)

            # Only do this on the first go 'round
            if created and not pus.geometry:
                pus.name = ws.name
                pus.fid = ws.fid
                p = ws.geometry.simplify(100)
                if p.geom_type == 'Polygon':
                    pus.geometry = MultiPolygon(p)
                elif p.geom_type == 'MultiPolygon':
                    pus.geometry = p

            # Calculate hits and best
            try:
                hits = puc[str(ws.pk)]
            except:
                hits = 0
            best = ws.pk in wids
            pus.hits += hits
            if best:
                pus.bests += 1
            pus.save()

    readme = """Watershed Prioritization Analysis
contact: [email protected]

Includes scenarios:
    %s

Contains HUC8s from Oregon, Washington, Idaho
    'bests' contains the number of scenarios in which the subbasin was included in the best run
    'hits' contains the number of times the subbasin was included in any run, cumulative across scenarios.
    """ % ('\n    '.join([i.name for i in instances]), )

    allpus = PlanningUnitShapes.objects.filter(stamp=stamp)
    shp_response = ShpResponder(allpus, readme=readme)
    filename = '_'.join([slugify(i.pk) for i in instances])
    shp_response.file_name = slugify('wp_' + filename)
    return shp_response()
Beispiel #7
0
def test_poly_shapefile(request, tp_id):
    test_poly_qs = TestPolygon.objects.filter(pk=tp_id)
    shp_response = ShpResponder(test_poly_qs)
    shp_response.file_name = 'test_poly'
    return shp_response()