|
13 | 13 | from shapely.geometry import shape
|
14 | 14 | from django.contrib.gis.utils import LayerMapping
|
15 | 15 | from django.views.decorators.csrf import csrf_exempt
|
| 16 | +from django.contrib.auth.models import User |
16 | 17 |
|
17 | 18 | from ScapTestProject import settings
|
18 | 19 | from scap.models import (AOIFeature, ForestCoverFile, CarbonStatistic, ForestCoverStatistic,
|
19 |
| - AOICollection, ForestCoverCollection, AGBCollection, PilotCountry) |
20 |
| -from scap.async_tasks import process_aoi_collection, process_fc_collection, process_agb_collection |
| 20 | + AOICollection, ForestCoverCollection, AGBCollection, CurrentTask, PilotCountry) |
| 21 | + |
| 22 | +from scap.async_tasks import process_updated_collection |
21 | 23 | import geopandas as gpd
|
22 | 24 |
|
23 | 25 | BASE_DIR = Path(__file__).resolve().parent.parent
|
@@ -463,40 +465,6 @@ def get_updated_series(request, country=None):
|
463 | 465 | 'lcs_defor': json.dumps(lcs_defor), 'lc_data': lcs_defor, 'region_country': pa_name})
|
464 | 466 |
|
465 | 467 |
|
466 |
| - |
467 |
| - |
468 |
| -def get_fc_area(fc_source, aoi, row_offset, col_offset): |
469 |
| - area = 0 |
470 |
| - with rio.open(fc_source) as fc_obj, rio.open(aoi) as aoi_obj: |
471 |
| - aoi_block = aoi_obj.read(1) |
472 |
| - corresponding_window = Window.from_slices((row_offset, row_offset + aoi_obj.height), |
473 |
| - (col_offset, col_offset + aoi_obj.width)) |
474 |
| - fc_block = fc_obj.read(1, window=corresponding_window) |
475 |
| - area += np.count_nonzero(fc_block * (aoi_block > 0)) |
476 |
| - |
477 |
| - return area |
478 |
| - |
479 |
| - |
480 |
| -def get_change_area(fcc_source, aoi, change_type, row_offset, col_offset): |
481 |
| - if change_type == 'loss': |
482 |
| - target_value = -1 |
483 |
| - else: |
484 |
| - # Default to gain |
485 |
| - target_value = 1 |
486 |
| - |
487 |
| - area = 0 |
488 |
| - with rio.open(fcc_source) as fcc_obj, rio.open(aoi) as aoi_obj: |
489 |
| - aoi_block = aoi_obj.read(1) |
490 |
| - corresponding_window = Window.from_slices((row_offset, row_offset + aoi_obj.height), |
491 |
| - (col_offset, col_offset + aoi_obj.width)) |
492 |
| - fcc_block = fcc_obj.read(1, window=corresponding_window) |
493 |
| - |
494 |
| - target_change_arr = (fcc_block == target_value) |
495 |
| - area += np.count_nonzero((target_change_arr > 0) * (aoi_block > 0)) |
496 |
| - |
497 |
| - return area |
498 |
| - |
499 |
| - |
500 | 468 | def generate_fcc_file(request):
|
501 | 469 | try:
|
502 | 470 | year = request.GET.get('year')
|
@@ -579,30 +547,76 @@ def get_available_colors():
|
579 | 547 | return colors
|
580 | 548 |
|
581 | 549 |
|
582 |
| -@csrf_exempt |
583 |
| -def stage_for_processing(request,pk): |
584 |
| - if request.POST.get('type')=='fc': |
585 |
| - fc_collection_name = request.POST.get('coll_name') |
586 |
| - coll=ForestCoverCollection.objects.get(name=fc_collection_name) |
| 550 | +def get_available_agbs(collection): |
| 551 | + owner = collection.owner |
| 552 | + scap_admin = User.objects.get(username='scap_admin') |
587 | 553 |
|
588 |
| - coll.processing_status="Staged" |
589 |
| - coll.save() |
| 554 | + available_agbs_scap = [] |
| 555 | + if owner != scap_admin: |
| 556 | + available_agbs_scap = list(AGBCollection.filter(owner=scap_admin)) |
590 | 557 |
|
591 |
| - process_fc_collection.delay(fc_collection_name) |
592 |
| - if request.POST.get('type')=='agb': |
593 |
| - agb_collection_name = request.POST.get('agb_name') |
594 |
| - coll=AGBCollection.objects.get(name=agb_collection_name) |
| 558 | + available_agbs = list(AGBCollection.filter(owner=owner)) + available_agbs_scap |
| 559 | + |
| 560 | + return available_agbs |
| 561 | + |
| 562 | + |
| 563 | +def get_available_fcs(collection): |
| 564 | + owner = collection.owner |
| 565 | + scap_admin = User.objects.get(username='scap_admin') |
595 | 566 |
|
596 |
| - coll.processing_status="Staged" |
597 |
| - coll.save() |
| 567 | + available_fcs_scap = [] |
| 568 | + if owner != scap_admin: |
| 569 | + available_fcs_scap = list(ForestCoverCollection.objects.filter(owner=scap_admin)) |
598 | 570 |
|
599 |
| - process_agb_collection.delay(agb_collection_name) |
600 |
| - if request.POST.get('type')=='aoi': |
| 571 | + available_fcs = list(ForestCoverCollection.objects.filter(owner=owner)) + available_fcs_scap |
| 572 | + |
| 573 | + return available_fcs |
| 574 | + |
| 575 | + |
| 576 | +def get_available_aois(collection): |
| 577 | + owner = collection.owner |
| 578 | + scap_admin = User.objects.get(username='scap_admin') |
| 579 | + |
| 580 | + available_aois_scap = [] |
| 581 | + if owner != scap_admin: |
| 582 | + available_aois_scap = list(AOICollection.objects.filter(owner=scap_admin)) |
| 583 | + |
| 584 | + available_aois = list(AOICollection.objects.filter(owner=owner)) + available_aois_scap |
| 585 | + |
| 586 | + return available_aois |
| 587 | + |
| 588 | + |
| 589 | +def get_available_agbs(collection): |
| 590 | + owner = collection.owner |
| 591 | + scap_admin = User.objects.get(username='scap_admin') |
| 592 | + |
| 593 | + available_agbs_scap = [] |
| 594 | + if owner != scap_admin: |
| 595 | + available_agbs_scap = list(AGBCollection.objects.filter(owner=scap_admin)) |
| 596 | + |
| 597 | + available_agbs = list(AGBCollection.objects.filter(owner=owner)) + available_agbs_scap |
| 598 | + |
| 599 | + return available_agbs |
| 600 | + |
| 601 | + |
| 602 | +@csrf_exempt |
| 603 | +def stage_for_processing(request,pk): |
| 604 | + collection_type = request.POST.get('type') |
| 605 | + if collection_type=='fc': |
| 606 | + fc_collection_name = request.POST.get('coll_name') |
| 607 | + collection = ForestCoverCollection.objects.get(name=fc_collection_name) |
| 608 | + elif collection_type=='agb': |
| 609 | + agb_collection_name = request.POST.get('agb_name') |
| 610 | + collection = AGBCollection.objects.get(name=agb_collection_name) |
| 611 | + else: |
601 | 612 | aoi_collection_name = request.POST.get('aoi_name')
|
602 |
| - coll=AOICollection.objects.get(name=aoi_collection_name) |
| 613 | + collection = AOICollection.objects.get(name=aoi_collection_name) |
603 | 614 |
|
604 |
| - coll.processing_status="Staged" |
605 |
| - coll.save() |
| 615 | + try: |
| 616 | + process_updated_collection.delay(collection.id, collection_type) |
| 617 | + except Exception as error: |
| 618 | + print(error) |
606 | 619 |
|
607 |
| - process_aoi_collection.delay(aoi_collection_name) |
| 620 | + collection.processing_status = "Staged" |
| 621 | + collection.save() |
608 | 622 | return JsonResponse({})
|
0 commit comments