Skip to content

Commit 97542fd

Browse files
author
alex
committed
Update processing
1 parent 0ccee92 commit 97542fd

15 files changed

+974
-418
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ scap/migrations/
77
media/
88
logfile
99
/scap/migrations/
10+
dump.rdb
11+
.idea/

.idea/.workspace.xml.swp

16 KB
Binary file not shown.

.idea/dictionaries/alex.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+47-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
326 Bytes
Binary file not shown.

scap/admin.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.contrib import admin
22
from import_export.admin import ImportExportModelAdmin
3-
from scap.models import (AOIFeature, AOICollection, ForestCoverCollection, AGBCollection,
3+
from scap.models import (AOIFeature, AOICollection, ForestCoverCollection, AGBCollection, CurrentTask,
44
ForestCoverStatistic, CarbonStatistic, ForestCoverFile, PilotCountry)
55

66

@@ -10,6 +10,9 @@ class AOIFeatureAdmin(ImportExportModelAdmin, admin.ModelAdmin):
1010
search_fields = ['name']
1111

1212

13+
class CurrentTaskAdmin(ImportExportModelAdmin, admin.ModelAdmin):
14+
list_display = ('id','overall_progress')
15+
1316

1417
class PilotCountryAdmin(ImportExportModelAdmin, admin.ModelAdmin):
1518
list_display = ('country_name','region','country_code','year_added','latitude','longitude','zoom_level')
@@ -41,7 +44,7 @@ class ForestCoverCollectionAdmin(ImportExportModelAdmin, admin.ModelAdmin):
4144

4245
class CarbonStatisticsAdmin(ImportExportModelAdmin, admin.ModelAdmin):
4346
list_display = ('id', 'fc_index', 'agb_index', 'aoi_index', 'year_index',
44-
'initial_carbon_stock', 'emissions', 'agb_value')
47+
'final_carbon_stock', 'emissions', 'agb_value')
4548
list_filter = ('fc_index', 'agb_index', 'year_index')
4649

4750

@@ -62,3 +65,5 @@ class ForestCoverStatisticsAdmin(ImportExportModelAdmin, admin.ModelAdmin):
6265

6366
admin.site.register(ForestCoverStatistic, ForestCoverStatisticsAdmin)
6467
admin.site.register(CarbonStatistic, CarbonStatisticsAdmin)
68+
69+
admin.site.register(CurrentTask, CurrentTaskAdmin)

scap/api.py

+69-55
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
from shapely.geometry import shape
1414
from django.contrib.gis.utils import LayerMapping
1515
from django.views.decorators.csrf import csrf_exempt
16+
from django.contrib.auth.models import User
1617

1718
from ScapTestProject import settings
1819
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
2123
import geopandas as gpd
2224

2325
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -463,40 +465,6 @@ def get_updated_series(request, country=None):
463465
'lcs_defor': json.dumps(lcs_defor), 'lc_data': lcs_defor, 'region_country': pa_name})
464466

465467

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-
500468
def generate_fcc_file(request):
501469
try:
502470
year = request.GET.get('year')
@@ -579,30 +547,76 @@ def get_available_colors():
579547
return colors
580548

581549

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')
587553

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))
590557

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')
595566

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))
598570

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:
601612
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)
603614

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)
606619

607-
process_aoi_collection.delay(aoi_collection_name)
620+
collection.processing_status = "Staged"
621+
collection.save()
608622
return JsonResponse({})

0 commit comments

Comments
 (0)