diff --git a/positioning/apps/main/migrations/0003_orto.py b/positioning/apps/main/migrations/0003_orto.py new file mode 100644 index 0000000..6daddce --- /dev/null +++ b/positioning/apps/main/migrations/0003_orto.py @@ -0,0 +1,53 @@ +# Generated by Django 4.2.19 on 2025-05-05 10:17 + +import django.contrib.gis.db.models.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("main", "0002_streetzone"), + ] + + operations = [ + migrations.CreateModel( + name="Orto", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=128, verbose_name="Наименование")), + ( + "raster", + django.contrib.gis.db.models.fields.RasterField( + srid=4326, verbose_name=".tif файл" + ), + ), + ( + "tiles_path", + models.FilePathField( + allow_files=False, + allow_folders=True, + blank=True, + null=True, + verbose_name="Каталог", + ), + ), + ], + options={ + "verbose_name": "ортофотоплан", + "verbose_name_plural": "ортофотопланы", + "ordering": ["name"], + "indexes": [ + models.Index(fields=["name"], name="main_orto_name_39c2d3_idx") + ], + }, + ), + ] diff --git a/positioning/apps/main/migrations/0004_ortho_delete_orto_ortho_main_ortho_name_ff77f0_idx.py b/positioning/apps/main/migrations/0004_ortho_delete_orto_ortho_main_ortho_name_ff77f0_idx.py new file mode 100644 index 0000000..0445b63 --- /dev/null +++ b/positioning/apps/main/migrations/0004_ortho_delete_orto_ortho_main_ortho_name_ff77f0_idx.py @@ -0,0 +1,58 @@ +# Generated by Django 4.2.19 on 2025-05-06 02:15 + +import django.contrib.gis.db.models.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("main", "0003_orto"), + ] + + operations = [ + migrations.CreateModel( + name="Ortho", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=128, verbose_name="Наименование")), + ( + "raster", + django.contrib.gis.db.models.fields.RasterField( + srid=4326, verbose_name=".tif файл" + ), + ), + ( + "tiles_path", + models.FilePathField( + allow_files=False, + allow_folders=True, + blank=True, + null=True, + path="D:\\Projects\\positioning\\media\\ortos", + verbose_name="Каталог", + ), + ), + ], + options={ + "verbose_name": "ортофотоплан", + "verbose_name_plural": "ортофотопланы", + "ordering": ["name"], + }, + ), + migrations.DeleteModel( + name="Orto", + ), + migrations.AddIndex( + model_name="ortho", + index=models.Index(fields=["name"], name="main_ortho_name_ff77f0_idx"), + ), + ] diff --git a/positioning/apps/main/migrations/0005_ortho_active_alter_ortho_tiles_path.py b/positioning/apps/main/migrations/0005_ortho_active_alter_ortho_tiles_path.py new file mode 100644 index 0000000..ffbe633 --- /dev/null +++ b/positioning/apps/main/migrations/0005_ortho_active_alter_ortho_tiles_path.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.19 on 2025-05-06 02:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("main", "0004_ortho_delete_orto_ortho_main_ortho_name_ff77f0_idx"), + ] + + operations = [ + migrations.AddField( + model_name="ortho", + name="active", + field=models.BooleanField(default=True), + ), + migrations.AlterField( + model_name="ortho", + name="tiles_path", + field=models.FilePathField( + allow_files=False, + allow_folders=True, + blank=True, + null=True, + path="D:\\Projects\\positioning\\media\\orthos", + verbose_name="Каталог", + ), + ), + ] diff --git a/positioning/apps/main/migrations/0006_alter_ortho_tiles_path.py b/positioning/apps/main/migrations/0006_alter_ortho_tiles_path.py new file mode 100644 index 0000000..a4cf754 --- /dev/null +++ b/positioning/apps/main/migrations/0006_alter_ortho_tiles_path.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.19 on 2025-05-06 05:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("main", "0005_ortho_active_alter_ortho_tiles_path"), + ] + + operations = [ + migrations.AlterField( + model_name="ortho", + name="tiles_path", + field=models.CharField(blank=True, null=True, verbose_name="Путь"), + ), + ] diff --git a/positioning/apps/main/migrations/0007_remove_ortho_raster_ortho_max_lat_ortho_max_lon_and_more.py b/positioning/apps/main/migrations/0007_remove_ortho_raster_ortho_max_lat_ortho_max_lon_and_more.py new file mode 100644 index 0000000..05e7252 --- /dev/null +++ b/positioning/apps/main/migrations/0007_remove_ortho_raster_ortho_max_lat_ortho_max_lon_and_more.py @@ -0,0 +1,45 @@ +# Generated by Django 4.2.19 on 2025-05-06 05:54 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("main", "0006_alter_ortho_tiles_path"), + ] + + operations = [ + migrations.RemoveField( + model_name="ortho", + name="raster", + ), + migrations.AddField( + model_name="ortho", + name="max_lat", + field=models.FloatField( + blank=True, null=True, verbose_name="Extent (max_lat)" + ), + ), + migrations.AddField( + model_name="ortho", + name="max_lon", + field=models.FloatField( + blank=True, null=True, verbose_name="Extent (max_lon)" + ), + ), + migrations.AddField( + model_name="ortho", + name="min_lat", + field=models.FloatField( + blank=True, null=True, verbose_name="Extent (min_lat)" + ), + ), + migrations.AddField( + model_name="ortho", + name="min_lon", + field=models.FloatField( + blank=True, null=True, verbose_name="Extent (min_lon)" + ), + ), + ] diff --git a/positioning/apps/main/models/model_ortho.py b/positioning/apps/main/models/model_ortho.py index ef2daf7..6df9a35 100644 --- a/positioning/apps/main/models/model_ortho.py +++ b/positioning/apps/main/models/model_ortho.py @@ -10,15 +10,16 @@ if not os.path.exists(orthos_path): class Ortho(models.Model): name = models.CharField(max_length=128, verbose_name="Наименование") - raster = models.RasterField(verbose_name=".tif файл") - tiles_path = models.FilePathField( - path=orthos_path, - allow_folders=True, - allow_files=False, - verbose_name="Каталог", + tiles_path = models.CharField( + verbose_name="Путь", blank=True, null=True, ) + min_lon = models.FloatField(null=True, blank=True, verbose_name="Extent (min_lon)") + min_lat = models.FloatField(null=True, blank=True, verbose_name="Extent (min_lat)") + max_lon = models.FloatField(null=True, blank=True, verbose_name="Extent (max_lon)") + max_lat = models.FloatField(null=True, blank=True, verbose_name="Extent (max_lat)") + active = models.BooleanField(default=True) def __str__(self) -> str: diff --git a/positioning/apps/management/subsystems/street_pos/views/views_ortho.py b/positioning/apps/management/subsystems/street_pos/views/views_ortho.py index 8731506..63c705c 100644 --- a/positioning/apps/management/subsystems/street_pos/views/views_ortho.py +++ b/positioning/apps/management/subsystems/street_pos/views/views_ortho.py @@ -2,8 +2,10 @@ import logging import os.path from typing import Any +import pyproj from apps.main.models.model_ortho import Ortho, orthos_path from apps.management.subsystems.forms.street_pos.ortho_form import OrthoForm +from django.conf import settings from django.contrib import messages from django.contrib.gis.gdal import GDALRaster from django.db.models import QuerySet @@ -62,7 +64,14 @@ class OrthoCreateView(CreateView): temp_file = form.files["image"] path = temp_file.temporary_file_path() rst = GDALRaster(path, write=True) - ortho_obj = Ortho.objects.create(name=name, raster=rst) + extent = self.get_extent(rst.extent) + ortho_obj = Ortho.objects.create( + name=name, + min_lon=extent[0], + min_lat=extent[1], + max_lon=extent[2], + max_lat=extent[3], + ) save_path = os.path.join(orthos_path, str(ortho_obj.id)) if not os.path.exists(save_path): @@ -76,10 +85,20 @@ class OrthoCreateView(CreateView): verbose=False, ) - ortho_obj.tiles_path = save_path + ortho_obj.tiles_path = ( + f"{settings.MEDIA_URL}orthos/{ortho_obj.id}/{{z}}/{{x}}/{{y}}.png" + ) ortho_obj.save() return redirect("management:street_pos:ortho-list") + def get_extent( + self, rst: tuple[float, float, float, float] + ) -> tuple[float, float, float, float]: + proj = pyproj.Transformer.from_crs(3857, 4326, always_xy=True) + min_lon, min_lat = proj.transform(rst[0], rst[1]) + max_lon, max_lat = proj.transform(rst[2], rst[3]) + return min_lon, min_lat, max_lon, max_lat + class OrthoUpdateView(UpdateView): model = Ortho