Ejemplo n.º 1
0
void ensure_equal_geometries(OGRGeometryH lhs, OGRGeometryH rhs, double tolerance)
{
    // Test raw pointers
    ensure("First geometry is NULL", NULL != lhs);
    ensure("Second geometry is NULL", NULL != rhs);
    ensure("Passed the same pointers to geometry", lhs != rhs);

    // Test basic properties
    ensure_equals("Geometry names do not match",
        std::string(OGR_G_GetGeometryName(lhs)), std::string(OGR_G_GetGeometryName(rhs)));

    ensure_equals("Sub-geometry counts do not match",
        OGR_G_GetGeometryCount(lhs), OGR_G_GetGeometryCount(rhs));

    ensure_equals("Point counts do not match",
        OGR_G_GetPointCount(lhs), OGR_G_GetPointCount(rhs));

    if (OGR_G_GetGeometryCount(lhs) > 0)
    {
        // Test sub-geometries recursively
        const int count = OGR_G_GetGeometryCount(lhs);
        for (int i = 0; i < count; ++i)
        {
            ensure_equal_geometries(OGR_G_GetGeometryRef(lhs, i),
                                    OGR_G_GetGeometryRef(rhs, i),
                                    tolerance);
        }
    }
    else
    {
        // Test geometry points
        const std::size_t csize = 3;
        double a[csize] = { 0 };
        double b[csize] = { 0 };
        double d[csize] = { 0 };
        double dmax = 0;

        const int count = OGR_G_GetPointCount(lhs);
        for (int i = 0; i < count; ++i)
        {
            OGR_G_GetPoint(lhs, i, &a[0], &a[1], &a[2]);
            OGR_G_GetPoint(rhs, i, &b[0], &b[1], &b[2]);

            // Test vertices
            for (std::size_t c = 0; c < csize; ++c)
            {
                d[c] = std::fabs(a[c] - b[c]);
            }

            const double* pos = std::max_element(d, d + csize);
            dmax = *pos;

            std::ostringstream os;
            os << "Error in vertex " << i << " off by " << dmax;

            ensure(os.str(), dmax < tolerance);
        }
    }
}
Ejemplo n.º 2
0
// Create a bounds from the Convex Hull of an OGR Geometry.
simplet_bounds_t*
simplet_bounds_from_ogr(OGRGeometryH geom){
  OGRGeometryH hull;

  // Grab the Convex Hull
  if(!(hull = OGR_G_ConvexHull(geom)))
    return NULL;

  // Create the bounds.
  simplet_bounds_t *bounds;
  if(!(bounds = simplet_bounds_new())){
    OGR_G_DestroyGeometry(hull);
    return NULL;
  }

  // Extend the bounds by adding the points from the Convex Hull.
  double x, y;
  for(int i = 0; i < OGR_G_GetGeometryCount(hull); i++){
    OGRGeometryH subgeom = OGR_G_GetGeometryRef(hull, i);
    if(subgeom == NULL)
      continue;
    for(int j = 0; j < OGR_G_GetPointCount(subgeom); j++){
      OGR_G_GetPoint(subgeom, j, &x, &y, NULL);
      simplet_bounds_extend(bounds, x, y);
    }
  }

  OGR_G_DestroyGeometry(hull);
  return bounds;
}