1 /*
2  *    Copyright 2019, 2020 Kai Pastor
3  *
4  *    This file is part of OpenOrienteering.
5  *
6  *    OpenOrienteering is free software: you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation, either version 3 of the License, or
9  *    (at your option) any later version.
10  *
11  *    OpenOrienteering is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "gdal_template.h"
21 
22 #include <QtGlobal>
23 #include <QByteArray>
24 #include <QImageReader>
25 #include <QString>
26 
27 #include "gdal/gdal_image_reader.h"
28 #include "gdal/gdal_manager.h"
29 
30 namespace OpenOrienteering {
31 
32 // static
supportedExtensions()33 const std::vector<QByteArray>& GdalTemplate::supportedExtensions()
34 {
35 	return GdalManager().supportedRasterExtensions();
36 }
37 
38 
GdalTemplate(const QString & path,Map * map)39 GdalTemplate::GdalTemplate(const QString& path, Map* map)
40 : TemplateImage(path, map)
41 {}
42 
43 GdalTemplate::GdalTemplate(const GdalTemplate& proto) = default;
44 
45 GdalTemplate::~GdalTemplate() = default;
46 
duplicate() const47 GdalTemplate* GdalTemplate::duplicate() const
48 {
49 	return new GdalTemplate(*this);
50 }
51 
getTemplateType() const52 const char* GdalTemplate::getTemplateType() const
53 {
54 	return "GdalTemplate";
55 }
56 
loadTemplateFileImpl(bool configuring)57 bool GdalTemplate::loadTemplateFileImpl(bool configuring)
58 {
59 	GdalImageReader reader(template_path);
60 	if (!reader.canRead())
61 	{
62 		setErrorString(reader.errorString());
63 		return false;
64 	}
65 
66 	qDebug("GdalTemplate: Using GDAL driver '%s'", reader.format().constData());
67 
68 	if (!reader.read(&image))
69 	{
70 		setErrorString(reader.errorString());
71 
72 		QImageReader image_reader(template_path);
73 		if (image_reader.canRead())
74 		{
75 			qDebug("GdalTemplate: Falling back to QImageReader, reason: %s", qPrintable(errorString()));
76 			if (!image_reader.read(&image))
77 			{
78 				setErrorString(image_reader.errorString());
79 				return false;
80 			}
81 		}
82 	}
83 
84 	// Duplicated from TemplateImage, for compatibility
85 	available_georef = findAvailableGeoreferencing(reader.readGeoTransform());
86 	if (!configuring && is_georeferenced)
87 	{
88 		if (!isGeoreferencingUsable())
89 		{
90 			// Image was georeferenced, but georeferencing info is gone -> deny to load template
91 			setErrorString(::OpenOrienteering::TemplateImage::tr("Georeferencing not found"));
92 			return false;
93 		}
94 
95 		calculateGeoreferencing();
96 	}
97 
98 	return true;
99 }
100 
101 
102 }  // namespace OpenOrienteering
103