QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
Loading...
Searching...
No Matches
qgsalgorithmimportphotos.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmimportphotos.cpp
3 ------------------
4 begin : March 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsogrutils.h"
21#include "qgsvectorlayer.h"
22
23#include <QDirIterator>
24#include <QFileInfo>
25#include <QRegularExpression>
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31
32QString QgsImportPhotosAlgorithm::name() const
33{
34 return u"importphotos"_s;
35}
36
37QString QgsImportPhotosAlgorithm::displayName() const
38{
39 return QObject::tr( "Import geotagged photos" );
40}
41
42QStringList QgsImportPhotosAlgorithm::tags() const
43{
44 return QObject::tr( "exif,metadata,gps,jpeg,jpg" ).split( ',' );
45}
46
47QString QgsImportPhotosAlgorithm::group() const
48{
49 return QObject::tr( "Vector creation" );
50}
51
52QString QgsImportPhotosAlgorithm::groupId() const
53{
54 return u"vectorcreation"_s;
55}
56
57void QgsImportPhotosAlgorithm::initAlgorithm( const QVariantMap & )
58{
59 addParameter( new QgsProcessingParameterFile( u"FOLDER"_s, QObject::tr( "Input folder" ), Qgis::ProcessingFileParameterBehavior::Folder ) );
60 addParameter( new QgsProcessingParameterBoolean( u"RECURSIVE"_s, QObject::tr( "Scan recursively" ), false ) );
61
62 auto output = std::make_unique<QgsProcessingParameterFeatureSink>( u"OUTPUT"_s, QObject::tr( "Photos" ), Qgis::ProcessingSourceType::VectorPoint, QVariant(), true );
63 output->setCreateByDefault( true );
64 addParameter( output.release() );
65
66 auto invalid = std::make_unique<QgsProcessingParameterFeatureSink>( u"INVALID"_s, QObject::tr( "Invalid photos table" ), Qgis::ProcessingSourceType::Vector, QVariant(), true );
67 invalid->setCreateByDefault( false );
68 addParameter( invalid.release() );
69}
70
71QString QgsImportPhotosAlgorithm::shortHelpString() const
72{
73 return QObject::tr(
74 "This algorithm creates a point layer corresponding to the geotagged locations from JPEG or HEIF/HEIC images from a source folder. Optionally the folder can be recursively scanned.\n\n"
75 "The point layer will contain a single PointZ feature per input file from which the geotags could be read. Any altitude information from the geotags will be used "
76 "to set the point's Z value.\n\n"
77 "Optionally, a table of unreadable or non-geotagged photos can also be created."
78 );
79}
80
81QString QgsImportPhotosAlgorithm::shortDescription() const
82{
83 return QObject::tr( "Creates a point layer corresponding to the geotagged locations from JPEG or HEIF/HEIC images from a source folder." );
84}
85
86QgsImportPhotosAlgorithm *QgsImportPhotosAlgorithm::createInstance() const
87{
88 return new QgsImportPhotosAlgorithm();
89}
90
91QVariant QgsImportPhotosAlgorithm::parseMetadataValue( const QString &value )
92{
93 const thread_local QRegularExpression numRx( u"^\\s*\\(\\s*([-\\.\\d]+)\\s*\\)\\s*$"_s );
94 const QRegularExpressionMatch numMatch = numRx.match( value );
95 if ( numMatch.hasMatch() )
96 {
97 return numMatch.captured( 1 ).toDouble();
98 }
99 return value;
100}
101
102bool QgsImportPhotosAlgorithm::extractGeoTagFromMetadata( const QVariantMap &metadata, QgsPointXY &tag )
103{
104 double x = 0.0;
105 if ( metadata.contains( u"EXIF_GPSLongitude"_s ) )
106 {
107 bool ok = false;
108 x = metadata.value( u"EXIF_GPSLongitude"_s ).toDouble( &ok );
109 if ( !ok )
110 return false;
111
112 if ( QStringView { metadata.value( u"EXIF_GPSLongitudeRef"_s ).toString() }.right( 1 ).compare( 'W'_L1, Qt::CaseInsensitive ) == 0 || metadata.value( u"EXIF_GPSLongitudeRef"_s ).toDouble() < 0 )
113 {
114 x = -x;
115 }
116 }
117 else
118 {
119 return false;
120 }
121
122 double y = 0.0;
123 if ( metadata.contains( u"EXIF_GPSLatitude"_s ) )
124 {
125 bool ok = false;
126 y = metadata.value( u"EXIF_GPSLatitude"_s ).toDouble( &ok );
127 if ( !ok )
128 return false;
129
130 if ( QStringView { metadata.value( u"EXIF_GPSLatitudeRef"_s ).toString() }.right( 1 ).compare( 'S'_L1, Qt::CaseInsensitive ) == 0 || metadata.value( u"EXIF_GPSLatitudeRef"_s ).toDouble() < 0 )
131 {
132 y = -y;
133 }
134 }
135 else
136 {
137 return false;
138 }
139
140 tag = QgsPointXY( x, y );
141 return true;
142}
143
144QVariant QgsImportPhotosAlgorithm::extractAltitudeFromMetadata( const QVariantMap &metadata )
145{
146 QVariant altitude;
147 if ( metadata.contains( u"EXIF_GPSAltitude"_s ) )
148 {
149 double alt = metadata.value( u"EXIF_GPSAltitude"_s ).toDouble();
150 if ( metadata.contains( u"EXIF_GPSAltitudeRef"_s ) && ( ( metadata.value( u"EXIF_GPSAltitudeRef"_s ).userType() == QMetaType::Type::QString && metadata.value( u"EXIF_GPSAltitudeRef"_s ).toString().right( 1 ) == "1"_L1 ) || metadata.value( u"EXIF_GPSAltitudeRef"_s ).toDouble() < 0 ) )
151 alt = -alt;
152 altitude = alt;
153 }
154 return altitude;
155}
156
157QVariant QgsImportPhotosAlgorithm::extractDirectionFromMetadata( const QVariantMap &metadata )
158{
159 QVariant direction;
160 if ( metadata.contains( u"EXIF_GPSImgDirection"_s ) )
161 {
162 direction = metadata.value( u"EXIF_GPSImgDirection"_s ).toDouble();
163 }
164 return direction;
165}
166
167QVariant QgsImportPhotosAlgorithm::extractOrientationFromMetadata( const QVariantMap &metadata )
168{
169 QVariant orientation;
170 if ( metadata.contains( u"EXIF_Orientation"_s ) )
171 {
172 switch ( metadata.value( u"EXIF_Orientation"_s ).toInt() )
173 {
174 case 1:
175 orientation = 0;
176 break;
177 case 2:
178 orientation = 0;
179 break;
180 case 3:
181 orientation = 180;
182 break;
183 case 4:
184 orientation = 180;
185 break;
186 case 5:
187 orientation = 90;
188 break;
189 case 6:
190 orientation = 90;
191 break;
192 case 7:
193 orientation = 270;
194 break;
195 case 8:
196 orientation = 270;
197 break;
198 }
199 }
200 return orientation;
201}
202
203QVariant QgsImportPhotosAlgorithm::extractTimestampFromMetadata( const QVariantMap &metadata )
204{
205 QVariant ts;
206 if ( metadata.contains( u"EXIF_DateTimeOriginal"_s ) )
207 {
208 ts = metadata.value( u"EXIF_DateTimeOriginal"_s );
209 }
210 else if ( metadata.contains( u"EXIF_DateTimeDigitized"_s ) )
211 {
212 ts = metadata.value( u"EXIF_DateTimeDigitized"_s );
213 }
214 else if ( metadata.contains( u"EXIF_DateTime"_s ) )
215 {
216 ts = metadata.value( u"EXIF_DateTime"_s );
217 }
218
219 if ( !ts.isValid() )
220 return ts;
221
222 const thread_local QRegularExpression dsRegEx( u"(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"_s );
223 const QRegularExpressionMatch dsMatch = dsRegEx.match( ts.toString() );
224 if ( dsMatch.hasMatch() )
225 {
226 const int year = dsMatch.captured( 1 ).toInt();
227 const int month = dsMatch.captured( 2 ).toInt();
228 const int day = dsMatch.captured( 3 ).toInt();
229 const int hour = dsMatch.captured( 4 ).toInt();
230 const int min = dsMatch.captured( 5 ).toInt();
231 const int sec = dsMatch.captured( 6 ).toInt();
232 return QDateTime( QDate( year, month, day ), QTime( hour, min, sec ) );
233 }
234 else
235 {
236 return QVariant();
237 }
238}
239
240QVariant QgsImportPhotosAlgorithm::parseCoord( const QString &string )
241{
242 const thread_local QRegularExpression coordRx( u"^\\s*\\(\\s*([-\\.\\d]+)\\s*\\)\\s*\\(\\s*([-\\.\\d]+)\\s*\\)\\s*\\(\\s*([-\\.\\d]+)\\s*\\)\\s*$"_s );
243 const QRegularExpressionMatch coordMatch = coordRx.match( string );
244 if ( coordMatch.hasMatch() )
245 {
246 const double hours = coordMatch.captured( 1 ).toDouble();
247 const double minutes = coordMatch.captured( 2 ).toDouble();
248 const double seconds = coordMatch.captured( 3 ).toDouble();
249 return hours + minutes / 60.0 + seconds / 3600.0;
250 }
251 else
252 {
253 return QVariant();
254 }
255}
256
257QVariantMap QgsImportPhotosAlgorithm::parseMetadataList( const QStringList &input )
258{
259 QVariantMap results;
260 const thread_local QRegularExpression splitRx( u"(.*?)=(.*)"_s );
261 for ( const QString &item : input )
262 {
263 const QRegularExpressionMatch match = splitRx.match( item );
264 if ( !match.hasMatch() )
265 continue;
266
267 const QString tag = match.captured( 1 );
268 QVariant value = parseMetadataValue( match.captured( 2 ) );
269
270 if ( tag == "EXIF_GPSLatitude"_L1 || tag == "EXIF_GPSLongitude"_L1 )
271 value = parseCoord( value.toString() );
272 results.insert( tag, value );
273 }
274 return results;
275}
276
277
278class SetEditorWidgetForPhotoAttributePostProcessor : public QgsProcessingLayerPostProcessorInterface
279{
280 public:
281 void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &, QgsProcessingFeedback * ) override
282 {
283 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
284 {
285 QVariantMap config;
286 // photo field shows picture viewer
287 config.insert( u"DocumentViewer"_s, 1 );
288 config.insert( u"FileWidget"_s, true );
289 config.insert( u"UseLink"_s, true );
290 config.insert( u"FullUrl"_s, true );
291 vl->setEditorWidgetSetup( vl->fields().lookupField( u"photo"_s ), QgsEditorWidgetSetup( u"ExternalResource"_s, config ) );
292
293 config.clear();
294 // path field is a directory link
295 config.insert( u"FileWidgetButton"_s, true );
296 config.insert( u"StorageMode"_s, 1 );
297 config.insert( u"UseLink"_s, true );
298 config.insert( u"FullUrl"_s, true );
299 vl->setEditorWidgetSetup( vl->fields().lookupField( u"directory"_s ), QgsEditorWidgetSetup( u"ExternalResource"_s, config ) );
300 }
301 }
302};
303
304QVariantMap QgsImportPhotosAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
305{
306 QGS_MARK_ALGORITHM_SOURCE
307
308 const QString folder = parameterAsFile( parameters, u"FOLDER"_s, context );
309
310 const QDir importDir( folder );
311 if ( !importDir.exists() )
312 {
313 throw QgsProcessingException( QObject::tr( "Directory %1 does not exist!" ).arg( folder ) );
314 }
315
316 const bool recurse = parameterAsBoolean( parameters, u"RECURSIVE"_s, context );
317
318 QgsFields outFields;
319 outFields.append( QgsField( u"photo"_s, QMetaType::Type::QString ) );
320 outFields.append( QgsField( u"filename"_s, QMetaType::Type::QString ) );
321 outFields.append( QgsField( u"directory"_s, QMetaType::Type::QString ) );
322 outFields.append( QgsField( u"altitude"_s, QMetaType::Type::Double ) );
323 outFields.append( QgsField( u"direction"_s, QMetaType::Type::Double ) );
324 outFields.append( QgsField( u"rotation"_s, QMetaType::Type::Int ) );
325 outFields.append( QgsField( u"longitude"_s, QMetaType::Type::QString ) );
326 outFields.append( QgsField( u"latitude"_s, QMetaType::Type::QString ) );
327 outFields.append( QgsField( u"timestamp"_s, QMetaType::Type::QDateTime ) );
328 QString outputDest;
329 std::unique_ptr<QgsFeatureSink> outputSink( parameterAsSink( parameters, u"OUTPUT"_s, context, outputDest, outFields, Qgis::WkbType::PointZ, QgsCoordinateReferenceSystem( u"EPSG:4326"_s ) ) );
330
331 QgsFields invalidFields;
332 invalidFields.append( QgsField( u"photo"_s, QMetaType::Type::QString ) );
333 invalidFields.append( QgsField( u"filename"_s, QMetaType::Type::QString ) );
334 invalidFields.append( QgsField( u"directory"_s, QMetaType::Type::QString ) );
335 invalidFields.append( QgsField( u"readable"_s, QMetaType::Type::Bool ) );
336 QString invalidDest;
337 std::unique_ptr<QgsFeatureSink> invalidSink( parameterAsSink( parameters, u"INVALID"_s, context, invalidDest, invalidFields ) );
338
339 const QStringList nameFilters { "*.jpeg", "*.jpg", "*.heic" };
340 QStringList files;
341
342 if ( !recurse )
343 {
344 const QFileInfoList fileInfoList = importDir.entryInfoList( nameFilters, QDir::NoDotAndDotDot | QDir::Files );
345 for ( auto infoIt = fileInfoList.constBegin(); infoIt != fileInfoList.constEnd(); ++infoIt )
346 {
347 files.append( infoIt->absoluteFilePath() );
348 }
349 }
350 else
351 {
352 QDirIterator it( folder, nameFilters, QDir::NoDotAndDotDot | QDir::Files, QDirIterator::Subdirectories );
353 while ( it.hasNext() )
354 {
355 it.next();
356 files.append( it.filePath() );
357 }
358 }
359
360 auto saveInvalidFile = [&invalidSink, &parameters, feedback, invalidDest]( QgsAttributes &attributes, bool readable ) {
361 if ( invalidSink )
362 {
363 QgsFeature f;
364 attributes.append( readable );
365 f.setAttributes( attributes );
366 if ( !invalidSink->addFeature( f, QgsFeatureSink::FastInsert ) )
367 throw QgsProcessingException( writeFeatureError( invalidSink.get(), parameters, u"INVALID"_s ) );
368 else
369 feedback->featureAddedToSink( u"INVALID"_s );
370 }
371 };
372
373 const double step = files.count() > 0 ? 100.0 / files.count() : 1;
374 int i = 0;
375 for ( const QString &file : files )
376 {
377 i++;
378 if ( feedback->isCanceled() )
379 {
380 break;
381 }
382
383 feedback->setProgress( i * step );
384
385 const QFileInfo fi( file );
386 QgsAttributes attributes;
387 attributes << QDir::toNativeSeparators( file ) << fi.completeBaseName() << QDir::toNativeSeparators( fi.absolutePath() );
388
389 const gdal::dataset_unique_ptr hDS( GDALOpen( file.toUtf8().constData(), GA_ReadOnly ) );
390 if ( !hDS )
391 {
392 feedback->reportError( QObject::tr( "Could not open %1" ).arg( QDir::toNativeSeparators( file ) ) );
393 saveInvalidFile( attributes, false );
394 continue;
395 }
396
397 CSLConstList GDALmetadata = GDALGetMetadata( hDS.get(), nullptr );
398 if ( !GDALmetadata )
399 {
400 GDALmetadata = GDALGetMetadata( hDS.get(), "EXIF" );
401 }
402 if ( !GDALmetadata )
403 {
404 feedback->reportError( QObject::tr( "No metadata found in %1" ).arg( QDir::toNativeSeparators( file ) ) );
405 saveInvalidFile( attributes, true );
406 }
407 else
408 {
409 if ( !outputSink )
410 continue;
411
412 QgsFeature f;
413 const QVariantMap metadata = parseMetadataList( QgsOgrUtils::cStringListToQStringList( GDALmetadata ) );
414
415 QgsPointXY tag;
416 if ( !extractGeoTagFromMetadata( metadata, tag ) )
417 {
418 // no geotag
419 feedback->reportError( QObject::tr( "Could not retrieve geotag for %1" ).arg( QDir::toNativeSeparators( file ) ) );
420 saveInvalidFile( attributes, true );
421 continue;
422 }
423
424 const QVariant altitude = extractAltitudeFromMetadata( metadata );
425 const QgsGeometry p = QgsGeometry( new QgsPoint( tag.x(), tag.y(), altitude.toDouble(), 0, Qgis::WkbType::PointZ ) );
426 f.setGeometry( p );
427
428 attributes << altitude << extractDirectionFromMetadata( metadata ) << extractOrientationFromMetadata( metadata ) << tag.x() << tag.y() << extractTimestampFromMetadata( metadata );
429 f.setAttributes( attributes );
430 if ( !outputSink->addFeature( f, QgsFeatureSink::FastInsert ) )
431 throw QgsProcessingException( writeFeatureError( outputSink.get(), parameters, u"OUTPUT"_s ) );
432 feedback->featureSinkFinalized( u"OUTPUT"_s );
433 }
434 }
435
436 QVariantMap outputs;
437 if ( outputSink )
438 {
439 outputSink->finalize();
440 feedback->featureSinkFinalized( u"OUTPUT"_s );
441 outputs.insert( u"OUTPUT"_s, outputDest );
442
443 if ( context.willLoadLayerOnCompletion( outputDest ) )
444 {
445 context.layerToLoadOnCompletionDetails( outputDest ).setPostProcessor( new SetEditorWidgetForPhotoAttributePostProcessor() );
446 }
447 }
448
449 if ( invalidSink )
450 {
451 invalidSink->finalize();
452 feedback->featureSinkFinalized( u"INVALID"_s );
453 outputs.insert( u"INVALID"_s, invalidDest );
454 }
455 return outputs;
456}
457
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3755
@ VectorPoint
Vector point layers.
Definition qgis.h:3750
@ Folder
Parameter is a folder.
Definition qgis.h:4009
@ PointZ
PointZ.
Definition qgis.h:313
A vector of attributes.
Represents a coordinate reference system (CRS).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
A geometry is the spatial representation of a feature.
static QStringList cStringListToQStringList(const char *const *stringList)
Converts a c string list to a QStringList.
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingContext::LayerDetails & layerToLoadOnCompletionDetails(const QString &layer)
Returns a reference to the details for a given layer which is loaded on completion of the algorithm o...
bool willLoadLayerOnCompletion(const QString &layer) const
Returns true if the given layer (by ID or datasource) will be loaded into the current project upon co...
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
An interface for layer post-processing handlers for execution following a processing algorithm operat...
virtual void postProcessLayer(QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback)=0
Post-processes the specified layer, following successful execution of a processing algorithm.
A boolean parameter for processing algorithms.
An input file or folder parameter for processing algorithms.
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.