_pygeos_api.pxd 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. Provides a wrapper for the shapely.lib C API for use in Cython.
  3. Internally, the shapely C extension uses a PyCapsule to provide run-time access
  4. to function pointers within the C API.
  5. To use these functions, you must first call the following function in each Cython module:
  6. `import_shapely_c_api()`
  7. This uses a macro to dynamically load the functions from pointers in the PyCapsule.
  8. Each C function in shapely.lib exposed in the C API must be specially-wrapped to enable
  9. this capability.
  10. Segfaults will occur if the C API is not imported properly.
  11. """
  12. cimport numpy as np
  13. from cpython.ref cimport PyObject
  14. from shapely._geos cimport GEOSContextHandle_t, GEOSCoordSequence, GEOSGeometry
  15. cdef extern from "c_api.h":
  16. cdef enum ShapelyErrorCode:
  17. PGERR_SUCCESS,
  18. PGERR_NOT_A_GEOMETRY,
  19. PGERR_GEOS_EXCEPTION,
  20. PGERR_NO_MALLOC,
  21. PGERR_GEOMETRY_TYPE,
  22. PGERR_MULTIPOINT_WITH_POINT_EMPTY,
  23. PGERR_COORD_OUT_OF_BOUNDS,
  24. PGERR_EMPTY_GEOMETRY,
  25. PGERR_GEOJSON_EMPTY_POINT,
  26. PGERR_LINEARRING_NCOORDS,
  27. PGERR_NAN_COORD,
  28. PGWARN_INVALID_WKB,
  29. PGWARN_INVALID_WKT,
  30. PGWARN_INVALID_GEOJSON,
  31. PGERR_PYSIGNAL
  32. cpdef enum ShapelyHandleNan:
  33. SHAPELY_HANDLE_NAN_ALLOW,
  34. SHAPELY_HANDLE_NAN_SKIP,
  35. SHAPELY_HANDLE_NANS_ERROR
  36. # shapely.lib C API loader; returns -1 on error
  37. # MUST be called before calling other C API functions
  38. int import_shapely_c_api() except -1
  39. # C functions provided by the shapely.lib C API
  40. # Note: GeometryObjects are always managed as Python objects
  41. # in Cython to avoid memory leaks, not PyObject* (even though
  42. # they are declared that way in the header file).
  43. object PyGEOS_CreateGeometry(GEOSGeometry *ptr, GEOSContextHandle_t ctx)
  44. char PyGEOS_GetGEOSGeometry(PyObject *obj, GEOSGeometry **out) nogil
  45. int PyGEOS_CoordSeq_FromBuffer(
  46. GEOSContextHandle_t ctx, const double* buf, unsigned int size,
  47. unsigned int dims, char is_ring, int handle_nan,
  48. GEOSCoordSequence** coord_seq) nogil