From ccb6b4169c6dd7f100d63f44c9e62313b7ea2fe2 Mon Sep 17 00:00:00 2001 From: zakhy <=> Date: Mon, 12 May 2025 16:10:38 +0700 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20extent=20=D0=B8=D0=B7=20tiff?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- positioning/apps/main/views/views_api.py | 2 +- .../street_pos/views/views_ortho.py | 45 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/positioning/apps/main/views/views_api.py b/positioning/apps/main/views/views_api.py index b539670..ba0ba49 100644 --- a/positioning/apps/main/views/views_api.py +++ b/positioning/apps/main/views/views_api.py @@ -116,7 +116,7 @@ class OrtoListApiView(View): class LayersApiView(View): def get(self, request: HttpRequest) -> JsonResponse: - orthos = Ortho.objects.filter(active=True) + orthos = Ortho.objects.filter(active=True).order_by("-id") dynamic_layers: List[dict] = [] static_layers: List[dict] = [] 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 72a40cd..cf095aa 100644 --- a/positioning/apps/management/subsystems/street_pos/views/views_ortho.py +++ b/positioning/apps/management/subsystems/street_pos/views/views_ortho.py @@ -60,45 +60,48 @@ class OrthoCreateView(CreateView): def get_context_data(self, **kwargs: Any) -> dict[str, Any]: context = super().get_context_data(**kwargs) context["title"] = _("Добавление нового ортофотоплана") - context = SettingsContextMixin.update_context_data(context) - return context + return SettingsContextMixin.update_context_data(context) def form_valid(self, form: OrthoForm) -> HttpResponse: name = form.cleaned_data["name"] - temp_file = form.files["image"] - path = temp_file.temporary_file_path() - rst = GDALRaster(path, write=True) + uploaded_file = form.files["image"] + + # Сохраняем файл во временное хранилище + with tempfile.NamedTemporaryFile(delete=False, suffix=".tif") as tmp: + for chunk in uploaded_file.chunks(): + tmp.write(chunk) + tmp_path = tmp.name + + rst = GDALRaster(tmp_path, write=False) extent = self.get_extent(rst) - ortho_obj = Ortho( + logger.info(extent) + + ortho_uuid = str(uuid.uuid4()) + save_path = os.path.join(orthos_path, ortho_uuid) + os.makedirs(save_path, exist_ok=True) + + ortho = Ortho( name=name, min_lon=extent[0], min_lat=extent[1], max_lon=extent[2], max_lat=extent[3], + tiles_path=f"{settings.MEDIA_URL}orthos/{ortho_uuid}/{{z}}/{{x}}/{{y}}.png", ) - ortho_uuid = str(uuid.uuid4()) - save_path = os.path.join(orthos_path, ortho_uuid) - if not os.path.exists(save_path): - os.makedirs(save_path) - - with tempfile.NamedTemporaryFile(delete=False, suffix=".tif") as tmp: - for chunk in temp_file.chunks(): - tmp.write(chunk) - tmp_path = tmp.name + ortho.save() + # Асинхронная генерация тайлов process = multiprocessing.Process( target=generate_tiles, args=(tmp_path, save_path) ) process.start() - ortho_obj.tiles_path = ( - f"{settings.MEDIA_URL}orthos/{ortho_uuid}/{{z}}/{{x}}/{{y}}.png" - ) - ortho_obj.save() - return redirect("management:street_pos:ortho-list") + + return redirect(self.success_url) def get_extent(self, rst: GDALRaster) -> tuple[float, float, float, float]: rst_extent = rst.extent # (min_x, min_y, max_x, max_y) - + if rst.srid == 4326: + return rst.extent source_srs = osr.SpatialReference() if rst.srs: source_srs.ImportFromWkt(rst.srs.wkt)