1 /***************************************************************************
2   qgsvirtuallayerblob.h : Functions to manipulate SpatiaLite geometry blobs
3 begin                : Nov 2015
4 copyright            : (C) 2015 Hugo Mercier, Oslandia
5 email                : hugo dot mercier at oslandia dot com
6  ***************************************************************************/
7 
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 
17 #ifndef QGSVIRTUALLAYER_BLOB_H
18 #define QGSVIRTUALLAYER_BLOB_H
19 
20 #include <cstdint>
21 
22 #include "qgsgeometry.h"
23 
24 // BLOB header
25 // name    size    value
26 // start     1      00
27 // endian    1      01
28 // srid      4      int
29 // mbr_min_x 8      double
30 // mbr_min_y 8      double
31 // mbr_max_x 8      double
32 // mbr_max_y 8      double
33 // mbr_end   1      7C
34 struct SpatialiteBlobHeader
35 {
36   unsigned char start = 0x00;
37   unsigned char endianness = 0x01;
38   int32_t srid = -1;
39   double mbrMinX = std::numeric_limits<double>::lowest();
40   double mbrMinY = std::numeric_limits<double>::lowest();
41   double mbrMaxX = std::numeric_limits<double>::max();
42   double mbrMaxY = std::numeric_limits<double>::max();
43   unsigned char end = 0x7C;
44 
45   SpatialiteBlobHeader() = default;
46 
47   static const size_t LENGTH = 39;
48 
49   void readFrom( const char *p );
50 
51   void writeTo( char *p ) const;
52 };
53 
54 /**
55  * Convert a QgsGeometry into a SpatiaLite geometry BLOB
56  * The blob will be allocated and must be handled by the caller
57  */
58 void qgsGeometryToSpatialiteBlob( const QgsGeometry &geom, int32_t srid, char *&blob, int &size );
59 
60 /**
61  * Returns the bounding box of a SpatiaLite geometry blob
62  */
63 QgsRectangle spatialiteBlobBbox( const char *blob, size_t size );
64 
65 /**
66  * Convert a SpatiaLite geometry BLOB to a QgsGeometry
67  */
68 QgsGeometry spatialiteBlobToQgsGeometry( const char *blob, size_t size );
69 
70 /**
71  * Gets geometry type and srid from a SpatiaLite geometry blob
72  */
73 QPair<QgsWkbTypes::Type, long> spatialiteBlobGeometryType( const char *blob, size_t size );
74 
75 #endif
76