Example #1
0
 def getLayer(self,
              user,
              repo,
              layer,
              refspec,
              extent=None,
              screenWidth=None,
              screenHeight=None,
              limit=None,
              simplifyGeom=True,
              filepath=None):
     user = cleanseUserName(user)
     repo = cleanseRepoName(repo)
     layer = cleanseLayerName(layer)
     refspec = cleanseRefSpec(refspec)
     query = QueryForLayerProto(self.connector)
     QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
     if limit != 1:
         startProgressBar("Transferring data from GeoGig", 0,
                          currentWindow().messageBar())
         setProgressValue(0)
     result = query.query(user,
                          repo,
                          layer,
                          refspec,
                          extent,
                          screenWidth,
                          screenHeight,
                          limit,
                          simplifyGeom=simplifyGeom,
                          filepath=filepath)
     if limit != 1:
         closeProgressBar()
     QApplication.restoreOverrideCursor()
     return result
Example #2
0
    def run(self):
        self.abort(
        )  # this shouldn't do anything since its running on the same thread
        self.queryForLayerProto = QueryForLayerProto(self.connector,
                                                     self.progressMade)

        if self.url is None:
            raise Exception("QueryThread called without url set")
        if self.query is None:
            raise Exception("QueryThread called without query set")
        try:
            self.started.emit(self.url,
                              self.query)  # alert that a data load has started
            self.nFeaturesReported = 0
            result = self.queryForLayerProto.querySimple(
                self.url, self.query, wrapInMemoryStore=False)
            cancelled = self.queryForLayerProto.isCancelled
            self.queryForLayerProto = None
            if cancelled:
                self.finished.emit(None)
            else:
                self.finished.emit(result)
        except CancelledException:  # not much to do here...
            self.queryForLayerProto = None
            self.finished.emit(None)
        except Exception:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type,
                                      exc_value,
                                      exc_traceback,
                                      limit=200,
                                      file=sys.stdout)
            self.queryForLayerProto = None
            self.finished.emit(None)
    def loadChanges(self):
        changesFile = self._changesFile()
        if os.path.exists(changesFile):
            _ft = None
            with open(changesFile, "rb") as f:
                query = QueryForLayerProto(None)
                ndeletes = struct.unpack('i', f.read(4))[0]
                deletedFeatureGigIDs = []
                for i in range(0, ndeletes):
                    len = struct.unpack('i', f.read(4))[0]
                    data = f.read(len)
                    id = data.decode()
                    deletedFeatureGigIDs.append(id)
                nModified = struct.unpack('i', f.read(4))[0]
                modifiedFeatures = []
                if nModified > 0:
                    _ft, modifiedFeatures = query.readFromStream(f)
                    modifiedFeatures = list(modifiedFeatures)
                nAdded = struct.unpack('i', f.read(4))[0]
                addedFeatures = []
                if nAdded > 0:
                    _ft, addedFeatures = query.readFromStream(f)
                addedFeatures = list(addedFeatures)

            if _ft is not None:
                fields = _ft.getFields()

            #process
            self.deletedFeatures = deletedFeatureGigIDs
            deletedFeats = [
                self.getFeatureFromGeogigId(gid)
                for gid in self.deletedFeatures
            ]
            deletedIds = [f.id() for f in deletedFeats if f is not None]
            self.layer.deleteFeatures(deletedIds)

            self.modifiedFeatures = []
            for feature in modifiedFeatures:
                feature.setFields(fields, initAttributes=False)
                previousFeature = self.getFeatureFromGeogigId(
                    feature[GEOGIGID_FIELD])
                if previousFeature is not None:
                    self.layer.dataProvider().deleteFeatures(
                        [previousFeature.id()])
                self.layer.dataProvider().addFeatures([feature])
                self.modifiedFeatures.append(
                    self.getFeatureFromGeogigId(feature[GEOGIGID_FIELD]).id())

            self.addedFeatures = []
            for feature in addedFeatures:
                feature.setFields(fields, initAttributes=False)
                self.layer.dataProvider().addFeatures([feature])
                self.addedFeatures.append(
                    self.getFeatureFromGeogigId(feature[GEOGIGID_FIELD]).id())

            self.layer.triggerRepaint()
Example #4
0
 def feature(self, user, repo, layer, fid, refspec):
     user = cleanseUserName(user)
     repo = cleanseRepoName(repo)
     layer = cleanseLayerName(layer)
     refspec = cleanseRefSpec(refspec)
     queryForLayerProto = QueryForLayerProto(self.connector)
     query = QueryForLayerProto.createQueryFIDs(refspec, [fid])
     url = QueryForLayerProto.createURL(self.connector, user, repo, layer)
     memLayer = queryForLayerProto.querySimple(url, query)
     return list(memLayer.getFeatures())[0]
Example #5
0
 def _diffPR(self, user, repo, layerName, prID):
     user = cleanseUserName(user)
     repo = cleanseRepoName(repo)
     prID = cleansePRId(prID)
     layerName = cleanseLayerName(layerName)
     q = QueryForLayerProto(self.connector)
     try:
         return q.queryDiffPR(user, repo, layerName, prID)
     except HTTPError as ee:
         raise
     except Exception as e:
         raise GeogigError("Error getting diff from server", str(e))
Example #6
0
    def diff(self,
             user,
             repo,
             layer,
             refspec,
             oldRef,
             featureFilter=None,
             returnAsIterator=False,
             oldRefUser=None,
             oldRefRepoName=None):
        user = cleanseUserName(user)
        repo = cleanseRepoName(repo)
        layer = cleanseLayerName(layer)
        refspec = cleanseRefSpec(refspec)
        oldRef = cleanseRefSpec(oldRef)
        q = QueryForLayerProto(self.connector)
        try:
            return q.queryDiff(user,
                               repo,
                               layer,
                               refspec,
                               oldRef,
                               featureFilter,
                               returnAsIterator=returnAsIterator,
                               oldRefUser=oldRefUser,
                               oldRefRepoName=oldRefRepoName)
        except:
            try:
                #might be a deleted layer. We try to compute the diff in reversed order of commits
                if oldRefRepoName is None:
                    diff = q.queryDiff(user, repo, layer, oldRef, refspec,
                                       featureFilter)
                else:
                    diff = q.queryDiff(oldRefUser,
                                       oldRefRepoName,
                                       layer,
                                       oldRef,
                                       refspec,
                                       featureFilter,
                                       oldRefUser=user,
                                       oldRefRepoName=repo)

                def invert(d):
                    d['geogig.changeType'] = 2
                    d['old'] = d['new']
                    d['new'] = None
                    return d

                inverted = [invert(d) for d in diff]
                return inverted
            except Exception as e:
                raise GeogigError("Error getting diff from server", str(e))
Example #7
0
 def syncBranch(self,
                user,
                repo,
                parentUser,
                parentRepo,
                transactionid,
                branch="master",
                parentBranch="master",
                commitMessage=""):
     user = cleanseUserName(user)
     repo = cleanseRepoName(repo)
     parentUser = cleanseUserName(parentUser)
     parentRepo = cleanseRepoName(parentRepo)
     branch = cleanseBranchName(branch)
     parentBranch = cleanseBranchName(parentBranch)
     transactionid = cleanseTransactionId(transactionid)
     headers = {"geogig-transaction-id": transactionid}
     pullArgs = {
         "remote_repo_owner": parentUser,
         "remote_repo_name": parentRepo,
         "remote_repo_head": parentBranch,
         "commit_message": commitMessage
     }
     ret = self.connector.post("repos/{}/{}/branches/{}/sync".format(
         user, repo, branch),
                               json=pullArgs,
                               headers=headers)
     taskId = ret["id"]
     ret = self.waitForTask(taskId, "Processing changes in server")
     layers = self.layers(user, repo)
     q = QueryForLayerProto(self.connector)
     result = {}
     conflictsFound = False
     for layerName in layers:
         try:
             conflicts = q.queryConflict(user, repo, layerName,
                                         transactionid)
             conflicts2 = {}
             if conflicts:
                 conflictsFound = True
             for conflict in conflicts:
                 conflicts2[conflict["ID"]] = {}
                 conflicts2[conflict["ID"]]["origin"] = conflict["ancestor"]
                 conflicts2[conflict["ID"]]["local"] = conflict["theirs"]
                 conflicts2[conflict["ID"]]["remote"] = conflict["ours"]
             result[layerName] = conflicts2
         except Exception as e:
             raise GeogigError("Error getting diff from server", str(e))
     if conflictsFound:
         return result
     return []
 def start(self):
     preExists = os.path.exists(self.filepath)
     query = QueryForLayerProto(self.server.connector, self.progressMade)
     query.query(self.user,
                 self.repo,
                 self.layername,
                 self.commitid,
                 filepath=self.filepath,
                 extent=self.extent)
     if not preExists:
         self.actualLayerName = getDataTableName(self.filepath)
     else:
         self.actualLayerName = self.layername
     self.completed.emit()
    def getLocalChangeset(self):
        deleted_geogigids = self.deletedFeatures
        modified_geogigids = [
            f[GEOGIGID_FIELD] for f in self.layer.getFeatures(
                QgsFeatureRequest(self.modifiedFeatures))
        ]

        geogigids = deleted_geogigids + modified_geogigids

        queryForLayerProto = QueryForLayerProto(self.server.connector)
        query = QueryForLayerProto.createQueryFIDs(self.commitid, geogigids)
        url = QueryForLayerProto.createURL(self.server.connector, self.user,
                                           self.repo, self.layername)
        memLayer = queryForLayerProto.querySimple(url, query)

        result = []
        for fid in self.addedFeatures:
            f = list(self.layer.getFeatures(QgsFeatureRequest([fid])))[0]
            item = {
                'ID': f[GEOGIGID_FIELD],
                'geogig.changeType': 0,
                'old': None,
                'new': f
            }
            result.append(item)

        for gigid in deleted_geogigids:
            f = getFeatureFromGeogigId(gigid, memLayer)
            item = {
                'ID': f[GEOGIGID_FIELD],
                'geogig.changeType': 2,
                'old': f,
                'new': None
            }
            result.append(item)

        for gigid in modified_geogigids:
            f = getFeatureFromGeogigId(gigid, memLayer)
            ff = self.getFeatureFromGeogigId(gigid)
            item = {
                'ID': f[GEOGIGID_FIELD],
                'geogig.changeType': 1,
                'old': f,
                'new': ff
            }
            result.append(item)

        return result
Example #10
0
 def createQuery(self,
                 refspec,
                 extent=None,
                 screenWidth=None,
                 screenHeight=None,
                 limit=None,
                 simplifyGeom=True,
                 screenMap_type="WithBBOX",
                 screenMap_factor=1.0,
                 ecqlFilter=None):
     self.query = QueryForLayerProto.createQuery(
         refspec,
         extent,
         screenWidth,
         screenHeight,
         limit,
         simplifyGeom,
         screenMap_type=screenMap_type,
         screenMap_factor=screenMap_factor,
         ecqlFilter=ecqlFilter)
Example #11
0
class QueryThread(QThread):

    started = pyqtSignal(
        str, dict)  # always paired with a finished (params=url,query)
    finished = pyqtSignal('PyQt_PyObject')
    progress_occurred = pyqtSignal('int')

    def __init__(self, connector):
        QThread.__init__(self)
        self.connector = connector
        self.url = None
        self.query = None
        self.queryForLayerProto = None
        self.nFeaturesReported = 0

    def __del__(self):
        self.wait()

    def abort(self):
        if self.queryForLayerProto is not None:
            self.queryForLayerProto.isCancelled = True

    def createURL(self, user, repo, layer):
        self.url = QueryForLayerProto.createURL(self.connector, user, repo,
                                                layer)

    def createQuery(self,
                    refspec,
                    extent=None,
                    screenWidth=None,
                    screenHeight=None,
                    limit=None,
                    simplifyGeom=True,
                    screenMap_type="WithBBOX",
                    screenMap_factor=1.0,
                    ecqlFilter=None):
        self.query = QueryForLayerProto.createQuery(
            refspec,
            extent,
            screenWidth,
            screenHeight,
            limit,
            simplifyGeom,
            screenMap_type=screenMap_type,
            screenMap_factor=screenMap_factor,
            ecqlFilter=ecqlFilter)

    def progressMade(self, nfeats):
        nreadBatch = nfeats - self.nFeaturesReported
        self.nFeaturesReported = nfeats
        self.progress_occurred.emit(nreadBatch)

    def run(self):
        self.abort(
        )  # this shouldn't do anything since its running on the same thread
        self.queryForLayerProto = QueryForLayerProto(self.connector,
                                                     self.progressMade)

        if self.url is None:
            raise Exception("QueryThread called without url set")
        if self.query is None:
            raise Exception("QueryThread called without query set")
        try:
            self.started.emit(self.url,
                              self.query)  # alert that a data load has started
            self.nFeaturesReported = 0
            result = self.queryForLayerProto.querySimple(
                self.url, self.query, wrapInMemoryStore=False)
            cancelled = self.queryForLayerProto.isCancelled
            self.queryForLayerProto = None
            if cancelled:
                self.finished.emit(None)
            else:
                self.finished.emit(result)
        except CancelledException:  # not much to do here...
            self.queryForLayerProto = None
            self.finished.emit(None)
        except Exception:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type,
                                      exc_value,
                                      exc_traceback,
                                      limit=200,
                                      file=sys.stdout)
            self.queryForLayerProto = None
            self.finished.emit(None)
Example #12
0
 def createURL(self, user, repo, layer):
     self.url = QueryForLayerProto.createURL(self.connector, user, repo,
                                             layer)