mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
Add GML3 to QgsOgcUtils
After the commit 969df016bcdbd1ead8a347b7a5d55c8b36db183a Moved GML import/export to a new class: QgsOgcUtils, I decided to add GML3. I updated QgsOgcUtils, QGIS WFS Server, and the Python Iterface and I added unit test for GML3.
This commit is contained in:
parent
173aee6e32
commit
7d66529d78
@ -28,5 +28,22 @@ public:
|
||||
/** read rectangle from GML2 Box */
|
||||
static QgsRectangle rectangleFromGMLBox( const QDomNode& boxNode );
|
||||
|
||||
|
||||
/** static method that creates geometry from GML3
|
||||
@param XML representation of the geometry. GML elements are expected to be
|
||||
in default namespace (<Point>...</Point>) or in "gml" namespace (<gml:Point>...</gml:Point>)
|
||||
*/
|
||||
static QgsGeometry* geometryFromGML3( const QString& xmlString );
|
||||
|
||||
/** static method that creates geometry from GML2
|
||||
*/
|
||||
static QgsGeometry* geometryFromGML3( const QDomNode& geometryNode );
|
||||
/** Exports the geometry to mGML3
|
||||
@return true in case of success and false else
|
||||
*/
|
||||
static QDomElement geometryToGML3( QgsGeometry* geometry, QDomDocument& doc );
|
||||
|
||||
/** read rectangle from GML3 Envelope */
|
||||
static QgsRectangle rectangleFromGMLEnvelope( const QDomNode& envelopeNode );
|
||||
};
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -44,6 +44,24 @@ public:
|
||||
/** read rectangle from GML2 Box */
|
||||
static QgsRectangle rectangleFromGMLBox( const QDomNode& boxNode );
|
||||
|
||||
|
||||
/** static method that creates geometry from GML3
|
||||
@param XML representation of the geometry. GML elements are expected to be
|
||||
in default namespace (<Point>...</Point>) or in "gml" namespace (<gml:Point>...</gml:Point>)
|
||||
*/
|
||||
static QgsGeometry* geometryFromGML3( const QString& xmlString );
|
||||
|
||||
/** static method that creates geometry from GML2
|
||||
*/
|
||||
static QgsGeometry* geometryFromGML3( const QDomNode& geometryNode );
|
||||
/** Exports the geometry to mGML3
|
||||
@return true in case of success and false else
|
||||
*/
|
||||
static QDomElement geometryToGML3( QgsGeometry* geometry, QDomDocument& doc );
|
||||
|
||||
/** read rectangle from GML3 Envelope */
|
||||
static QgsRectangle rectangleFromGMLEnvelope( const QDomNode& envelopeNode );
|
||||
|
||||
private:
|
||||
/** static method that creates geometry from GML2 Point */
|
||||
static QgsGeometry* geometryFromGML2Point( const QDomElement& geometryElement );
|
||||
@ -62,6 +80,24 @@ private:
|
||||
@param elem the <gml:coordinates> element
|
||||
@return boolean for success*/
|
||||
static bool readGML2Coordinates( std::list<QgsPoint>& coords, const QDomElement elem );
|
||||
|
||||
/** static method that creates geometry from GML3 Point */
|
||||
static QgsGeometry* geometryFromGML3Point( const QDomElement& geometryElement );
|
||||
/** static method that creates geometry from GML3 LineString */
|
||||
static QgsGeometry* geometryFromGML3LineString( const QDomElement& geometryElement );
|
||||
/** static method that creates geometry from GML3 Polygon */
|
||||
static QgsGeometry* geometryFromGML3Polygon( const QDomElement& geometryElement );
|
||||
/** static method that creates geometry from GML3 MultiPoint */
|
||||
static QgsGeometry* geometryFromGML3MultiPoint( const QDomElement& geometryElement );
|
||||
/** static method that creates geometry from GML3 MultiLineString */
|
||||
static QgsGeometry* geometryFromGML3MultiLineString( const QDomElement& geometryElement );
|
||||
/** static method that creates geometry from GML3 MultiPolygon */
|
||||
static QgsGeometry* geometryFromGML3MultiPolygon( const QDomElement& geometryElement );
|
||||
/** Reads the <gml:pos> or <gml:posList> element and extracts the coordinates as points
|
||||
@param coords list where the found coordinates are appended
|
||||
@param elem the <gml:pos> or <gml:posList> element
|
||||
@return boolean for success*/
|
||||
static bool readGML3Positions( std::list<QgsPoint>& coords, const QDomElement elem );
|
||||
};
|
||||
|
||||
#endif // QGSOGCUTILS_H
|
||||
|
@ -203,6 +203,8 @@ QDomDocument QgsWFSServer::getCapabilities()
|
||||
getFeatureElement.appendChild( getFeatureFormatElement );
|
||||
QDomElement gmlFormatElement = doc.createElement( "GML2" );/*wfs:GML2*/
|
||||
getFeatureFormatElement.appendChild( gmlFormatElement );
|
||||
QDomElement gml3FormatElement = doc.createElement( "GML3" );/*wfs:GML3*/
|
||||
getFeatureFormatElement.appendChild( gml3FormatElement );
|
||||
QDomElement geojsonFormatElement = doc.createElement( "GeoJSON" );/*wfs:GeoJSON*/
|
||||
getFeatureFormatElement.appendChild( geojsonFormatElement );
|
||||
QDomElement getFeatureDhcTypeGetElement = dcpTypeElement.cloneNode().toElement();//this is the same as for 'GetCapabilities'
|
||||
@ -1093,15 +1095,31 @@ void QgsWFSServer::startGetFeature( QgsRequestHandler& request, const QString& f
|
||||
|
||||
QDomDocument doc;
|
||||
QDomElement bbElem = doc.createElement( "gml:boundedBy" );
|
||||
QDomElement boxElem = createBoxGML2( rect, doc );
|
||||
if ( !boxElem.isNull() )
|
||||
if ( format == "GML3" )
|
||||
{
|
||||
if ( crs.isValid() )
|
||||
QDomElement envElem = createEnvelopeGML3( rect, doc );
|
||||
if ( !envElem.isNull() )
|
||||
{
|
||||
boxElem.setAttribute( "srsName", crs.authid() );
|
||||
if ( crs.isValid() )
|
||||
{
|
||||
envElem.setAttribute( "srsName", crs.authid() );
|
||||
}
|
||||
bbElem.appendChild( envElem );
|
||||
doc.appendChild( bbElem );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QDomElement boxElem = createBoxGML2( rect, doc );
|
||||
if ( !boxElem.isNull() )
|
||||
{
|
||||
if ( crs.isValid() )
|
||||
{
|
||||
boxElem.setAttribute( "srsName", crs.authid() );
|
||||
}
|
||||
bbElem.appendChild( boxElem );
|
||||
doc.appendChild( bbElem );
|
||||
}
|
||||
bbElem.appendChild( boxElem );
|
||||
doc.appendChild( bbElem );
|
||||
}
|
||||
result = doc.toByteArray();
|
||||
request.sendGetFeatureResponse( &result );
|
||||
@ -1132,8 +1150,17 @@ void QgsWFSServer::sendGetFeature( QgsRequestHandler& request, const QString& fo
|
||||
else
|
||||
{
|
||||
QDomDocument gmlDoc;
|
||||
QDomElement featureElement = createFeatureGML2( feat, gmlDoc, crs, fields, excludedAttributes );
|
||||
gmlDoc.appendChild( featureElement );
|
||||
QDomElement featureElement;
|
||||
if ( format == "GML3" )
|
||||
{
|
||||
featureElement = createFeatureGML3( feat, gmlDoc, crs, fields, excludedAttributes );
|
||||
gmlDoc.appendChild( featureElement );
|
||||
}
|
||||
else
|
||||
{
|
||||
featureElement = createFeatureGML2( feat, gmlDoc, crs, fields, excludedAttributes );
|
||||
gmlDoc.appendChild( featureElement );
|
||||
}
|
||||
|
||||
result = gmlDoc.toByteArray();
|
||||
request.sendGetFeatureResponse( &result );
|
||||
@ -1714,6 +1741,64 @@ QDomElement QgsWFSServer::createBoxGML2( QgsRectangle* box, QDomDocument& doc )
|
||||
return boxElem;
|
||||
}
|
||||
|
||||
QDomElement QgsWFSServer::createFeatureGML3( QgsFeature* feat, QDomDocument& doc, QgsCoordinateReferenceSystem& crs, QgsFields fields, QSet<QString> excludedAttributes ) /*const*/
|
||||
{
|
||||
//gml:FeatureMember
|
||||
QDomElement featureElement = doc.createElement( "gml:featureMember"/*wfs:FeatureMember*/ );
|
||||
|
||||
//qgs:%TYPENAME%
|
||||
QDomElement typeNameElement = doc.createElement( "qgs:" + mTypeName /*qgs:%TYPENAME%*/ );
|
||||
typeNameElement.setAttribute( "gml:id", mTypeName + "." + QString::number( feat->id() ) );
|
||||
featureElement.appendChild( typeNameElement );
|
||||
|
||||
if ( mWithGeom )
|
||||
{
|
||||
//add geometry column (as gml)
|
||||
QgsGeometry* geom = feat->geometry();
|
||||
|
||||
QDomElement geomElem = doc.createElement( "qgs:geometry" );
|
||||
QDomElement gmlElem = QgsOgcUtils::geometryToGML3( geom, doc );
|
||||
if ( !gmlElem.isNull() )
|
||||
{
|
||||
QgsRectangle box = geom->boundingBox();
|
||||
QDomElement bbElem = doc.createElement( "gml:boundedBy" );
|
||||
QDomElement boxElem = createEnvelopeGML3( &box, doc );
|
||||
|
||||
if ( crs.isValid() )
|
||||
{
|
||||
boxElem.setAttribute( "srsName", crs.authid() );
|
||||
gmlElem.setAttribute( "srsName", crs.authid() );
|
||||
}
|
||||
|
||||
bbElem.appendChild( boxElem );
|
||||
typeNameElement.appendChild( bbElem );
|
||||
|
||||
geomElem.appendChild( gmlElem );
|
||||
typeNameElement.appendChild( geomElem );
|
||||
}
|
||||
}
|
||||
|
||||
//read all attribute values from the feature
|
||||
QgsAttributes featureAttributes = feat->attributes();
|
||||
for ( int i = 0; i < featureAttributes.count(); ++i )
|
||||
{
|
||||
|
||||
QString attributeName = fields[i].name();
|
||||
//skip attribute if is explicitely excluded from WFS publication
|
||||
if ( excludedAttributes.contains( attributeName ) )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
QDomElement fieldElem = doc.createElement( "qgs:" + attributeName.replace( QString( " " ), QString( "_" ) ) );
|
||||
QDomText fieldText = doc.createTextNode( featureAttributes[i].toString() );
|
||||
fieldElem.appendChild( fieldText );
|
||||
typeNameElement.appendChild( fieldElem );
|
||||
}
|
||||
|
||||
return featureElement;
|
||||
}
|
||||
|
||||
QDomElement QgsWFSServer::createCoordinateGML2( const QVector<QgsPoint> points, QDomDocument& doc ) const
|
||||
{
|
||||
QDomElement coordElem = doc.createElement( "gml:coordinates" );
|
||||
@ -1737,3 +1822,32 @@ QDomElement QgsWFSServer::createCoordinateGML2( const QVector<QgsPoint> points,
|
||||
coordElem.appendChild( coordText );
|
||||
return coordElem;
|
||||
}
|
||||
|
||||
QDomElement QgsWFSServer::createEnvelopeGML3( QgsRectangle* env, QDomDocument& doc ) /*const*/
|
||||
{
|
||||
if ( !env )
|
||||
{
|
||||
return QDomElement();
|
||||
}
|
||||
|
||||
QDomElement envElem = doc.createElement( "gml:Envelope" );
|
||||
QString posList;
|
||||
|
||||
QDomElement lowerCornerElem = doc.createElement( "gml:lowerCorner" );
|
||||
posList = QString::number( env->xMinimum(), 'f', 8 ).remove( QRegExp( "[0]{1,7}$" ) );
|
||||
posList += " ";
|
||||
posList = QString::number( env->yMinimum(), 'f', 8 ).remove( QRegExp( "[0]{1,7}$" ) );
|
||||
QDomText lowerCornerText = doc.createTextNode( posList );
|
||||
lowerCornerElem.appendChild( lowerCornerText );
|
||||
envElem.appendChild( lowerCornerElem );
|
||||
|
||||
QDomElement upperCornerElem = doc.createElement( "gml:upperCorner" );
|
||||
posList = QString::number( env->xMaximum(), 'f', 8 ).remove( QRegExp( "[0]{1,7}$" ) );
|
||||
posList += " ";
|
||||
posList = QString::number( env->yMaximum(), 'f', 8 ).remove( QRegExp( "[0]{1,7}$" ) );
|
||||
QDomText upperCornerText = doc.createTextNode( posList );
|
||||
upperCornerElem.appendChild( upperCornerText );
|
||||
envElem.appendChild( upperCornerElem );
|
||||
|
||||
return envElem;
|
||||
}
|
||||
|
@ -115,6 +115,10 @@ class QgsWFSServer
|
||||
@param coordString out: GML coord string
|
||||
@return 0 in case of success*/
|
||||
QDomElement createCoordinateGML2( const QVector<QgsPoint> points, QDomDocument& doc ) const;
|
||||
|
||||
//methods to write GML3
|
||||
QDomElement createFeatureGML3( QgsFeature* feat, QDomDocument& doc, QgsCoordinateReferenceSystem& crs, QgsFields fields, QSet<QString> excludedAttributes ) /*const*/;
|
||||
QDomElement createEnvelopeGML3( QgsRectangle* env, QDomDocument& doc ) /* const */;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -44,6 +44,18 @@ void TestQgsOgcUtils::testGeometryFromGML()
|
||||
QgsGeometry* geomBox = QgsOgcUtils::geometryFromGML2( "<gml:Box srsName=\"foo\"><gml:coordinates>135.2239,34.4879 135.8578,34.8471</gml:coordinates></gml:Box>" );
|
||||
QVERIFY( geomBox );
|
||||
QVERIFY( geomBox->wkbType() == QGis::WKBPolygon );
|
||||
|
||||
geom = QgsOgcUtils::geometryFromGML3( "<Point><pos>123 456</pos></Point>" );
|
||||
QVERIFY( geom );
|
||||
QVERIFY( geom->wkbType() == QGis::WKBPoint );
|
||||
QVERIFY( geom->asPoint() == QgsPoint( 123, 456 ) );
|
||||
|
||||
geomBox = QgsOgcUtils::geometryFromGML3( "<gml:Envelope srsName=\"foo\"><gml:lowerCorner>135.2239 34.4879</gml:lowerCorner><gml:upperCorner>135.8578 34.8471</gml:upperCorner></gml:Envelope>" );
|
||||
QVERIFY( geomBox );
|
||||
QVERIFY( geomBox->wkbType() == QGis::WKBPolygon );
|
||||
|
||||
delete geom;
|
||||
delete geomBox;
|
||||
}
|
||||
|
||||
void TestQgsOgcUtils::testGeometryToGML()
|
||||
@ -55,11 +67,23 @@ void TestQgsOgcUtils::testGeometryToGML()
|
||||
|
||||
QgsGeometry* geomPoint = QgsGeometry::fromPoint( QgsPoint( 111, 222 ) );
|
||||
QDomElement elemPoint = QgsOgcUtils::geometryToGML2( geomPoint, doc );
|
||||
delete geomPoint;
|
||||
QVERIFY( !elemPoint.isNull() );
|
||||
|
||||
doc.appendChild( elemPoint );
|
||||
QCOMPARE( doc.toString( -1 ), QString( "<gml:Point><gml:coordinates cs=\",\" ts=\" \">111.0,222.0</gml:coordinates></gml:Point>" ) );
|
||||
doc.removeChild( elemPoint );
|
||||
|
||||
elemInvalid = QgsOgcUtils::geometryToGML3( 0, doc );
|
||||
QVERIFY( elemInvalid.isNull() );
|
||||
|
||||
elemPoint = QgsOgcUtils::geometryToGML3( geomPoint, doc );
|
||||
QVERIFY( !elemPoint.isNull() );
|
||||
|
||||
doc.appendChild( elemPoint );
|
||||
QCOMPARE( doc.toString( -1 ), QString( "<gml:Point><gml:pos srsDimension=\"2\">111.0 222.0</gml:pos></gml:Point>" ) );
|
||||
doc.removeChild( elemPoint );
|
||||
|
||||
delete geomPoint;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user