From c738651384a8639a40db051dc5a7b1ad46782fab Mon Sep 17 00:00:00 2001
From: "benjamin.jakimow" <benjamin.jakimow@geo.hu-berlin.de>
Date: Fri, 18 May 2018 14:51:24 +0200
Subject: [PATCH] wip TemporalProfile in-memory layer

---
 timeseriesviewer/mapcanvas.py            |  2 +
 timeseriesviewer/profilevisualization.py |  4 +-
 timeseriesviewer/temporalprofiles2d.py   | 60 ++++++++++++++++++++++--
 3 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/timeseriesviewer/mapcanvas.py b/timeseriesviewer/mapcanvas.py
index 8337d1bd..7767465f 100644
--- a/timeseriesviewer/mapcanvas.py
+++ b/timeseriesviewer/mapcanvas.py
@@ -1126,5 +1126,7 @@ if __name__ == '__main__':
     c.setExtent(lyr1.extent())
     c.setCrs(QgsCoordinateReferenceSystem('EPSG:32632'))
     c.setExtent(c.spatialExtentHint())
+
+
     c.refresh()
     qgsApp.exec_()
\ No newline at end of file
diff --git a/timeseriesviewer/profilevisualization.py b/timeseriesviewer/profilevisualization.py
index 2416b522..59c498a3 100644
--- a/timeseriesviewer/profilevisualization.py
+++ b/timeseriesviewer/profilevisualization.py
@@ -2221,7 +2221,9 @@ if __name__ == '__main__':
         STVis.createNewPlotStyle3D()
         STVis.ui.listWidget.setCurrentRow(1)
 
-        STVis.loadCoordinate(cpND)
+        STVis.loadCoordinate(cpND, backgroundProcess=False)
+
+        STVis.tpCollection.mTemporalProfiles[0].setName('My Name')
 
         from example import  exampleEvents
         #STVis.loadCoordinatesFromOgr(exampleEvents)
diff --git a/timeseriesviewer/temporalprofiles2d.py b/timeseriesviewer/temporalprofiles2d.py
index 2820ba0d..86a9b461 100644
--- a/timeseriesviewer/temporalprofiles2d.py
+++ b/timeseriesviewer/temporalprofiles2d.py
@@ -1131,10 +1131,16 @@ class TemporalProfileCollection(QAbstractTableModel):
         self.mColumNames = [self.mcnName, self.mcnLoaded, self.mcnCoordinate]
 
         crs = QgsCoordinateReferenceSystem('EPSG:4862')
-        uri = 'Point?crs={}'.format(crs.authid())
+        uri = 'Point?crs={}&field=id:integer&field=name:string(120)'.format(crs.authid())
+        self.mLocations = QgsVectorLayer(uri, 'LOCATIONS', 'memory')
+        symbol = QgsFillSymbol.createSimple({'style': 'no', 'color': 'red', 'outline_color': 'black'})
+        self.mLocations.renderer().setSymbol(symbol)
 
         self.TS = None
-        self.mLocations = QgsVectorLayer(uri, 'LOCATIONS', 'memory')
+
+
+
+
         self.mTemporalProfiles = []
         self.mTPLookupSpatialPoint = {}
         self.mTPLookupID = {}
@@ -1274,6 +1280,8 @@ class TemporalProfileCollection(QAbstractTableModel):
             self.prune(nMax=self.mMaxProfiles - l)
 
             self.beginInsertRows(QModelIndex(), i, i + l - 1)
+
+            features = []
             for temporalProfile in temporalProfiles:
                 assert isinstance(temporalProfile, TemporalProfile)
                 id = self.nextID
@@ -1282,9 +1290,21 @@ class TemporalProfileCollection(QAbstractTableModel):
                 self.mTemporalProfiles.insert(i, temporalProfile)
                 self.mTPLookupID[id] = temporalProfile
                 self.mTPLookupSpatialPoint[temporalProfile.mCoordinate] = temporalProfile
+
+                f = QgsFeature(self.mLocations.fields())
+                f.setFields(self.mLocations.fields())
+                f.setAttribute('id', QVariant(id))
+                f.setAttribute('name', QVariant(temporalProfile.name()))
+                f.setGeometry(QgsGeometry.fromPointXY(temporalProfile.coordinate()))
+                features.append(f)
+
+
                 temporalProfile.sigDataChanged.connect(lambda: self.onUpdate(temporalProfile))
-                temporalProfile.sigNameChanged.connect(lambda: self.onUpdate(temporalProfile))
+                temporalProfile.sigNameChanged.connect(lambda: self.onUpdateName(temporalProfile))
                 i += 1
+            self.mLocations.startEditing()
+            assert self.mLocations.dataProvider().addFeatures(features)
+            assert self.mLocations.commitChanges()
             self.endInsertRows()
 
             self.sigTemporalProfilesAdded.emit(temporalProfiles)
@@ -1302,6 +1322,19 @@ class TemporalProfileCollection(QAbstractTableModel):
         else:
             return None
 
+    def temporalProfileFromFeature(self, feature):
+
+        return None
+
+    def temporalProfileToLocationFeature(self, tp:TemporalProfile):
+
+        self.mLocations.selectByIds([tp.id()])
+        for f in self.mLocations.selectedFeatures():
+            assert isinstance(f, QgsFeature)
+            return f
+
+        return None
+
     def id(self, temporalProfile):
         """
         Returns the id of an TemporalProfile
@@ -1429,9 +1462,26 @@ class TemporalProfileCollection(QAbstractTableModel):
 
         if tp in self.mTemporalProfiles:
             idx0 = self.tp2idx(tp)
-            idx1 = self.createIndex(idx0.row(), self.rowCount())
+            idx1 = self.createIndex(idx0.row(), self.columnCount())
             self.dataChanged.emit(idx0, idx1, [Qt.DisplayRole])
 
+
+
+    def onUpdateName(self, tp):
+        assert isinstance(tp, TemporalProfile)
+
+        if tp in self.mTemporalProfiles:
+            idx0 = self.tp2idx(tp)
+            c = self.mColumNames.index(self.mcnName)
+            idx = self.createIndex(idx0.row(), c)
+            self.dataChanged.emit(idx, idx, [Qt.DisplayRole])
+
+            f = self.temporalProfileToLocationFeature(tp)
+
+            if isinstance(f, QgsFeature):
+                f.setAttribute('name', tp.name())
+
+
     def sort(self, col, order):
         if self.rowCount() == 0:
             return
@@ -1540,7 +1590,7 @@ if __name__ == '__main__':
     qgsApp = utils.initQgisApplication()
     DEBUG = False
 
-    w = TemporalProfilePlotStyle3DWidget()
+    w = TemporalProfile2DPlotStyle()
     w.show()
     print(w.plotStyle())
 
-- 
GitLab