为OGR API使用的数据设计通用数据结构

时间:2017-03-08 11:58:36

标签: c++ algorithm

我正在尝试创建一个处理和创建功能的通用数据结构

我正在尝试制作更通用的序列化/反序列化格式,以便通过BinaryMessageBuffer推送数据,并通过BinaryMessageBuffer获取数据。

这是创建数据的代码。

void S3W::IWFSAreaData::ProcessBuffer(S3W::BinaryMessageBuffer &buffer)
{

    std::string areaName = "";
    unsigned int numGeometries;
    double minH;
    double maxH;

    areaName = buffer.consumeString();
    minH = buffer.consumeDouble();
    maxH = buffer.consumeDouble();
    numGeometries = buffer.consumeUint();

    OGRMultiPoint mp;

    for (size_t k = 0; k < numGeometries; k++)
    {
        double x, y;
        x = buffer.consumeDouble();
        y = buffer.consumeDouble();
        OGRPoint point;
        point.setX(x);
        point.setY(y);
        mp.addGeometry(&point);
    }
    OGRFeature *poFeature = OGRFeature::CreateFeature(m_Layer->GetLayerDefn());

    if (poFeature)
    {
        poFeature->SetField("gfname", areaName.c_str());
        poFeature->SetField("minHeight", minH);
        poFeature->SetField("maxHeight", maxH);


        poFeature->SetGeometry(&mp);


        if (m_Layer->CreateFeature(poFeature) != OGRERR_NONE)
        {
            std::cout << "error inserting an area" << std::endl;
        }
        else
        {
            std::cout << "Created a feature" << std::endl;
        }
    }

    OGRFeature::DestroyFeature(poFeature);


}

这是接收数据的代码:

void S3W::IWFSAreaData::SrvReceive(S3W::BinaryMessageBuffer & data)
{


    // Reset Reading (Although not necessary now because it hasnt been read yet, this is good practice before reading WFS data)
    m_Layer->ResetReading();

    OGRFeature *Feature;

    while ((Feature = m_Layer->GetNextFeature()) != NULL)
    {
        // Variable initialised to contain the definitions of all of the fileds in the WFS Layer
        OGRFeatureDefn *FeatureDefinition = m_Layer->GetLayerDefn();

        for (int i = 0; i < FeatureDefinition->GetFieldCount(); i++)
        {
            // Variable initialised to contain field data at the current index of i
            OGRFieldDefn *FieldDefinition = FeatureDefinition->GetFieldDefn(i);

            std::string fieldName = FieldDefinition->GetNameRef();

            if (FieldDefinition->GetType() == OFTReal && fieldName == "minHeight")
            {
                double minHeight = Feature->GetFieldAsDouble(i);
                data.add(minHeight);
            }

            else if (FieldDefinition->GetType() == OFTReal && fieldName == "maxHeight")
            {
                double maxHeight = Feature->GetFieldAsDouble(i);
                data.add(maxHeight);
            }
            else if (FieldDefinition->GetType() == OFTString && fieldName == "gfname")
            {
                const char *gfName = Feature->GetFieldAsString(i);
                data.add(gfName);
            }
        }

        // Variable initialised to contain Geometry point data for the field at the current index of i
        OGRGeometry *Geometry = Feature->GetGeometryRef();

        if (Geometry != NULL && wkbFlatten(Geometry->getGeometryType()) == wkbMultiPoint)
        {
            OGRMultiPoint *poMultipoint = (OGRMultiPoint *)Geometry;
            if (poMultipoint)
            {
                unsigned int NumberOfGeometries = poMultipoint->getNumGeometries();
                data.add(NumberOfGeometries);

                for (unsigned int i = 0; i < NumberOfGeometries; i++)
                {
                    OGRGeometry *geom = poMultipoint->getGeometryRef(i);
                    if (geom)
                    {
                        OGRPoint *Point = (OGRPoint *)geom;
                        if (Point)
                        {
                            double x = Point->getX();
                            double y = Point->getY();
                            data.add(x);
                            data.add(y);
                        }
                    }
                }
            }
        }
        else
        {
        // if area has no geometry add empty positions
            double x = 0, y = 0; 
            for (int i = 0; i < 2; i++)
            {
                data.add(x);
                data.add(y);
            }

        }

        OGRFeature::DestroyFeature(Feature);
    }

}

可以使用哪种数据结构来设计创建功能和接收功能的更常规工作,使用通用“字段”。

0 个答案:

没有答案