Commit 77ef3eb4 authored by jay's avatar jay
Browse files

Supports from database

parent 9002a782
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
@@ -1593,16 +1593,25 @@ WHERE points.active = True AND measures.active=TRUE AND measures.jigreject=FALSE
        """
        sourceSession, _ = new_connection(config)
        sourcesession = sourceSession()
        sourceimages = sourcesession.execute(query_string).all()
        #sourceimageids = [i.id for i in sourceimages]
        #sourcecameras = sourcesession.query(Cameras).filter(Cameras.id.in_(sourceimageids)).all()
        
        session = Session()
        session.add_all(sourceimages)
        session.commit()
        session.close()
        sourceimages = sourcesession.execute(query_string).fetchall()
        
        destinationsession = Session()
        destinationsession.execute(Images.__table__.insert(), sourceimages)

        # Get the camera objects to manually join. Keeps the caller from
        # having to remember to bring cameras as well.
        ids = [i[0] for i in sourceimages]
        cameras = sourcesession.query(Cameras).filter(Cameras.image_id.in_(ids)).all()
        for c in cameras:
            destinationsession.merge(c)

        destinationsession.commit()
        destinationsession.close()
        sourcesession.close()
        obj = cls.from_database()
        obj._execute_sql(compute_overlaps_sql)
        return obj

    @classmethod
    def from_database(cls, query_string='SELECT * FROM public.images'):
+1 −1
Original line number Diff line number Diff line
@@ -499,7 +499,7 @@ class NetworkNode(Node):
        super(NetworkNode, self).__init__(*args, **kwargs)
        # If this is the first time that the image is seen, add it to the DB
        if parent is None:
            self.parent = Parent(config)
            self.parent = Parent(config['database'])
        else:
            self.parent = parent

+8 −8
Original line number Diff line number Diff line
@@ -12,10 +12,11 @@ import yaml

class Parent:
    def __init__(self, config):
        self.session, _ = new_connection(config)
        Session, _ = new_connection(config)
        self.session = Session()
        self.session.begin()

def new_connection(config):
def new_connection(dbconfig):
    """
    Using the user supplied config create a NullPool database connection.

@@ -27,12 +28,11 @@ def new_connection(config):
    engine : object
             An SQLAlchemy engine object
    """
    db = config['database']
    db_uri = 'postgresql://{}:{}@{}:{}/{}'.format(db['username'],
                                                  db['password'],
                                                  db['host'],
                                                  db['pgbouncer_port'],
                                                  db['name'])    
    db_uri = 'postgresql://{}:{}@{}:{}/{}'.format(dbconfig['username'],
                                                  dbconfig['password'],
                                                  dbconfig['host'],
                                                  dbconfig['pgbouncer_port'],
                                                  dbconfig['name'])    
    engine = sqlalchemy.create_engine(db_uri,
                                      poolclass=sqlalchemy.pool.NullPool)
    Session = sqlalchemy.orm.sessionmaker(bind=engine, autocommit=True)
+1 −4
Original line number Diff line number Diff line
@@ -62,17 +62,14 @@ def create_camera(config, geodata, imagepath):
    # Create the camera entry
    label = pvl.dumps(geodata.metadata).decode()
    url = config['pfeffernusse']['url']
    response = requests_retry_session.post(url, json={'label':label})
    response = requests_retry_session().post(url, json={'label':label})
    response = response.json()
    model_name = response.get('name_model', None)
    if model_name is None:
        return (None, None)
    isdpath = os.path.splitext(imagepath)[0] + '.json'
    try:
    with open(isdpath, 'w') as f:
        json.dump(response, f)
    except Exception as e:
        warnings.warn('Failed to write JSON ISD for image {}.\n{}'.format(imagepath, e))
    isd = csmapi.Isd(imagepath)
    plugin = csmapi.Plugin.findPlugin('UsgsAstroPluginCSM')
    camera = plugin.constructModelFromISD(isd, model_name)