diff --git a/bats_ai/core/admin/grts_cells.py b/bats_ai/core/admin/grts_cells.py index 8ca38ab..b4d6ee9 100644 --- a/bats_ai/core/admin/grts_cells.py +++ b/bats_ai/core/admin/grts_cells.py @@ -1,8 +1,16 @@ from django.contrib import admin + from bats_ai.core.models import GRTSCells + @admin.register(GRTSCells) class GRTSCellsAdmin(admin.ModelAdmin): - list_display = ('id', 'grts_cell_id', 'sample_frame_id', 'water_p', 'outside_p') # Add other fields you want to display in the list + list_display = ( + 'id', + 'grts_cell_id', + 'sample_frame_id', + 'water_p', + 'outside_p', + ) # Add other fields you want to display in the list search_fields = ('id', 'grts_cell_id', 'sample_frame_id') # Add fields for searching list_filter = ('location_1_type', 'location_2_type') # Add fields for filtering diff --git a/bats_ai/core/management/commands/importGRTSCells.py b/bats_ai/core/management/commands/importGRTSCells.py index b451c20..4a2da81 100644 --- a/bats_ai/core/management/commands/importGRTSCells.py +++ b/bats_ai/core/management/commands/importGRTSCells.py @@ -1,7 +1,9 @@ import csv + +from django.core.exceptions import ValidationError from django.core.management.base import BaseCommand + from bats_ai.core.models import GRTSCells -from django.core.exceptions import ValidationError class Command(BaseCommand): @@ -16,7 +18,7 @@ def handle(self, *args, **options): # Get all field names of the GRTSCells model model_fields = [field.name for field in GRTSCells._meta.get_fields()] - with open(csv_file, 'r') as file: + with open(csv_file) as file: reader = csv.DictReader(file) total_rows = sum(1 for _ in reader) # Get total number of rows in the CSV file.seek(0) # Reset file pointer to start @@ -30,7 +32,7 @@ def handle(self, *args, **options): for key, value in filtered_row.items(): if value == '': filtered_row[key] = None - + # Convert boolean fields from string to boolean values for boolean_field in ['priority_frame', 'priority_state', 'clipped']: if filtered_row.get(boolean_field): @@ -39,16 +41,18 @@ def handle(self, *args, **options): elif filtered_row[boolean_field].lower() == 'false': filtered_row[boolean_field] = False else: - raise ValidationError(f'Invalid boolean value for field {boolean_field}: {filtered_row[boolean_field]}') + raise ValidationError( + f'Invalid boolean value for field {boolean_field}: {filtered_row[boolean_field]}' + ) # Check if a record with all the data already exists if GRTSCells.objects.filter(**filtered_row).exists(): - #self.stdout.write(f'Skipping row because it already exists: {filtered_row}') + # self.stdout.write(f'Skipping row because it already exists: {filtered_row}') counter += 1 self.stdout.write(f'Processed {counter} of {total_rows} rows') continue - try: + try: GRTSCells.objects.create(**filtered_row) counter += 1 self.stdout.write(f'Processed {counter} of {total_rows} rows') diff --git a/bats_ai/core/migrations/0008_grtscells_recording_recorded_time.py b/bats_ai/core/migrations/0008_grtscells_recording_recorded_time.py index b8d1376..9fc1f95 100644 --- a/bats_ai/core/migrations/0008_grtscells_recording_recorded_time.py +++ b/bats_ai/core/migrations/0008_grtscells_recording_recorded_time.py @@ -5,7 +5,6 @@ class Migration(migrations.Migration): - dependencies = [ ('core', '0007_temporalannotations'), ] @@ -17,7 +16,12 @@ class Migration(migrations.Migration): ('id', models.IntegerField(primary_key=True, serialize=False)), ('grts_cell_id', models.IntegerField()), ('sample_frame_id', models.IntegerField(blank=True, null=True)), - ('grts_geom', django.contrib.gis.db.models.fields.GeometryField(blank=True, null=True, srid=4326)), + ( + 'grts_geom', + django.contrib.gis.db.models.fields.GeometryField( + blank=True, null=True, srid=4326 + ), + ), ('water_p', models.FloatField(blank=True, null=True)), ('outside_p', models.FloatField(blank=True, null=True)), ('location_1_type', models.CharField(blank=True, max_length=255, null=True)), diff --git a/bats_ai/core/models/grts_cells.py b/bats_ai/core/models/grts_cells.py index d9abc80..90ba95d 100644 --- a/bats_ai/core/models/grts_cells.py +++ b/bats_ai/core/models/grts_cells.py @@ -14,6 +14,7 @@ 26: 'Offshore Mexico', } + class GRTSCells(models.Model): id = models.IntegerField(primary_key=True) grts_cell_id = models.IntegerField() @@ -47,6 +48,4 @@ def sampleFrameMapping(self): @staticmethod def sort_order(): - return [ - 14, 20, 15, 24, 21, 19, 12, 22, 23, 25, 26 - ] + return [14, 20, 15, 24, 21, 19, 12, 22, 23, 25, 26] diff --git a/bats_ai/core/views/grts_cells.py b/bats_ai/core/views/grts_cells.py index 6c6c34e..95a3c07 100644 --- a/bats_ai/core/views/grts_cells.py +++ b/bats_ai/core/views/grts_cells.py @@ -1,17 +1,17 @@ -from django.http import JsonResponse -from bats_ai.core.models import GRTSCells -from django.http import HttpRequest +from django.contrib.gis.geos import Point, Polygon +from django.http import HttpRequest, JsonResponse +from ninja import Query from ninja.pagination import RouterPaginated -from django.contrib.gis.geos import Polygon -from django.contrib.gis.geos import Point -from django.contrib.gis.measure import D -from ninja import Query +from bats_ai.core.models import GRTSCells router = RouterPaginated() + @router.get('/grid_cell_id') -def get_grid_cell_id(request: HttpRequest, latitude: float = Query(...), longitude: float = Query(...)): +def get_grid_cell_id( + request: HttpRequest, latitude: float = Query(...), longitude: float = Query(...) # noqa: B008 +): try: # Create a point object from the provided latitude and longitude point = Point(longitude, latitude, srid=4326) @@ -23,7 +23,9 @@ def get_grid_cell_id(request: HttpRequest, latitude: float = Query(...), longitu # Return the grid cell ID return JsonResponse({'grid_cell_id': cell.grts_cell_id}) else: - return JsonResponse({'error': 'No grid cell found for the provided latitude and longitude'}, status=200) + return JsonResponse( + {'error': 'No grid cell found for the provided latitude and longitude'}, status=200 + ) except Exception as e: return JsonResponse({'error': str(e)}, status=200) @@ -32,20 +34,19 @@ def get_grid_cell_id(request: HttpRequest, latitude: float = Query(...), longitu def get_cell_center(request: HttpRequest, id: int, quadrant: str = None): try: cells = GRTSCells.objects.filter(grts_cell_id=id) - + # Define a custom order for sample_frame_id custom_order = GRTSCells.sort_order() # Define your custom order here - + # Define a custom key function to sort cells based on the custom order def custom_sort_key(cell): return custom_order.index(cell.sample_frame_id) - + # Sort the cells queryset based on the custom order sorted_cells = sorted(cells, key=custom_sort_key) cell = sorted_cells[0] geom_4326 = cell.geom_4326 - # Get the centroid of the entire cell polygon center = geom_4326.centroid @@ -80,4 +81,3 @@ def custom_sort_key(cell): return JsonResponse({'latitude': center_latitude, 'longitude': center_longitude}) except GRTSCells.DoesNotExist: return JsonResponse({'error': f'Cell with cellId={id} does not exist'}, status=200) -