• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MerginMaps / input / 6690232974

30 Oct 2023 08:34AM UTC coverage: 62.205% (-0.04%) from 62.243%
6690232974

Pull #2882

github

wonder-sk
code style
Pull Request #2882: Download data in parallel during sync

7686 of 12356 relevant lines covered (62.2%)

105.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.94
/app/projectwizard.cpp
1
/***************************************************************************
2
 *                                                                         *
3
 *   This program is free software; you can redistribute it and/or modify  *
4
 *   it under the terms of the GNU General Public License as published by  *
5
 *   the Free Software Foundation; either version 2 of the License, or     *
6
 *   (at your option) any later version.                                   *
7
 *                                                                         *
8
 ***************************************************************************/
9

10
#include "projectwizard.h"
11
#include "inpututils.h"
12
#include "coreutils.h"
13

14
#include "qgsproject.h"
15
#include "qgsvectortilelayer.h"
16
#include "qgsvectorlayer.h"
17
#include "qgsvectorfilewriter.h"
18
#include "qgsdatetimefieldformatter.h"
19
#include "qgsmarkersymbollayer.h"
20
#include "qgis.h"
21
#include "qgssymbollayer.h"
22
#include "qgssymbol.h"
23
#include "qgsmarkersymbol.h"
24
#include "qgssinglesymbolrenderer.h"
25
#include "inpututils.h"
26
#include "coreutils.h"
27

28
const QString TILES_URL = QStringLiteral( "https://tiles.merginmaps.com" );
29

30
ProjectWizard::ProjectWizard( const QString &dataDir, QObject *parent )
18✔
31
  : QObject( parent )
32
  , mDataDir( dataDir )
54✔
33
{
34

35
  mSettings = std::unique_ptr<QgsMapSettings>( new QgsMapSettings );
18✔
36
}
18✔
37

38
QgsVectorLayer *ProjectWizard::createGpkgLayer( QString const &projectDir, QList<FieldConfiguration> const &fieldsConfig )
×
39
{
40
  QString gpkgName( QStringLiteral( "data" ) );
×
41
  QString projectGpkgPath( QString( "%1/%2.%3" ).arg( projectDir ).arg( gpkgName ).arg( "gpkg" ) );
×
42
  QString layerName( QStringLiteral( "Survey" ) );
×
43
  QgsCoordinateReferenceSystem layerCrs( LAYER_CRS_ID );
×
44
  QgsFields predefinedFields = createFields( fieldsConfig );
×
45

46
  // Write layer as gpkg
47
  QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "PointZ?crs=%1" ).arg( LAYER_CRS_ID ), layerName, "memory" );
×
48
  layer->startEditing();
×
49
  layer->setCrs( layerCrs );
×
50
  for ( QgsField f : predefinedFields )
×
51
  {
52
    layer->addAttribute( f );
×
53
  }
×
54
  layer->updateFields();
×
55
  layer->commitChanges();
×
56

57
  QgsVectorFileWriter::SaveVectorOptions options;
×
58
  options.driverName = QStringLiteral( "GPKG" );
×
59
  options.layerName = layerName;
×
60
  options.fileEncoding = QStringLiteral( "UTF-8" );
×
61
  std::unique_ptr< QgsVectorFileWriter > writer( QgsVectorFileWriter::create( projectGpkgPath, predefinedFields, Qgis::WkbType::PointZ, layerCrs, QgsCoordinateTransformContext(), options ) );
×
62

63

64
  QString errorMessage;
×
65
  QgsVectorFileWriter::writeAsVectorFormatV2(
×
66
    layer,
67
    projectGpkgPath,
68
    layer->transformContext(),
×
69
    options,
70
    nullptr,
71
    nullptr,
72
    &errorMessage );
73

74
  // Check and configure layer
75
  QgsVectorLayer *l = new QgsVectorLayer( projectGpkgPath, layerName, "ogr" );
×
76

77
  Q_ASSERT( l->isValid() );
×
78

79
  l->setCrs( layerCrs );
×
80
  for ( int i = 0; i < l->fields().count(); ++i )
×
81
  {
82
    QgsField f = l->fields().at( i );
×
83
    QgsEditorWidgetSetup setup = InputUtils::getEditorWidgetSetup( f, findWidgetTypeByFieldName( f.name(), fieldsConfig ) );
×
84
    l->setEditorWidgetSetup( i, setup );
×
85
  }
×
86
  l->setRenderer( surveyLayerRenderer() );
×
87

88
  return l;
×
89
}
×
90

91
void ProjectWizard::createProject( QString const &projectName, FieldsModel *fieldsModel )
×
92
{
93
  if ( !CoreUtils::isValidName( projectName ) )
×
94
  {
95
    emit projectCreationFailed( tr( "Project name contains invalid characters" ) );
×
96
    return;
×
97
  }
98

99
  QString projectDir = CoreUtils::createUniqueProjectDirectory( mDataDir, projectName );
×
100
  QString projectFilepath( QString( "%1/%2.%3" ).arg( projectDir ).arg( projectName ).arg( "qgz" ) );
×
101
  QString gpkgName( QStringLiteral( "data" ) );
×
102
  QString projectGpkgPath( QString( "%1/%2.%3" ).arg( projectDir ).arg( gpkgName ).arg( "gpkg" ) );
×
103

104
  QgsProject project;
×
105

106
  // add layers
107
  QgsDataSourceUri dsUri;
×
108
  dsUri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
×
109
  dsUri.setParam( QStringLiteral( "url" ), QStringLiteral( "%1/data/default/{z}/{x}/{y}.pbf" ).arg( TILES_URL ) );
×
110
  dsUri.setParam( QStringLiteral( "styleUrl" ), QStringLiteral( "%1/styles/default.json" ).arg( TILES_URL ) );
×
111
  dsUri.setParam( QStringLiteral( "zmin" ), QStringLiteral( "0" ) );
×
112
  dsUri.setParam( QStringLiteral( "zmax" ), QStringLiteral( "14" ) );
×
113
  QgsVectorTileLayer *bgLayer = new QgsVectorTileLayer( dsUri.encodedUri(), QStringLiteral( "OpenMapTiles (OSM)" ) );
×
114
  bool ok;
115
  QString error = bgLayer->loadDefaultStyle( ok );
×
116
  QgsLayerMetadata metadata;
×
117
  metadata.setRights( QStringList() << QStringLiteral( "© OpenMapTiles © OpenStreetMap contributors" ) );
×
118
  bgLayer->setMetadata( metadata );
×
119
  QgsVectorLayer *layer = createGpkgLayer( projectDir, fieldsModel->fields() );
×
120
  QList<QgsMapLayer *> layers;
×
121
  layers << layer << bgLayer;
×
122
  project.addMapLayers( layers );
×
123

124
  // Configurate mapSettings
125
  QgsCoordinateReferenceSystem projectCrs( PROJECT_CRS_ID );
×
126
  mSettings->setExtent( bgLayer->extent() );
×
127
  mSettings->setEllipsoid( "WGS84" );
×
128
  mSettings->setDestinationCrs( projectCrs );
×
129
  mSettings->setLayers( layers );
×
130

131
  // Using writeProject signal to append mapCanvas project setting
132
  connect( &project, &QgsProject::writeProject,
×
133
           this, &ProjectWizard::writeMapCanvasSetting );
134

135
  project.setCrs( projectCrs );
×
136
  project.writePath( projectGpkgPath );
×
137
  project.write( projectFilepath );
×
138

139
  emit notify( tr( "Project %1 created" ).arg( projectName ) );
×
140
  emit projectCreated( projectDir, projectName );
×
141
}
×
142

143
void ProjectWizard::writeMapCanvasSetting( QDomDocument &doc )
×
144
{
145
  QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "qgis" ) );
×
146
  if ( !nl.count() )
×
147
  {
148
    QgsDebugError( QStringLiteral( "Unable to find qgis element in project file" ) );
149
    return;
×
150
  }
151
  QDomNode qgisNode = nl.item( 0 );  // there should only be one, so zeroth element OK
×
152

153
  QDomElement mapcanvasNode = doc.createElement( QStringLiteral( "mapcanvas" ) );
×
154
  mapcanvasNode.setAttribute( QStringLiteral( "annotationsVisible" ), false );
×
155
  qgisNode.appendChild( mapcanvasNode );
×
156
  mSettings->writeXml( mapcanvasNode, doc );
×
157
}
×
158

159
QgsFields ProjectWizard::createFields( const QList<FieldConfiguration> fieldsConfig ) const
×
160
{
161

162
  QgsFields fields;
×
163
  for ( const FieldConfiguration &fc : fieldsConfig )
×
164
  {
165
    QString type = widgetToType( fc.widgetType );
×
166
    QVariant::Type qtype = parseType( type );
×
167
    QgsField field( fc.attributeName, qtype, type );
×
168
    fields.append( field );
×
169
  }
×
170
  return fields;
×
171
}
×
172

173
QgsSingleSymbolRenderer *ProjectWizard::surveyLayerRenderer()
×
174
{
175
  QgsSimpleMarkerSymbolLayer *markerLayer = new QgsSimpleMarkerSymbolLayer( Qgis::MarkerShape::Circle );
×
176
  markerLayer->setSize( 3.0 );
×
177
  markerLayer->setFillColor( QColor( "#d73027" ) );
×
178
  markerLayer->setStrokeColor( QColor( "#e8e8e8" ) );
×
179
  markerLayer->setStrokeWidth( 0.4 );
×
180
  QgsMarkerSymbol *symbol = new QgsMarkerSymbol( QgsSymbolLayerList() << markerLayer );
×
181
  return new QgsSingleSymbolRenderer( symbol );
×
182
}
183

184
QVariant::Type ProjectWizard::parseType( const QString &type ) const
×
185
{
186
  if ( type == QLatin1String( "text" ) )
×
187
    return QVariant::String;
×
188
  else if ( type == QLatin1String( "integer" ) )
×
189
    return QVariant::Int;
×
190
  else if ( type == QLatin1String( "integer64" ) )
×
191
    return QVariant::Int;
×
192
  else if ( type == QLatin1String( "real" ) )
×
193
    return QVariant::Double;
×
194
  else if ( type == QLatin1String( "date" ) )
×
195
    return QVariant::Date;
×
196
  else if ( type == QLatin1String( "datetime" ) )
×
197
    return QVariant::DateTime;
×
198
  else if ( type == QLatin1String( "bool" ) )
×
199
    return QVariant::Bool;
×
200
  else if ( type == QLatin1String( "binary" ) )
×
201
    return QVariant::ByteArray;
×
202

203
  return QVariant::Invalid;
×
204
}
205

206
QString ProjectWizard::widgetToType( const QString &widgetType ) const
×
207
{
208
  if ( widgetType == QStringLiteral( "TextEdit" ) )
×
209
    return QStringLiteral( "text" );
×
210
  else if ( widgetType == QStringLiteral( "Range" ) )
×
211
    return QStringLiteral( "integer" );
×
212
  else if ( widgetType == QStringLiteral( "DateTime" ) )
×
213
    return QStringLiteral( "datetime" );
×
214
  else if ( widgetType == QStringLiteral( "CheckBox" ) )
×
215
    return QStringLiteral( "bool" );
×
216
  else if ( widgetType == QStringLiteral( "ExternalResource" ) )
×
217
    return QStringLiteral( "text" );
×
218

219
  return QStringLiteral( "text" );
×
220
}
221

222
QString ProjectWizard::findWidgetTypeByFieldName( const QString name, const QList<FieldConfiguration> fieldsConfig ) const
×
223
{
224

225
  for ( int i = 0; i < fieldsConfig.count(); ++i )
×
226
  {
227
    if ( fieldsConfig.at( i ).attributeName == name )
×
228
      return fieldsConfig.at( i ).widgetType;
×
229
  }
230
  return QString( "TextEdit" );
×
231
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc