Skip to content

Commit

Permalink
Remove Image.query_images, refactor Image.find_images so it can handl…
Browse files Browse the repository at this point in the history
…e area overlaps.
  • Loading branch information
rknop committed Oct 4, 2024
1 parent fa10ee6 commit 7dbe7b6
Show file tree
Hide file tree
Showing 10 changed files with 797 additions and 742 deletions.
46 changes: 29 additions & 17 deletions models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,8 +1936,8 @@ def _find_possibly_containing_temptable( cls, ra, dec, session, prov_id=None ):
session : Session
Required here, otherwise the temp table would be useless.
prov_id : str or None
If not None, search for objects with this provenance.
prov_id : str, list of str, or None
If not None, search for objects with this provenance, or any of these provenances if a list.
"""
session.execute( sa.text( "DROP TABLE IF EXISTS temp_find_containing" ) )
Expand All @@ -1963,8 +1963,14 @@ def _find_possibly_containing_temptable( cls, ra, dec, session, prov_id=None ):
)
subdict = { "ra": ra, "dec": dec }
if prov_id is not None:
query += " AND provenance_id=:prov"
subdict['prov'] = prov_id
if isinstance( prov_id, str ):
query += " AND provenance_id=:prov"
subdict['prov'] = prov_id
elif isinstance( prov_id, list ):
query += " AND provenance_id IN :prov"
subidct['prov'] = tuple( prov_id )
else:
raise TypeError( "prov_id must be a a str or a list of str" )

session.execute( sa.text( query ), subdict )

Expand All @@ -1977,8 +1983,8 @@ def find_containing( cls, ra, dec, prov_id=None, session=None ):
----------
ra, dec: float, decimal degrees
prov_id: str or None
id of the provenance of cls objects to search; if None, won't filter on provenance
prov_id : str, list of str, or None
If not None, search for objects with this provenance, or any of these provenances if a list.
Returns
-------
Expand All @@ -2001,12 +2007,13 @@ def find_containing( cls, ra, dec, prov_id=None, session=None ):
# we'll q3c_poly_query.

with SmartSession( session ) as sess:
cls._find_possibly_containing_temptable( ra, dec, session, prov_id=prov_id )
query = sa.text( f"SELECT i._id FROM temp_find_containing i "
f"WHERE q3c_poly_query( {ra}, {dec}, ARRAY[ i.ra_corner_00, i.dec_corner_00, "
f" i.ra_corner_01, i.dec_corner_01, "
f" i.ra_corner_11, i.dec_corner_11, "
f" i.ra_corner_10, i.dec_corner_10 ])" )
cls._find_possibly_containing_temptable( ra, dec, sess, prov_id=prov_id )
query = sa.text( f"SELECT i.* FROM {cls.__tablename__} i "
f"INNER JOIN temp_find_containing t ON t._id=i._id "
f"WHERE q3c_poly_query( {ra}, {dec}, ARRAY[ t.ra_corner_00, t.dec_corner_00, "
f" t.ra_corner_01, t.dec_corner_01, "
f" t.ra_corner_11, t.dec_corner_11, "
f" t.ra_corner_10, t.dec_corner_10 ])" )
objs = sess.scalars( sa.select( cls ).from_statement( query ) ).all()
sess.execute( sa.text( "DROP TABLE temp_find_containing" ) )
return objs
Expand All @@ -2031,9 +2038,8 @@ def _find_potential_overlapping_temptable( cls, fcobj, session, prov_id=None ):
session : Session
required here; otherwise, the temp table wouldn't be useful
prov_id : str, default None
The id of the provenance of objects to look for; defaults to
not filtering on provenance (which is almost never what you want).
prov_id: str or None
id of the provenance of cls objects to search; if None, won't filter on provenance
"""

Expand Down Expand Up @@ -2072,8 +2078,14 @@ def _find_potential_overlapping_temptable( cls, fcobj, session, prov_id=None ):
subdict = { 'minra': fcobj.minra, 'maxra': fcobj.maxra,
'mindec': fcobj.mindec, 'maxdec': fcobj.maxdec }
if prov_id is not None:
query += "AND provenance_id=:prov"
subdict['prov'] = prov_id
if isinstance( prov_id, str ):
query += " AND provenance_id=:prov"
subdict['prov'] = prov_id
elif isinstance( prov_id, list ):
query += " AND provenance_id IN :prov"
subidct['prov'] = tuple( prov_id )
else:
raise TypeError( "prov_id must be a a str or a list of str" )

session.execute( sa.text( query ), subdict )

Expand Down
Loading

0 comments on commit 7dbe7b6

Please sign in to comment.