def setTimeRestriction(self, timePosition, timeFrame):
        TimeVectorLayer.setTimeRestriction(self, timePosition, timeFrame)

        start_epoch = datetime_to_epoch(self.getStartTime(timePosition, timeFrame))
        end_epoch =  datetime_to_epoch(self.getEndTime(timePosition, timeFrame))

        #info("setTimeRestriction Called {} times".format(self.n))
        #info("size of layer at {}:{}".format(start_epoch,self.memLayer.featureCount(),))

        geoms = self.getInterpolatedGeometries(start_epoch, end_epoch)
        #Add the geometries as features
        self._clearMemoryLayer()

        self.previous_ids = set()

        features = []
        for i,geom in enumerate(geoms):

            feature = QgsFeature(id = start_epoch+i)
            feature.setGeometry(QgsGeometry.fromPoint(geom))
            #feature.setAttributes([start_epoch+i])
            features.append(feature) # if no attributes, it will fail
            self.previous_ids.add(feature.id())
            self.n = self.n + 1

        #info("add {}features:".format(len(features)))
        res = self.memLayer.dataProvider().addFeatures(features)
        assert(res)
        self.memLayer.triggerRepaint()
    def setTimeRestriction(self, timePosition, timeFrame):
        TimeVectorLayer.setTimeRestriction(self, timePosition, timeFrame)

        start_epoch = datetime_to_epoch(
            self.getStartTime(timePosition, timeFrame))
        end_epoch = datetime_to_epoch(self.getEndTime(timePosition, timeFrame))

        #info("setTimeRestriction Called {} times".format(self.n))
        #info("size of layer at {}:{}".format(start_epoch,self.memLayer.featureCount(),))
        geoms = self.getInterpolatedGeometries(start_epoch, end_epoch)
        #Add the geometries as features
        self._clearMemoryLayer()

        features = []
        for i, geom in enumerate(geoms):
            feature = QgsFeature(id=start_epoch + i)
            feature.setGeometry(QgsGeometry.fromPoint(geom))
            #feature.setAttributes([start_epoch+i])
            features.append(feature)  # if no attributes, it will fail
            self.n = self.n + 1

        #info("add {}features:".format(len(features)))
        res = self.memLayer.dataProvider().addFeatures(features)
        assert (res)
        self.memLayer.triggerRepaint()
Esempio n. 3
0
    def updateTimePositionFromSliderPct(self, pct):
        """See the percentage the slider is at and determine the datetime"""
        if not self.propagateGuiChanges:
            return
        timeExtents = self.getTimeLayerManager().getProjectTimeExtents()
        try:
            realEpochTime = int(pct *
                                (time_util.datetime_to_epoch(timeExtents[1]) -
                                 time_util.datetime_to_epoch(timeExtents[0])) +
                                time_util.datetime_to_epoch(timeExtents[0]))
        except:
            # extents are not set yet?
            return

        self.getTimeLayerManager().setCurrentTimePosition(
            time_util.epoch_to_datetime(realEpochTime))
Esempio n. 4
0
def build_query(start_dt, end_dt, from_attr, to_attr, date_type, date_format, query_idiom, acc):
    """Build subset query"""

    if acc:
        # features never die
        start_dt = time_util.get_min_dt()

    comparison = "<" if to_attr == from_attr else "<="

    if date_type == DateTypes.IntegerTimestamps:
        start_epoch = time_util.datetime_to_epoch(start_dt)
        end_epoch = time_util.datetime_to_epoch(end_dt)
        return INT_FORMAT.format(from_attr, comparison, end_epoch, to_attr,
                                 start_epoch)

    start_str = time_util.datetime_to_str(start_dt, date_format)
    end_str = time_util.datetime_to_str(end_dt, date_format)

    if date_type == DateTypes.DatesAsStringsArchaelogical:
        return build_query_archaelogical(start_str, end_str, from_attr, to_attr, comparison,
                                         query_idiom)

    if can_compare_lexicographically(date_format):
        if query_idiom == QueryIdioms.OGR:
            return STRINGCAST_FORMAT.format(from_attr, comparison, end_str, to_attr, start_str)
        else:
            return STRING_FORMAT.format(from_attr, comparison, end_str, to_attr, start_str)

    else:
        # thankfully, SQL & OGR syntax agree on substr and concat
        if date_type != DateTypes.DatesAsStrings:
            raise QueryBuildingException()
        ioy = date_format.find("%Y")
        iom = date_format.find("%m")
        iod = date_format.find("%d")
        ioh = date_format.find("%H")

        sub1 = create_ymd_substring(ioy, iom, iod, ioh, from_attr,
                                    quote_type='"')  # quote type for column
        # names
        sub2 = create_ymd_substring(ioy, iom, iod, ioh, end_str,
                                    quote_type='\'')  # quote type for values
        sub3 = create_ymd_substring(ioy, iom, iod, ioh, to_attr, quote_type='"')
        sub4 = create_ymd_substring(ioy, iom, iod, ioh, start_str, quote_type='\'')
        query = "CONCAT({}) {} CONCAT({}) AND CONCAT({})>=CONCAT({})".format(sub1, comparison,
                                                                             sub2, sub3, sub4)
        return query
Esempio n. 5
0
    def refreshGuiTimeExtents(self, timeExtents):
        """Update time extents showing in labels and represented by horizontalTimeSlider
        :param timeExtents: a tuple of start and end datetimes
        """
        self.setPropagateGuiChanges(False)
        if timeExtents[
                1] is not None:  # timeExtents[0] is set in different places, so only check timeExtents[1]
            startText = time_util.datetime_to_str(timeExtents[0],
                                                  time_util.DEFAULT_FORMAT)
            endText = time_util.datetime_to_str(timeExtents[1],
                                                time_util.DEFAULT_FORMAT)
            self.guiControl.dock.labelStartTime.setText(startText)
            self.guiControl.dock.labelEndTime.setText(endText)

            timeLength = time_util.datetime_to_epoch(
                timeExtents[1]) - time_util.datetime_to_epoch(timeExtents[0])

            if timeLength > MAX_TIME_LENGTH_SECONDS_SLIDER:
                new_granularity = int(
                    math.ceil(1.0 * timeLength /
                              MAX_TIME_LENGTH_SECONDS_SLIDER))
                self.setGranularitySeconds(new_granularity)
                # trick because timeLength must fit in an integer
                # since it interfaces with a C++ class
                newTimeLength = int(
                    math.ceil(1.0 * timeLength / new_granularity))
                timeLength = newTimeLength

            else:
                self.setGranularitySeconds(conf.DEFAULT_GRANULARITY_IN_SECONDS)

            self.guiControl.dock.horizontalTimeSlider.setMinimum(0)
            self.guiControl.dock.horizontalTimeSlider.setMaximum(timeLength)

        else:  # set to default values
            self.setGranularitySeconds(conf.DEFAULT_GRANULARITY_IN_SECONDS)
            self.guiControl.dock.labelStartTime.setText('not set')
            self.guiControl.dock.labelEndTime.setText('not set')
            self.guiControl.dock.horizontalTimeSlider.setMinimum(
                conf.MIN_TIMESLIDER_DEFAULT)
            self.guiControl.dock.horizontalTimeSlider.setMaximum(
                conf.MAX_TIMESLIDER_DEFAULT)

        self.setPropagateGuiChanges(True)
Esempio n. 6
0
def build_query(start_dt, end_dt, from_attr, to_attr, date_type, date_format, query_idiom, acc):
    """Build subset query"""
    if acc: # features never die
        start_dt = time_util.get_min_dt()
    
    comparison = "<" # simplified because of: https://github.com/anitagraser/TimeManager/issues/235 
    #                  (original: # comparison = "<" if to_attr == from_attr else "<=")    

    if date_type == time_util.DateTypes.IntegerTimestamps:
        start_epoch = time_util.datetime_to_epoch(start_dt)
        end_epoch = time_util.datetime_to_epoch(end_dt)
        return INT_FORMAT.format(from_attr, comparison, end_epoch, to_attr, start_epoch)
    
    start_str = time_util.datetime_to_str(start_dt, date_format)
    end_str = time_util.datetime_to_str(end_dt, date_format)    
    
    if date_type == time_util.DateTypes.DatesAsStringsArchaelogical:
        # kept <= option here since I'm not sure about implications in archaelogical mode 
        comparison = "<" if to_attr == from_attr else "<=" 
        return build_query_archaelogical(start_str, end_str, from_attr, to_attr, comparison, query_idiom)
    
    if can_compare_lexicographically(date_format):
        if query_idiom == QueryIdioms.OGR:
            return STRINGCAST_FORMAT.format(from_attr, comparison, end_str, to_attr, start_str)
        else:
            return STRING_FORMAT.format(from_attr, comparison, end_str, to_attr, start_str)
    else:
        # thankfully, SQL & OGR syntax agree on substr and concat
        if date_type != time_util.DateTypes.DatesAsStrings:
            raise QueryBuildingException()
        ioy = date_format.find("%Y")
        iom = date_format.find("%m")
        iod = date_format.find("%d")
        ioh = date_format.find("%H")

        sub1 = create_ymd_substring(ioy, iom, iod, ioh, from_attr, quote_type='"')  # quote type for column names
        sub2 = create_ymd_substring(ioy, iom, iod, ioh, end_str, quote_type='\'')  # quote type for values
        sub3 = create_ymd_substring(ioy, iom, iod, ioh, to_attr, quote_type='"')
        sub4 = create_ymd_substring(ioy, iom, iod, ioh, start_str, quote_type='\'')
        query = "CONCAT({}) {} CONCAT({}) AND CONCAT({})>=CONCAT({})".format(sub1, comparison, sub2, sub3, sub4)
        return query
Esempio n. 7
0
def build_query(start_dt, end_dt, from_attr, to_attr, date_type, date_format,
                query_idiom):
    """Build subset query"""

    comparison = "<" if to_attr == from_attr else "<="

    if date_type == DateTypes.IntegerTimestamps:
        start_epoch = time_util.datetime_to_epoch(start_dt)
        end_epoch = time_util.datetime_to_epoch(end_dt)
        return INT_FORMAT.format(from_attr, comparison, end_epoch, to_attr,
                                 start_epoch)

    start_str = time_util.datetime_to_str(start_dt, date_format)
    end_str = time_util.datetime_to_str(end_dt, date_format)

    if can_compare_lexicographically(date_format):
        if query_idiom == QueryIdioms.OGR:
            return STRINGCAST_FORMAT.format(from_attr, comparison, end_str,
                                            to_attr, start_str)
        else:
            return STRING_FORMAT.format(from_attr, comparison, end_str,
                                        to_attr, start_str)

    else:
        # thankfully, SQL & OGR syntax agree on substr and concat
        if date_type != DateTypes.DatesAsStrings:
            raise QueryBuildingException()
        ioy = date_format.find("%Y")
        iom = date_format.find("%m")
        iod = date_format.find("%d")
        sub1 = create_ymd_substring(ioy, iom, iod, from_attr, quote_type='"')
        sub2 = create_ymd_substring(ioy, iom, iod, end_str, quote_type='\'')
        sub3 = create_ymd_substring(ioy, iom, iod, to_attr, quote_type='"')
        sub4 = create_ymd_substring(ioy, iom, iod, start_str, quote_type='\'')
        return "CONCAT({}) {} CONCAT({}) AND CONCAT({})>=CONCAT({})".format(
            sub1, comparison, sub2, sub3, sub4)
Esempio n. 8
0
    def refreshGuiWithCurrentTime(self, currentTimePosition, sender=None):
        """
        Update the gui when time has changed by refreshing/repainting the layers
        and changing the time showing in dateTimeEditCurrentTime and horizontalTimeSlider
        
        Setting the gui elements should not fire the event for
        timeChanged, since they were changed to be in sync with the rest of the system on
        purpose, no need to sync the system again
        """
        self.setPropagateGuiChanges(False)
        timeExtent = self.getTimeLayerManager().getProjectTimeExtents()
        if currentTimePosition is None or timeExtent[0] is None or timeExtent[
                1] is None:
            self.setPropagateGuiChanges(True)
            return

        time_util.updateUi(self.guiControl.getTimeWidget(),
                           currentTimePosition)
        timeval = time_util.datetime_to_epoch(currentTimePosition)
        timeExtents = self.getTimeLayerManager().getProjectTimeExtents()
        try:
            pct = (timeval - time_util.datetime_to_epoch(timeExtents[0])
                   ) * 1.0 / (time_util.datetime_to_epoch(timeExtents[1]) -
                              time_util.datetime_to_epoch(timeExtents[0]))
            sliderVal = self.guiControl.dock.horizontalTimeSlider.minimum(
            ) + int(pct *
                    (self.guiControl.dock.horizontalTimeSlider.maximum() -
                     self.guiControl.dock.horizontalTimeSlider.minimum()))
            self.guiControl.dock.horizontalTimeSlider.setValue(sliderVal)
            self.guiControl.repaintRasters()
            self.guiControl.repaintJoined()
            self.guiControl.repaintVectors()
            self.guiControl.refreshMapCanvas()
            self.updateLegendCount()
        except Exception, e:
            error(e)