//------------------------------------------------------------------------------ void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const QString& filePath, bool storeFile, bool generateThumbnail) { Q_Q(ctkDICOMDatabase); // Check to see if the file has already been loaded // TODO: // It could make sense to actually remove the dataset and re-add it. This needs the remove // method we still have to write. // QString sopInstanceUID ( ctkDataset.GetElementAsString(DCM_SOPInstanceUID) ); QSqlQuery fileExists ( Database ); fileExists.prepare("SELECT InsertTimestamp,Filename FROM Images WHERE SOPInstanceUID == :sopInstanceUID"); fileExists.bindValue(":sopInstanceUID",sopInstanceUID); bool success = fileExists.exec(); if (!success) { logger.error("SQLITE ERROR: " + fileExists.lastError().driverText()); return; } qDebug() << "filename is: " << fileExists.value(1).toString(); qDebug() << "modified date is: " << QFileInfo(fileExists.value(1).toString()).lastModified(); qDebug() << "db mod date is: " << QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate); if ( fileExists.next() && QFileInfo(fileExists.value(1).toString()).lastModified() < QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate) ) { logger.debug ( "File " + fileExists.value(1).toString() + " already added" ); return; } //If the following fields can not be evaluated, cancel evaluation of the DICOM file QString patientsName(ctkDataset.GetElementAsString(DCM_PatientName) ); QString studyInstanceUID(ctkDataset.GetElementAsString(DCM_StudyInstanceUID) ); QString seriesInstanceUID(ctkDataset.GetElementAsString(DCM_SeriesInstanceUID) ); QString patientID(ctkDataset.GetElementAsString(DCM_PatientID) ); if ( patientsName.isEmpty() && !patientID.isEmpty() ) { // Use patient id as name if name is empty - can happen on anonymized datasets // see: http://www.na-mic.org/Bug/view.php?id=1643 patientsName = patientID; } if ( patientsName.isEmpty() || studyInstanceUID.isEmpty() || patientID.isEmpty() ) { logger.error("Dataset is missing necessary information!"); return; } QString patientsBirthDate(ctkDataset.GetElementAsString(DCM_PatientBirthDate) ); QString patientsBirthTime(ctkDataset.GetElementAsString(DCM_PatientBirthTime) ); QString patientsSex(ctkDataset.GetElementAsString(DCM_PatientSex) ); QString patientsAge(ctkDataset.GetElementAsString(DCM_PatientAge) ); QString patientComments(ctkDataset.GetElementAsString(DCM_PatientComments) ); QString studyID(ctkDataset.GetElementAsString(DCM_StudyID) ); QString studyDate(ctkDataset.GetElementAsString(DCM_StudyDate) ); QString studyTime(ctkDataset.GetElementAsString(DCM_StudyTime) ); QString accessionNumber(ctkDataset.GetElementAsString(DCM_AccessionNumber) ); QString modalitiesInStudy(ctkDataset.GetElementAsString(DCM_ModalitiesInStudy) ); QString institutionName(ctkDataset.GetElementAsString(DCM_InstitutionName) ); QString performingPhysiciansName(ctkDataset.GetElementAsString(DCM_PerformingPhysicianName) ); QString referringPhysician(ctkDataset.GetElementAsString(DCM_ReferringPhysicianName) ); QString studyDescription(ctkDataset.GetElementAsString(DCM_StudyDescription) ); QString seriesDate(ctkDataset.GetElementAsString(DCM_SeriesDate) ); QString seriesTime(ctkDataset.GetElementAsString(DCM_SeriesTime) ); QString seriesDescription(ctkDataset.GetElementAsString(DCM_SeriesDescription) ); QString bodyPartExamined(ctkDataset.GetElementAsString(DCM_BodyPartExamined) ); QString frameOfReferenceUID(ctkDataset.GetElementAsString(DCM_FrameOfReferenceUID) ); QString contrastAgent(ctkDataset.GetElementAsString(DCM_ContrastBolusAgent) ); QString scanningSequence(ctkDataset.GetElementAsString(DCM_ScanningSequence) ); long seriesNumber(ctkDataset.GetElementAsInteger(DCM_SeriesNumber) ); long acquisitionNumber(ctkDataset.GetElementAsInteger(DCM_AcquisitionNumber) ); long echoNumber(ctkDataset.GetElementAsInteger(DCM_EchoNumbers) ); long temporalPosition(ctkDataset.GetElementAsInteger(DCM_TemporalPositionIdentifier) ); // store the file if the database is not in memomry // TODO: if we are called from insert(file) we // have to do something else // QString filename = filePath; if ( storeFile && !q->isInMemory() && !seriesInstanceUID.isEmpty() ) { // QString studySeriesDirectory = studyInstanceUID + "/" + seriesInstanceUID; QString destinationDirectoryName = q->databaseDirectory() + "/dicom/"; QDir destinationDir(destinationDirectoryName); filename = destinationDirectoryName + studyInstanceUID + "/" + seriesInstanceUID + "/" + sopInstanceUID; destinationDir.mkpath(studyInstanceUID + "/" + seriesInstanceUID); if(filePath.isEmpty()) { logger.debug ( "Saving file: " + filename ); if ( !ctkDataset.SaveToFile( filename) ) { logger.error ( "Error saving file: " + filename ); return; } } else { // we're inserting an existing file QFile currentFile( filePath ); currentFile.copy(filename); logger.debug( "Copy file from: " + filePath ); logger.debug( "Copy file to : " + filename ); } } QSqlQuery checkPatientExistsQuery(Database); //The dbPatientID is a unique number within the database, //generated by the sqlite autoincrement //The patientID is the (non-unique) DICOM patient id int dbPatientID = -1; if ( patientID != "" && patientsName != "" ) { //Speed up: Check if patient is the same as in last file; // very probable, as all images belonging to a study have the same patient if ( lastPatientID != patientID || lastPatientsBirthDate != patientsBirthDate || lastPatientsName != patientsName ) { // Ok, something is different from last insert, let's insert him if he's not // already in the db. // // Check if patient is already present in the db // TODO: maybe add birthdate check for extra safety checkPatientExistsQuery.prepare ( "SELECT * FROM Patients WHERE PatientID = ? AND PatientsName = ?" ); checkPatientExistsQuery.bindValue ( 0, patientID ); checkPatientExistsQuery.bindValue ( 1, patientsName ); loggedExec(checkPatientExistsQuery); if (checkPatientExistsQuery.next()) { // we found him dbPatientID = checkPatientExistsQuery.value(checkPatientExistsQuery.record().indexOf("UID")).toInt(); } else { // Insert it QSqlQuery insertPatientStatement ( Database ); insertPatientStatement.prepare ( "INSERT INTO Patients ('UID', 'PatientsName', 'PatientID', 'PatientsBirthDate', 'PatientsBirthTime', 'PatientsSex', 'PatientsAge', 'PatientsComments' ) values ( NULL, ?, ?, ?, ?, ?, ?, ? )" ); insertPatientStatement.bindValue ( 0, patientsName ); insertPatientStatement.bindValue ( 1, patientID ); insertPatientStatement.bindValue ( 2, patientsBirthDate ); insertPatientStatement.bindValue ( 3, patientsBirthTime ); insertPatientStatement.bindValue ( 4, patientsSex ); // TODO: shift patient's age to study, // since this is not a patient level attribute in images // insertPatientStatement.bindValue ( 5, patientsAge ); insertPatientStatement.bindValue ( 6, patientComments ); loggedExec(insertPatientStatement); dbPatientID = insertPatientStatement.lastInsertId().toInt(); logger.debug ( "New patient inserted: " + QString().setNum ( dbPatientID ) ); } /// keep this for the next image lastPatientUID = dbPatientID; lastPatientID = patientID; lastPatientsBirthDate = patientsBirthDate; lastPatientsName = patientsName; } // Patient is in now. Let's continue with the study if ( studyInstanceUID != "" && lastStudyInstanceUID != studyInstanceUID ) { QSqlQuery checkStudyExistsQuery (Database); checkStudyExistsQuery.prepare ( "SELECT * FROM Studies WHERE StudyInstanceUID = ?" ); checkStudyExistsQuery.bindValue ( 0, studyInstanceUID ); checkStudyExistsQuery.exec(); if(!checkStudyExistsQuery.next()) { QSqlQuery insertStudyStatement ( Database ); insertStudyStatement.prepare ( "INSERT INTO Studies ( 'StudyInstanceUID', 'PatientsUID', 'StudyID', 'StudyDate', 'StudyTime', 'AccessionNumber', 'ModalitiesInStudy', 'InstitutionName', 'ReferringPhysician', 'PerformingPhysiciansName', 'StudyDescription' ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" ); insertStudyStatement.bindValue ( 0, studyInstanceUID ); insertStudyStatement.bindValue ( 1, dbPatientID ); insertStudyStatement.bindValue ( 2, studyID ); insertStudyStatement.bindValue ( 3, QDate::fromString ( studyDate, "yyyyMMdd" ) ); insertStudyStatement.bindValue ( 4, studyTime ); insertStudyStatement.bindValue ( 5, accessionNumber ); insertStudyStatement.bindValue ( 6, modalitiesInStudy ); insertStudyStatement.bindValue ( 7, institutionName ); insertStudyStatement.bindValue ( 8, referringPhysician ); insertStudyStatement.bindValue ( 9, performingPhysiciansName ); insertStudyStatement.bindValue ( 10, studyDescription ); if ( !insertStudyStatement.exec() ) { logger.error ( "Error executing statament: " + insertStudyStatement.lastQuery() + " Error: " + insertStudyStatement.lastError().text() ); } else { lastStudyInstanceUID = studyInstanceUID; } } } if ( seriesInstanceUID != "" && seriesInstanceUID != lastSeriesInstanceUID ) { QSqlQuery checkSeriesExistsQuery (Database); checkSeriesExistsQuery.prepare ( "SELECT * FROM Series WHERE SeriesInstanceUID = ?" ); checkSeriesExistsQuery.bindValue ( 0, seriesInstanceUID ); logger.warn ( "Statement: " + checkSeriesExistsQuery.lastQuery() ); loggedExec(checkSeriesExistsQuery); if(!checkSeriesExistsQuery.next()) { QSqlQuery insertSeriesStatement ( Database ); insertSeriesStatement.prepare ( "INSERT INTO Series ( 'SeriesInstanceUID', 'StudyInstanceUID', 'SeriesNumber', 'SeriesDate', 'SeriesTime', 'SeriesDescription', 'BodyPartExamined', 'FrameOfReferenceUID', 'AcquisitionNumber', 'ContrastAgent', 'ScanningSequence', 'EchoNumber', 'TemporalPosition' ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" ); insertSeriesStatement.bindValue ( 0, seriesInstanceUID ); insertSeriesStatement.bindValue ( 1, studyInstanceUID ); insertSeriesStatement.bindValue ( 2, static_cast<int>(seriesNumber) ); insertSeriesStatement.bindValue ( 3, seriesDate ); insertSeriesStatement.bindValue ( 4, QDate::fromString ( seriesTime, "yyyyMMdd" ) ); insertSeriesStatement.bindValue ( 5, seriesDescription ); insertSeriesStatement.bindValue ( 6, bodyPartExamined ); insertSeriesStatement.bindValue ( 7, frameOfReferenceUID ); insertSeriesStatement.bindValue ( 8, static_cast<int>(acquisitionNumber) ); insertSeriesStatement.bindValue ( 9, contrastAgent ); insertSeriesStatement.bindValue ( 10, scanningSequence ); insertSeriesStatement.bindValue ( 11, static_cast<int>(echoNumber) ); insertSeriesStatement.bindValue ( 12, static_cast<int>(temporalPosition) ); if ( !insertSeriesStatement.exec() ) { logger.error ( "Error executing statament: " + insertSeriesStatement.lastQuery() + " Error: " + insertSeriesStatement.lastError().text() ); lastSeriesInstanceUID = ""; } else { lastSeriesInstanceUID = seriesInstanceUID; } } } // TODO: what to do with imported files // if ( !filename.isEmpty() && !seriesInstanceUID.isEmpty() ) { QSqlQuery checkImageExistsQuery (Database); checkImageExistsQuery.prepare ( "SELECT * FROM Images WHERE Filename = ?" ); checkImageExistsQuery.bindValue ( 0, filename ); checkImageExistsQuery.exec(); if(!checkImageExistsQuery.next()) { QSqlQuery insertImageStatement ( Database ); insertImageStatement.prepare ( "INSERT INTO Images ( 'SOPInstanceUID', 'Filename', 'SeriesInstanceUID', 'InsertTimestamp' ) VALUES ( ?, ?, ?, ? )" ); insertImageStatement.bindValue ( 0, sopInstanceUID ); insertImageStatement.bindValue ( 1, filename ); insertImageStatement.bindValue ( 2, seriesInstanceUID ); insertImageStatement.bindValue ( 3, QDateTime::currentDateTime() ); insertImageStatement.exec(); } } if( generateThumbnail && thumbnailGenerator && !seriesInstanceUID.isEmpty() ) { QString studySeriesDirectory = studyInstanceUID + "/" + seriesInstanceUID; //Create thumbnail here QString thumbnailPath = q->databaseDirectory() + "/thumbs/" + studyInstanceUID + "/" + seriesInstanceUID + "/" + sopInstanceUID + ".png"; QFileInfo thumbnailInfo(thumbnailPath); if( !(thumbnailInfo.exists() && (thumbnailInfo.lastModified() > QFileInfo(filename).lastModified()))) { QDir(q->databaseDirectory() + "/thumbs/").mkpath(studySeriesDirectory); DicomImage dcmImage(QDir::toNativeSeparators(filename).toAscii()); thumbnailGenerator->generateThumbnail(&dcmImage, thumbnailPath); } } if (q->isInMemory()) { emit q->databaseChanged(); } } }
// ------------------------------------------------------------------------ void groupDicomFiles(const gdcm::Directory::FilenamesType &filenames, SeriesTransform::Map &transforms) { // set up the dicom scanner gdcm::Scanner scanner; gdcm::Tag seriesNumberTag(0x0020,0x0011); gdcm::Tag instanceTag(0x0020,0x0013); gdcm::Tag numberOfImagesTag(0x0018,0x1090); gdcm::Tag seriesDescription(0x0008,0x103e); gdcm::Tag sliceThickness(0x0018,0x0050); scanner.AddTag(seriesNumberTag); scanner.AddTag(sliceThickness); scanner.AddTag(seriesDescription); scanner.AddTag(instanceTag); scanner.AddTag(numberOfImagesTag); scanner.Scan(filenames); gdcm::Scanner::MappingType mapping = scanner.GetMappings(); for(unsigned int i = 0; i < filenames.size(); i++) { const char * fname = filenames[i].c_str(); // is the filename valid if(mapping.count(fname) > 0) { if(mapping[fname].count(seriesNumberTag) > 0 && mapping[fname].count(instanceTag) > 0 && mapping[fname].count(numberOfImagesTag) > 0) { // extract the dicom header information std::string seriesNumberStr, instanceNumberStr, numberOfImagesStr, descriptionStr, thicknessStr; seriesNumberStr = mapping[fname][seriesNumberTag]; instanceNumberStr = mapping[fname][instanceTag]; numberOfImagesStr = mapping[fname][numberOfImagesTag]; descriptionStr = mapping[fname][seriesDescription]; thicknessStr = mapping[fname][sliceThickness]; unsigned int seriesNum = QString::fromStdString(seriesNumberStr).toInt(); unsigned int instanceNum = QString::fromStdString(instanceNumberStr).toInt(); unsigned int numberOfImages = QString::fromStdString(numberOfImagesStr).toInt(); double thickness = QString::fromStdString(thicknessStr).toDouble(); descriptionStr = QString::fromStdString(descriptionStr).replace(" ","").toStdString(); if(transforms.count(seriesNum) == 0) continue; // resize the image list if needed if(transforms[seriesNum].imageFilenames.size() != numberOfImages) { transforms[seriesNum].imageFilenames.resize(numberOfImages); } // put the image file into the image list transforms[seriesNum].description = descriptionStr; transforms[seriesNum].sliceThickness = thickness; transforms[seriesNum].imageFilenames[instanceNum-1] = fname; } } } }