|
@@ -29,56 +29,29 @@ QgsJSONExporter::QgsJSONExporter( int precision, bool includeGeometry, bool incl |
|
|
QString QgsJSONExporter::exportFeature( const QgsFeature& feature, const QVariantMap& extraProperties, |
|
|
const QVariant& id ) const |
|
|
{ |
|
|
QString s = "{\n \"type\":\"Feature\",\n"; |
|
|
|
|
|
// ID |
|
|
s += QString( " \"id\":%1" ).arg( !id.isValid() ? QString::number( feature.id() ) : QgsJSONUtils::encodeValue( id ) ); |
|
|
|
|
|
if ( mIncludeAttributes || mIncludeGeometry || !extraProperties.isEmpty() ) |
|
|
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 ( mIncludeAttributes || !extraProperties.isEmpty() ) |
|
|
s += ",\n"; |
|
|
else |
|
|
s += '\n'; |
|
|
} |
|
|
//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 |
|
|
s += " \"properties\":{\n"; |
|
|
int attributeCounter = 0; |
|
|
|
|
|
|
|
|
if ( mIncludeAttributes ) |
|
|
{ |
|
|
const QgsFields* fields = feature.fields(); |
|
|
|
|
|
for ( int i = 0; i < fields->count(); ++i ) |
|
|
{ |
|
|
if ( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) |
|
|
if (( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) || mExcludedAttributeIndexes.contains( i ) ) |
|
|
continue; |
|
|
|
|
|
if ( attributeCounter > 0 ) |
|
|
s += ",\n"; |
|
|
properties += ",\n"; |
|
|
QVariant val = feature.attributes().at( i ); |
|
|
|
|
|
s += QString( " \"%1\":%2" ).arg( fields->at( i ).name(), QgsJSONUtils::encodeValue( val ) ); |
|
|
properties += QString( " \"%1\":%2" ).arg( fields->at( i ).name(), QgsJSONUtils::encodeValue( val ) ); |
|
|
|
|
|
++attributeCounter; |
|
|
} |
|
@@ -90,18 +63,53 @@ QString QgsJSONExporter::exportFeature( const QgsFeature& feature, const QVarian |
|
|
for ( ; it != extraProperties.constEnd(); ++it ) |
|
|
{ |
|
|
if ( attributeCounter > 0 ) |
|
|
s += ",\n"; |
|
|
properties += ",\n"; |
|
|
|
|
|
s += QString( " \"%1\":%2" ).arg( it.key(), QgsJSONUtils::encodeValue( it.value() ) ); |
|
|
properties += QString( " \"%1\":%2" ).arg( it.key(), QgsJSONUtils::encodeValue( it.value() ) ); |
|
|
|
|
|
++attributeCounter; |
|
|
} |
|
|
} |
|
|
} |
|
|
bool hasProperties = attributeCounter > 0; |
|
|
|
|
|
s += "\n }\n"; |
|
|
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 += "}"; |
|
|
s += '}'; |
|
|
|
|
|
return s; |
|
|
} |
|
|