1

to start off with:

Coding in C++

Using WFS Version 1.0.0

Using QT 5.4.1

Windows 8 Latest versions

I'm trying to read in a WFS in code via Gdal compiled with Xerces - I'm able to read the GML files so I can pull spatial point/lines/polygons etc. But I'm not too sure how I do this with the feature names/layers. Any help?]

This is what I'm trying to get: The Cities layer name and Continents layer name, but via code.

<schema xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" 
targetNamespace="http://mapserver.gis.umn.edu/mapserver"elementFormDefault="qualified" version="0.1">
<import namespace="http://www.opengis.net/gml"     
schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<element name="continents" type="ms:continentsType"      
substitutionGroup="gml:_Feature"/>

<complexType name="continentsType">
<complexContent>
<extension base="gml:AbstractFeatureType">
<sequence>
<element name="msGeometry" type="gml:GeometryPropertyType" minOccurs="0"maxOccurs="1"/>
<element name="NA2DESC" type="string"/>
<element name="NA3DESC" type="string"/>
</sequence>
</extension>
</complexContent>
</complexType>
<element name="cities" type="ms:citiesType" substitutionGroup="gml:_Feature"/>
<complexType name="citiesType">
<complexContent>
<extension base="gml:AbstractFeatureType">
<sequence>
<element name="msGeometry" type="gml:GeometryPropertyType" minOccurs="0" maxOccurs="1"/>
<element name="POPULATION" type="string"/>
<element name="NAME" type="string"/>
</sequence>
</extension>
</complexContent>
</complexType>
</schema>

You can call the Features type by using this WFS GET request http://demo.mapserver.org/cgi-bin/wfs?SERVICE=WFS&VERSION=1.0.0&REQUEST=DescribeFeatureType

1 Answer 1

1

After a bit of tinkering with my program I've managed to pull the Layer name and its elements/variables!

See code below on how to do it:

void main()
{
    // Register your drivers
    OGRRegisterAll();

   // Get your wfs here - (M_DATASOURCE IN HEADER)
   m_datasource = OGRSFDriverRegistrar::Open(m_ui->m_line_wfs_name->text().toStdString().c_str(), false);

   // Call the function to start it all off
   readShapeFile();
}

 // This functions finds all the possible layers
std::vector<std::string> DialogWfsReader::getLayerName()
{
     // This vector holds all the layers that we have found
     std::vector<std::string> layer_names;

     // Loop for the ammount of layers in ogr_datasource - this is where the .shp is loaded into
     for(int i = 0; i < m_datasource->GetLayerCount(); i++)
     {
         // This gets the current layer
         auto layer = m_datasource->GetLayer(i);
         // Then gets the current layer name from layer
         layer_names.push_back(layer->GetName());
     }

     // Then return the layer names
     return layer_names;
}

void DialogWfsReader::readShapeFile()
{

// Creating a pointer to the layer
OGRLayer *poLayer;

// If this is a nullptr then the file has not opened
if(m_datasource  == nullptr)
{
    // Put your error codes here :)
    // Means your can not open that WFS/SHP file!
}
else
{

    // Loop over all the layer names
    for(const auto& layer_name : getLayerName())
    {
        // Get layers for each name
        poLayer = m_datasource ->GetLayerByName(layer_name.c_str());

        // Reset your reading, you need to start from the beginning!
        poLayer->ResetReading();

        // Varible to hold the feature
        OGRFeature* feature;


        // Start looping all the features, and assign it to feature!
        while((feature = poLayer->GetNextFeature()) != nullptr)
        {
            // Get some more layer stuff
            OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();

            /// This is the layer name - For example "Cities"
            poLayer->GetName();

            /// Start getting all the fields for that layer name
            for(int iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
            {
                //Set poFieldDefn to the current field in the loop
                OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );

                /// This is where you get all your elements/varibles
                /// E.G CityName, CityPopulation , CityId etc....
                poFieldDefn->GetNameRef();
            }
        }
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.