/
qgsjsonutils.cpp
196 lines (161 loc) · 5.69 KB
/
qgsjsonutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***************************************************************************
qgsjsonutils.h
-------------
Date : May 206
Copyright : (C) 2016 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsjsonutils.h"
#include "qgsogrutils.h"
#include "qgsgeometry.h"
QgsJSONExporter::QgsJSONExporter( int precision, bool includeGeometry, bool includeAttributes )
: mPrecision( precision )
, mIncludeGeometry( includeGeometry )
, mIncludeAttributes( includeAttributes )
{
}
QString QgsJSONExporter::exportFeature( const QgsFeature& feature, const QVariantMap& extraProperties,
const QVariant& id ) const
{
//first step is to build up the properties list
QString properties;
int attributeCounter = 0;
if ( mIncludeAttributes || !extraProperties.isEmpty() )
{
//read all attribute values from the feature
if ( mIncludeAttributes )
{
const QgsFields* fields = feature.fields();
for ( int i = 0; i < fields->count(); ++i )
{
if (( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) || mExcludedAttributeIndexes.contains( i ) )
continue;
if ( attributeCounter > 0 )
properties += ",\n";
QVariant val = feature.attributes().at( i );
properties += QString( " \"%1\":%2" ).arg( fields->at( i ).name(), QgsJSONUtils::encodeValue( val ) );
++attributeCounter;
}
}
if ( !extraProperties.isEmpty() )
{
QVariantMap::const_iterator it = extraProperties.constBegin();
for ( ; it != extraProperties.constEnd(); ++it )
{
if ( attributeCounter > 0 )
properties += ",\n";
properties += QString( " \"%1\":%2" ).arg( it.key(), QgsJSONUtils::encodeValue( it.value() ) );
++attributeCounter;
}
}
}
bool hasProperties = attributeCounter > 0;
QString s = "{\n \"type\":\"Feature\",\n";
// ID
s += QString( " \"id\":%1" ).arg( !id.isValid() ? QString::number( feature.id() ) : QgsJSONUtils::encodeValue( id ) );
if ( hasProperties || mIncludeGeometry )
s += ",\n";
else
s += '\n';
const QgsGeometry* geom = feature.constGeometry();
if ( geom && !geom->isEmpty() && mIncludeGeometry )
{
QgsRectangle box = geom->boundingBox();
if ( QgsWKBTypes::flatType( geom->geometry()->wkbType() ) != QgsWKBTypes::Point )
{
s += QString( " \"bbox\":[%1, %2, %3, %4],\n" ).arg( qgsDoubleToString( box.xMinimum(), mPrecision ),
qgsDoubleToString( box.yMinimum(), mPrecision ),
qgsDoubleToString( box.xMaximum(), mPrecision ),
qgsDoubleToString( box.yMaximum(), mPrecision ) );
}
s += " \"geometry\":\n ";
s += geom->exportToGeoJSON( mPrecision );
if ( hasProperties )
s += ",\n";
else
s += '\n';
}
if ( hasProperties )
{
//read all attribute values from the feature
s += " \"properties\":{\n" + properties + "\n }\n";
}
s += '}';
return s;
}
//
// QgsJSONUtils
//
QgsFeatureList QgsJSONUtils::stringToFeatureList( const QString &string, const QgsFields &fields, QTextCodec *encoding )
{
return QgsOgrUtils::stringToFeatureList( string, fields, encoding );
}
QgsFields QgsJSONUtils::stringToFields( const QString &string, QTextCodec *encoding )
{
return QgsOgrUtils::stringToFields( string, encoding );
}
QString QgsJSONUtils::encodeValue( const QVariant &value )
{
if ( value.isNull() )
return "null";
switch ( value.type() )
{
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::Double:
return value.toString();
case QVariant::Bool:
return value.toBool() ? "true" : "false";
case QVariant::StringList:
{
QStringList input = value.toStringList();
QStringList output;
Q_FOREACH ( const QString& string, input )
{
output << encodeValue( string );
}
return output.join( "," ).prepend( '[' ).append( ']' );
}
case QVariant::List:
{
QVariantList input = value.toList();
QStringList output;
Q_FOREACH ( const QVariant& v, input )
{
output << encodeValue( v );
}
return output.join( "," ).prepend( '[' ).append( ']' );
}
case QVariant::Map:
{
QMap< QString, QVariant > input = value.toMap();
QStringList output;
QMap< QString, QVariant >::const_iterator it = input.constBegin();
for ( ; it != input.constEnd(); ++it )
{
output << encodeValue( it.key() ) + ':' + encodeValue( it.value() );
}
return output.join( ",\n" ).prepend( '{' ).append( '}' );
}
default:
case QVariant::String:
QString v = value.toString()
.replace( '\\', "\\\\" )
.replace( '"', "\\\"" )
.replace( '\r', "\\r" )
.replace( '\b', "\\b" )
.replace( '\t', "\\t" )
.replace( '/', "\\/" )
.replace( '\n', "\\n" );
return v.prepend( '"' ).append( '"' );
}
}