Posts Tagged ‘fdo’

WKB hex decoder in C++

Thursday, August 21st, 2008

In PostGIS world, I often need to construct geometry from Well-Known-Binary (WKB) or PostGIS EWKB stream encoded as hex stream. It’s easy to do if I have access to PostgreSQL/PostGIS client which accepts SQL queries:

SELECT
   ST_AsText(
      ST_GeomFromWKB(
         decode('0101000000e5d022dbf93e2e40dbf97e6abc743540', 'hex'),
         4326))

I often need to do the same directly in C++ code – parse hex encoded binary stream to raw stream of bytes. Here is simple hex decoder I use:

#include <sstream>
#include <string>
#include <vector>
typedef std::vector<unsigned char> ewkb_t;

// bytes [out] - buffer for binary output
void hex_to_bytes(std::string const& hexstr, ewkb_t& bytes)
{
    bytes.clear();
    for(std::string::size_type i = 0; i < hexstr.size() / 2; ++i)
    {
        std::istringstream iss(hexstr.substr(i * 2, 2));
        unsigned int n;
        iss >> std::hex >> n;
        bytes.push_back(static_cast<unsigned char>(n));
    }
}

For example, I use it to build FDO geometry objects using another utility CreateGeometryFromExtendedWkb defined in FDO provider for PostGIS:

// POINT (1.234 5.678)
std::string ewkbhex("01010000005839B4C876BEF33F83C0CAA145B61640");
ewkb_t ewkb;
hex_to_bytes(ewkbhex, ewkb);
if (!ewkb.empty())
{
   FdoPtr<fdoigeometry> g = CreateGeometryFromExtendedWkb(ewkb);
   // ... use geometry
}

PostGIS provider for FDO update

Friday, May 9th, 2008

During last weeks, a lot of fresh bits have been contributed to the PostGIS provider for FDO Open Source. The great part of this work was committed by Bruno Scott and his colleagues. Bruno has recently joined the FDO development team. I’d like to express my deep gratitude to Bruno for his fantastic help in improving the PostGIS provider. The provider has got better shape and stability.

Recently, I’ve failed my duties in the provider development, so motivated by the load of Bruno’s work I found some gaps in time to submit a few fixes too :-)

(more…)