Thanks Nyall!!

With a minimal adjustments this just works:

def temporal_filter_for_layer(layer, canvas):
  if canvas.mapSettings().isTemporal():
    if not 
layer.temporalProperties().isVisibleInTemporalRange(canvas.temporalRange()):
      return False # nothing is visible
    temporal_context = QgsVectorLayerTemporalContext()
    temporal_context.setLayer(layer)
    return layer.temporalProperties().createFilterString(temporal_context, 
canvas.temporalRange())
  else:
    return None

print( temporal_filter_for_layer(iface.mapCanvas().currentLayer(), 
iface.mapCanvas()) )

Now prints:

("time" > make_datetime(2022,3,20,10,30,0) AND "time" < make_datetime(2022,3,20,12,30,0)) 
OR "time" IS NULL

Which I can use as expression in: layer.selectByExpression(...)

Regards,

Richard Duivenvoorde

PS also see my Feature Request: https://github.com/qgis/QGIS/issues/47929 about 
adding an option to switch between selecting visible vs non visible features...

On 3/21/22 23:59, Nyall Dawson wrote:
Anybody aware of a way to do this?
Going to try to get the temporal expression maybe and then select by
expression?

untested code ported from tested c++ code, sorry, but it's something like this:

def temporal_filter_for_layer(layer, canvas):
   if canvas.mapSettings().isTemporal():
     if not layer.temporalProperties().isVisibleInTemporalRange(
canvas.temporalRange() ) ):
       return "FALSE"; # nothing is visible

     temporal_context = QgsVectorLayerTemporalContext()
     temporal_context.setLayer( layer )
     return layer.temporalProperties().createFilterString(
temporal_context, canvas.temporalRange() )
   else:
     return None

Nyall

_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to