<commit_before># distutils: language = c++
# cython: profile=True
#

from collections import namedtuple
import logging

import numpy as np
cimport numpy as np

from rasterio cimport _gdal, _ogr, _io, _features
from rasterio import dtypes


cdef extern from "gdalwarper.h" nogil:

    ctypedef struct GDALWarpOptions

    cdef cppclass GDALWarpOperation:
        GDALWarpOperation() except +
        int Initialize(const GDALWarpOptions *psNewOptions)
        const GDALWarpOptions *GetOptions()
        int ChunkAndWarpImage( 
            int nDstXOff, int nDstYOff, int nDstXSize, int nDstYSize )
        int ChunkAndWarpMulti( 
            int nDstXOff, int nDstYOff, int nDstXSize, int nDstYSize )
        int WarpRegion( int nDstXOff, int nDstYOff, 
                        int nDstXSize, int nDstYSize,
                        int nSrcXOff=0, int nSrcYOff=0,
                        int nSrcXSize=0, int nSrcYSize=0,
                        double dfProgressBase=0.0, double dfProgressScale=1.0)
        int WarpRegionToBuffer( int nDstXOff, int nDstYOff, 
                                int nDstXSize, int nDstYSize, 
                                void *pDataBuf, 
                                int eBufDataType,
                                int nSrcXOff=0, int nSrcYOff=0,
                                int nSrcXSize=0, int nSrcYSize=0,
                                double dfProgressBase=0.0, 
                                double dfProgressScale=1.0)


RESAMPLING = namedtuple('RESAMPLING', [
                'nearest', 
                'bilinear', 
                'cubic', 
                'cubic_spline', 
                'lanczos', 
                'average', 
                'mode'] )(*list(range(7)))


cdef extern from "ogr_geometry.h" nogil:

    cdef cppclass OGRGeometry:
        pass

    cdef cppclass OGRGeometryFactory:
        void * transformWithOptions(void *geom, void *ct, char **options)
#            const OGRGeometry* poSrcGeom,
#            OGRCoordinateTransformation *poCT,
#            char** papszOptions


cdef extern from "ogr_spatialref.h":

    cdef cppclass OGRCoordinateTransformation:
        pass


log = logging.getLogger('rasterio')
class NullHandler(logging.Handler):
    def emit(self, record):
        pass
log.addHandler(NullHandler())


def tastes_like_gdal(t):
    return t[2] == t[4] == 0.0 and t[1] > 0 and t[5] < 0


cdef void *_osr_from_crs(object crs):
    cdef char *proj_c = NULL
    cdef void *osr
    osr = _gdal.OSRNewSpatialReference(NULL)
    params = []
    # Normally, we expect a CRS dict.
    if isinstance(crs, dict):
        # EPSG is a special case.
        init = crs.get('init')
        if init:
            auth, val = init.split(':')
            if auth.upper() == 'EPSG':
                _gdal.OSRImportFromEPSG(osr, int(val))
        else:
            crs['wktext'] = True
            for k, v in crs.items():
                if v is True or (k in ('no_defs', 'wktext') and v):
                    params.append("+%s" % k)
                else:
                    params.append("+%s=%s" % (k, v))
            proj = " ".join(params)
            log.debug("PROJ.4 to be imported: %r", proj)
            proj_b = proj.encode('utf-8')
            proj_c = proj_b
            _gdal.OSRImportFromProj4(osr, proj_c)
    # Fall back for CRS strings like "EPSG:3857."
    else:
        proj_b = crs.encode('utf-8')
        proj_c = proj_b
        _gdal.OSRSetFromUserInput(osr, proj_c)
    return osr


def _transform(src_crs, dst_crs, xs, ys):
    cdef double *x, *y
    cdef char *proj_c = NULL
    cdef void *src, *dst
    cdef void *transform
    cdef int i

    assert len(xs) == len(ys)
    
    src = _osr_from_crs(src_crs)
    dst = _osr_from_crs(dst_crs)

    n = len(xs)
    x = <double *>_gdal.CPLMalloc(n*sizeof(double))
    y = <double *>_gdal.CPLMalloc(n*sizeof(double))
    for i in range(n):
        x[i] = xs[i]
        y[i] = ys[i]

    transform = _gdal.OCTNewCoordinateTransformation(src, dst)
    res = _gdal.OCTTransform(transform, n, x, y, NULL)
    #if res:
    #    raise ValueError("Failed coordinate transformation")

    res_xs = [0]*n
    res_ys = [0]*n

    for i in range(n):
        res_xs[i] = x[i]
        res_ys[i] = y[i]

    _gdal.CPLFree(x)
    _gdal.CPLFree(y)
    _gdal.OCTDestroyCoordinateTransformation(transform)
    _gdal.OSRDestroySpatialReference(src)
    _gdal.OSRDestroySpatialReference(dst)
    return res_xs, res_ys


def _transform_geom(
        src_crs, dst_crs, geom, antimeridian_cutting, antimeridian_offset,
        precision):
    """Return a transformed geometry."""
    cdef char *proj_c = NULL
    cdef char *key_c = NULL
    cdef char *val_c = NULL
    cdef char **options = NULL
    cdef void *src, *dst
    cdef void *transform
    cdef OGRGeometryFactory *factory
    cdef void *src_ogr_geom
    cdef void *dst_ogr_geom
    cdef int i

    src = _osr_from_crs(src_crs)
    dst = _osr_from_crs(dst_crs)
    transform = _gdal.OCTNewCoordinateTransformation(src, dst)

    # Transform options.
    val_b = str(antimeridian_offset).encode('utf-8')
    val_c = val_b
    options = _gdal.CSLSetNameValue(
                options, "DATELINEOFFSET", val_c)<commit_msg>Add optional zs parameter to _transform, see #199<commit_after># distutils: language = c++
# cython: profile=True
#

from collections import namedtuple
import logging

import numpy as np
cimport numpy as np

from rasterio cimport _gdal, _ogr, _io, _features
from rasterio import dtypes


cdef extern from "gdalwarper.h" nogil:

    ctypedef struct GDALWarpOptions

    cdef cppclass GDALWarpOperation:
        GDALWarpOperation() except +
        int Initialize(const GDALWarpOptions *psNewOptions)
        const GDALWarpOptions *GetOptions()
        int ChunkAndWarpImage( 
            int nDstXOff, int nDstYOff, int nDstXSize, int nDstYSize )
        int ChunkAndWarpMulti( 
            int nDstXOff, int nDstYOff, int nDstXSize, int nDstYSize )
        int WarpRegion( int nDstXOff, int nDstYOff, 
                        int nDstXSize, int nDstYSize,
                        int nSrcXOff=0, int nSrcYOff=0,
                        int nSrcXSize=0, int nSrcYSize=0,
                        double dfProgressBase=0.0, double dfProgressScale=1.0)
        int WarpRegionToBuffer( int nDstXOff, int nDstYOff, 
                                int nDstXSize, int nDstYSize, 
                                void *pDataBuf, 
                                int eBufDataType,
                                int nSrcXOff=0, int nSrcYOff=0,
                                int nSrcXSize=0, int nSrcYSize=0,
                                double dfProgressBase=0.0, 
                                double dfProgressScale=1.0)


RESAMPLING = namedtuple('RESAMPLING', [
                'nearest', 
                'bilinear', 
                'cubic', 
                'cubic_spline', 
                'lanczos', 
                'average', 
                'mode'] )(*list(range(7)))


cdef extern from "ogr_geometry.h" nogil:

    cdef cppclass OGRGeometry:
        pass

    cdef cppclass OGRGeometryFactory:
        void * transformWithOptions(void *geom, void *ct, char **options)
#            const OGRGeometry* poSrcGeom,
#            OGRCoordinateTransformation *poCT,
#            char** papszOptions


cdef extern from "ogr_spatialref.h":

    cdef cppclass OGRCoordinateTransformation:
        pass


log = logging.getLogger('rasterio')
class NullHandler(logging.Handler):
    def emit(self, record):
        pass
log.addHandler(NullHandler())


def tastes_like_gdal(t):
    return t[2] == t[4] == 0.0 and t[1] > 0 and t[5] < 0


cdef void *_osr_from_crs(object crs):
    cdef char *proj_c = NULL
    cdef void *osr
    osr = _gdal.OSRNewSpatialReference(NULL)
    params = []
    # Normally, we expect a CRS dict.
    if isinstance(crs, dict):
        # EPSG is a special case.
        init = crs.get('init')
        if init:
            auth, val = init.split(':')
            if auth.upper() == 'EPSG':
                _gdal.OSRImportFromEPSG(osr, int(val))
        else:
            crs['wktext'] = True
            for k, v in crs.items():
                if v is True or (k in ('no_defs', 'wktext') and v):
                    params.append("+%s" % k)
                else:
                    params.append("+%s=%s" % (k, v))
            proj = " ".join(params)
            log.debug("PROJ.4 to be imported: %r", proj)
            proj_b = proj.encode('utf-8')
            proj_c = proj_b
            _gdal.OSRImportFromProj4(osr, proj_c)
    # Fall back for CRS strings like "EPSG:3857."
    else:
        proj_b = crs.encode('utf-8')
        proj_c = proj_b
        _gdal.OSRSetFromUserInput(osr, proj_c)
    return osr


def _transform(src_crs, dst_crs, xs, ys, zs):
    cdef double *x, *y, *z = NULL
    cdef char *proj_c = NULL
    cdef void *src, *dst
    cdef void *transform
    cdef int i

    assert len(xs) == len(ys)
    assert zs is None or len(xs) == len(zs)

    src = _osr_from_crs(src_crs)
    dst = _osr_from_crs(dst_crs)

    n = len(xs)
    x = <double *>_gdal.CPLMalloc(n*sizeof(double))
    y = <double *>_gdal.CPLMalloc(n*sizeof(double))
    for i in range(n):
        x[i] = xs[i]
        y[i] = ys[i]

    if zs is not None:
        z = <double *>_gdal.CPLMalloc(n*sizeof(double))
        for i in range(n):
            z[i] = zs[i]

    transform = _gdal.OCTNewCoordinateTransformation(src, dst)
    res = _gdal.OCTTransform(transform, n, x, y, z)
    #if res:
    #    raise ValueError("Failed coordinate transformation")

    res_xs = [0]*n
    res_ys = [0]*n

    for i in range(n):
        res_xs[i] = x[i]
        res_ys[i] = y[i]

    if zs is not None:
        res_zs = [0]*n
        for i in range(n):
            res_zs[i] = z[i]
        _gdal.CPLFree(z)

        retval = (res_xs, res_ys, res_zs)
    else:
        retval = (res_xs, res_ys)

    _gdal.CPLFree(x)
    _gdal.CPLFree(y)
    _gdal.OCTDestroyCoordinateTransformation(transform)
    _gdal.OSRDestroySpatialReference(src)
    _gdal.OSRDestroySpatialReference(dst)
    return retval


def _transform_geom(
        src_crs, dst_crs, geom, antimeridian_cutting, antimeridian_offset,
        precision):
    """Return a transformed geometry."""
    cdef char *proj_c = NULL
    cdef char *key_c = NULL
    cdef char *val_c = NULL
    cdef char **options = NULL
    cdef void *src, *dst
    cdef void *transform
    cdef OGRGeometryFactory *factory
    cdef void *src_ogr_geom
    cdef void *dst_ogr_geom
    cdef int i

    src = _osr_from_crs(src_crs)
    dst = _osr_from_crs(dst_crs)
    transform = _gdal.OCTNewCoordinateTransformation(src, dst)

    # Transform options.
    val_b = str(antimeridian_offset).encode('utf-8')
    val_c = val_b
    options = _gdal.CSLSetNameValue(
                options, "DATELINEOFFSET", val_c)
{'fraction_non_alphanumeric': 0.0979253112033195, 'fraction_numerical': 0.007007837713231904, 'mean_word_length': 2.019487750556793, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '722326', 'n_tokens_mistral': 3939, 'n_tokens_neox': 3738, 'n_words': 901}