Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More layer info #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,11 @@ class Layer(Dateable):
null=True,
)

colour = models.CharField("colour",max_length=32, blank=True)
colour_munsell = models.CharField("colour_munsell", max_length=32, blank=True)
colour_hex = models.CharField("colour_rgb", max_length=7, blank=True)
texture = models.CharField("texture", max_length=15, blank=True)

#
## References
#
Expand Down
18 changes: 17 additions & 1 deletion main/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db.models.signals import post_save, m2m_changed, post_delete
import numpy
from django.db.models.signals import pre_save, post_save, m2m_changed, post_delete
from django.dispatch import receiver
from main.models import (
Layer,
Expand All @@ -16,6 +17,7 @@
import statistics
from django.templatetags.static import static
import re
import colour


# after deleting or adding a date from a dateable - update the mean_upper and mean_lower
Expand Down Expand Up @@ -174,3 +176,17 @@ def update_order(sender, instance, **kwargs):

except:
pass

# this is done pre-save! check if appropriate
@receiver(pre_save, sender=Layer)
def update_colour(sender, instance, **kwargs):

if instance.colour_munsell is not numpy.nan and instance.colour_munsell != "":
#print("Creating hex value")
#print(instance.colour_munsell)
colour_munsell = instance.colour_munsell
colour_sRGB = colour.XYZ_to_RGB(colour.xyY_to_XYZ(colour.munsell_colour_to_xyY(colour_munsell)), "sRGB")
colour_RGB = [min(round(x), 255) for x in (colour.models.eotf_inverse_sRGB(colour_sRGB) * 255)] # linearise sRGB values and convert to integer RGB

instance.colour_hex = "#%02x%02x%02x" % tuple(colour_RGB) # convert to hex and assign. we convert to hex, as it works better than RGB for html

12 changes: 12 additions & 0 deletions main/templates/main/profile/layer-tablecells.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
{% endfor %}
{% endif %}
</td>
<td bgcolor="{% if layer.colour_hex %}{{ layer.colour_hex }}{% else %}white{% endif %}"></td>
<!-- texture -->
<td>
{{layer.texture|default:"N/A"}}
</td>
<!-- colour -->

<td>
{{layer.colour|default:"N/A"}}
<br><span class="text-gray" style="font-size:11pt;">{{layer.colour_munsell}}</span>
</td>

<td class="{% if layer.culture %}{{layer.culture.classname}}{% else %}sterile{% endif%}" style="width: 5px;white-space: nowrap;">
{% if layer.parent %}
<strong>{{layer.name}}</strong>
Expand Down
2 changes: 2 additions & 0 deletions main/templates/main/profile/layer-tablehead.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<th style="width: 5px;">Layer</th>
<th style="width: 5px;"></th>
<th>Texture</th>
<th>Colour</th>
<th>Dating</th>
<th>Culture</th>
<th>Epoch</th>
88 changes: 88 additions & 0 deletions main/tools/imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pandas as pd
import numpy as np
from .. import models

# when importing, take care of layer names! also: if sublayers have the same colour as parent layer, apply parent layer colouration to sublayer
def update_colour_from_tsv(file_path:str, column_name=3, column_colour_m=6, column_site_name=1):
df = pd.read_csv(file_path, sep="\t")
for i in range(1, len(df.index)):
print(i)
if df.iat[i, column_site_name] is np.nan or df.iat[i, column_colour_m] is np.nan or df.iat[i, column_colour_m] == "manual review":
print("no color given")
continue

site = models.Site.objects.filter(name=df.iat[i, column_site_name])
if site.exists():
site = site[0]
else:
print("site does not exist")
continue

# still needs to be able to deal with sublayers (layer_name*)
layer = models.Layer.objects.filter(site=site, name=df.iat[i,column_name])
if layer.exists():
layer = layer[0]
if df.iat[i,5] != "":
layer.colour = df.iat[i,5]
if df.iat[i,column_colour_m] != "":
layer.colour_munsell = df.iat[i,column_colour_m]
layer.save()
else:
print("Layer " + str(df.iat[i,column_name]) + " does not exist")

def update_texture_from_tsv(file_path:str, column_layer_name=3, column_texture=7, column_site_name=1):
df = pd.read_csv(file_path, sep="\t")
for i in range(1, len(df.index)):
print(i)
if df.iat[i, column_site_name] is np.nan or df.iat[i, column_texture] is np.nan:
continue

site = models.Site.objects.filter(name=df.iat[i, column_site_name])
if site.exists():
site = site[0]
else:
#print("site does not exist")
continue

layer = models.Layer.objects.filter(site=site, name=df.iat[i, column_layer_name])
if layer.exists():
layer = layer[0]
if df.iat[i, column_texture] != "":
layer.texture = df.iat[i, column_texture]
print(layer.texture)
layer.save()
else:
#print("Layer " + str(df.iat[i, column_layer_name]) + " does not exist")
continue


#for testing only! dont use!
def import_from_csv(file_path:str):
df = pd.read_csv(file_path)
for i in range(0,len(df.index)):
#create site instance
if df.iat[i,1] is np.nan:
#print("skip line: nan")
continue
site, created = models.Site.objects.get_or_create(name=df.iat[i,1],
coredb_id=df.iat[i,2],
country=df.iat[i,3])

if created or not site.profile.exists() or site.profile.name == "":
profile = models.Profile.objects.create(name="Main " + site.name, site=site)
profile.save()
print("created new profile")

#create layers
layer = models.Layer.objects.filter(name=df.iat[i,5], site=site)
if not layer.exists():
layer = models.Layer.objects.create(name=df.iat[i,5],
#description=df.iat[i,16], #sample location
site=site,
pos=len(site.layer.all()))
layer.pos = len(site.layer.all())
layer.save()
layer.profile.set(models.Profile.objects.filter(site=layer.site))
print("created layer " + str(layer.name) + " at " + site.name)
else:
layer = layer[0]