Example #1
0
def shape(request):
    qs = Flaws.objects.all()
    shp_response = ShpResponder(qs)
    shp_response.readme = 'This is a string of a readme\nplease read this.'
    shp_response.file_name = 'Roadflaw Data'
    #shp_response.geo_field = 'geometry'
    return shp_response()
Example #2
0
def ShapeDump(request):
    qs = Allotment.objects.all()
    shp_response = ShpResponder(qs)
    shp_response.readme = ''
    shp_response.file_name = 'Allotments'
    #shp_response.geo_field = 'geometry'
    return shp_response()
Example #3
0
def exportShape():
    print "I GOT CALLED!!!"
    #w = CollarData.objects.all()
    w =CollarData.objects.filter(collar=30812)
    print "AGAIN!!!"
    try:
        shp_response = ShpResponder(w)
        print "ANOOOTER"
        shp_response.file_name = 'Collar Datas'
        print "I HATE THIS"
        return shp_response()
    except Exception,e:
        print type(e),e
Example #4
0
def export_routes_shapefile(request, month='all'):
    if not request.user.is_staff:
        return HttpResponse('You do not have permission to view this feature', status=401)
    if request.method != 'POST':
        return HttpResponse('Action not permitted', status=403)
    form = ExportSurveysForm(request.POST)
        
    if not form.is_valid():
        return render_to_response( template, RequestContext( request, {'export_form':form} ) )
    #compile a queryset of shapes from the completed surveys
    completed_routes = compile_completed_routes(month)    
    shp_response = ShpResponder(completed_routes)
    username = clean_username(User.objects.get(username = request.user).first_name)
    shp_response.file_name = '%s_%s_routes_%s' % (username, month, datetime.date.today())
    return shp_response()
    def download_detail(self, request, **kwargs):
        pk = kwargs.pop('pk', None)
        geotable = self._meta.queryset.get(pk=pk)

        from shapes.views import ShpResponder
        qs = geotable.content_type.model_class().objects.all()
        # proj = form.cleaned_data['projection']
        # query = form.cleaned_data['query']
        # if query:
        #     where = query.replace("%", "%%")
        #     qs = qs.extra(where=[where])
        # bbox = form.cleaned_data['bbox']
        # if bbox:
        #     bbox_arr = map(float, bbox.split(","))
        #     geom = Polygon.from_bbox(bbox_arr)
        #     filter_kwargs = {}
        #     key = obj.geometry_field_name + "__intersects"
        #     filter_kwargs[key] = geom
        #     qs = qs.filter(**filter_kwargs)
        shp_response = ShpResponder(qs)  # , proj_transform=proj)
        shp_response.file_name = geotable.title  # + ("" if proj is None else "_" + str(proj))
        return shp_response()
Example #6
0
def watershed_shapefile(request, instances):
    from seak.models import PlanningUnitShapes, Scenario, PuVsAux, PuVsCf, PuVsCost
    wshds = PlanningUnit.objects.prefetch_related(
        'puvsaux_set',   
        'puvsaux_set__aux', 
        'puvscf_set', 
        'puvscf_set__cf', 
        'puvscost_set', 
        'puvscost_set__cost'
        )
    wshd_fids = [x.fid for x in wshds]
    results = {}

    for w in wshds:
        fid = w.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} 

        for puCf in PuVsCf.objects.filter(pu=w):
            results[fid][puCf.cf.dbf_fieldname] = puCf.amount
        for puAux in PuVsAux.objects.filter(pu=w):
            results[fid][puAux.aux.dbf_fieldname] = puAux.amount
        for puCost in PuVsCost.objects.filter(pu=w):
            results[fid]['C_' + puCost.cost.dbf_fieldname] = puCost.amount

    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\r\n\
contact: [email protected]\r\n\
\r\n\
Includes scenarios:\r\n\
    %s\r\n\
\r\n\
    'pu' contains the name of the Planning Unit\r\n\
    'bests' contains the number of scenarios in which the subbasin was included in the best run\r\n\
    'hits' contains the number of times the subbasin was included in any run, cumulative across scenarios.\r\n\
    'name' contains the name of the Planning Unit\r\n\
    'ACEC' contains the acres of ACEC (Areas of Critical Environmental Concern) lands within the HUC5 planning unit\r\n\
    'BLM_WCHAR' contains the acres of BLM designated Wilderness Characteristics Areas within the HUC5 planning unit\r\n\
    'BLM_WILD' contains the acres of BLM designated Wilderness land within the HUC5 planning unit\r\n\
    'BLM_WSA' contains the acres of BLM designated Study Areas within the HUC5 planning unit\r\n\
    'BLMLAND' contains the acres of BLM land within the HUC5 planning unit\r\n\
    'C_ACEC' contains the acres of land unavailable to BLM remediation\r\n\
    'C_BLM_WCHAR' contains the acres of land unavailable to BLM remediation\r\n\
    'C_BLM_WILD' contains the acres of land unavailable to BLM remediation\r\n\
    'C_BLM_WSA' contains the acres of land unavailable to BLM remediation\r\n\
    'C_GT20PERC' contains the acres of slope greater than 20 percent in the HUC5 planning unit\r\n\
    'C_WETLAND' contains the wetland acreage within the HUC5 planning unit\r\n\
    'GRZ_ALLOT' contains the BLM Grazing Allotment acreage within the HUC5 planning unit\r\n\
    'HIST_JUNPR' contains the acres of historic juniper within the HUC5 planning unit\r\n\
    'INV_WEED' contains the acreage of noxious and invasive weeds identified on BLM Lands within the HUC5 planning unit\r\n\
    'JUNP1' contains the acreage of Juniper Phase 1 \r\n\
    'JUNP2' contains the acreage of Juniper Phase 2 \r\n\
    'JUNP3' contains the acreage of Juniper Phase 3 \r\n\
    'P_RABBIT' contains the acreage of pygmy rabbit habitat within the HUC5 planning unit\r\n\
    'PR_JP1' contains the acreage of overlap between phase 1 juniper and pygmy rabbits within the HUC5 planning unit\r\n\
    'PR_JP2' contains the acreage of overlap between phase 2 juniper and pygmy rabbits within the HUC5 planning unit\r\n\
    'PR_JP3' contains the acreage of overlap between phase 3 juniper and pygmy rabbits within the HUC5 planning unit\r\n\
    'PROT_BLM' contains the acres of BLM owned land with protected status. Includes BLM Designated Wilderness, Study Areas and lands with wilderness characteristics.\r\n\
    'PROT_OTH' contains the acres of non-BLM owned land with protected status. Includes Federal, State and Private Conservation Lands.\r\n\
    'PRIV_MILE' contains the miles of perennial rivers, streams, or creeks within the HUC5 planning unit.  Serves as proxy to riparian coverage, until better data is supplied.\r\n\
    'SG_ALL' contains the acreage of combined sage grouse habitat (PPH&PGH) within the HUC5 planning unit\r\n\
    'SGPGH' contains the acreage of sage grouse preliminary general habitat within the HUC5 planning unit\r\n\
    'SGPGH_JP1' contains the acreage of overlap between phase 1 juniper and sage grouse preliminary general habitat within the HUC5 planning unit\r\n\
    'SGPGH_JP2' contains the acreage of overlap between phase 2 juniper and sage grouse preliminary general habitat within the HUC5 planning unit\r\n\
    'SGPGH_JP3' contains the acreage of overlap between phase 3 juniper and sage grouse preliminary general habitat within the HUC5 planning unit\r\n\
    'SGPPH' contains the acreage of sage grouse preliminary priority habitat within the HUC5 planning unit\r\n\
    'SGPPH_JP1' contains the acreage of overlap between phase 1 juniper and sage grouse preliminary priority habitat within the HUC5 planning unit\r\n\
    'SGPPH_JP2' contains the acreage of overlap between phase 2 juniper and sage grouse preliminary priority habitat within the HUC5 planning unit\r\n\
    'SGPPH_JP3' contains the acreage of overlap between phase 3 juniper and sage grouse preliminary priority habitat within the HUC5 planning unit\r\n\
    'TRT_REVEG' contains the acreage of BLM Revegetation Treatments within the HUC5 planning unit\r\n\
    'TRTBURNJUN' contains the acreage of BLM Prescribed Fire Treatments on Juniper within the HUC5 planning unit\r\n\
    'TRTHARVJUN' contains the acreage of BLM Harvest Treatments on Juniper within the HUC5 planning unit\r\n\
    'TRTMECHJ' contains the acreage of BLM Mechanical Treatments on Juniper within the HUC5 planning unit\r\n\
    'WETLAND' contains the acreage of wetland within the HUC5 planning unit\r\n\
    'WS_RIV' contains the acreage of Wild and Scenic Rivers within the HUC5 planning unit" % ('\n    '.join([i.name for i in instances]), )

    pu_list = []
    for fid in results.keys():
        r = results[fid]
        pu_list.append(PlanningUnitShapes(stamp=stamp, fid=fid, geometry=r['geometry'], bests=r['bests'], hits=r['hits'], pu=r['pu'], name=r['name']))

    PlanningUnitShapes.objects.bulk_create(pu_list)

    allpus = PlanningUnitShapes.objects.filter(stamp=stamp)
    for newPu in allpus:
        r = results[newPu.fid]
        for key in r:
            setattr(newPu, key, r[key])

    shp_response = ShpResponder(allpus, readme=readme)
    filename = '_'.join([slugify(i.pk) for i in instances])
    shp_response.file_name = slugify('scenarios_' + filename)

    addl_cols_aux = [x.aux.dbf_fieldname for x in PuVsAux.objects.filter(pu=allpus[0].pu)]
    addl_cols_cf = [x.cf.dbf_fieldname for x in PuVsCf.objects.filter(pu=allpus[0].pu)]
    addl_cols_cost = ['C_' + x.cost.dbf_fieldname for x in PuVsCost.objects.filter(pu=allpus[0].pu)]
    addl_cols = addl_cols_aux + addl_cols_cf + addl_cols_cost
    addl_cols.sort()

    return shp_response(addl_cols)
Example #7
0
def shapefile(request):
    form = ShapefileForm(request.GET)
    if form.is_valid():
        cd = form.cleaned_data
        layer = cd['layer']
    else:
        return HttpResponseBadRequest('error, unknown layer')

    if layer == 'impacts':
        # Return impacts shapefile
        data = Impact.objects.all()
        shp_response = ShpResponder(data)
        shp_response.file_name = "impacts"
    elif layer == 'county_counts':
        data = CountyCount.objects.all()
        shp_response = ShpResponder(data)
        shp_response.file_name = "county_counts"
    elif layer == 'zip_counts':
        data = ZipCount.objects.all()
        shp_response = ShpResponder(data)
        shp_response.file_name = "zip_counts"
    elif layer == 'storm_events':
        data = StormEvents.objects.all()
        shp_response = ShpResponder(data)
        shp_response.file_name = 'storm_events'
    elif layer == 'normals':
        data = Normals.objects.all()
        shp_response = ShpResponder(data)
        shp_response.file_name = 'normals'
    else:
        return HttpResponseBadRequest('error, unsupported layer')

    return shp_response()