|
@@ -71,169 +71,7 @@ QgsCoordinateReferenceSystem QgsJsonExporter::sourceCrs() const |
|
|
QString QgsJsonExporter::exportFeature( const QgsFeature &feature, const QVariantMap &extraProperties, |
|
|
const QVariant &id ) const |
|
|
{ |
|
|
|
|
|
QString s = QStringLiteral( "{\n \"type\":\"Feature\",\n" ); |
|
|
|
|
|
// ID |
|
|
s += QStringLiteral( " \"id\":%1,\n" ).arg( !id.isValid() ? QString::number( feature.id() ) : QgsJsonUtils::encodeValue( id ) ); |
|
|
|
|
|
QgsGeometry geom = feature.geometry(); |
|
|
if ( !geom.isNull() && mIncludeGeometry ) |
|
|
{ |
|
|
if ( mCrs.isValid() ) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
QgsGeometry transformed = geom; |
|
|
if ( transformed.transform( mTransform ) == 0 ) |
|
|
geom = transformed; |
|
|
} |
|
|
catch ( QgsCsException &cse ) |
|
|
{ |
|
|
Q_UNUSED( cse ); |
|
|
} |
|
|
} |
|
|
QgsRectangle box = geom.boundingBox(); |
|
|
|
|
|
if ( QgsWkbTypes::flatType( geom.wkbType() ) != QgsWkbTypes::Point ) |
|
|
{ |
|
|
s += QStringLiteral( " \"bbox\":[%1, %2, %3, %4],\n" ).arg( qgsDoubleToString( box.xMinimum(), mPrecision ), |
|
|
qgsDoubleToString( box.yMinimum(), mPrecision ), |
|
|
qgsDoubleToString( box.xMaximum(), mPrecision ), |
|
|
qgsDoubleToString( box.yMaximum(), mPrecision ) ); |
|
|
} |
|
|
s += QLatin1String( " \"geometry\":\n " ); |
|
|
s += geom.asJson( mPrecision ); |
|
|
s += QLatin1String( ",\n" ); |
|
|
} |
|
|
else |
|
|
{ |
|
|
s += QLatin1String( " \"geometry\":null,\n" ); |
|
|
} |
|
|
|
|
|
// build up properties element |
|
|
QString properties; |
|
|
int attributeCounter = 0; |
|
|
if ( mIncludeAttributes || !extraProperties.isEmpty() ) |
|
|
{ |
|
|
//read all attribute values from the feature |
|
|
|
|
|
if ( mIncludeAttributes ) |
|
|
{ |
|
|
QgsFields fields = mLayer ? mLayer->fields() : feature.fields(); |
|
|
// List of formatters through we want to pass the values |
|
|
QStringList formattersWhiteList; |
|
|
formattersWhiteList << QStringLiteral( "KeyValue" ) |
|
|
<< QStringLiteral( "List" ) |
|
|
<< QStringLiteral( "ValueRelation" ) |
|
|
<< QStringLiteral( "ValueMap" ); |
|
|
|
|
|
for ( int i = 0; i < fields.count(); ++i ) |
|
|
{ |
|
|
if ( ( !mAttributeIndexes.isEmpty() && !mAttributeIndexes.contains( i ) ) || mExcludedAttributeIndexes.contains( i ) ) |
|
|
continue; |
|
|
|
|
|
if ( attributeCounter > 0 ) |
|
|
properties += QLatin1String( ",\n" ); |
|
|
QVariant val = feature.attributes().at( i ); |
|
|
|
|
|
if ( mLayer ) |
|
|
{ |
|
|
QgsEditorWidgetSetup setup = fields.at( i ).editorWidgetSetup(); |
|
|
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() ); |
|
|
if ( formattersWhiteList.contains( fieldFormatter->id() ) ) |
|
|
val = fieldFormatter->representValue( mLayer.data(), i, setup.config(), QVariant(), val ); |
|
|
} |
|
|
|
|
|
QString name = fields.at( i ).name(); |
|
|
if ( mAttributeDisplayName ) |
|
|
{ |
|
|
name = mLayer->attributeDisplayName( i ); |
|
|
} |
|
|
|
|
|
properties += QStringLiteral( " \"%1\":%2" ).arg( name, QgsJsonUtils::encodeValue( val ) ); |
|
|
|
|
|
++attributeCounter; |
|
|
} |
|
|
} |
|
|
|
|
|
if ( !extraProperties.isEmpty() ) |
|
|
{ |
|
|
QVariantMap::const_iterator it = extraProperties.constBegin(); |
|
|
for ( ; it != extraProperties.constEnd(); ++it ) |
|
|
{ |
|
|
if ( attributeCounter > 0 ) |
|
|
properties += QLatin1String( ",\n" ); |
|
|
|
|
|
properties += QStringLiteral( " \"%1\":%2" ).arg( it.key(), QgsJsonUtils::encodeValue( it.value() ) ); |
|
|
|
|
|
++attributeCounter; |
|
|
} |
|
|
} |
|
|
|
|
|
// related attributes |
|
|
if ( mLayer && mIncludeRelatedAttributes ) |
|
|
{ |
|
|
QList< QgsRelation > relations = QgsProject::instance()->relationManager()->referencedRelations( mLayer.data() ); |
|
|
const auto constRelations = relations; |
|
|
for ( const QgsRelation &relation : constRelations ) |
|
|
{ |
|
|
if ( attributeCounter > 0 ) |
|
|
properties += QLatin1String( ",\n" ); |
|
|
|
|
|
QgsFeatureRequest req = relation.getRelatedFeaturesRequest( feature ); |
|
|
req.setFlags( QgsFeatureRequest::NoGeometry ); |
|
|
QgsVectorLayer *childLayer = relation.referencingLayer(); |
|
|
QString relatedFeatureAttributes; |
|
|
if ( childLayer ) |
|
|
{ |
|
|
QgsFeatureIterator it = childLayer->getFeatures( req ); |
|
|
QVector<QVariant> attributeWidgetCaches; |
|
|
int fieldIndex = 0; |
|
|
const QgsFields fields = childLayer->fields(); |
|
|
for ( const QgsField &field : fields ) |
|
|
{ |
|
|
QgsEditorWidgetSetup setup = field.editorWidgetSetup(); |
|
|
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() ); |
|
|
attributeWidgetCaches.append( fieldFormatter->createCache( childLayer, fieldIndex, setup.config() ) ); |
|
|
fieldIndex++; |
|
|
} |
|
|
|
|
|
QgsFeature relatedFet; |
|
|
int relationFeatures = 0; |
|
|
while ( it.nextFeature( relatedFet ) ) |
|
|
{ |
|
|
if ( relationFeatures > 0 ) |
|
|
relatedFeatureAttributes += QLatin1String( ",\n" ); |
|
|
|
|
|
relatedFeatureAttributes += QgsJsonUtils::exportAttributes( relatedFet, childLayer, attributeWidgetCaches ); |
|
|
relationFeatures++; |
|
|
} |
|
|
} |
|
|
relatedFeatureAttributes.prepend( '[' ).append( ']' ); |
|
|
|
|
|
properties += QStringLiteral( " \"%1\":%2" ).arg( relation.name(), relatedFeatureAttributes ); |
|
|
attributeCounter++; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
bool hasProperties = attributeCounter > 0; |
|
|
|
|
|
s += QLatin1String( " \"properties\":" ); |
|
|
if ( hasProperties ) |
|
|
{ |
|
|
//read all attribute values from the feature |
|
|
s += "{\n" + properties + "\n }\n"; |
|
|
} |
|
|
else |
|
|
{ |
|
|
s += QLatin1String( "null\n" ); |
|
|
} |
|
|
|
|
|
s += '}'; |
|
|
|
|
|
return s; |
|
|
return QString::fromStdString( exportFeatureToJsonObject( feature, extraProperties, id ).dump() ); |
|
|
} |
|
|
|
|
|
json QgsJsonExporter::exportFeatureToJsonObject( const QgsFeature &feature, const QVariantMap &extraProperties, const QVariant &id ) const |
|
|