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

Behav merge #193

Merged
merged 7 commits into from
Nov 14, 2023
Merged
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
17 changes: 17 additions & 0 deletions data/behav.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
behav,behav_cat
attacked,behavBird
with_prey,behavBird
klepto,behavBird
diving,behavBird
follow_boat,behavBird
random_flight,behavBird
circular_flight,behavBird
straight_flight,behavBird
bow,behavMam
milling,behavMam
fast_swimming,behavMam
slow_swimming,behavMam
diving,behavMam
breaching,behavMam
fishing,behavShip
route,behavShip
465 changes: 465 additions & 0 deletions data/species.csv

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion sammo.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,13 @@ def onGpsFrame(
# we udpate the database if we don't need to wait for speed/course
if not self.gps_wait:
self.session.addGps(
longitude, latitude, h, m, s, speed, course
longitude,
latitude,
h,
m,
s,
self.session.lastGpsInfo["gprmc"]["speed"],
self.session.lastGpsInfo["gprmc"]["course"],
)
self.session.lastCaptureTime = gpsNow

Expand Down
55 changes: 46 additions & 9 deletions src/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

GPS_TABLE = "gps"
SPECIES_TABLE = "species"
BEHAVIOUR_SPECIES_TABLE = "behaviour"
OBSERVERS_TABLE = "observers"
FOLLOWERS_TABLE = "followers"
SIGHTINGS_TABLE = "sightings"
Expand Down Expand Up @@ -70,7 +71,6 @@ def init(self, directory: str) -> bool:
ENVIRONMENT_TABLE,
QgsWkbTypes.Point,
)
self._createTable(self._createFieldsForSpeciesTable(), SPECIES_TABLE)
self._createTable(
self._fieldsSightings(), SIGHTINGS_TABLE, QgsWkbTypes.Point
)
Expand All @@ -86,8 +86,15 @@ def init(self, directory: str) -> bool:
self._createTable(self._fieldsObserver(), OBSERVERS_TABLE)

# administrator table
self._createTable(self._createFieldsForSpeciesTable(), SPECIES_TABLE)
self._populateTable(SPECIES_TABLE, "species.csv")
self._createTable(
self._createFieldsForBehaviourSpeciesTable(),
BEHAVIOUR_SPECIES_TABLE,
)
self._populateTable(BEHAVIOUR_SPECIES_TABLE, "behav.csv")
self._createTable(self._fieldsBoat(), BOAT_TABLE)
self._populateBoatTable()
self._populateTable(BOAT_TABLE, "boat.csv")
self._createTable(self._fieldsSurvey(), SURVEY_TABLE)
self._createTable(self._fieldsTransect(), TRANSECT_TABLE)
self._createTable(self._fieldsStrate(), STRATE_TABLE)
Expand Down Expand Up @@ -171,10 +178,25 @@ def _createFieldsForEnvironmentTable(self) -> QgsFields:
def _createFieldsForSpeciesTable(self) -> QgsFields:
fields = QgsFields()
fields.append(self._createFieldShortText("species"))
fields.append(self._createFieldShortText("commonName"))
fields.append(self._createFieldShortText("latinName"))
fields.append(self._createFieldShortText("groupName"))
fields.append(self._createFieldShortText("family"))
fields.append(self._createFieldShortText("behav_cat"))
fields.append(self._createFieldShortText("name_latin"))
fields.append(self._createFieldShortText("taxon_eng"))
fields.append(self._createFieldShortText("family_eng"))
fields.append(self._createFieldShortText("group_eng"))
fields.append(self._createFieldShortText("name_eng"))
fields.append(self._createFieldShortText("taxon_fr"))
fields.append(self._createFieldShortText("family_fr"))
fields.append(self._createFieldShortText("group_fr"))
fields.append(self._createFieldShortText("name_fr"))
fields.append(self._createFieldShortText("taxon_spa"))
fields.append(self._createFieldShortText("family_spa"))
fields.append(self._createFieldShortText("group_spa"))
fields.append(self._createFieldShortText("name_spa"))
return fields

def _createFieldsForBehaviourSpeciesTable(self) -> QgsFields:
fields = QgsFields()
fields.append(self._createFieldShortText("behav"))
fields.append(self._createFieldShortText("taxon"))
return fields

Expand All @@ -193,9 +215,7 @@ def _fieldsSightings(self) -> QgsFields:
fields.append(QgsField("angle", QVariant.Int))
fields.append(QgsField("direction", QVariant.Int))
fields.append(self._createFieldShortText("behaviour"))
fields.append(self._createFieldShortText("behavBird"))
fields.append(self._createFieldShortText("behavMam"))
fields.append(self._createFieldShortText("behavShip"))
fields.append(self._createFieldShortText("behavSpecies"))
fields.append(self._createFieldShortText("behavGroup"))
fields.append(QgsField("comment", QVariant.String, len=200))
fields.append(self._createFieldShortText("soundFile", len=80))
Expand Down Expand Up @@ -259,6 +279,23 @@ def _fieldsBoat(self) -> QgsFields:
fields.append(self._createFieldShortText("name"))
return fields

def _populateTable(self, layer_id: str, csv_name: str) -> None:
lyr = QgsVectorLayer(self.tableUri(layer_id), "no_matter", "ogr")
file = Path(__file__).parent.parent.parent / "data" / csv_name
lines = []
if file.exists():
with open(file.as_posix()) as f:
lines = [
{k: v for k, v in row.items()} for row in csv.DictReader(f)
]
lyr.startEditing()
for attr in lines:
ft = QgsFeature(lyr.fields())
for k, v in attr.items():
ft[k] = v
lyr.addFeature(ft)
lyr.commitChanges()

def _populateBoatTable(self) -> None:
boatLyr = QgsVectorLayer(self.tableUri(BOAT_TABLE), "boat", "ogr")
file = Path(__file__).parent.parent.parent / "data" / "boat.csv"
Expand Down
1 change: 1 addition & 0 deletions src/core/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
from .observers import SammoObserversLayer
from .sightings import SammoSightingsLayer
from .plateform import SammoPlateformLayer
from .behav import SammoBehaviourSpeciesLayer
from .environment import SammoEnvironmentLayer
23 changes: 23 additions & 0 deletions src/core/layers/behav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf8

__contact__ = "[email protected]"
__copyright__ = "Copyright (c) 2023 Hytech Imaging"

from qgis.core import QgsVectorLayer
from ..database import (
SammoDataBase,
BEHAVIOUR_SPECIES_TABLE,
)

from .layer import SammoLayer


class SammoBehaviourSpeciesLayer(SammoLayer):
def __init__(self, db: SammoDataBase):
super().__init__(db, BEHAVIOUR_SPECIES_TABLE, "Behaviour_species")

def _init(self, layer: QgsVectorLayer) -> None:
self._init_widgets(layer)

def _init_widgets(self, layer: QgsVectorLayer) -> None:
pass
126 changes: 54 additions & 72 deletions src/core/layers/sightings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@
)

from .layer import SammoLayer, NULL
from .behav import SammoBehaviourSpeciesLayer


class SammoSightingsLayer(SammoLayer):
def __init__(self, db: SammoDataBase):
def __init__(
self,
db: SammoDataBase,
behaviourSpeciesLayer: SammoBehaviourSpeciesLayer,
):
super().__init__(
db,
SIGHTINGS_TABLE,
"Sightings",
soundAction=True,
duplicateAction=True,
)
self.behaviourSpeciesLayer = behaviourSpeciesLayer

def _init(self, layer: QgsVectorLayer) -> None:
self._init_symbology(layer)
Expand Down Expand Up @@ -192,51 +198,29 @@ def _init_widgets(self, layer: QgsVectorLayer) -> None:
layer.setEditorWidgetSetup(idx, setup)
layer.setFieldAlias(idx, "group")

# behavMam
idx = layer.fields().indexFromName("behavMam")
cfg = {}
cfg["map"] = [
{"<NULL>": NULL},
{"bow": "bow"},
{"milling": "milling"},
{"fast_swimming": "fast_swimming"},
{"slow_swimming": "slow_swimming"},
{"diving": "diving"},
{"breaching": "breaching"},
]
setup = QgsEditorWidgetSetup("ValueMap", cfg)
layer.setEditorWidgetSetup(idx, setup)
layer.setFieldAlias(idx, "mam")

# behavBird
idx = layer.fields().indexFromName("behavBird")
cfg = {}
cfg["map"] = [
{"<NULL>": NULL},
{"attacked": "attacked"},
{"with_prey": "with_prey"},
{"klepto": "klepto"},
{"diving": "diving"},
{"follow_boat": "follow_boat"},
{"random_flight": "random_flight"},
{"circular_flight": "circular_flight"},
{"straight_flight": "straight_flight"},
]
setup = QgsEditorWidgetSetup("ValueMap", cfg)
layer.setEditorWidgetSetup(idx, setup)
layer.setFieldAlias(idx, "bird")

# behavShip
idx = layer.fields().indexFromName("behavShip")
cfg = {}
cfg["map"] = [
{"<NULL>": NULL},
{"fishing": "fishing"},
{"route": "route"},
]
setup = QgsEditorWidgetSetup("ValueMap", cfg)
# behavSpecies
idx = layer.fields().indexFromName("behavSpecies")
cfg = {
"AllowMulti": False,
"AllowNull": True,
"Description": "",
"FilterExpression": (
'"behav_cat" = attribute(get_feature('
"layer_property('Species', 'id')"
",'species',current_value('species')),'behav_cat')"
),
"Key": "behav",
"Layer": self.behaviourSpeciesLayer.layer.id(),
"LayerName": self.behaviourSpeciesLayer.name,
"LayerProviderName": "ogr",
"LayerSource": self.behaviourSpeciesLayer.uri,
"NofColumns": 1,
"OrderByValue": False,
"UseCompleter": False,
"Value": "behav",
}
setup = QgsEditorWidgetSetup("ValueRelation", cfg)
layer.setEditorWidgetSetup(idx, setup)
layer.setFieldAlias(idx, "ship")

# soundFile, soundStart, soundEnd, dateTime
for field in [
Expand Down Expand Up @@ -329,44 +313,42 @@ def _init_conditional_style(self, layer: QgsVectorLayer) -> None:
'species',
attribute('species')
)
, 'taxon'
, 'behav_cat'
)
),
@value is NULL,
{}
)
"""
addExpr = """
(
if( "behavMam" ,1,0) +
if( "behavBird" ,1,0) +
if( "behavShip" ,1,0)
> 1
behavs = (
"'"
+ "','".join(
self.behaviourSpeciesLayer.layer.uniqueValues(
self.behaviourSpeciesLayer.layer.fields().indexOf(
"behav_cat"
)
)
)
"""

taxons = "'Marine Mammal', 'Seabird', 'Ship'"
style = QgsConditionalStyle(expr.format(taxons, "False"))
+ "'"
)
style = QgsConditionalStyle(expr.format(behavs, "False"))
style.setBackgroundColor(QColor("orange"))
layer.conditionalStyles().setFieldStyles("behaviour", [style])

# behavMam
taxon = "'Marine Mammal'"
style = QgsConditionalStyle(expr.format(taxon, addExpr))
style.setBackgroundColor(QColor("orange"))
layer.conditionalStyles().setFieldStyles("behavMam", [style])

# behavBird
taxon = "'Seabird'"
style = QgsConditionalStyle(expr.format(taxon, addExpr))
style.setBackgroundColor(QColor("orange"))
layer.conditionalStyles().setFieldStyles("behavBird", [style])

# behavShip
taxon = "'Ship'"
style = QgsConditionalStyle(expr.format(taxon, addExpr))
# behavSpecies
expr = """
attribute(get_feature(
layer_property('Species', 'id')
,'species',"species"
),'behav_cat')
!= attribute(get_feature(
layer_property('Behaviour_species', 'id')
,'behav',"behavSpecies"
),'behav_cat')
"""
style = QgsConditionalStyle(expr)
style.setBackgroundColor(QColor("orange"))
layer.conditionalStyles().setFieldStyles("behavShip", [style])
layer.conditionalStyles().setFieldStyles("behavSpecies", [style])

# species
expr = """
Expand Down
Loading