2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-24 00:01:54 +00:00
troggle/core/models/survex.py

293 lines
11 KiB
Python
Raw Normal View History

2023-10-27 20:13:14 +01:00
import math
2023-01-19 18:35:56 +00:00
import os
import re
from urllib.parse import urljoin
from pathlib import Path
from django.conf import settings
2023-01-19 18:35:56 +00:00
from django.db import models
2020-06-18 21:50:16 +01:00
from django.urls import reverse
2023-11-05 20:59:02 +00:00
from troggle.core.utils import height_from_utm, throw
# from troggle.core.models.troggle import DataIssue # circular import. Hmm
2011-07-11 02:10:22 +01:00
class SurvexFile(models.Model):
path = models.CharField(max_length=200)
#survexdirectory = models.ForeignKey("SurvexDirectory", blank=True, null=True, on_delete=models.SET_NULL)
primary = models.ForeignKey(
"SurvexFile", related_name="primarysurvex", blank=True, null=True, on_delete=models.SET_NULL
)
cave = models.ForeignKey("Cave", blank=True, null=True, on_delete=models.SET_NULL)
2011-07-11 02:10:22 +01:00
class Meta:
ordering = ("id",)
2020-06-28 14:42:26 +01:00
2020-06-29 21:16:13 +01:00
# Don't change from the default as that breaks troggle webpages and internal referencing!
# def __str__(self):
# return "[SurvexFile:"+str(self.path) + "-" + str(self.survexdirectory) + "-" + str(self.cave)+"]"
2020-06-28 14:42:26 +01:00
2011-07-11 02:10:22 +01:00
def exists(self):
2023-03-23 19:05:25 +00:00
"""This is only used within the Django templates
"""
fname = Path(settings.SURVEX_DATA, self.path + ".svx")
return fname.is_file()
2023-03-23 19:05:25 +00:00
# Don't change from the default as that breaks troggle webpages and internal referencing!
# def __str__(self):
# return "[SurvexFile:"+str(self.path) + "-" + str(self.survexdirectory) + "-" + str(self.cave)+"]"
def __str__(self):
return self.path
2011-07-11 02:10:22 +01:00
class SurvexStationLookUpManager(models.Manager):
2023-03-06 22:30:36 +00:00
"""what this does,
2023-03-06 16:37:38 +00:00
https://docs.djangoproject.com/en/dev/topics/db/managers/
This changes the .objects thinggy to use a case-insensitive match name__iexact
so that now SurvexStation.objects.lookup() works as a case-insensitive match
"""
2011-07-11 02:10:22 +01:00
def lookup(self, name):
blocknames, sep, stationname = name.rpartition(".")
return self.get(block=SurvexBlock.objects.lookup(blocknames), name__iexact=stationname)
2011-07-11 02:10:22 +01:00
class SurvexStation(models.Model):
name = models.CharField(max_length=100)
# block = models.ForeignKey("SurvexBlock", null=True, on_delete=models.SET_NULL)
2023-10-07 00:26:52 +01:00
# block not used since 2020. survex stations objects are only used for entrance locations and all taken from the .3d file
objects = SurvexStationLookUpManager() # overwrites SurvexStation.objects and enables lookup()
2011-07-11 02:10:22 +01:00
x = models.FloatField(blank=True, null=True)
y = models.FloatField(blank=True, null=True)
z = models.FloatField(blank=True, null=True)
entrance = models.ForeignKey("Entrance", blank=True, null=True, on_delete=models.SET_NULL)
class Meta:
ordering = ("id",)
def __str__(self):
return self.name and str(self.name) or "no name"
2023-05-01 00:01:41 +01:00
def latlong(self):
return utmToLatLng(33, self.x, self.y, northernHemisphere=True)
def lat(self):
return utmToLatLng(33, self.x, self.y, northernHemisphere=True)[0]
def long(self):
return utmToLatLng(33, self.x, self.y, northernHemisphere=True)[1]
2023-10-27 20:13:14 +01:00
def srtm_alt(self):
2023-11-01 23:05:36 +00:00
"""Caches the srtm data so that searches are not done twice on the same page"""
if not hasattr(self,"srtm"):
self.srtm = height_from_utm(self.x, self.y) # height, distance from reference point
return self.srtm # (nearest point, nearest distance)
2023-10-27 20:13:14 +01:00
def srtm_diff(self):
2023-11-01 23:05:36 +00:00
alt, ref = self.srtm_alt()
2023-10-27 20:13:14 +01:00
diff = alt - self.z
2023-11-05 00:24:37 +00:00
if diff >= 0.3:
2023-11-01 23:05:36 +00:00
colour = "blue"
sign = "+"
2023-11-05 00:24:37 +00:00
elif diff <= -0.3:
2023-11-01 23:05:36 +00:00
colour = "red"
sign = ""
2023-11-05 00:24:37 +00:00
else:
diff = 0
colour = "grey"
sign = ""
2023-11-01 23:05:36 +00:00
if abs(diff) > 60:
weight = "bold"
else:
weight = "normal"
2023-11-05 20:59:02 +00:00
diff_str = f"<span style='color:{colour}; font-weight:{weight}'>{sign}{diff:.0f}</span>"
2023-11-05 20:59:02 +00:00
if ref >= throw:
colour = "grey"
weight = "normal"
diff = "XX"
ref = float("nan")
diff_str = f"<span style='color:{colour}'>XX</span>"
2023-10-27 20:13:14 +01:00
return diff_str, ref
2023-05-01 00:01:41 +01:00
2023-09-07 23:26:01 +01:00
def utmToLatLng(zone, easting, northing, northernHemisphere=True): # move this to utils.py ?
2023-05-01 00:01:41 +01:00
if not northernHemisphere:
northing = 10000000 - northing
a = 6378137
e = 0.081819191
e1sq = 0.006739497
k0 = 0.9996
arc = northing / k0
mu = arc / (a * (1 - math.pow(e, 2) / 4.0 - 3 * math.pow(e, 4) / 64.0 - 5 * math.pow(e, 6) / 256.0))
ei = (1 - math.pow((1 - e * e), (1 / 2.0))) / (1 + math.pow((1 - e * e), (1 / 2.0)))
ca = 3 * ei / 2 - 27 * math.pow(ei, 3) / 32.0
cb = 21 * math.pow(ei, 2) / 16 - 55 * math.pow(ei, 4) / 32
cc = 151 * math.pow(ei, 3) / 96
cd = 1097 * math.pow(ei, 4) / 512
phi1 = mu + ca * math.sin(2 * mu) + cb * math.sin(4 * mu) + cc * math.sin(6 * mu) + cd * math.sin(8 * mu)
n0 = a / math.pow((1 - math.pow((e * math.sin(phi1)), 2)), (1 / 2.0))
r0 = a * (1 - e * e) / math.pow((1 - math.pow((e * math.sin(phi1)), 2)), (3 / 2.0))
fact1 = n0 * math.tan(phi1) / r0
_a1 = 500000 - easting
dd0 = _a1 / (n0 * k0)
fact2 = dd0 * dd0 / 2
t0 = math.pow(math.tan(phi1), 2)
Q0 = e1sq * math.pow(math.cos(phi1), 2)
fact3 = (5 + 3 * t0 + 10 * Q0 - 4 * Q0 * Q0 - 9 * e1sq) * math.pow(dd0, 4) / 24
fact4 = (61 + 90 * t0 + 298 * Q0 + 45 * t0 * t0 - 252 * e1sq - 3 * Q0 * Q0) * math.pow(dd0, 6) / 720
lof1 = _a1 / (n0 * k0)
lof2 = (1 + 2 * t0 + Q0) * math.pow(dd0, 3) / 6.0
lof3 = (5 - 2 * Q0 + 28 * t0 - 3 * math.pow(Q0, 2) + 8 * e1sq + 24 * math.pow(t0, 2)) * math.pow(dd0, 5) / 120
_a2 = (lof1 - lof2 + lof3) / math.cos(phi1)
_a3 = _a2 * 180 / math.pi
latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / math.pi
if not northernHemisphere:
latitude = -latitude
longitude = ((zone > 0) and (6 * zone - 183.0) or 3.0) - _a3
return (latitude, longitude)
2011-07-11 02:10:22 +01:00
#
# Single SurvexBlock
#
2023-03-06 22:30:36 +00:00
class SurvexBlockLookUpManager(models.Manager):
"""what this does,
https://docs.djangoproject.com/en/dev/topics/db/managers/
2023-03-06 16:37:38 +00:00
2023-03-06 22:30:36 +00:00
This adds a method to the .objects thinggy to use a case-insensitive match name__iexact
so that now SurvexBlock.objects.lookup() works as a case-insensitive match.
This is used in lookup() in SurvexStationLookUpManager()
which is used in Entrance().other_location() which is used in the Cave webpage
"""
def lookup(self, name):
if name == "":
blocknames = []
else:
blocknames = name.split(".")
block = SurvexBlock.objects.get(parent=None, survexfile__path=settings.SURVEX_TOPNAME)
2023-03-06 22:30:36 +00:00
for blockname in blocknames:
block = SurvexBlock.objects.get(parent=block, name__iexact=blockname)
return block
2011-07-11 02:10:22 +01:00
2011-07-11 02:10:22 +01:00
class SurvexBlock(models.Model):
"""One begin..end block within a survex file. The basic element of a survey trip.
Multiple anonymous survex blocks are possible within the same surfex file
Blocks can span several *included survexfile though.
"""
objects = SurvexBlockLookUpManager() # overwrites SurvexBlock.objects and enables lookup()
name = models.CharField(max_length=100)
title = models.CharField(max_length=200)
parent = models.ForeignKey("SurvexBlock", blank=True, null=True, on_delete=models.SET_NULL)
date = models.DateField(blank=True, null=True)
expedition = models.ForeignKey("Expedition", blank=True, null=True, on_delete=models.SET_NULL)
# if the survexfile object is deleted, then all the survex-blocks in it should be too,
# though a block can span more than one file...
survexfile = models.ForeignKey("SurvexFile", blank=True, null=True, on_delete=models.CASCADE)
2023-09-07 23:26:01 +01:00
# survexpath = models.CharField(max_length=200, blank=True, null=True) No need for this anymore
scanswallet = models.ForeignKey(
"Wallet", null=True, on_delete=models.SET_NULL
) # only ONE wallet per block. The most recent seen overwites.. ugh.
legsall = models.IntegerField(null=True) # summary data for this block
2020-07-04 13:31:46 +01:00
legslength = models.FloatField(null=True)
2011-07-11 02:10:22 +01:00
class Meta:
ordering = ("id",)
2011-07-11 02:10:22 +01:00
2020-06-28 14:42:26 +01:00
def __str__(self):
2023-03-13 20:27:27 +00:00
return self.name and str(self.name) or "no_name-#" + str(self.id)
2020-06-28 14:42:26 +01:00
def isSurvexBlock(self): # Function used in templates
2011-07-11 02:10:22 +01:00
return True
def DayIndex(self):
"""This is used to set different colours for the different trips on
the calendar view of the expedition"""
2023-07-14 10:13:06 +01:00
# print(f"SurvexBlock DayIndex {self.name} '{self.date}' {len(list(SurvexBlock.objects.filter(date=self.date)))} on this date")
2023-01-30 15:28:11 +00:00
mx = 10
2023-07-14 10:13:06 +01:00
todays = list(SurvexBlock.objects.filter(date=self.date))
if self in todays:
index = todays.index(self)
else:
print(f"DayIndex: Synchronization error in survex blocks. Restart server or do full reset. {self}")
2023-07-13 21:20:01 +01:00
index = 0
2023-01-30 15:28:11 +00:00
if index not in range(0, mx):
2023-07-14 10:13:06 +01:00
print(f"DayIndex: More than {mx-1} SurvexBlock items on one day '{index}' {self}, restarting colour sequence.")
index = index % mx
return index
2011-07-11 02:10:22 +01:00
2011-07-11 02:10:22 +01:00
class SurvexPersonRole(models.Model):
2023-03-06 16:37:38 +00:00
"""The CASCADE means that if a SurvexBlock or a Person is deleted, then the SurvexPersonRole
is deleted too
"""
survexblock = models.ForeignKey("SurvexBlock", on_delete=models.CASCADE)
2023-09-07 23:26:01 +01:00
# increasing levels of precision, Surely we only need survexblock and (either person or personexpedition)?
personname = models.CharField(max_length=100)
2023-03-06 16:37:38 +00:00
person = models.ForeignKey("Person", blank=True, null=True, on_delete=models.CASCADE) # not needed
personexpedition = models.ForeignKey("PersonExpedition", blank=True, null=True, on_delete=models.SET_NULL)
2020-05-26 02:21:36 +01:00
def __str__(self):
return str(self.personname) + " - " + str(self.survexblock)
2020-06-24 00:18:01 +01:00
class SingleScan(models.Model):
"""A single file holding an image. Could be raw notes, an elevation plot or whatever"""
ffile = models.CharField(max_length=200)
name = models.CharField(max_length=200)
wallet = models.ForeignKey("Wallet", null=True, on_delete=models.SET_NULL)
2011-07-11 02:10:22 +01:00
class Meta:
ordering = ("name",)
2011-07-11 02:10:22 +01:00
def get_absolute_url(self):
# we do not use URL_ROOT any more.
return reverse("scansingle", kwargs={"path": re.sub("#", "%23", self.wallet.walletname), "file": self.name})
2020-05-26 02:21:36 +01:00
def __str__(self):
return "Scan Image: " + str(self.name) + " in " + str(self.wallet)
2021-04-26 18:08:42 +01:00
class DrawingFile(models.Model):
"""A file holding a Therion (several types) or a Tunnel drawing
Most of the implied capabilities are not implemented yet"""
dwgpath = models.CharField(max_length=200)
dwgname = models.CharField(max_length=200)
dwgwallets = models.ManyToManyField("Wallet") # implicitly links via folders to scans to SVX files
scans = models.ManyToManyField("SingleScan") # implicitly links via scans to SVX files
dwgcontains = models.ManyToManyField("DrawingFile") # case when its a frame type
filesize = models.IntegerField(default=0)
npaths = models.IntegerField(default=0)
survexfiles = models.ManyToManyField("SurvexFile") # direct link to SVX files - not populated yet
2011-07-11 02:10:22 +01:00
class Meta:
ordering = ("dwgpath",)
def __str__(self):
return "Drawing File: " + str(self.dwgname) + " (" + str(self.filesize) + " bytes)"