Skip to content

Commit

Permalink
Support plain tables from WFS sources
Browse files Browse the repository at this point in the history
  • Loading branch information
palmerj authored and mhugent committed Oct 4, 2012
1 parent c8a05c1 commit aedddc2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -69,6 +69,8 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
case QGis::Polygon:
stackedPlacement->setCurrentWidget( pagePolygon );
break;
case QGis::NoGeometry:
break;
default:
Q_ASSERT( 0 && "NOOOO!" );
}
Expand Down
7 changes: 5 additions & 2 deletions src/providers/wfs/qgswfsdata.cpp
Expand Up @@ -127,7 +127,7 @@ int QgsWFSData::getWFSData()
delete reply;
delete progressDialog;

if ( mExtent )
if ( mExtent && *mWkbType != QGis::WKBNoGeometry )
{
if ( mExtent->isEmpty() )
{
Expand Down Expand Up @@ -317,7 +317,10 @@ void QgsWFSData::endElement( const XML_Char* el )
}


mCurrentFeature->setGeometryAndOwnership( mCurrentWKB, mCurrentWKBSize );
if ( mCurrentWKBSize > 0 )
{
mCurrentFeature->setGeometryAndOwnership( mCurrentWKB, mCurrentWKBSize );
}
mFeatures.insert( mCurrentFeature->id(), mCurrentFeature );
if ( !mCurrentFeatureId.isEmpty() )
{
Expand Down
59 changes: 42 additions & 17 deletions src/providers/wfs/qgswfsprovider.cpp
Expand Up @@ -153,11 +153,14 @@ void QgsWFSProvider::copyFeature( QgsFeature* f, QgsFeature& feature, bool fetch

//copy the geometry
QgsGeometry* geometry = f->geometry();
unsigned char *geom = geometry->asWkb();
int geomSize = geometry->wkbSize();
unsigned char* copiedGeom = new unsigned char[geomSize];
memcpy( copiedGeom, geom, geomSize );
feature.setGeometryAndOwnership( copiedGeom, geomSize );
if ( geometry )
{
unsigned char *geom = geometry->asWkb();
int geomSize = geometry->wkbSize();
unsigned char* copiedGeom = new unsigned char[geomSize];
memcpy( copiedGeom, geom, geomSize );
feature.setGeometryAndOwnership( copiedGeom, geomSize );
}

//and the attributes
const QgsAttributeMap& attributes = f->attributeMap();
Expand Down Expand Up @@ -210,7 +213,7 @@ bool QgsWFSProvider::nextFeature( QgsFeature& feature )
continue;
}

copyFeature( f, feature, true, mAttributesToFetch );
copyFeature( f, feature, mFetchGeom, mAttributesToFetch );

if ( mUseIntersect )
{
Expand Down Expand Up @@ -277,6 +280,12 @@ void QgsWFSProvider::select( QgsAttributeList fetchAttributes,
bool fetchGeometry,
bool useIntersect )
{

if ( geometryType() == QGis::WKBNoGeometry )
{
fetchGeometry = false;
}

mUseIntersect = useIntersect;
mAttributesToFetch = fetchAttributes;
mFetchGeom = fetchGeometry;
Expand Down Expand Up @@ -789,10 +798,13 @@ int QgsWFSProvider::getFeatureGET( const QString& uri, const QString& geometryAt
QgsDebugMsg( QString( "feature count after request is: %1" ).arg( mFeatures.size() ) );
QgsDebugMsg( QString( "mExtent after request is: %1" ).arg( mExtent.toString() ) );

for ( QMap<QgsFeatureId, QgsFeature*>::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
if (mWKBType != QGis::WKBNoGeometry)
{
QgsDebugMsg( "feature " + FID_TO_STRING(( *it )->id() ) );
mSpatialIndex->insertFeature( *( it.value() ) );
for ( QMap<QgsFeatureId, QgsFeature*>::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
{
QgsDebugMsg( "feature " + FID_TO_STRING(( *it )->id() ) );
mSpatialIndex->insertFeature( *( it.value() ) );
}
}

mFeatureCount = mFeatures.size();
Expand Down Expand Up @@ -820,7 +832,7 @@ int QgsWFSProvider::getFeatureFILE( const QString& uri, const QString& geometryA

QDomElement featureCollectionElement = gmlDoc.documentElement();
//get and set Extent
if ( getExtentFromGML2( &mExtent, featureCollectionElement ) != 0 )
if ( mWKBType != QGis::WKBNoGeometry && QGis::WKBNoGeometry && getExtentFromGML2( &mExtent, featureCollectionElement ) != 0 )
{
return 3;
}
Expand Down Expand Up @@ -905,7 +917,7 @@ int QgsWFSProvider::describeFeatureTypeFile( const QString& uri, QString& geomet
std::list<QString> thematicAttributes;

//if this fails (e.g. no schema file), try to guess the geometry attribute and the names of the thematic attributes from the .gml file
if ( guessAttributesFromFile( uri, geometryAttribute, thematicAttributes ) != 0 )
if ( guessAttributesFromFile( uri, geometryAttribute, thematicAttributes, geomType ) != 0 )
{
return 1;
}
Expand Down Expand Up @@ -985,6 +997,8 @@ int QgsWFSProvider::readAttributesFromSchema( QDomDocument& schemaDoc, QString&
return 5;
}

bool foundGeometryAttribute = false;

for ( uint i = 0; i < attributeNodeList.length(); ++i )
{
QDomElement attributeElement = attributeNodeList.at( i ).toElement();
Expand All @@ -998,6 +1012,7 @@ int QgsWFSProvider::readAttributesFromSchema( QDomDocument& schemaDoc, QString&
QRegExp gmlPT( "gml:(.*)PropertyType" );
if ( type.indexOf( gmlPT ) == 0 || name.isEmpty() )
{
foundGeometryAttribute = true;
geometryAttribute = name;
geomType = geomTypeFromPropertyType( geometryAttribute, gmlPT.cap( 1 ) );
}
Expand All @@ -1019,10 +1034,15 @@ int QgsWFSProvider::readAttributesFromSchema( QDomDocument& schemaDoc, QString&
fields[fields.size()] = QgsField( name, attributeType, type );
}
}
if ( !foundGeometryAttribute )
{
geomType = QGis::WKBNoGeometry;
}

return 0;
}

int QgsWFSProvider::guessAttributesFromFile( const QString& uri, QString& geometryAttribute, std::list<QString>& thematicAttributes ) const
int QgsWFSProvider::guessAttributesFromFile( const QString& uri, QString& geometryAttribute, std::list<QString>& thematicAttributes, QGis::WkbType& geomType ) const
{
QFile gmlFile( uri );
if ( !gmlFile.open( QIODevice::ReadOnly ) )
Expand Down Expand Up @@ -1056,6 +1076,7 @@ int QgsWFSProvider::guessAttributesFromFile( const QString& uri, QString& geomet
QString attributeText;
QDomElement attributeChildElement;
QString attributeChildLocalName;
bool foundGeometryAttribute = false;

while ( !attributeNode.isNull() )//loop over attributes
{
Expand Down Expand Up @@ -1083,6 +1104,11 @@ int QgsWFSProvider::guessAttributesFromFile( const QString& uri, QString& geomet
attributeNode = attributeNode.nextSibling();
}

if ( !foundGeometryAttribute )
{
geomType = QGis::WKBNoGeometry;
}

return 0;
}

Expand Down Expand Up @@ -1232,7 +1258,6 @@ int QgsWFSProvider::getFeaturesFromGML2( const QDomElement& wfsCollectionElement
QDomElement layerNameElem;
QDomNode currentAttributeChild;
QDomElement currentAttributeElement;
int counter = 0;
QgsFeature* f = 0;
unsigned char* wkb = 0;
int wkbSize = 0;
Expand All @@ -1241,7 +1266,7 @@ int QgsWFSProvider::getFeaturesFromGML2( const QDomElement& wfsCollectionElement

for ( int i = 0; i < featureTypeNodeList.size(); ++i )
{
f = new QgsFeature( counter );
f = new QgsFeature( mFeatureCount );
currentFeatureMemberElem = featureTypeNodeList.at( i ).toElement();
//the first child element is always <namespace:layer>
layerNameElem = currentFeatureMemberElem.firstChild().toElement();
Expand Down Expand Up @@ -1281,10 +1306,10 @@ int QgsWFSProvider::getFeaturesFromGML2( const QDomElement& wfsCollectionElement
{
//insert bbox and pointer to feature into search tree
mSpatialIndex->insertFeature( *f );
mFeatures.insert( f->id(), f );
++mFeatureCount;
}
++counter;

mFeatures.insert( f->id(), f );
++mFeatureCount;
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wfs/qgswfsprovider.h
Expand Up @@ -217,7 +217,7 @@ class QgsWFSProvider: public QgsVectorDataProvider
/**Reads the name of the geometry attribute, the thematic attributes and their types from a dom document. Returns 0 in case of success*/
int readAttributesFromSchema( QDomDocument& schemaDoc, QString& geometryAttribute, QgsFieldMap& fields, QGis::WkbType& geomType );
/**This method tries to guess the geometry attribute and the other attribute names from the .gml file if no schema is present. Returns 0 in case of success*/
int guessAttributesFromFile( const QString& uri, QString& geometryAttribute, std::list<QString>& thematicAttributes ) const;
int guessAttributesFromFile( const QString& uri, QString& geometryAttribute, std::list<QString>& thematicAttributes, QGis::WkbType& geomType ) const;

/**Copies feature attributes / geometry from f to feature*/
void copyFeature( QgsFeature* f, QgsFeature& feature, bool fetchGeometry, QgsAttributeList fetchAttributes );
Expand Down

0 comments on commit aedddc2

Please sign in to comment.