Skip to content

Commit

Permalink
Merge pull request #194 from hytechimaging/ship_management
Browse files Browse the repository at this point in the history
Ship management
  • Loading branch information
SebastienPeillet authored Nov 16, 2023
2 parents 62e1582 + 8eea27b commit fff0911
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 156 deletions.
10 changes: 10 additions & 0 deletions data/survey_type.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name
PELGAS
PELACUS_springtime
IBTS
PELACUS_automn
CAMANOC
JUVENA
CGFS
EVHOE
MOOSE
43 changes: 24 additions & 19 deletions src/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
QgsFeature,
QgsProject,
QgsWkbTypes,
QgsGeometry,
QgsApplication,
QgsFeatureSink,
QgsVectorLayer,
Expand All @@ -37,8 +38,8 @@
WORLD_TABLE = "world"
BOAT_TABLE = "boat"
SURVEY_TABLE = "survey"
SURVEY_TYPE_TABLE = "survey_type"
TRANSECT_TABLE = "transect"
STRATE_TABLE = "strate"
PLATEFORM_TABLE = "plateform"


Expand Down Expand Up @@ -95,12 +96,14 @@ def init(self, directory: str) -> bool:
self._populateTable(BEHAVIOUR_SPECIES_TABLE, "behav.csv")
self._createTable(self._fieldsBoat(), BOAT_TABLE)
self._populateTable(BOAT_TABLE, "boat.csv")
self._createTable(self._fieldsSurveyType(), SURVEY_TYPE_TABLE)
self._populateTable(SURVEY_TYPE_TABLE, "survey_type.csv")
self._createTable(self._fieldsSurvey(), SURVEY_TABLE)
self._populateTable(SURVEY_TABLE, "survey.csv")
self._createTable(self._fieldsTransect(), TRANSECT_TABLE)
self._populateTable(TRANSECT_TABLE, "transect.csv")
self._createTable(self._fieldsStrate(), STRATE_TABLE)
self._populateTable(STRATE_TABLE, "strate.csv")
self._createTable(
self._fieldsTransect(), TRANSECT_TABLE, QgsWkbTypes.LineString
)
self._populateTable(TRANSECT_TABLE, "transect.csv", ";")
self._createTable(self._fieldsPlateform(), PLATEFORM_TABLE)
self._populatePlateformTable()

Expand Down Expand Up @@ -144,6 +147,7 @@ def _createFieldsForEnvironmentTable(self) -> QgsFields:
fields.append(self._createFieldShortText("right"))
fields.append(QgsField("dateTime", QVariant.DateTime))
fields.append(self._createFieldShortText("plateformId"))
fields.append(self._createFieldShortText("transectId"))
fields.append(self._createFieldShortText("routeType"))
fields.append(QgsField("speed", QVariant.Int))
fields.append(QgsField("courseAverage", QVariant.Int))
Expand All @@ -163,9 +167,6 @@ def _createFieldsForEnvironmentTable(self) -> QgsFields:
fields.append(self._createFieldShortText("camera"))
fields.append(QgsField("comment", QVariant.String, len=200))
fields.append(self._createFieldShortText("center"))
fields.append(self._createFieldShortText("transect"))
fields.append(self._createFieldShortText("strate"))
fields.append(self._createFieldShortText("length"))
fields.append(self._createFieldShortText("status", len=5))

fields.append(self._createFieldShortText("soundFile", len=80))
Expand Down Expand Up @@ -281,20 +282,26 @@ def _fieldsBoat(self) -> QgsFields:
fields.append(self._createFieldShortText("name"))
return fields

def _populateTable(self, layer_id: str, csv_name: str) -> None:
def _populateTable(
self, layer_id: str, csv_name: str, delimiter=","
) -> 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)
{k: v for k, v in row.items()}
for row in csv.DictReader(f, delimiter=delimiter)
]
lyr.startEditing()
for attr in lines:
ft = QgsFeature(lyr.fields())
for k, v in attr.items():
if lyr.fields()[k].typeName() == "Integer":
if k == "wkt":
geom = QgsGeometry.fromWkt(v)
ft.setGeometry(geom)
elif lyr.fields()[k].typeName() == "Integer":
ft[k] = int(v)
elif lyr.fields()[k].typeName() == "Real":
ft[k] = float(v)
Expand All @@ -303,6 +310,11 @@ def _populateTable(self, layer_id: str, csv_name: str) -> None:
lyr.addFeature(ft)
lyr.commitChanges()

def _fieldsSurveyType(self) -> QgsFields:
fields = QgsFields()
fields.append(self._createFieldShortText("name"))
return fields

def _populateBoatTable(self) -> None:
boatLyr = QgsVectorLayer(self.tableUri(BOAT_TABLE), "boat", "ogr")
file = Path(__file__).parent.parent.parent / "data" / "boat.csv"
Expand Down Expand Up @@ -335,15 +347,8 @@ def _fieldsTransect(self) -> QgsFields:
fields = QgsFields()
fields.append(self._createFieldShortText("transect"))
fields.append(self._createFieldShortText("strate"))
fields.append(QgsField("length", QVariant.Int))

return fields

def _fieldsStrate(self) -> QgsFields:
fields = QgsFields()
fields.append(self._createFieldShortText("strate"))
fields.append(self._createFieldShortText("subRegion"))
fields.append(self._createFieldShortText("region"))
fields.append(QgsField("length", QVariant.Int))

return fields

Expand Down
2 changes: 1 addition & 1 deletion src/core/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from .boat import SammoBoatLayer
from .world import SammoWorldLayer
from .survey import SammoSurveyLayer
from .strate import SammoStrateLayer
from .species import SammoSpeciesLayer
from .transect import SammoTransectLayer
from .followers import SammoFollowersLayer
from .observers import SammoObserversLayer
from .sightings import SammoSightingsLayer
from .plateform import SammoPlateformLayer
from .behav import SammoBehaviourSpeciesLayer
from .survey_type import SammoSurveyTypeLayer
from .environment import SammoEnvironmentLayer
26 changes: 26 additions & 0 deletions src/core/layers/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from .layer import SammoLayer
from .transect import SammoTransectLayer
from .plateform import SammoPlateformLayer
from .observers import SammoObserversLayer

Expand All @@ -29,6 +30,7 @@ def __init__(
db: SammoDataBase,
observersLayer: SammoObserversLayer,
plateformLayer: SammoPlateformLayer,
transectLayer: SammoTransectLayer,
):
super().__init__(
db,
Expand All @@ -39,6 +41,7 @@ def __init__(
)
self.observersLayer = observersLayer
self.plateformLayer = plateformLayer
self.transectLayer = transectLayer

def _init(self, layer: QgsVectorLayer):
self._init_symbology(layer)
Expand Down Expand Up @@ -93,6 +96,29 @@ def _init_widgets(self, layer: QgsVectorLayer) -> None:
layer.setEditFormConfig(form_config)
layer.setFieldAlias(idx, "plateform")

# transect
idx = layer.fields().indexFromName("transectId")
cfg = {
"AllowMulti": False,
"AllowNull": True,
"Description": "transect",
"Key": "fid",
"Layer": self.transectLayer.layer.id(),
"LayerName": self.transectLayer.name,
"LayerProviderName": "ogr",
"LayerSource": self.transectLayer.uri,
"NofColumns": 1,
"OrderByValue": False,
"UseCompleter": False,
"Value": "transect",
}
setup = QgsEditorWidgetSetup("ValueRelation", cfg)
layer.setEditorWidgetSetup(idx, setup)
form_config = layer.editFormConfig()
form_config.setReadOnly(idx, False)
layer.setEditFormConfig(form_config)
layer.setFieldAlias(idx, "transect")

# route type
idx = layer.fields().indexFromName("routeType")
cfg = {}
Expand Down
34 changes: 23 additions & 11 deletions src/core/layers/plateform.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,14 @@ def _init(self, layer: QgsVectorLayer) -> None:
self._init_widgets(layer)

def _init_widgets(self, layer: QgsVectorLayer) -> None:
# shipName
idx = layer.fields().indexFromName("ship")
cfg = {}
cfg["map"] = [
{"Thalassa": "Thalassa"},
{"Europe": "Europe"},
{"PourquoiPas": "PourquoiPas"},
]
setup = QgsEditorWidgetSetup("ValueMap", cfg)
layer.setEditorWidgetSetup(idx, setup)

# plateform
idx = layer.fields().indexFromName("plateform")
cfg = {}
cfg["map"] = [
{"bridge": "bridge"},
{"bridge_inside": "bridge_inside"},
{"bridge_outside": "bridge_outside"},
{"upper_bridge": "upper_bridge"},
{"upper_bridge_outside": "upper_bridge_outside"},
{"upper_bridge_inside": "upper_bridge_inside"},
{"deck": "deck"},
Expand All @@ -56,3 +47,24 @@ def _init_widgets(self, layer: QgsVectorLayer) -> None:
}
setup = QgsEditorWidgetSetup("Range", cfg)
layer.setEditorWidgetSetup(idx, setup)

def _link_boat(self, boatLayer: SammoLayer):
# shipName
idx = self.layer.fields().indexFromName("ship")
cfg = {
"AllowMulti": False,
"AllowNull": False,
"Description": '"ship"',
"FilterExpression": "",
"Key": "name",
"Layer": boatLayer.layer.id(),
"LayerName": boatLayer.name,
"LayerProviderName": "ogr",
"LayerSource": boatLayer.uri,
"NofColumns": 1,
"OrderByValue": False,
"UseCompleter": False,
"Value": "name",
}
setup = QgsEditorWidgetSetup("ValueRelation", cfg)
self.layer.setEditorWidgetSetup(idx, setup)
44 changes: 0 additions & 44 deletions src/core/layers/strate.py

This file was deleted.

38 changes: 24 additions & 14 deletions src/core/layers/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
)
from .layer import SammoLayer
from .boat import SammoBoatLayer
from .survey_type import SammoSurveyTypeLayer


class SammoSurveyLayer(SammoLayer):
def __init__(self, db: SammoDataBase, boatLayer: SammoBoatLayer):
def __init__(
self,
db: SammoDataBase,
boatLayer: SammoBoatLayer,
surveyTypeLayer: SammoSurveyTypeLayer,
):
super().__init__(db, SURVEY_TABLE, "Survey")
self.boatLayer = boatLayer
self.surveyTypeLayer = surveyTypeLayer

def _init(self, layer: QgsVectorLayer) -> None:
self._init_widgets(layer)
Expand All @@ -36,19 +43,22 @@ def _init_widgets(self, layer: QgsVectorLayer) -> None:

# survey
idx = layer.fields().indexFromName("survey")
cfg = {}
cfg["map"] = [
{"PELGAS": "PELGAS"},
{"PELACUS_springtime": "PELACUS_springtime"},
{"IBTS": "IBTS"},
{"PELACUS_automn": "PELACUS_automn"},
{"CAMANOC": "CAMANOC"},
{"JUVENA": "JUVENA"},
{"CGFS": "CGFS"},
{"EVHOE": "EVHOE"},
{"MOOSE": "MOOSE"},
]
setup = QgsEditorWidgetSetup("ValueMap", cfg)
cfg = {
"AllowMulti": False,
"AllowNull": False,
"Description": '"survey"',
"FilterExpression": "",
"Key": "name",
"Layer": self.surveyTypeLayer.layer.id(),
"LayerName": self.surveyTypeLayer.name,
"LayerProviderName": "ogr",
"LayerSource": self.surveyTypeLayer.uri,
"NofColumns": 1,
"OrderByValue": False,
"UseCompleter": False,
"Value": "name",
}
setup = QgsEditorWidgetSetup("ValueRelation", cfg)
layer.setEditorWidgetSetup(idx, setup)

# shipName
Expand Down
23 changes: 23 additions & 0 deletions src/core/layers/survey_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf8

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

from qgis.core import QgsVectorLayer

from ..database import (
SURVEY_TYPE_TABLE,
SammoDataBase,
)
from .layer import SammoLayer


class SammoSurveyTypeLayer(SammoLayer):
def __init__(self, db: SammoDataBase):
super().__init__(db, SURVEY_TYPE_TABLE, "Survey_type")

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

def _init_widgets(self, layer: QgsVectorLayer) -> None:
pass
Loading

0 comments on commit fff0911

Please sign in to comment.