a
    J5d?@                     @   s   d Z ddlZddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ dd	gZd
d Zdd Zdd Zdd Zdd Zdd Zdd Zd&ddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd'd%d	ZdS )(a>  
This modules provides a conversion to / from a ragged (or "jagged") array
representation of the geometries.

A ragged array is an irregular array of arrays of which each element can have
a different length. As a result, such an array cannot be represented as a
standard, rectangular nD array.
The coordinates of geometries can be represented as arrays of arrays of
coordinate pairs (possibly multiple levels of nesting, depending on the
geometry type).


Geometries, as a ragged array of coordinates, can be efficiently represented
as contiguous arrays of coordinates provided that there is another data
structure that keeps track of which range of coordinate values corresponds
to a given geometry. This can be done using offsets, counts, or indices.

This module currently implements offsets into the coordinates array. This
is the ragged array representation defined by the the Apache Arrow project
as "variable size list array" (https://arrow.apache.org/docs/format/Columnar.html#variable-size-list-layout).
See for example https://cfconventions.org/Data/cf-conventions/cf-conventions-1.9/cf-conventions.html#representations-features
for different options.

The exact usage of the Arrow list array with varying degrees of nesting for the
different geometry types is defined by the GeoArrow project:
https://github.com/geoarrow/geoarrow

    N   )creation)GeometryTypeget_coordinate_dimension	get_parts	get_ringsget_type_id)get_coordinates)is_emptyto_ragged_arrayfrom_ragged_arrayc                 C   sX   t | |d}t| }| rPt|d }|tt| }tj||tjdd}|dfS )N	include_zr   Zaxis )	r	   r
   anynpZnonzeroarangeleninsertnan)arrr   coordsemptiesindicesr   r   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/shapely/_ragged_array.py_get_arrays_point0   s    r   c                 C   sR   t t |  dd}t||d krNt j|d|d t| fd|d d}|S )Nr   r   Zconstant)Zconstant_values)r   r   bincountcumsumr   pad)r   noffsetsr   r   r   _indices_to_offsets>   s    r#   c                 C   s4   t | dd\}}t|t| }t| |d}||ffS )NTreturn_indexr   )r   r#   r   r	   )r   r   _part_indicesr"   r   r   r   r   _get_arrays_multipointK   s    r(   c                 C   s*   t | d|d\}}t|t| }||ffS )NTr%   r   )r	   r#   r   )r   r   r   r   r"   r   r   r   _get_arrays_linestringW   s    r*   c                 C   sT   t | dd\}}t|t| }t|d|d\}}tt| dd}|||ffS NTr$   r)   r   )r   r#   r   r	   r   r   r   r   )r   r   arr_flatr'   offsets2r   r   offsets1r   r   r   _get_arrays_multilinestring_   s
    r/   c                 C   sT   t | dd\}}t|t| }t|d|d\}}tt| dd}|||ffS r+   )r   r#   r   r	   r   r   r   r   )r   r   r,   ring_indicesr-   r   r   r.   r   r   r   _get_arrays_polygonl   s
    r1   c                 C   s~   t | dd\}}t|t| }t|dd\}}tt| dd}t|d|d\}}	tt|	 dd}
||
||ffS )NTr$   r   r)   )	r   r#   r   r   r   r   r   r   r	   )r   r   r,   r'   offsets3Z	arr_flat2r0   r-   r   r   r.   r   r   r   _get_arrays_multipolygony   s    r3   c                 C   s  t | } |du r.t t| t|   dk}t t| }||dk }t|dkrt|d }|tj	kr|t
| |\}}n|tjkrt| |\}}nz|tjkrt| |\}}n`|tjkrt| |\}}nF|tjkrt| |\}}n,|tjkrt| |\}}ntd|j dnt|dkrt|tj	tjhkrLtj}t| |\}}nrt|tjtjhkrxtj}t| |\}}nFt|tjtjhkrtj}t| |\}}ntdd	d
 |D  dntddd
 |D  d|||fS )a1  
    Converts geometries to a ragged array representation using a contiguous
    array of coordinates and offset arrays.

    This function converts an array of geometries to a ragged array
    (i.e. irregular array of arrays) of coordinates, represented in memory
    using a single contiguous array of the coordinates, and
    up to 3 offset arrays that keep track where each sub-array
    starts and ends.

    This follows the in-memory layout of the variable size list arrays defined
    by Apache Arrow, as specified for geometries by the GeoArrow project:
    https://github.com/geoarrow/geoarrow.

    Parameters
    ----------
    geometries : array_like
        Array of geometries (1-dimensional).
    include_z : bool, default None
        If False, return 2D geometries. If True, include the third dimension
        in the output (if a geometry has no third dimension, the z-coordinates
        will be NaN). By default, will infer the dimensionality from the
        input geometries. Note that this inference can be unreliable with
        empty geometries (for a guaranteed result, it is recommended to
        specify the keyword).

    Returns
    -------
    tuple of (geometry_type, coords, offsets)
        geometry_type : GeometryType
            The type of the input geometries (required information for
            roundtrip).
        coords : np.ndarray
            Contiguous array of shape (n, 2) or (n, 3) of all coordinates
            of all input geometries.
        offsets: tuple of np.ndarray
            Offset arrays that make it possible to reconstruct the
            geometries from the flat coordinates array. The number of
            offset arrays depends on the geometry type. See
            https://github.com/geoarrow/geoarrow/blob/main/format.md
            for details.

    Notes
    -----
    Mixed singular and multi geometry types of the same basic type are
    allowed (e.g., Point and MultiPoint) and all singular types will be
    treated as multi types.
    GeometryCollections and other mixed geometry types are not supported.

    See also
    --------
    from_ragged_array

    Examples
    --------
    Consider a Polygon with one hole (interior ring):

    >>> import shapely
    >>> polygon = shapely.Polygon(
    ...     [(0, 0), (10, 0), (10, 10), (0, 10)],
    ...     holes=[[(2, 2), (3, 2), (2, 3)]]
    ... )
    >>> polygon
    <POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 3 2, 2 3, 2 2))>

    This polygon can be thought of as a list of rings (first ring is the
    exterior ring, subsequent rings are the interior rings), and each ring
    as a list of coordinate pairs. This is very similar to how GeoJSON
    represents the coordinates:

    >>> import json
    >>> json.loads(shapely.to_geojson(polygon))["coordinates"]
    [[[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0], [0.0, 0.0]],
     [[2.0, 2.0], [3.0, 2.0], [2.0, 3.0], [2.0, 2.0]]]

    This function will return a similar list of lists of lists, but
    using a single contiguous array of coordinates, and multiple arrays of
    offsets:

    >>> geometry_type, coords, offsets = shapely.to_ragged_array([polygon])
    >>> geometry_type
    <GeometryType.POLYGON: 3>
    >>> coords
    array([[ 0.,  0.],
           [10.,  0.],
           [10., 10.],
           [ 0., 10.],
           [ 0.,  0.],
           [ 2.,  2.],
           [ 3.,  2.],
           [ 2.,  3.],
           [ 2.,  2.]])

    >>> offsets
    (array([0, 5, 9]), array([0, 2]))

    As an example how to interpret the offsets: the i-th ring in the
    coordinates is represented by ``offsets[0][i]`` to ``offsets[0][i+1]``:

    >>> exterior_ring_start, exterior_ring_end = offsets[0][0], offsets[0][1]
    >>> coords[exterior_ring_start:exterior_ring_end]
    array([[ 0.,  0.],
           [10.,  0.],
           [10., 10.],
           [ 0., 10.],
           [ 0.,  0.]])

    N   r   r   Geometry type  is not supported   z,Geometry type combination is not supported (c                 S   s   g | ]}t |jqS r   r   name.0tr   r   r   
<listcomp>       z#to_ragged_array.<locals>.<listcomp>)c                 S   s   g | ]}t |jqS r   r8   r:   r   r   r   r=   %  r>   )r   Zasarrayr   r   r
   uniquer   r   r   POINTr   
LINESTRINGr*   POLYGONr1   
MULTIPOINTr(   MULTILINESTRINGr/   MULTIPOLYGONr3   
ValueErrorr9   set)Z
geometriesr   Z
geom_typestypr   r"   r   r   r   r      sV    m






c                 C   s@   t | }t| jdd}| r<t jdtjd	 ||< |S )Nr   r   Z	geom_type)
r   pointsr   isnanallr   emptyr   rA   item)r   resultr   r   r   r   _point_from_flatcoords.  s
    
rQ   c                 C   sp   t | }t|}ttt||}tjt|d td}t j	|||d}t jdt
jd ||dk< |S Nr   Zdtyper   outrJ   r   )r   rK   r   diffrepeatr   r   rN   objectZmultipointsr   rD   rO   )r   r"   rK   Zmultipoint_partsZmultipoint_indicesrP   r   r   r   _multipoint_from_flatcoords:  s    

rY   c                 C   sf   t |}t t t||}t jt|d td}tj| ||d}tjdt	j
d ||dk< |S rR   )r   rV   rW   r   r   rN   rX   r   linestringsr   rB   rO   )r   r"   Zlinestring_nZlinestring_indicesrP   r   r   r   _linestring_from_flatcoordsK  s    
r[   c                 C   sp   t | |}t|}ttt||}tjt|d td}tj	|||d}tjdt
jd ||dk< |S rR   )r[   r   rV   rW   r   r   rN   rX   r   Zmultilinestringsr   rE   rO   )r   r.   r-   rZ   Zmultilinestring_partsZmultilinestring_indicesrP   r   r   r   !_multilinestrings_from_flatcoordsX  s    

r\   c           	      C   s   t |}t t t||}tj| |d}t |}t t t||}t jt|d td}tj	|||d}tjdt
jd ||dk< |S )N)r   r   rS   rT   rJ   r   )r   rV   rW   r   r   r   ZlinearringsrN   rX   polygonsr   rC   rO   )	r   r.   r-   Zring_lengthsr0   ZringsZpolygon_rings_nZpolygon_indicesrP   r   r   r   _polygon_from_flatcoordsm  s    

r^   c                 C   sr   t | ||}t|}ttt||}tjt|d td}tj	|||d}tjdt
jd ||dk< |S rR   )r^   r   rV   rW   r   r   rN   rX   r   Zmultipolygonsr   rF   rO   )r   r.   r-   r2   r]   Zmultipolygon_partsZmultipolygon_indicesrP   r   r   r   _multipolygons_from_flatcoords  s    
r_   c                 C   s   | t jkr*|du s"t|dks"J t|S | t jkrDt|g|R  S | t jkr^t|g|R  S | t jkrxt	|g|R  S | t j
krt|g|R  S | t jkrt|g|R  S td| j ddS )aL  
    Creates geometries from a contiguous array of coordinates
    and offset arrays.

    This function creates geometries from the ragged array representation
    as returned by ``to_ragged_array``.

    This follows the in-memory layout of the variable size list arrays defined
    by Apache Arrow, as specified for geometries by the GeoArrow project:
    https://github.com/geoarrow/geoarrow.

    See :func:`to_ragged_array` for more details.

    Parameters
    ----------
    geometry_type : GeometryType
        The type of geometry to create.
    coords : np.ndarray
        Contiguous array of shape (n, 2) or (n, 3) of all coordinates
        for the geometries.
    offsets: tuple of np.ndarray
        Offset arrays that allow to reconstruct the geometries based on the
        flat coordinates array. The number of offset arrays depends on the
        geometry type. See
        https://github.com/geoarrow/geoarrow/blob/main/format.md for details.

    Returns
    -------
    np.ndarray
        Array of geometries (1-dimensional).

    See Also
    --------
    to_ragged_array

    Nr   r5   r6   )r   rA   r   rQ   rB   r[   rC   r^   rD   rY   rE   r\   rF   r_   rG   r9   )Zgeometry_typer   r"   r   r   r   r     s    %





)N)N)__doc__numpyr    r   Z	_geometryr   r   r   r   r   Zcoordinatesr	   Z
predicatesr
   __all__r   r#   r(   r*   r/   r1   r3   r   rQ   rY   r[   r\   r^   r_   r   r   r   r   r   <module>   s,   
 $