QGIS API Documentation  3.19.0-Master (26212d215f)
qgsogrutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsogrutils.cpp
3  ---------------
4  begin : February 2016
5  copyright : (C) 2016 Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsogrutils.h"
17 #include "qgsapplication.h"
18 #include "qgslogger.h"
19 #include "qgsgeometry.h"
20 #include "qgsfields.h"
21 #include "qgslinestring.h"
22 #include "qgsmultipoint.h"
23 #include "qgsmultilinestring.h"
24 #include "qgsogrprovider.h"
25 #include <QTextCodec>
26 #include <QUuid>
27 #include <cpl_error.h>
28 #include <QJsonDocument>
29 #include <QFileInfo>
30 #include <QDir>
31 #include <QTextStream>
32 #include <QDataStream>
33 
34 #include "ogr_srs_api.h"
35 
36 // Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
37 // whereas previously there was only unset fields. For QGIS purposes, both
38 // states (unset/null) are equivalent.
39 #ifndef OGRNullMarker
40 #define OGR_F_IsFieldSetAndNotNull OGR_F_IsFieldSet
41 #endif
42 
43 
44 
45 void gdal::OGRDataSourceDeleter::operator()( OGRDataSourceH source )
46 {
47  OGR_DS_Destroy( source );
48 }
49 
50 
51 void gdal::OGRGeometryDeleter::operator()( OGRGeometryH geometry )
52 {
53  OGR_G_DestroyGeometry( geometry );
54 }
55 
56 void gdal::OGRFldDeleter::operator()( OGRFieldDefnH definition )
57 {
58  OGR_Fld_Destroy( definition );
59 }
60 
61 void gdal::OGRFeatureDeleter::operator()( OGRFeatureH feature )
62 {
63  OGR_F_Destroy( feature );
64 }
65 
67 {
68  GDALClose( dataset );
69 }
70 
71 void gdal::fast_delete_and_close( gdal::dataset_unique_ptr &dataset, GDALDriverH driver, const QString &path )
72 {
73  // see https://github.com/qgis/QGIS/commit/d024910490a39e65e671f2055c5b6543e06c7042#commitcomment-25194282
74  // faster if we close the handle AFTER delete, but doesn't work for windows
75 #ifdef Q_OS_WIN
76  // close dataset handle
77  dataset.reset();
78 #endif
79 
80  CPLPushErrorHandler( CPLQuietErrorHandler );
81  GDALDeleteDataset( driver, path.toUtf8().constData() );
82  CPLPopErrorHandler();
83 
84 #ifndef Q_OS_WIN
85  // close dataset handle
86  dataset.reset();
87 #endif
88 }
89 
90 
91 void gdal::GDALWarpOptionsDeleter::operator()( GDALWarpOptions *options )
92 {
93  GDALDestroyWarpOptions( options );
94 }
95 
96 QgsFeature QgsOgrUtils::readOgrFeature( OGRFeatureH ogrFet, const QgsFields &fields, QTextCodec *encoding )
97 {
98  QgsFeature feature;
99  if ( !ogrFet )
100  {
101  feature.setValid( false );
102  return feature;
103  }
104 
105  feature.setId( OGR_F_GetFID( ogrFet ) );
106  feature.setValid( true );
107 
108  if ( !readOgrFeatureGeometry( ogrFet, feature ) )
109  {
110  feature.setValid( false );
111  }
112 
113  if ( !readOgrFeatureAttributes( ogrFet, fields, feature, encoding ) )
114  {
115  feature.setValid( false );
116  }
117 
118  return feature;
119 }
120 
121 QgsFields QgsOgrUtils::readOgrFields( OGRFeatureH ogrFet, QTextCodec *encoding )
122 {
123  QgsFields fields;
124 
125  if ( !ogrFet )
126  return fields;
127 
128  int fieldCount = OGR_F_GetFieldCount( ogrFet );
129  for ( int i = 0; i < fieldCount; ++i )
130  {
131  OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, i );
132  if ( !fldDef )
133  {
134  fields.append( QgsField() );
135  continue;
136  }
137 
138  QString name = encoding ? encoding->toUnicode( OGR_Fld_GetNameRef( fldDef ) ) : QString::fromUtf8( OGR_Fld_GetNameRef( fldDef ) );
139  QVariant::Type varType;
140  switch ( OGR_Fld_GetType( fldDef ) )
141  {
142  case OFTInteger:
143  if ( OGR_Fld_GetSubType( fldDef ) == OFSTBoolean )
144  varType = QVariant::Bool;
145  else
146  varType = QVariant::Int;
147  break;
148  case OFTInteger64:
149  varType = QVariant::LongLong;
150  break;
151  case OFTReal:
152  varType = QVariant::Double;
153  break;
154  case OFTDate:
155  varType = QVariant::Date;
156  break;
157  case OFTTime:
158  varType = QVariant::Time;
159  break;
160  case OFTDateTime:
161  varType = QVariant::DateTime;
162  break;
163  case OFTString:
164  if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
165  varType = QVariant::Map;
166  else
167  varType = QVariant::String;
168  break;
169  default:
170  varType = QVariant::String; // other unsupported, leave it as a string
171  }
172  fields.append( QgsField( name, varType ) );
173  }
174  return fields;
175 }
176 
177 
178 QVariant QgsOgrUtils::getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsFields &fields, int attIndex, QTextCodec *encoding, bool *ok )
179 {
180  if ( attIndex < 0 || attIndex >= fields.count() )
181  {
182  if ( ok )
183  *ok = false;
184  return QVariant();
185  }
186 
187  const QgsField field = fields.at( attIndex );
188  return getOgrFeatureAttribute( ogrFet, field, attIndex, encoding, ok );
189 }
190 
191 QVariant QgsOgrUtils::getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsField &field, int attIndex, QTextCodec *encoding, bool *ok )
192 {
193  if ( !ogrFet || attIndex < 0 )
194  {
195  if ( ok )
196  *ok = false;
197  return QVariant();
198  }
199 
200  OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, attIndex );
201 
202  if ( ! fldDef )
203  {
204  if ( ok )
205  *ok = false;
206 
207  QgsDebugMsg( QStringLiteral( "ogrFet->GetFieldDefnRef(attindex) returns NULL" ) );
208  return QVariant();
209  }
210 
211  QVariant value;
212 
213  if ( ok )
214  *ok = true;
215 
216  if ( OGR_F_IsFieldSetAndNotNull( ogrFet, attIndex ) )
217  {
218  switch ( field.type() )
219  {
220  case QVariant::String:
221  {
222  if ( encoding )
223  value = QVariant( encoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attIndex ) ) );
224  else
225  value = QVariant( QString::fromUtf8( OGR_F_GetFieldAsString( ogrFet, attIndex ) ) );
226 
227 #ifdef Q_OS_WIN
228  // Fixes GH #41076 (empty strings shown as NULL), because we have checked before that it was NOT NULL
229  // Note: QVariant( QString( ) ).isNull( ) is still true on windows so we really need string literal :(
230  if ( value.isNull() )
231  value = QVariant( QStringLiteral( "" ) ); // skip-keyword-check
232 #endif
233 
234  break;
235  }
236  case QVariant::Int:
237  value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) );
238  break;
239  case QVariant::Bool:
240  value = QVariant( bool( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) ) );
241  break;
242  case QVariant::LongLong:
243  value = QVariant( OGR_F_GetFieldAsInteger64( ogrFet, attIndex ) );
244  break;
245  case QVariant::Double:
246  value = QVariant( OGR_F_GetFieldAsDouble( ogrFet, attIndex ) );
247  break;
248  case QVariant::Date:
249  case QVariant::DateTime:
250  case QVariant::Time:
251  {
252  int year, month, day, hour, minute, second, tzf;
253 
254  OGR_F_GetFieldAsDateTime( ogrFet, attIndex, &year, &month, &day, &hour, &minute, &second, &tzf );
255  if ( field.type() == QVariant::Date )
256  value = QDate( year, month, day );
257  else if ( field.type() == QVariant::Time )
258  value = QTime( hour, minute, second );
259  else
260  value = QDateTime( QDate( year, month, day ), QTime( hour, minute, second ) );
261  }
262  break;
263 
264  case QVariant::ByteArray:
265  {
266  int size = 0;
267  const GByte *b = OGR_F_GetFieldAsBinary( ogrFet, attIndex, &size );
268 
269  // QByteArray::fromRawData is funny. It doesn't take ownership of the data, so we have to explicitly call
270  // detach on it to force a copy which owns the data
271  QByteArray ba = QByteArray::fromRawData( reinterpret_cast<const char *>( b ), size );
272  ba.detach();
273 
274  value = ba;
275  break;
276  }
277 
278  case QVariant::List:
279  {
280  if ( field.subType() == QVariant::String )
281  {
282  QStringList list;
283  char **lst = OGR_F_GetFieldAsStringList( ogrFet, attIndex );
284  const int count = CSLCount( lst );
285  if ( count > 0 )
286  {
287  for ( int i = 0; i < count; i++ )
288  {
289  if ( encoding )
290  list << encoding->toUnicode( lst[i] );
291  else
292  list << QString::fromUtf8( lst[i] );
293  }
294  }
295  value = list;
296  }
297  else
298  {
299  Q_ASSERT_X( false, "QgsOgrUtils::getOgrFeatureAttribute", "unsupported field type" );
300  if ( ok )
301  *ok = false;
302  }
303  break;
304  }
305 
306  case QVariant::Map:
307  {
308  //it has to be JSON
309  //it's null if no json format
310  if ( encoding )
311  value = QJsonDocument::fromJson( encoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attIndex ) ).toUtf8() ).toVariant();
312  else
313  value = QJsonDocument::fromJson( QString::fromUtf8( OGR_F_GetFieldAsString( ogrFet, attIndex ) ).toUtf8() ).toVariant();
314  break;
315  }
316  default:
317  Q_ASSERT_X( false, "QgsOgrUtils::getOgrFeatureAttribute", "unsupported field type" );
318  if ( ok )
319  *ok = false;
320  }
321  }
322  else
323  {
324  value = QVariant( field.type() );
325  }
326 
327  return value;
328 }
329 
330 bool QgsOgrUtils::readOgrFeatureAttributes( OGRFeatureH ogrFet, const QgsFields &fields, QgsFeature &feature, QTextCodec *encoding )
331 {
332  // read all attributes
333  feature.initAttributes( fields.count() );
334  feature.setFields( fields );
335 
336  if ( !ogrFet )
337  return false;
338 
339  bool ok = false;
340  for ( int idx = 0; idx < fields.count(); ++idx )
341  {
342  QVariant value = getOgrFeatureAttribute( ogrFet, fields, idx, encoding, &ok );
343  if ( ok )
344  {
345  feature.setAttribute( idx, value );
346  }
347  }
348  return true;
349 }
350 
351 bool QgsOgrUtils::readOgrFeatureGeometry( OGRFeatureH ogrFet, QgsFeature &feature )
352 {
353  if ( !ogrFet )
354  return false;
355 
356  OGRGeometryH geom = OGR_F_GetGeometryRef( ogrFet );
357  if ( !geom )
358  feature.clearGeometry();
359  else
360  feature.setGeometry( ogrGeometryToQgsGeometry( geom ) );
361 
362  return true;
363 }
364 
365 std::unique_ptr< QgsPoint > ogrGeometryToQgsPoint( OGRGeometryH geom )
366 {
367  QgsWkbTypes::Type wkbType = static_cast<QgsWkbTypes::Type>( OGR_G_GetGeometryType( geom ) );
368 
369  double x, y, z, m;
370  OGR_G_GetPointZM( geom, 0, &x, &y, &z, &m );
371  return std::make_unique< QgsPoint >( wkbType, x, y, z, m );
372 }
373 
374 std::unique_ptr< QgsMultiPoint > ogrGeometryToQgsMultiPoint( OGRGeometryH geom )
375 {
376  std::unique_ptr< QgsMultiPoint > mp = std::make_unique< QgsMultiPoint >();
377 
378  const int count = OGR_G_GetGeometryCount( geom );
379  mp->reserve( count );
380  for ( int i = 0; i < count; ++i )
381  {
382  mp->addGeometry( ogrGeometryToQgsPoint( OGR_G_GetGeometryRef( geom, i ) ).release() );
383  }
384 
385  return mp;
386 }
387 
388 std::unique_ptr< QgsLineString > ogrGeometryToQgsLineString( OGRGeometryH geom )
389 {
390  QgsWkbTypes::Type wkbType = static_cast<QgsWkbTypes::Type>( OGR_G_GetGeometryType( geom ) );
391 
392  int count = OGR_G_GetPointCount( geom );
393  QVector< double > x( count );
394  QVector< double > y( count );
395  QVector< double > z;
396  double *pz = nullptr;
397  if ( QgsWkbTypes::hasZ( wkbType ) )
398  {
399  z.resize( count );
400  pz = z.data();
401  }
402  double *pm = nullptr;
403  QVector< double > m;
404  if ( QgsWkbTypes::hasM( wkbType ) )
405  {
406  m.resize( count );
407  pm = m.data();
408  }
409  OGR_G_GetPointsZM( geom, x.data(), sizeof( double ), y.data(), sizeof( double ), pz, sizeof( double ), pm, sizeof( double ) );
410 
411  return std::make_unique< QgsLineString>( x, y, z, m, wkbType == QgsWkbTypes::LineString25D );
412 }
413 
414 std::unique_ptr< QgsMultiLineString > ogrGeometryToQgsMultiLineString( OGRGeometryH geom )
415 {
416  std::unique_ptr< QgsMultiLineString > mp = std::make_unique< QgsMultiLineString >();
417 
418  const int count = OGR_G_GetGeometryCount( geom );
419  mp->reserve( count );
420  for ( int i = 0; i < count; ++i )
421  {
422  mp->addGeometry( ogrGeometryToQgsLineString( OGR_G_GetGeometryRef( geom, i ) ).release() );
423  }
424 
425  return mp;
426 }
427 
429 {
430  switch ( ogrGeomType )
431  {
432  case wkbUnknown: return QgsWkbTypes::Type::Unknown;
433  case wkbPoint: return QgsWkbTypes::Type::Point;
434  case wkbLineString: return QgsWkbTypes::Type::LineString;
435  case wkbPolygon: return QgsWkbTypes::Type::Polygon;
436  case wkbMultiPoint: return QgsWkbTypes::Type::MultiPoint;
437  case wkbMultiLineString: return QgsWkbTypes::Type::MultiLineString;
438  case wkbMultiPolygon: return QgsWkbTypes::Type::MultiPolygon;
439  case wkbGeometryCollection: return QgsWkbTypes::Type::GeometryCollection;
440  case wkbCircularString: return QgsWkbTypes::Type::CircularString;
441  case wkbCompoundCurve: return QgsWkbTypes::Type::CompoundCurve;
442  case wkbCurvePolygon: return QgsWkbTypes::Type::CurvePolygon;
443  case wkbMultiCurve: return QgsWkbTypes::Type::MultiCurve;
444  case wkbMultiSurface: return QgsWkbTypes::Type::MultiSurface;
445  case wkbCurve: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
446  case wkbSurface: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
447  case wkbPolyhedralSurface: return QgsWkbTypes::Type::Unknown; // no actual matching
448  case wkbTIN: return QgsWkbTypes::Type::Unknown; // no actual matching
449  case wkbTriangle: return QgsWkbTypes::Type::Triangle;
450 
451  case wkbNone: return QgsWkbTypes::Type::NoGeometry;
452  case wkbLinearRing: return QgsWkbTypes::Type::LineString; // approximate match
453 
454  case wkbCircularStringZ: return QgsWkbTypes::Type::CircularStringZ;
455  case wkbCompoundCurveZ: return QgsWkbTypes::Type::CompoundCurveZ;
456  case wkbCurvePolygonZ: return QgsWkbTypes::Type::CurvePolygonZ;
457  case wkbMultiCurveZ: return QgsWkbTypes::Type::MultiCurveZ;
458  case wkbMultiSurfaceZ: return QgsWkbTypes::Type::MultiSurfaceZ;
459  case wkbCurveZ: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
460  case wkbSurfaceZ: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
461  case wkbPolyhedralSurfaceZ: return QgsWkbTypes::Type::Unknown; // no actual matching
462  case wkbTINZ: return QgsWkbTypes::Type::Unknown; // no actual matching
463  case wkbTriangleZ: return QgsWkbTypes::Type::TriangleZ;
464 
465  case wkbPointM: return QgsWkbTypes::Type::PointM;
466  case wkbLineStringM: return QgsWkbTypes::Type::LineStringM;
467  case wkbPolygonM: return QgsWkbTypes::Type::PolygonM;
468  case wkbMultiPointM: return QgsWkbTypes::Type::MultiPointM;
469  case wkbMultiLineStringM: return QgsWkbTypes::Type::MultiLineStringM;
470  case wkbMultiPolygonM: return QgsWkbTypes::Type::MultiPolygonM;
471  case wkbGeometryCollectionM: return QgsWkbTypes::Type::GeometryCollectionM;
472  case wkbCircularStringM: return QgsWkbTypes::Type::CircularStringM;
473  case wkbCompoundCurveM: return QgsWkbTypes::Type::CompoundCurveM;
474  case wkbCurvePolygonM: return QgsWkbTypes::Type::CurvePolygonM;
475  case wkbMultiCurveM: return QgsWkbTypes::Type::MultiCurveM;
476  case wkbMultiSurfaceM: return QgsWkbTypes::Type::MultiSurfaceM;
477  case wkbCurveM: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
478  case wkbSurfaceM: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
479  case wkbPolyhedralSurfaceM: return QgsWkbTypes::Type::Unknown; // no actual matching
480  case wkbTINM: return QgsWkbTypes::Type::Unknown; // no actual matching
481  case wkbTriangleM: return QgsWkbTypes::Type::TriangleM;
482 
483  case wkbPointZM: return QgsWkbTypes::Type::PointZM;
484  case wkbLineStringZM: return QgsWkbTypes::Type::LineStringZM;
485  case wkbPolygonZM: return QgsWkbTypes::Type::PolygonZM;
486  case wkbMultiPointZM: return QgsWkbTypes::Type::MultiPointZM;
487  case wkbMultiLineStringZM: return QgsWkbTypes::Type::MultiLineStringZM;
488  case wkbMultiPolygonZM: return QgsWkbTypes::Type::MultiPolygonZM;
489  case wkbGeometryCollectionZM: return QgsWkbTypes::Type::GeometryCollectionZM;
490  case wkbCircularStringZM: return QgsWkbTypes::Type::CircularStringZM;
491  case wkbCompoundCurveZM: return QgsWkbTypes::Type::CompoundCurveZM;
492  case wkbCurvePolygonZM: return QgsWkbTypes::Type::CurvePolygonZM;
493  case wkbMultiCurveZM: return QgsWkbTypes::Type::MultiCurveZM;
494  case wkbMultiSurfaceZM: return QgsWkbTypes::Type::MultiSurfaceZM;
495  case wkbCurveZM: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
496  case wkbSurfaceZM: return QgsWkbTypes::Type::Unknown; // not an actual concrete type
497  case wkbPolyhedralSurfaceZM: return QgsWkbTypes::Type::Unknown; // no actual matching
498  case wkbTINZM: return QgsWkbTypes::Type::Unknown; // no actual matching
499  case wkbTriangleZM: return QgsWkbTypes::Type::TriangleZM;
500 
501  case wkbPoint25D: return QgsWkbTypes::Type::PointZ;
502  case wkbLineString25D: return QgsWkbTypes::Type::LineStringZ;
503  case wkbPolygon25D: return QgsWkbTypes::Type::PolygonZ;
504  case wkbMultiPoint25D: return QgsWkbTypes::Type::MultiPointZ;
505  case wkbMultiLineString25D: return QgsWkbTypes::Type::MultiLineStringZ;
506  case wkbMultiPolygon25D: return QgsWkbTypes::Type::MultiPolygonZ;
507  case wkbGeometryCollection25D: return QgsWkbTypes::Type::GeometryCollectionZ;
508  }
509 
510  // should not reach that point normally
511  return QgsWkbTypes::Type::Unknown;
512 }
513 
515 {
516  if ( !geom )
517  return QgsGeometry();
518 
519  const auto ogrGeomType = OGR_G_GetGeometryType( geom );
520  QgsWkbTypes::Type wkbType = ogrGeometryTypeToQgsWkbType( ogrGeomType );
521 
522  // optimised case for some geometry classes, avoiding wkb conversion on OGR/QGIS sides
523  // TODO - extend to other classes!
524  switch ( QgsWkbTypes::flatType( wkbType ) )
525  {
526  case QgsWkbTypes::Point:
527  {
528  return QgsGeometry( ogrGeometryToQgsPoint( geom ) );
529  }
530 
532  {
533  return QgsGeometry( ogrGeometryToQgsMultiPoint( geom ) );
534  }
535 
537  {
538  // optimised case for line -- avoid wkb conversion
539  return QgsGeometry( ogrGeometryToQgsLineString( geom ) );
540  }
541 
543  {
544  // optimised case for line -- avoid wkb conversion
546  }
547 
548  default:
549  break;
550  };
551 
552  // Fallback to inefficient WKB conversions
553 
554  if ( wkbFlatten( wkbType ) == wkbGeometryCollection )
555  {
556  // Shapefile MultiPatch can be reported as GeometryCollectionZ of TINZ
557  if ( OGR_G_GetGeometryCount( geom ) >= 1 &&
558  wkbFlatten( OGR_G_GetGeometryType( OGR_G_GetGeometryRef( geom, 0 ) ) ) == wkbTIN )
559  {
560  auto newGeom = OGR_G_ForceToMultiPolygon( OGR_G_Clone( geom ) );
561  auto ret = ogrGeometryToQgsGeometry( newGeom );
562  OGR_G_DestroyGeometry( newGeom );
563  return ret;
564  }
565  }
566 
567  // get the wkb representation
568  int memorySize = OGR_G_WkbSize( geom );
569  unsigned char *wkb = new unsigned char[memorySize];
570  OGR_G_ExportToWkb( geom, static_cast<OGRwkbByteOrder>( QgsApplication::endian() ), wkb );
571 
572  // Read original geometry type
573  uint32_t origGeomType;
574  memcpy( &origGeomType, wkb + 1, sizeof( uint32_t ) );
575  bool hasZ = ( origGeomType >= 1000 && origGeomType < 2000 ) || ( origGeomType >= 3000 && origGeomType < 4000 );
576  bool hasM = ( origGeomType >= 2000 && origGeomType < 3000 ) || ( origGeomType >= 3000 && origGeomType < 4000 );
577 
578  // PolyhedralSurface and TINs are not supported, map them to multipolygons...
579  if ( origGeomType % 1000 == 16 ) // is TIN, TINZ, TINM or TINZM
580  {
581  // TIN has the same wkb layout as a multipolygon, just need to overwrite the geom types...
582  int nDims = 2 + hasZ + hasM;
583  uint32_t newMultiType = static_cast<uint32_t>( QgsWkbTypes::zmType( QgsWkbTypes::MultiPolygon, hasZ, hasM ) );
584  uint32_t newSingleType = static_cast<uint32_t>( QgsWkbTypes::zmType( QgsWkbTypes::Polygon, hasZ, hasM ) );
585  unsigned char *wkbptr = wkb;
586 
587  // Endianness
588  wkbptr += 1;
589 
590  // Overwrite geom type
591  memcpy( wkbptr, &newMultiType, sizeof( uint32_t ) );
592  wkbptr += 4;
593 
594  // Geom count
595  uint32_t numGeoms;
596  memcpy( &numGeoms, wkb + 5, sizeof( uint32_t ) );
597  wkbptr += 4;
598 
599  // For each part, overwrite the geometry type to polygon (Z|M)
600  for ( uint32_t i = 0; i < numGeoms; ++i )
601  {
602  // Endianness
603  wkbptr += 1;
604 
605  // Overwrite geom type
606  memcpy( wkbptr, &newSingleType, sizeof( uint32_t ) );
607  wkbptr += sizeof( uint32_t );
608 
609  // skip coordinates
610  uint32_t nRings;
611  memcpy( &nRings, wkbptr, sizeof( uint32_t ) );
612  wkbptr += sizeof( uint32_t );
613 
614  for ( uint32_t j = 0; j < nRings; ++j )
615  {
616  uint32_t nPoints;
617  memcpy( &nPoints, wkbptr, sizeof( uint32_t ) );
618  wkbptr += sizeof( uint32_t ) + sizeof( double ) * nDims * nPoints;
619  }
620  }
621  }
622  else if ( origGeomType % 1000 == 15 ) // PolyhedralSurface, PolyhedralSurfaceZ, PolyhedralSurfaceM or PolyhedralSurfaceZM
623  {
624  // PolyhedralSurface has the same wkb layout as a MultiPolygon, just need to overwrite the geom type...
625  uint32_t newType = static_cast<uint32_t>( QgsWkbTypes::zmType( QgsWkbTypes::MultiPolygon, hasZ, hasM ) );
626  // Overwrite geom type
627  memcpy( wkb + 1, &newType, sizeof( uint32_t ) );
628  }
629 
630  QgsGeometry g;
631  g.fromWkb( wkb, memorySize );
632  return g;
633 }
634 
635 QgsFeatureList QgsOgrUtils::stringToFeatureList( const QString &string, const QgsFields &fields, QTextCodec *encoding )
636 {
637  QgsFeatureList features;
638  if ( string.isEmpty() )
639  return features;
640 
641  QString randomFileName = QStringLiteral( "/vsimem/%1" ).arg( QUuid::createUuid().toString() );
642 
643  // create memory file system object from string buffer
644  QByteArray ba = string.toUtf8();
645  VSIFCloseL( VSIFileFromMemBuffer( randomFileName.toUtf8().constData(), reinterpret_cast< GByte * >( ba.data() ),
646  static_cast< vsi_l_offset >( ba.size() ), FALSE ) );
647 
648  gdal::ogr_datasource_unique_ptr hDS( OGROpen( randomFileName.toUtf8().constData(), false, nullptr ) );
649  if ( !hDS )
650  {
651  VSIUnlink( randomFileName.toUtf8().constData() );
652  return features;
653  }
654 
655  OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS.get(), 0 );
656  if ( !ogrLayer )
657  {
658  hDS.reset();
659  VSIUnlink( randomFileName.toUtf8().constData() );
660  return features;
661  }
662 
664  while ( oFeat.reset( OGR_L_GetNextFeature( ogrLayer ) ), oFeat )
665  {
666  QgsFeature feat = readOgrFeature( oFeat.get(), fields, encoding );
667  if ( feat.isValid() )
668  features << feat;
669  }
670 
671  hDS.reset();
672  VSIUnlink( randomFileName.toUtf8().constData() );
673 
674  return features;
675 }
676 
677 QgsFields QgsOgrUtils::stringToFields( const QString &string, QTextCodec *encoding )
678 {
679  QgsFields fields;
680  if ( string.isEmpty() )
681  return fields;
682 
683  QString randomFileName = QStringLiteral( "/vsimem/%1" ).arg( QUuid::createUuid().toString() );
684 
685  // create memory file system object from buffer
686  QByteArray ba = string.toUtf8();
687  VSIFCloseL( VSIFileFromMemBuffer( randomFileName.toUtf8().constData(), reinterpret_cast< GByte * >( ba.data() ),
688  static_cast< vsi_l_offset >( ba.size() ), FALSE ) );
689 
690  gdal::ogr_datasource_unique_ptr hDS( OGROpen( randomFileName.toUtf8().constData(), false, nullptr ) );
691  if ( !hDS )
692  {
693  VSIUnlink( randomFileName.toUtf8().constData() );
694  return fields;
695  }
696 
697  OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS.get(), 0 );
698  if ( !ogrLayer )
699  {
700  hDS.reset();
701  VSIUnlink( randomFileName.toUtf8().constData() );
702  return fields;
703  }
704 
706  //read in the first feature only
707  if ( oFeat.reset( OGR_L_GetNextFeature( ogrLayer ) ), oFeat )
708  {
709  fields = readOgrFields( oFeat.get(), encoding );
710  }
711 
712  hDS.reset();
713  VSIUnlink( randomFileName.toUtf8().constData() );
714  return fields;
715 }
716 
717 QStringList QgsOgrUtils::cStringListToQStringList( char **stringList )
718 {
719  QStringList strings;
720 
721  // presume null terminated string list
722  for ( qgssize i = 0; stringList[i]; ++i )
723  {
724  strings.append( QString::fromUtf8( stringList[i] ) );
725  }
726 
727  return strings;
728 }
729 
731 {
732  if ( !srs )
733  return QString();
734 
735  char *pszWkt = nullptr;
736  const QByteArray multiLineOption = QStringLiteral( "MULTILINE=NO" ).toLocal8Bit();
737  const QByteArray formatOption = QStringLiteral( "FORMAT=WKT2" ).toLocal8Bit();
738  const char *const options[] = {multiLineOption.constData(), formatOption.constData(), nullptr};
739  OSRExportToWktEx( srs, &pszWkt, options );
740 
741  const QString res( pszWkt );
742  CPLFree( pszWkt );
743  return res;
744 }
745 
747 {
748  const QString wkt = OGRSpatialReferenceToWkt( srs );
749  if ( wkt.isEmpty() )
751 
753 }
754 
755 QString QgsOgrUtils::readShapefileEncoding( const QString &path )
756 {
757  const QString cpgEncoding = readShapefileEncodingFromCpg( path );
758  if ( !cpgEncoding.isEmpty() )
759  return cpgEncoding;
760 
761  return readShapefileEncodingFromLdid( path );
762 }
763 
764 QString QgsOgrUtils::readShapefileEncodingFromCpg( const QString &path )
765 {
766 #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,1,0)
767  QString errCause;
768  QgsOgrLayerUniquePtr layer = QgsOgrProviderUtils::getLayer( path, false, QStringList(), 0, errCause, false );
769  return layer ? layer->GetMetadataItem( QStringLiteral( "ENCODING_FROM_CPG" ), QStringLiteral( "SHAPEFILE" ) ) : QString();
770 #else
771  if ( !QFileInfo::exists( path ) )
772  return QString();
773 
774  // first try to read cpg file, if present
775  const QFileInfo fi( path );
776  const QString baseName = fi.completeBaseName();
777  const QString cpgPath = fi.dir().filePath( QStringLiteral( "%1.%2" ).arg( baseName, fi.suffix() == QLatin1String( "SHP" ) ? QStringLiteral( "CPG" ) : QStringLiteral( "cpg" ) ) );
778  if ( QFile::exists( cpgPath ) )
779  {
780  QFile cpgFile( cpgPath );
781  if ( cpgFile.open( QIODevice::ReadOnly ) )
782  {
783  QTextStream cpgStream( &cpgFile );
784  const QString cpgString = cpgStream.readLine();
785  cpgFile.close();
786 
787  if ( !cpgString.isEmpty() )
788  {
789  // from OGRShapeLayer::ConvertCodePage
790  // https://github.com/OSGeo/gdal/blob/master/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp#L342
791  bool ok = false;
792  int cpgCodePage = cpgString.toInt( &ok );
793  if ( ok && ( ( cpgCodePage >= 437 && cpgCodePage <= 950 )
794  || ( cpgCodePage >= 1250 && cpgCodePage <= 1258 ) ) )
795  {
796  return QStringLiteral( "CP%1" ).arg( cpgCodePage );
797  }
798  else if ( cpgString.startsWith( QLatin1String( "8859" ) ) )
799  {
800  if ( cpgString.length() > 4 && cpgString.at( 4 ) == '-' )
801  return QStringLiteral( "ISO-8859-%1" ).arg( cpgString.mid( 5 ) );
802  else
803  return QStringLiteral( "ISO-8859-%1" ).arg( cpgString.mid( 4 ) );
804  }
805  else if ( cpgString.startsWith( QLatin1String( "UTF-8" ), Qt::CaseInsensitive ) ||
806  cpgString.startsWith( QLatin1String( "UTF8" ), Qt::CaseInsensitive ) )
807  return QStringLiteral( "UTF-8" );
808  else if ( cpgString.startsWith( QLatin1String( "ANSI 1251" ), Qt::CaseInsensitive ) )
809  return QStringLiteral( "CP1251" );
810 
811  return cpgString;
812  }
813  }
814  }
815 
816  return QString();
817 #endif
818 }
819 
820 QString QgsOgrUtils::readShapefileEncodingFromLdid( const QString &path )
821 {
822 #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,1,0)
823  QString errCause;
824  QgsOgrLayerUniquePtr layer = QgsOgrProviderUtils::getLayer( path, false, QStringList(), 0, errCause, false );
825  return layer ? layer->GetMetadataItem( QStringLiteral( "ENCODING_FROM_LDID" ), QStringLiteral( "SHAPEFILE" ) ) : QString();
826 #else
827  // from OGRShapeLayer::ConvertCodePage
828  // https://github.com/OSGeo/gdal/blob/master/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp#L342
829 
830  if ( !QFileInfo::exists( path ) )
831  return QString();
832 
833  // first try to read cpg file, if present
834  const QFileInfo fi( path );
835  const QString baseName = fi.completeBaseName();
836 
837  // fallback to LDID value, read from DBF file
838  const QString dbfPath = fi.dir().filePath( QStringLiteral( "%1.%2" ).arg( baseName, fi.suffix() == QLatin1String( "SHP" ) ? QStringLiteral( "DBF" ) : QStringLiteral( "dbf" ) ) );
839  if ( QFile::exists( dbfPath ) )
840  {
841  QFile dbfFile( dbfPath );
842  if ( dbfFile.open( QIODevice::ReadOnly ) )
843  {
844  dbfFile.read( 29 );
845  QDataStream dbfIn( &dbfFile );
846  dbfIn.setByteOrder( QDataStream::LittleEndian );
847  quint8 ldid;
848  dbfIn >> ldid;
849  dbfFile.close();
850 
851  int nCP = -1; // Windows code page.
852 
853  // http://www.autopark.ru/ASBProgrammerGuide/DBFSTRUC.HTM
854  switch ( ldid )
855  {
856  case 1: nCP = 437; break;
857  case 2: nCP = 850; break;
858  case 3: nCP = 1252; break;
859  case 4: nCP = 10000; break;
860  case 8: nCP = 865; break;
861  case 10: nCP = 850; break;
862  case 11: nCP = 437; break;
863  case 13: nCP = 437; break;
864  case 14: nCP = 850; break;
865  case 15: nCP = 437; break;
866  case 16: nCP = 850; break;
867  case 17: nCP = 437; break;
868  case 18: nCP = 850; break;
869  case 19: nCP = 932; break;
870  case 20: nCP = 850; break;
871  case 21: nCP = 437; break;
872  case 22: nCP = 850; break;
873  case 23: nCP = 865; break;
874  case 24: nCP = 437; break;
875  case 25: nCP = 437; break;
876  case 26: nCP = 850; break;
877  case 27: nCP = 437; break;
878  case 28: nCP = 863; break;
879  case 29: nCP = 850; break;
880  case 31: nCP = 852; break;
881  case 34: nCP = 852; break;
882  case 35: nCP = 852; break;
883  case 36: nCP = 860; break;
884  case 37: nCP = 850; break;
885  case 38: nCP = 866; break;
886  case 55: nCP = 850; break;
887  case 64: nCP = 852; break;
888  case 77: nCP = 936; break;
889  case 78: nCP = 949; break;
890  case 79: nCP = 950; break;
891  case 80: nCP = 874; break;
892  case 87: return QStringLiteral( "ISO-8859-1" );
893  case 88: nCP = 1252; break;
894  case 89: nCP = 1252; break;
895  case 100: nCP = 852; break;
896  case 101: nCP = 866; break;
897  case 102: nCP = 865; break;
898  case 103: nCP = 861; break;
899  case 104: nCP = 895; break;
900  case 105: nCP = 620; break;
901  case 106: nCP = 737; break;
902  case 107: nCP = 857; break;
903  case 108: nCP = 863; break;
904  case 120: nCP = 950; break;
905  case 121: nCP = 949; break;
906  case 122: nCP = 936; break;
907  case 123: nCP = 932; break;
908  case 124: nCP = 874; break;
909  case 134: nCP = 737; break;
910  case 135: nCP = 852; break;
911  case 136: nCP = 857; break;
912  case 150: nCP = 10007; break;
913  case 151: nCP = 10029; break;
914  case 200: nCP = 1250; break;
915  case 201: nCP = 1251; break;
916  case 202: nCP = 1254; break;
917  case 203: nCP = 1253; break;
918  case 204: nCP = 1257; break;
919  default: break;
920  }
921 
922  if ( nCP != -1 )
923  {
924  return QStringLiteral( "CP%1" ).arg( nCP );
925  }
926  }
927  }
928  return QString();
929 #endif
930 }
static endian_t endian()
Returns whether this machine uses big or little endian.
This class represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromWkt(const QString &wkt)
Creates a CRS from a WKT spatial ref sys definition string.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
Definition: qgsfeature.cpp:236
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:209
void setFields(const QgsFields &fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
Definition: qgsfeature.cpp:169
void setId(QgsFeatureId id)
Sets the feature ID for this feature.
Definition: qgsfeature.cpp:114
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:158
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:195
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:190
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:144
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
QVariant::Type type
Definition: qgsfield.h:58
QVariant::Type subType() const
If the field is a collection, gets its element's type.
Definition: qgsfield.cpp:133
Container of fields for a vector layer.
Definition: qgsfields.h:45
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
void fromWkb(unsigned char *wkb, int length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length.
static QString readShapefileEncoding(const QString &path)
Reads the encoding of the shapefile at the specified path (where path is the location of the "....
static bool readOgrFeatureAttributes(OGRFeatureH ogrFet, const QgsFields &fields, QgsFeature &feature, QTextCodec *encoding)
Reads all attributes from an OGR feature into a QgsFeature.
static QgsGeometry ogrGeometryToQgsGeometry(OGRGeometryH geom)
Converts an OGR geometry representation to a QgsGeometry object.
static QString OGRSpatialReferenceToWkt(OGRSpatialReferenceH srs)
Returns a WKT string corresponding to the specified OGR srs object.
static QgsFeature readOgrFeature(OGRFeatureH ogrFet, const QgsFields &fields, QTextCodec *encoding)
Reads an OGR feature and converts it to a QgsFeature.
Definition: qgsogrutils.cpp:96
static QStringList cStringListToQStringList(char **stringList)
Converts a c string list to a QStringList.
static QgsFields readOgrFields(OGRFeatureH ogrFet, QTextCodec *encoding)
Reads an OGR feature and returns a corresponding fields collection.
static QgsWkbTypes::Type ogrGeometryTypeToQgsWkbType(OGRwkbGeometryType ogrGeomType)
Converts a OGRwkbGeometryType to QgsWkbTypes::Type.
static QgsCoordinateReferenceSystem OGRSpatialReferenceToCrs(OGRSpatialReferenceH srs)
Returns a QgsCoordinateReferenceSystem corresponding to the specified OGR srs object,...
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields, QTextCodec *encoding)
Attempts to parse a string representing a collection of features using OGR.
static QString readShapefileEncodingFromCpg(const QString &path)
Reads the encoding of the shapefile at the specified path (where path is the location of the "....
static bool readOgrFeatureGeometry(OGRFeatureH ogrFet, QgsFeature &feature)
Reads the geometry from an OGR feature into a QgsFeature.
static QgsFields stringToFields(const QString &string, QTextCodec *encoding)
Attempts to retrieve the fields from a string representing a collection of features using OGR.
static QString readShapefileEncodingFromLdid(const QString &path)
Reads the encoding of the shapefile at the specified path (where path is the location of the "....
static QVariant getOgrFeatureAttribute(OGRFeatureH ogrFet, const QgsFields &fields, int attIndex, QTextCodec *encoding, bool *ok=nullptr)
Retrieves an attribute value from an OGR feature.
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1100
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
static Type zmType(Type type, bool hasZ, bool hasM) SIP_HOLDGIL
Returns the modified input geometry type according to hasZ / hasM.
Definition: qgswkbtypes.h:801
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:702
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1050
void CORE_EXPORT fast_delete_and_close(dataset_unique_ptr &dataset, GDALDriverH driver, const QString &path)
Performs a fast close of an unwanted GDAL dataset handle by deleting the underlying data store.
Definition: qgsogrutils.cpp:71
std::unique_ptr< std::remove_pointer< OGRFeatureH >::type, OGRFeatureDeleter > ogr_feature_unique_ptr
Scoped OGR feature.
Definition: qgsogrutils.h:129
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
Definition: qgsogrutils.h:134
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
Definition: qgsogrutils.h:114
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
Definition: qgis.h:717
void * GDALDatasetH
void * OGRSpatialReferenceH
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:614
const QgsField & field
Definition: qgsfield.h:463
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
std::unique_ptr< QgsLineString > ogrGeometryToQgsLineString(OGRGeometryH geom)
std::unique_ptr< QgsMultiLineString > ogrGeometryToQgsMultiLineString(OGRGeometryH geom)
#define OGR_F_IsFieldSetAndNotNull
Definition: qgsogrutils.cpp:40
std::unique_ptr< QgsMultiPoint > ogrGeometryToQgsMultiPoint(OGRGeometryH geom)
std::unique_ptr< QgsPoint > ogrGeometryToQgsPoint(OGRGeometryH geom)
void CORE_EXPORT operator()(GDALDatasetH datasource)
Destroys an gdal dataset, using the correct gdal calls.
Definition: qgsogrutils.cpp:66
void CORE_EXPORT operator()(GDALWarpOptions *options)
Destroys GDAL warp options, using the correct gdal calls.
Definition: qgsogrutils.cpp:91
void CORE_EXPORT operator()(OGRDataSourceH source)
Destroys an OGR data source, using the correct gdal calls.
Definition: qgsogrutils.cpp:45
void CORE_EXPORT operator()(OGRFeatureH feature)
Destroys an OGR feature, using the correct gdal calls.
Definition: qgsogrutils.cpp:61
void CORE_EXPORT operator()(OGRFieldDefnH definition)
Destroys an OGR field definition, using the correct gdal calls.
Definition: qgsogrutils.cpp:56
void CORE_EXPORT operator()(OGRGeometryH geometry)
Destroys an OGR geometry, using the correct gdal calls.
Definition: qgsogrutils.cpp:51