11/*
2- * Copyright 2016-2018 Kai Pastor
2+ * Copyright 2016-2019 Kai Pastor
33 *
44 * This file is part of OpenOrienteering.
55 *
2121#include " ogr_file_format_p.h" // IWYU pragma: associated
2222
2323#include < algorithm>
24+ #include < cmath>
2425#include < cstddef>
2526#include < functional>
2627#include < iterator>
@@ -413,7 +414,7 @@ namespace {
413414 }
414415
415416
416- class AverageLatLon
417+ class AverageCoords
417418 {
418419 private:
419420 double x = 0 ;
@@ -452,14 +453,8 @@ namespace {
452453 }
453454
454455 public:
455- AverageLatLon (OGRDataSourceH data_source)
456+ AverageCoords (OGRDataSourceH data_source, OGRDataSourceH srs )
456457 {
457- auto geo_srs = ogr::unique_srs { OSRNewSpatialReference (nullptr ) };
458- OSRSetWellKnownGeogCS (geo_srs.get (), " WGS84" );
459- #if GDAL_VERSION_MAJOR >= 3
460- OSRSetAxisMappingStrategy (geo_srs.get (), OAMS_TRADITIONAL_GIS_ORDER);
461- #endif
462-
463458 auto num_layers = OGR_DS_GetLayerCount (data_source);
464459 for (int i = 0 ; i < num_layers; ++i)
465460 {
@@ -469,7 +464,7 @@ namespace {
469464 if (!spatial_reference)
470465 continue ;
471466
472- auto transformation = ogr::unique_transformation{ OCTNewCoordinateTransformation (spatial_reference, geo_srs. get () ) };
467+ auto transformation = ogr::unique_transformation{ OCTNewCoordinateTransformation (spatial_reference, srs ) };
473468 if (!transformation)
474469 continue ;
475470
@@ -490,9 +485,9 @@ namespace {
490485 }
491486 }
492487
493- operator LatLon () const
488+ operator QPointF () const
494489 {
495- return num_coords ? LatLon{ y / num_coords, x / num_coords } : LatLon {};
490+ return num_coords ? QPointF{ x / num_coords, y / num_coords } : QPointF {};
496491 }
497492
498493 };
@@ -546,7 +541,6 @@ OgrFileImport::OgrFileImport(const QString& path, Map* map, MapView* view, UnitT
546541 : Importer(path, map, view)
547542 , manager{ OGR_SM_Create (nullptr ) }
548543 , unit_type{ unit_type }
549- , georeferencing_import_enabled{ true }
550544{
551545 GdalManager ().configure ();
552546
@@ -826,13 +820,16 @@ ogr::unique_srs OgrFileImport::importGeoreferencing(OGRDataSourceH data_source)
826820 if (projected_srs_spec)
827821 {
828822 // Found a suitable projected SRS
823+ auto center = calcAverageCoords (data_source, suitable_srs.get ());
829824 auto georef = map->getGeoreferencing (); // copy
830825 georef.setProjectedCRS (QStringLiteral (" PROJ.4" ), QString::fromLatin1 (projected_srs_spec));
826+ georef.setProjectedRefPoint ({std::round (center.x ()), std::round (center.y ())});
831827 map->setGeoreferencing (georef);
832828 CPLFree (projected_srs_spec);
833829 return suitable_srs;
834830 }
835- else if (suitable_srs)
831+
832+ if (suitable_srs)
836833 {
837834 // Found a suitable SRS but it is not projected.
838835 // Setting up a local orthographic projection.
@@ -850,18 +847,17 @@ ogr::unique_srs OgrFileImport::importGeoreferencing(OGRDataSourceH data_source)
850847 map->setGeoreferencing (ortho_georef);
851848 return srsFromMap ();
852849 }
853- else if (local_srs || no_srs)
850+
851+ if (local_srs || no_srs)
854852 {
855853 auto georef = Georeferencing ();
856854 georef.setScaleDenominator (int (map->getScaleDenominator ()));
857855 georef.setDeclination (map->getGeoreferencing ().getDeclination ());
858856 map->setGeoreferencing (georef);
859857 return local_srs ? std::move (local_srs) : srsFromMap ();
860858 }
861- else
862- {
863- throw FileFormatException (tr (" The geospatial data has no suitable spatial reference." ));
864- }
859+
860+ throw FileFormatException (tr (" The geospatial data has no suitable spatial reference." ));
865861}
866862
867863
@@ -1003,7 +999,8 @@ Object* OgrFileImport::importPointGeometry(OGRFeatureH feature, OGRGeometryH geo
1003999 object->setPosition (toMapCoord (OGR_G_GetX (geometry, 0 ), OGR_G_GetY (geometry, 0 )));
10041000 return object;
10051001 }
1006- else if (symbol->getType () == Symbol::Text)
1002+
1003+ if (symbol->getType () == Symbol::Text)
10071004 {
10081005 const auto & description = symbol->getDescription ();
10091006 auto length = description.length ();
@@ -1038,7 +1035,7 @@ Object* OgrFileImport::importPointGeometry(OGRFeatureH feature, OGRGeometryH geo
10381035 applyLabelAnchor (anchor, object);
10391036 }
10401037
1041- auto angle = QStringRef (&description, 3 , split-3 ).toFloat (&ok);
1038+ auto angle = QStringRef (&description, 3 , split-3 ).toDouble (&ok);
10421039 if (ok)
10431040 {
10441041 object->setRotation (qDegreesToRadians (angle));
@@ -1124,7 +1121,7 @@ PathObject* OgrFileImport::importPolygonGeometry(OGRFeatureH feature, OGRGeometr
11241121
11251122Symbol* OgrFileImport::getSymbol (Symbol::Type type, const char * raw_style_string)
11261123{
1127- auto style_string = QByteArray::fromRawData (raw_style_string, qstrlen (raw_style_string));
1124+ auto style_string = QByteArray::fromRawData (raw_style_string, int ( qstrlen (raw_style_string) ));
11281125 Symbol* symbol = nullptr ;
11291126 switch (type)
11301127 {
@@ -1167,7 +1164,7 @@ Symbol* OgrFileImport::getSymbol(Symbol::Type type, const char* raw_style_string
11671164
11681165MapColor* OgrFileImport::makeColor (OGRStyleToolH tool, const char * color_string)
11691166{
1170- auto key = QByteArray::fromRawData (color_string, qstrlen (color_string));
1167+ auto key = QByteArray::fromRawData (color_string, int ( qstrlen (color_string) ));
11711168 auto color = colors.value (key);
11721169 if (!color)
11731170 {
@@ -1372,7 +1369,7 @@ PointSymbol* OgrFileImport::getSymbolForOgrSymbol(OGRStyleToolH tool, const QByt
13721369 break ;
13731370 default :
13741371 return nullptr ;
1375- };
1372+ }
13761373
13771374 int is_null;
13781375 auto color_string = OGR_ST_GetParamStr (tool, color_key, &is_null);
@@ -1415,7 +1412,7 @@ TextSymbol* OgrFileImport::getSymbolForLabel(OGRStyleToolH tool, const QByteArra
14151412
14161413 // Don't use the style string as a key: The style contains the label.
14171414 QByteArray key;
1418- key.reserve (qstrlen (color_string) + qstrlen (font_size_string) + 1 );
1415+ key.reserve (int ( qstrlen (color_string) + qstrlen (font_size_string) + 1 ) );
14191416 key.append (color_string);
14201417 key.append (font_size_string);
14211418 auto text_symbol = static_cast <TextSymbol*>(text_symbols.value (key));
@@ -1449,7 +1446,7 @@ TextSymbol* OgrFileImport::getSymbolForLabel(OGRStyleToolH tool, const QByteArra
14491446 angle = 0.0 ;
14501447
14511448 QString description;
1452- description.reserve (qstrlen (label_string) + 100 );
1449+ description.reserve (int ( qstrlen (label_string) + 100 ) );
14531450 description.append (QString::number (100 + anchor));
14541451 description.append (QString::number (angle, ' g' , 1 ));
14551452 description.append (QLatin1Char (' ' ));
@@ -1464,7 +1461,7 @@ LineSymbol* OgrFileImport::getSymbolForPen(OGRStyleToolH tool, const QByteArray&
14641461 Q_ASSERT (OGR_ST_GetType (tool) == OGRSTCPen);
14651462
14661463 auto raw_tool_key = OGR_ST_GetStyleString (tool);
1467- auto tool_key = QByteArray::fromRawData (raw_tool_key, qstrlen (raw_tool_key));
1464+ auto tool_key = QByteArray::fromRawData (raw_tool_key, int ( qstrlen (raw_tool_key) ));
14681465 auto symbol = line_symbols.value (tool_key);
14691466 if (symbol && symbol->getType () == Symbol::Line)
14701467 return static_cast <LineSymbol*>(symbol);
@@ -1496,7 +1493,7 @@ AreaSymbol* OgrFileImport::getSymbolForBrush(OGRStyleToolH tool, const QByteArra
14961493 Q_ASSERT (OGR_ST_GetType (tool) == OGRSTCBrush);
14971494
14981495 auto raw_tool_key = OGR_ST_GetStyleString (tool);
1499- auto tool_key = QByteArray::fromRawData (raw_tool_key, qstrlen (raw_tool_key));
1496+ auto tool_key = QByteArray::fromRawData (raw_tool_key, int ( qstrlen (raw_tool_key) ));
15001497 auto symbol = area_symbols.value (tool_key);
15011498 if (symbol && symbol->getType () == Symbol::Area)
15021499 return static_cast <AreaSymbol*>(symbol);
@@ -1608,9 +1605,24 @@ LatLon OgrFileImport::calcAverageLatLon(const QString& path)
16081605// static
16091606LatLon OgrFileImport::calcAverageLatLon (OGRDataSourceH data_source)
16101607{
1611- return AverageLatLon (data_source);
1608+ auto geo_srs = ogr::unique_srs { OSRNewSpatialReference (nullptr ) };
1609+ OSRSetWellKnownGeogCS (geo_srs.get (), " WGS84" );
1610+ #if GDAL_VERSION_MAJOR >= 3
1611+ OSRSetAxisMappingStrategy (geo_srs.get (), OAMS_TRADITIONAL_GIS_ORDER);
1612+ #endif
1613+
1614+ auto const average = calcAverageCoords (data_source, geo_srs.get ());
1615+ return {average.y (), average.x ()};
16121616}
16131617
1618+
1619+ // static
1620+ QPointF OgrFileImport::calcAverageCoords (OGRDataSourceH data_source, OGRDataSourceH srs)
1621+ {
1622+ return QPointF{AverageCoords (data_source, srs)};
1623+ }
1624+
1625+
16141626// ### OgrFileExport ###
16151627
16161628OgrFileExport::OgrFileExport (const QString& path, const Map* map, const MapView* view)
@@ -1997,7 +2009,7 @@ void OgrFileExport::addTextToLayer(OGRLayerH layer, const std::function<bool (co
19972009 {
19982010 // There is no label field, or the text is too long.
19992011 // Put the text directly in the label.
2000- text.replace (QRegularExpression (QLatin1String (" ([ \ "\\\\ ] )" ), QRegularExpression::MultilineOption), QLatin1String (" \\\\ 1" ));
2012+ text.replace (QRegularExpression (QLatin1String (R"( ([ "\\]) )" ), QRegularExpression::MultilineOption), QLatin1String (" \\\\ 1" ));
20012013 style.replace (" {Name}" , text.toUtf8 ());
20022014 }
20032015 OGR_F_SetStyleString (po_feature.get (), style);
@@ -2016,7 +2028,7 @@ void OgrFileExport::addLinesToLayer(OGRLayerH layer, const std::function<bool (c
20162028 auto add_feature = [&](const Object* object) {
20172029 const auto * symbol = object->getSymbol ();
20182030 const auto * path = object->asPath ();
2019- if (path->parts ().size () < 1 )
2031+ if (path->parts ().empty () )
20202032 return ;
20212033
20222034 QString sym_name = symbol->getPlainTextName ();
@@ -2057,7 +2069,7 @@ void OgrFileExport::addAreasToLayer(OGRLayerH layer, const std::function<bool (c
20572069 auto add_feature = [&](const Object* object) {
20582070 const auto * symbol = object->getSymbol ();
20592071 const auto * path = object->asPath ();
2060- if (path->parts ().size () < 1 )
2072+ if (path->parts ().empty () )
20612073 return ;
20622074
20632075 auto po_feature = ogr::unique_feature (OGR_F_Create (OGR_L_GetLayerDefn (layer)));
0 commit comments