85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
import os
|
|
import shutil
|
|
import uuid
|
|
|
|
from django.conf import settings
|
|
from django.db import models
|
|
from django.db.models.signals import post_delete
|
|
from django.dispatch import receiver
|
|
|
|
|
|
def get_upload_path(instance, filename):
|
|
ext = filename.split(".")[-1]
|
|
new_filename = f"{uuid.uuid4()}.{ext}"
|
|
return os.path.join("layers/", new_filename)
|
|
|
|
|
|
def get_style_template() -> dict:
|
|
return {
|
|
"fill-color": "rgba(255, 0, 0, 0.5)",
|
|
"fill-opacity": 1,
|
|
"stroke-color": "blue",
|
|
"stroke-width": 2,
|
|
"stroke-opacity": 1,
|
|
}
|
|
|
|
|
|
orthos_path = os.path.join(settings.MEDIA_ROOT, "map", "orthos")
|
|
if not os.path.exists(orthos_path):
|
|
os.makedirs(orthos_path)
|
|
|
|
|
|
class StaticLayer(models.Model):
|
|
"""
|
|
Модель слоев
|
|
Типы слоев: Tile (плитки), Vector (точки, линии, полигоны)
|
|
"""
|
|
|
|
LAYER_TYPES = (
|
|
("Tile", "Растровый"),
|
|
("BaseTile", "Базовая карта"),
|
|
("Vector", "Векторный"),
|
|
)
|
|
|
|
name = models.CharField(max_length=255, verbose_name="Название слоя")
|
|
file = models.FileField(
|
|
upload_to=get_upload_path, verbose_name="Файл слоя", null=True,
|
|
blank=True
|
|
)
|
|
url = models.CharField(
|
|
max_length=255,
|
|
blank=True,
|
|
null=True,
|
|
verbose_name="Каталог (для растровых слоев)",
|
|
)
|
|
layer_type = models.CharField(
|
|
max_length=50, verbose_name="Тип слоя", default="Vector",
|
|
choices=LAYER_TYPES
|
|
)
|
|
is_visible = models.BooleanField(default=True,
|
|
verbose_name="Видимость слоя")
|
|
order = models.IntegerField(default=0, verbose_name="Порядок отображения")
|
|
|
|
min_lon = models.FloatField("min_lon", blank=True, null=True)
|
|
min_lat = models.FloatField("min_lat", blank=True, null=True)
|
|
max_lon = models.FloatField("max_lon", blank=True, null=True)
|
|
max_lat = models.FloatField("max_lat", blank=True, null=True)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
class Meta:
|
|
verbose_name = "Статический слой"
|
|
verbose_name_plural = "Статические слои"
|
|
indexes = [models.Index(fields=["name"]),
|
|
models.Index(fields=["is_visible"])]
|
|
|
|
|
|
@receiver(post_delete, sender=StaticLayer)
|
|
def remove_tiles(sender, instance, **kwargs):
|
|
if instance.url is not None:
|
|
path = instance.url
|
|
rm_len = len(path) - 16
|
|
rm_path = settings.MEDIA_ROOT + path[6:rm_len]
|
|
shutil.rmtree(rm_path)
|