File: _geometry_helpers.pyx

package info (click to toggle)
python-shapely 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,564 kB
  • sloc: python: 18,650; ansic: 6,615; makefile: 88; sh: 62
file content (765 lines) | stat: -rw-r--r-- 28,027 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
cimport cython
from cpython cimport PyObject
from cython cimport view
from libc.stdint cimport uintptr_t

import numpy as np

cimport numpy as np

import shapely

from shapely._geos cimport (
    GEOSContextHandle_t,
    GEOSCoordSeq_clone_r,
    GEOSCoordSeq_getSize_r,
    GEOSCoordSequence,
    GEOSGeom_clone_r,
    GEOSGeom_createCollection_r,
    GEOSGeom_createEmptyPolygon_r,
    GEOSGeom_createLinearRing_r,
    GEOSGeom_createLineString_r,
    GEOSGeom_createPoint_r,
    GEOSGeom_createPolygon_r,
    GEOSGeom_destroy_r,
    GEOSGeom_getCoordSeq_r,
    GEOSGeometry,
    GEOSGeomTypeId_r,
    GEOSGetExteriorRing_r,
    GEOSGetGeometryN_r,
    GEOSGetInteriorRingN_r,
    get_geos_handle,
)
from shapely._pygeos_api cimport (
    import_shapely_c_api,
    PGERR_NAN_COORD,
    PGERR_SUCCESS,
    PGERR_GEOS_EXCEPTION,
    PGERR_LINEARRING_NCOORDS,
    PyGEOS_CoordSeq_FromBuffer,
    PyGEOS_CreateGeometry,
    PyGEOS_GetGEOSGeometry,
    ShapelyHandleNan,
)

# initialize Shapely C API
import_shapely_c_api()


def _check_out_array(object out, Py_ssize_t size):
    if out is None:
        return np.empty(shape=(size, ), dtype=object)
    if not isinstance(out, np.ndarray):
        raise TypeError("out array must be of numpy.ndarray type")
    if not out.flags.writeable:
        raise TypeError("out array must be writeable")
    if out.dtype != object:
        raise TypeError("out array dtype must be object")
    if out.ndim != 1:
        raise TypeError("out must be a one-dimensional array.")
    if out.shape[0] < size:
        raise ValueError(f"out array is too small ({out.shape[0]} < {size})")
    return out


@cython.boundscheck(False)
@cython.wraparound(False)
cdef int _create_simple_geometry(
    GEOSContextHandle_t geos_handle,
    const double[:, :] coord_view,
    Py_ssize_t idx,
    unsigned int n_coords,
    unsigned int dims,
    int geometry_type,
    char is_ring,
    int handle_nan,
    GEOSGeometry **geom,
) noexcept nogil:
    """
    Helper function to create a geometry with single CoordinateSequence
    (Point, LineString, LinearRing).
    """
    cdef GEOSCoordSequence *seq = NULL
    cdef int errstate
    cdef unsigned int actual_n_coords = 0

    errstate = PyGEOS_CoordSeq_FromBuffer(
        geos_handle, &coord_view[idx, 0], n_coords, dims,
        is_ring, handle_nan, &seq
    )
    if errstate != PGERR_SUCCESS:
        return errstate

    if geometry_type == 0:
        geom[0] = GEOSGeom_createPoint_r(geos_handle, seq)
    elif geometry_type == 1:
        geom[0] = GEOSGeom_createLineString_r(geos_handle, seq)
    elif geometry_type == 2:
        # check the resulting size to prevent invalid rings
        if GEOSCoordSeq_getSize_r(geos_handle, seq, &actual_n_coords) == 0:
            return PGERR_GEOS_EXCEPTION
        if 0 < actual_n_coords < 4:
            return PGERR_LINEARRING_NCOORDS
        geom[0] = GEOSGeom_createLinearRing_r(geos_handle, seq)

    if geom[0] == NULL:
        return PGERR_GEOS_EXCEPTION

    return PGERR_SUCCESS


def _create_simple_geometry_raise_error(int errstate):
    """
    Handle error raising for _create_simple_geometry above (so that the function
    itself can be fully C function without Python error checking).
    """
    if errstate == PGERR_NAN_COORD:
        raise ValueError(
            "A NaN, Inf or -Inf coordinate was supplied. Remove the "
            "coordinate or adapt the 'handle_nan' parameter."
        )
    elif errstate == PGERR_LINEARRING_NCOORDS:
        # the error equals PGERR_LINEARRING_NCOORDS (in shapely/src/geos.h)
        raise ValueError("A linearring requires at least 4 coordinates.")
    else:
        # GEOSException is raised by get_geos_handle
        return


@cython.boundscheck(False)
@cython.wraparound(False)
def simple_geometries_1d(object coordinates, object indices, int geometry_type, int handle_nan, object out = None):
    cdef Py_ssize_t idx = 0
    cdef unsigned int coord_idx = 0
    cdef Py_ssize_t geom_idx = 0
    cdef unsigned int n_coords = 0
    cdef unsigned int ring_closure = 0
    cdef GEOSGeometry *geom = NULL
    cdef GEOSCoordSequence *seq = NULL

    # Cast input arrays and define memoryviews for later usage
    coordinates = np.asarray(coordinates, dtype=np.float64, order="C")
    if coordinates.ndim != 2:
        raise TypeError("coordinates must be a two-dimensional array.")

    indices = np.asarray(indices, dtype=np.intp)  # intp is what bincount takes
    if indices.ndim != 1:
        raise TypeError("indices must be a one-dimensional array.")

    if coordinates.shape[0] != indices.shape[0]:
        raise ValueError("geometries and indices do not have equal size.")

    cdef unsigned int dims = coordinates.shape[1]
    if dims not in {2, 3}:
        raise ValueError("coordinates should be N by 2 or N by 3.")

    if geometry_type not in {0, 1, 2}:
        raise ValueError(f"Invalid geometry_type: {geometry_type}.")

    cdef char is_ring = 1 if geometry_type == 2 else 0

    if coordinates.shape[0] == 0:
        # return immediately if there are no geometries to return
        return np.empty(shape=(0, ), dtype=np.object_)

    if np.any(indices[1:] < indices[:indices.shape[0] - 1]):
        raise ValueError("The indices must be sorted.")

    cdef const double[:, :] coord_view = coordinates

    # get the geometry count per collection (this raises on negative indices)
    cdef unsigned int[:] coord_counts = np.bincount(indices).astype(np.uint32)

    # The final target array
    cdef Py_ssize_t n_geoms = coord_counts.shape[0]
    # Allow missing indices only if 'out' was given explicitly (if 'out' is not
    # supplied by the user, we would have to come up with an output value ourselves).
    cdef char allow_missing = out is not None
    out = _check_out_array(out, n_geoms)
    cdef object[:] out_view = out

    with get_geos_handle() as geos_handle:
        for geom_idx in range(n_geoms):
            n_coords = coord_counts[geom_idx]

            if n_coords == 0:
                if allow_missing:
                    continue
                else:
                    raise ValueError(
                        f"Index {geom_idx} is missing from the input indices."
                    )
            errstate = _create_simple_geometry(
                geos_handle, coord_view, idx, n_coords, dims, geometry_type,
                is_ring, handle_nan, &geom
            )
            if errstate != PGERR_SUCCESS:
                return _create_simple_geometry_raise_error(errstate)

            idx += n_coords

            out_view[geom_idx] = PyGEOS_CreateGeometry(geom, geos_handle)

    return out



cdef const GEOSGeometry* GetRingN(GEOSContextHandle_t handle, GEOSGeometry* polygon, int n):
    if n == 0:
        return GEOSGetExteriorRing_r(handle, polygon)
    else:
        return GEOSGetInteriorRingN_r(handle, polygon, n - 1)



@cython.boundscheck(False)
@cython.wraparound(False)
def get_parts(object[:] array, bint extract_rings=0):
    cdef Py_ssize_t geom_idx = 0
    cdef Py_ssize_t part_idx = 0
    cdef Py_ssize_t idx = 0
    cdef Py_ssize_t count
    cdef GEOSGeometry *geom = NULL
    cdef const GEOSGeometry *part = NULL

    if extract_rings:
        counts = shapely.get_num_interior_rings(array)
        is_polygon = (shapely.get_type_id(array) == 3) & (~shapely.is_empty(array))
        counts += is_polygon
        count = counts.sum()
    else:
        counts = shapely.get_num_geometries(array)
        count = counts.sum()

    if count == 0:
        # return immediately if there are no geometries to return
        return (
            np.empty(shape=(0, ), dtype=object),
            np.empty(shape=(0, ), dtype=np.intp)
        )

    parts = np.empty(shape=(count, ), dtype=object)
    index = np.empty(shape=(count, ), dtype=np.intp)

    cdef int[:] counts_view = counts
    cdef object[:] parts_view = parts
    cdef np.intp_t[:] index_view = index

    with get_geos_handle() as geos_handle:
        for geom_idx in range(array.size):
            if counts_view[geom_idx] <= 0:
                # No parts to return, skip this item
                continue

            if PyGEOS_GetGEOSGeometry(<PyObject *>array[geom_idx], &geom) == 0:
                raise TypeError("One of the arguments is of incorrect type. "
                                "Please provide only Geometry objects.")

            if geom == NULL:
                continue

            for part_idx in range(counts_view[geom_idx]):
                index_view[idx] = geom_idx

                if extract_rings:
                    part = GetRingN(geos_handle, geom, part_idx)
                else:
                    part = GEOSGetGeometryN_r(geos_handle, geom, part_idx)
                if part == NULL:
                    return  # GEOSException is raised by get_geos_handle

                # clone the geometry to keep it separate from the inputs
                part = GEOSGeom_clone_r(geos_handle, part)
                if part == NULL:
                    return  # GEOSException is raised by get_geos_handle

                # cast part back to <GEOSGeometry> to discard const qualifier
                # pending issue #227
                parts_view[idx] = PyGEOS_CreateGeometry(<GEOSGeometry *>part, geos_handle)

                idx += 1

    return parts, index


@cython.boundscheck(False)
@cython.wraparound(False)
cdef void _deallocate_arr(GEOSContextHandle_t handle, np.intp_t[:] arr, Py_ssize_t last_geom_i) noexcept nogil:
    """Deallocate a temporary geometry array to prevent memory leaks"""
    cdef Py_ssize_t i = 0
    cdef GEOSGeometry *g

    for i in range(last_geom_i):
        g = <GEOSGeometry *>arr[i]
        if g != NULL:
            GEOSGeom_destroy_r(handle, <GEOSGeometry *>arr[i])


@cython.boundscheck(False)
@cython.wraparound(False)
def collections_1d(object geometries, object indices, int geometry_type = 7, object out = None):
    """Converts geometries + indices to collections

    Allowed geometry type conversions are:
    - linearrings to polygons
    - points to multipoints
    - linestrings/linearrings to multilinestrings
    - polygons to multipolygons
    - any to geometrycollections
    """
    cdef Py_ssize_t geom_idx_1 = 0
    cdef Py_ssize_t coll_idx = 0
    cdef unsigned int coll_size = 0
    cdef Py_ssize_t coll_geom_idx = 0
    cdef GEOSGeometry *geom = NULL
    cdef GEOSGeometry *coll = NULL
    cdef int expected_type = -1
    cdef int expected_type_alt = -1
    cdef int curr_type = -1

    if geometry_type == 3:  # POLYGON
        expected_type = 2
    elif geometry_type == 4:  # MULTIPOINT
        expected_type = 0
    elif geometry_type == 5:  # MULTILINESTRING
        expected_type = 1
        expected_type_alt = 2
    elif geometry_type == 6:  # MULTIPOLYGON
        expected_type = 3
    elif geometry_type == 7:
        pass
    else:
        raise ValueError(f"Invalid geometry_type: {geometry_type}.")

    # Cast input arrays and define memoryviews for later usage
    geometries = np.asarray(geometries, dtype=object)
    if geometries.ndim != 1:
        raise TypeError("geometries must be a one-dimensional array.")

    indices = np.asarray(indices, dtype=np.intp)  # intp is what bincount takes
    if indices.ndim != 1:
        raise TypeError("indices must be a one-dimensional array.")

    if geometries.shape[0] != indices.shape[0]:
        raise ValueError("geometries and indices do not have equal size.")

    if geometries.shape[0] == 0:
        # return immediately if there are no geometries to return
        return np.empty(shape=(0, ), dtype=object)

    if np.any(indices[1:] < indices[:indices.shape[0] - 1]):
        raise ValueError("The indices should be sorted.")

    # get the geometry count per collection (this raises on negative indices)
    cdef int[:] collection_size = np.bincount(indices).astype(np.int32)

    # A temporary array for the geometries that will be given to CreateCollection.
    # Its size equals max(collection_size) to accommodate the largest collection.
    temp_geoms = np.empty(shape=(np.max(collection_size), ), dtype=np.intp)
    cdef np.intp_t[:] temp_geoms_view = temp_geoms

    # The final target array
    cdef Py_ssize_t n_colls = collection_size.shape[0]
    # Allow missing indices only if 'out' was given explicitly (if 'out' is not
    # supplied by the user, we would have to come up with an output value ourselves).
    cdef char allow_missing = out is not None
    out = _check_out_array(out, n_colls)
    cdef object[:] out_view = out

    with get_geos_handle() as geos_handle:
        for coll_idx in range(n_colls):
            if collection_size[coll_idx] == 0:
                if allow_missing:
                    continue
                else:
                    raise ValueError(
                        f"Index {coll_idx} is missing from the input indices."
                    )
            coll_size = 0

            # fill the temporary array with geometries belonging to this collection
            for coll_geom_idx in range(collection_size[coll_idx]):
                if PyGEOS_GetGEOSGeometry(<PyObject *>geometries[geom_idx_1 + coll_geom_idx], &geom) == 0:
                    _deallocate_arr(geos_handle, temp_geoms_view, coll_size)
                    raise TypeError(
                        "One of the arguments is of incorrect type. Please provide only Geometry objects."
                    )

                # ignore missing values
                if geom == NULL:
                    continue

                # Check geometry subtype for non-geometrycollections
                if geometry_type != 7:
                    curr_type = GEOSGeomTypeId_r(geos_handle, geom)
                    if curr_type == -1:
                        _deallocate_arr(geos_handle, temp_geoms_view, coll_size)
                        return  # GEOSException is raised by get_geos_handle
                    if curr_type != expected_type and curr_type != expected_type_alt:
                        _deallocate_arr(geos_handle, temp_geoms_view, coll_size)
                        raise TypeError(
                            f"One of the arguments has unexpected geometry type {curr_type}."
                        )

                # assign to the temporary geometry array
                geom = GEOSGeom_clone_r(geos_handle, geom)
                if geom == NULL:
                    _deallocate_arr(geos_handle, temp_geoms_view, coll_size)
                    return  # GEOSException is raised by get_geos_handle
                temp_geoms_view[coll_size] = <np.intp_t>geom
                coll_size += 1

            # create the collection
            if geometry_type != 3:  # Collection
                coll = GEOSGeom_createCollection_r(
                    geos_handle,
                    geometry_type,
                    <GEOSGeometry**> &temp_geoms_view[0],
                    coll_size
                )
            elif coll_size != 0:  # Polygon, non-empty
                coll = GEOSGeom_createPolygon_r(
                    geos_handle,
                    <GEOSGeometry*> temp_geoms_view[0],
                    <GEOSGeometry**> NULL if coll_size <= 1 else <GEOSGeometry**> &temp_geoms_view[1],
                    coll_size - 1
                )
            else:  # Polygon, empty
                coll = GEOSGeom_createEmptyPolygon_r(
                    geos_handle
                )

            if coll == NULL:
                return  # GEOSException is raised by get_geos_handle

            out_view[coll_idx] = PyGEOS_CreateGeometry(coll, geos_handle)

            geom_idx_1 += collection_size[coll_idx]

    return out


@cython.boundscheck(False)
@cython.wraparound(False)
def _from_ragged_array_multi_linear(
    const double[:, ::1] coordinates,
    const np.int64_t[:] offsets1,
    const np.int64_t[:] offsets2,
    int geometry_type,
):
    """
    Create Polygons or MultiLineStrings from coordinate and offset arrays.

    Polygon (geometry_type 3): linear_type is a LinearRing (2)
    MultiLineString (geometry_type 5): linear_type is a LineString (1)
    """
    cdef:
        Py_ssize_t n_total_coords, n_rings, n_geoms
        Py_ssize_t i, k
        Py_ssize_t i1, i2, k1, k2
        Py_ssize_t n_coords, linear_idx
        int errstate
        GEOSContextHandle_t geos_handle
        GEOSGeometry *linear = NULL
        GEOSGeometry *geom = NULL

    n_total_coords = coordinates.shape[0]
    n_rings = offsets1.shape[0] - 1
    n_geoms = offsets2.shape[0] - 1

    if offsets2[n_geoms] > n_rings:
        raise ValueError(
            f"Number of rings indicated by the geometry offsets ({offsets2[n_geoms]}) "
            f"larger than indicated by the shape of the linear offsets array ({n_rings})"
        )

    if offsets1[n_rings] > n_total_coords:
        raise ValueError(
            f"Number of coordinates indicated by the linear offsets ({offsets1[n_rings]}) "
            f"larger than the shape of the coordinates array ({n_total_coords})"
        )

    # A temporary array for the geometries that will be given to CreatePolygon/Collection.
    # For simplicity, we use n_rings instead of calculating the max needed size
    # as max(diff(offsets2)) (trading performance for a bit more memory usage)
    temp_linear = np.empty(shape=(n_rings, ), dtype=np.intp)
    cdef np.intp_t[:] temp_linear_view = temp_linear
    # A temporary array for resulting geometries
    temp_geoms = np.empty(shape=(n_geoms, ), dtype=np.intp)
    cdef np.intp_t[:] temp_geoms_view = temp_geoms

    # The final target array
    result = np.empty(shape=(n_geoms, ), dtype=object)
    cdef object[:] result_view = result

    cdef unsigned int dims = coordinates.shape[1]
    if dims not in {2, 3}:
        raise ValueError("coordinates should be N by 2 or N by 3.")

    cdef int linear_type
    cdef char is_ring
    if geometry_type == 3:
        # Polygon
        linear_type = 2
        is_ring = 1
    else:
        # MultiLineString
        linear_type = 1
        is_ring = 0
    cdef int handle_nan = 0

    with get_geos_handle() as geos_handle:
        with nogil:
            # iterating through the Polygons/MultiLineStrings
            for i in range(n_geoms):

                # each geometry can consist of multiple rings/lines
                # (for polygon: exterior ring + potentially interior rings(s))
                i1 = offsets2[i]
                i2 = offsets2[i + 1]

                # iterating through the linear elements
                linear_idx = 0
                for k in range(i1, i2):

                    # each ring/line consists of certain number of coords
                    k1 = offsets1[k]
                    k2 = offsets1[k + 1]
                    n_coords = k2 - k1
                    errstate = _create_simple_geometry(
                        geos_handle, coordinates, k1, n_coords, dims, linear_type,
                        is_ring, handle_nan, &linear
                    )
                    if errstate != PGERR_SUCCESS:
                        _deallocate_arr(geos_handle, temp_linear_view, linear_idx)
                        _deallocate_arr(geos_handle, temp_geoms_view, i - 1)
                        with gil:
                            return _create_simple_geometry_raise_error(errstate)

                    temp_linear_view[linear_idx] = <np.intp_t>linear
                    linear_idx += 1

                if geometry_type == 3:
                    # create Polygon
                    if linear_idx > 0:
                        geom = GEOSGeom_createPolygon_r(
                            geos_handle,
                            <GEOSGeometry*> temp_linear_view[0],
                            <GEOSGeometry**> &temp_linear_view[1 if linear_idx > 1 else 0],
                            linear_idx - 1
                        )
                    else:
                        geom = GEOSGeom_createEmptyPolygon_r(geos_handle)
                else:
                    # create MultiLineString collection
                    geom = GEOSGeom_createCollection_r(
                        geos_handle,
                        geometry_type,
                        <GEOSGeometry**> &temp_linear_view[0],
                        linear_idx
                    )
                if geom == NULL:
                    _deallocate_arr(geos_handle, temp_linear_view, linear_idx - 1)
                    _deallocate_arr(geos_handle, temp_geoms_view, i - 1)
                    with gil:
                        return  # GEOSException is raised by get_geos_handle

                temp_geoms_view[i] = <np.intp_t>geom

        for i in range(n_geoms):
            result_view[i] = PyGEOS_CreateGeometry(<GEOSGeometry *>temp_geoms_view[i], geos_handle)

    return result


@cython.boundscheck(False)
@cython.wraparound(False)
def _from_ragged_array_multipolygon(
    const double[:, ::1] coordinates,
    const np.int64_t[:] offsets1,
    const np.int64_t[:] offsets2,
    const np.int64_t[:] offsets3,
):
    """
    Create MultiPolygons from coordinate and offset arrays.
    """
    cdef:
        Py_ssize_t n_total_coords, n_rings, n_parts, n_geoms
        Py_ssize_t i, j, k
        Py_ssize_t i1, i2, j1, j2, k1, k2
        Py_ssize_t n_coords, rings_idx, parts_idx
        int errstate
        GEOSContextHandle_t geos_handle
        GEOSGeometry *ring = NULL
        GEOSGeometry *part = NULL
        GEOSGeometry *geom = NULL

    n_total_coords = coordinates.shape[0]
    n_rings = offsets1.shape[0] - 1
    n_parts = offsets2.shape[0] - 1
    n_geoms = offsets3.shape[0] - 1

    if offsets3[n_geoms] > n_parts:
        raise ValueError(
            f"Number of geometry parts indicated by the geometry offsets ({offsets3[n_geoms]}) "
            f"larger than indicated by the shape of the part offsets array ({n_parts})"
        )

    if offsets2[n_parts] > n_rings:
        raise ValueError(
            f"Number of rings indicated by the part offsets ({offsets2[n_parts]}) "
            f"larger than indicated by the shape of the linear offsets array ({n_rings})"
        )

    if offsets1[n_rings] > n_total_coords:
        raise ValueError(
            f"Number of coordinates indicated by the linear offsets ({offsets1[n_rings]}) "
            f"larger than the shape of the coordinates array ({n_total_coords})"
        )

    # A temporary array for the geometries that will be given to CreatePolygon
    # and CreateCollection. For simplicity, we use n_rings/n_parts instead of
    # calculating the max needed size (trading performance for a bit more memory usage)
    temp_rings = np.empty(shape=(n_rings, ), dtype=np.intp)
    cdef np.intp_t[:] temp_rings_view = temp_rings
    temp_parts = np.empty(shape=(n_parts, ), dtype=np.intp)
    cdef np.intp_t[:] temp_parts_view = temp_parts
    # A temporary array for resulting geometries
    temp_geoms = np.empty(shape=(n_geoms, ), dtype=np.intp)
    cdef np.intp_t[:] temp_geoms_view = temp_geoms

    # The final target array
    result = np.empty(shape=(n_geoms, ), dtype=object)
    cdef object[:] result_view = result

    cdef unsigned int dims = coordinates.shape[1]
    if dims not in {2, 3}:
        raise ValueError("coordinates should be N by 2 or N by 3.")

    cdef int ring_type = 2
    cdef int geometry_type = 6  # MultiPolygon
    cdef char is_ring = 1
    cdef int handle_nan = 0

    with get_geos_handle() as geos_handle:
        with nogil:
            # iterating through the MultiPolygons
            for i in range(n_geoms):

                # getting the indices for their parts
                i1 = offsets3[i]
                i2 = offsets3[i + 1]

                # Iterating through the geometry parts
                parts_idx = 0
                for j in range(i1, i2):

                    # each part (polygon) can consist of multiple rings
                    # (exterior ring + potentially interior rings(s))
                    j1 = offsets2[j]
                    j2 = offsets2[j + 1]

                    # iterating through the rings
                    rings_idx = 0
                    for k in range(j1, j2):

                        # each ring consists of certain number of coords
                        k1 = offsets1[k]
                        k2 = offsets1[k + 1]
                        n_coords = k2 - k1
                        errstate = _create_simple_geometry(
                            geos_handle, coordinates, k1, n_coords, dims, ring_type,
                            is_ring, handle_nan, &ring
                        )
                        if errstate != PGERR_SUCCESS:
                            _deallocate_arr(geos_handle, temp_rings_view, rings_idx)
                            _deallocate_arr(geos_handle, temp_parts_view, parts_idx - 1)
                            _deallocate_arr(geos_handle, temp_geoms_view, i - 1)
                            with gil:
                                return _create_simple_geometry_raise_error(errstate)

                        temp_rings_view[rings_idx] = <np.intp_t>ring
                        rings_idx += 1

                    part = GEOSGeom_createPolygon_r(
                        geos_handle,
                        <GEOSGeometry*> temp_rings_view[0],
                        <GEOSGeometry**> &temp_rings_view[1 if rings_idx > 1 else 0],
                        rings_idx - 1
                    )
                    if part == NULL:
                        _deallocate_arr(geos_handle, temp_rings_view, rings_idx - 1)
                        _deallocate_arr(geos_handle, temp_parts_view, parts_idx)
                        _deallocate_arr(geos_handle, temp_geoms_view, i - 1)
                        with gil:
                            return  # GEOSException is raised by get_geos_handle

                    temp_parts_view[parts_idx] = <np.intp_t>part
                    parts_idx += 1

                geom = GEOSGeom_createCollection_r(
                    geos_handle,
                    geometry_type,
                    <GEOSGeometry**> &temp_parts_view[0],
                    parts_idx
                    )
                if geom == NULL:
                    _deallocate_arr(geos_handle, temp_parts_view, parts_idx - 1)
                    _deallocate_arr(geos_handle, temp_geoms_view, i - 1)
                    with gil:
                        return  # GEOSException is raised by get_geos_handle

                temp_geoms_view[i] = <np.intp_t>geom

        for i in range(n_geoms):
            result_view[i] = PyGEOS_CreateGeometry(<GEOSGeometry *>temp_geoms_view[i], geos_handle)

    return result


def _geom_factory(uintptr_t g):

    with get_geos_handle() as geos_handle:
        geom = PyGEOS_CreateGeometry(<GEOSGeometry *>g, geos_handle)

    return geom


def linestring_to_linearring(object line):
    cdef GEOSGeometry *geom = NULL
    cdef const GEOSCoordSequence *seq = NULL
    cdef GEOSCoordSequence *seq_cloned = NULL
    cdef GEOSGeometry *ring = NULL
    cdef int geom_type

    with get_geos_handle() as geos_handle:
        if PyGEOS_GetGEOSGeometry(<PyObject*> line, &geom) == 0:
            raise TypeError(
                "The argument is of incorrect type. Please provide a Geometry object."
            )
        if geom == NULL:
            raise TypeError(
                "The argument is of incorrect type. Please provide a Geometry object."
            )

        geom_type = GEOSGeomTypeId_r(geos_handle, geom)
        if geom_type != 1:
            raise TypeError(
                "The argument is of incorrect type. Please provide a LineString object."
            )

        seq = GEOSGeom_getCoordSeq_r(geos_handle, geom)
        if seq == NULL:
            return

        seq_cloned = GEOSCoordSeq_clone_r(geos_handle, seq)
        if seq_cloned == NULL:
            return

        ring = GEOSGeom_createLinearRing_r(geos_handle, seq_cloned)
        if ring == NULL:
            return

        result = PyGEOS_CreateGeometry(ring, geos_handle)

    return result