/* osgEarth
* Copyright 2025 Pelican Mapping
* MIT License
*/
#ifndef OSGEARTHFEATURES_FEATURE_OGR_UTILS
#define OSGEARTHFEATURES_FEATURE_OGR_UTILS 1

#include <osgEarth/Common>
#include <osgEarth/Feature>
#include <osgEarth/Geometry>
#include <osgEarth/StringUtils>
#include <osg/Notify>
#include <ogr_api.h>

namespace osgEarth { namespace Util
{
    struct OSGEARTH_EXPORT OgrUtils
    {
        static void populate( OGRGeometryH geomHandle, Geometry* target, int numPoints );
    
        static Polygon* createPolygon( OGRGeometryH geomHandle, bool rewindPolygons = true);

        static MultiGeometry* createTIN(OGRGeometryH geomHandle);
       
        static Geometry* createGeometry( OGRGeometryH geomHandle, bool rewindPolygons = true);

        static OGRGeometryH encodePart( const Geometry* geometry, OGRwkbGeometryType part_type );

        static OGRGeometryH encodeShape( const Geometry* geometry, OGRwkbGeometryType shape_type, OGRwkbGeometryType part_type );    

        static OGRGeometryH createOgrGeometry(const Geometry* geometry, OGRwkbGeometryType requestedType = wkbUnknown);

        static AttributeType getAttributeType( OGRFieldType type );

        static OGRwkbGeometryType getOGRGeometryType(const Geometry::Type& type);
    
        static OGRwkbGeometryType getOGRGeometryType(const Geometry* geometry);

        struct OSGEARTH_EXPORT OGRFeatureFactory
        {
            const SpatialReference* srs = nullptr;
            optional<GeoInterpolation> interp = { GEOINTERP_DEFAULT };
            bool keepNullValues = true;
            bool rewindPolygons = true;

            Feature* createFeature(OGRFeatureH handle) const;
        };

        OE_DEPRECATED("Use OGRFeatureFactory::create instead")
        inline Feature* createFeature(OGRFeatureH handle, const SpatialReference* srs, const optional<GeoInterpolation>& interp, bool rewindPolygons = true) {
            OGRFeatureFactory factory;
            factory.srs = srs;
            factory.interp = interp;
            factory.rewindPolygons = rewindPolygons;
            return factory.createFeature(handle);
        }

        OE_DEPRECATED("Use OGRFeatureFactory::create instead")
            inline Feature* createFeature(OGRFeatureH handle, const SpatialReference* srs, bool rewindPolygons) {
            OGRFeatureFactory factory;
            factory.srs = srs;
            factory.rewindPolygons = rewindPolygons;
            return factory.createFeature(handle);
        }
    };
} }

#endif // OSGEARTHFEATURES_FEATURE_OGR_GEOM_UTILS

