
    hy                         d Z ddlZddlZddlZddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZmZ d Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZ	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zy)z7Functions for reading and writing GeoPandas dataframes.    N)HAS_GEOPANDASPANDAS_GE_15PANDAS_GE_20PANDAS_GE_22PANDAS_GE_30PYARROW_GE_19)DataSourceError)DRIVERS_NO_MIXED_DIMENSIONSDRIVERS_NO_MIXED_SINGLE_MULTI_get_write_path_driverread
read_arrowwritec                 b    t        | t              r| S t        | d      r| j                         S | S )zFConvert path-like to a string if possible, pass-through other objects.
__fspath__)
isinstancestrhasattrr   )paths    D/var/www/html/immo/lib/python3.12/site-packages/pyogrio/geopandas.py_stringify_pathr      s1    $ t\"   K    c                 ,   dd l }t        rddi}nt        rddd}nddi}t        j                         5  t        j
                  ddt               	  |j                  | fi |}d d d        j                  d	v r	  |j                  | fd
di|}|j                  j                  dk(  r?t        r|j                  j                  d      }|S |j                  j                  d      }|S # t        $ r | }Y w xY w# 1 sw Y   xY w# t        $ r Y w xY w)Nr   formatISO8601ignore)r   errors	yearfirstTz6.*parsing datetimes with mixed time zones will raise.*)objectstringutcMms)freq)pandasr   r   warningscatch_warningsfilterwarningsFutureWarningto_datetime	Exceptiondtypekinddtas_unitround)serpddatetime_kwargsress       r   _try_parse_datetimer5   (   s   #Y/	%.(C&-		 	 	" D	
	 "..88C yy((	 "..B$B/BC yy~~ &&..&C J &&,,D,)CJ#  	C	    		s;   C;C*;D *C85C;7C88C;;D	DDc                   t         st        d      ddl}ddl}ddl}t        |       } |2t        t        t        j                  j                  dd                  }|rt        nt        }|rdn|}|sd|d<    || f|||||||||	|
||||d	|}|rddl}|\  }}d
di}t        rt        s|j!                         |j#                  t$        j&                        |j)                         |j#                  t$        j&                        |j+                         |j#                  t$        j&                        ij                  |d<   ||j-                  |       	  |j.                  di |}~|r&|j5                  |d         }dg|j6                  _        |d   xs d}|s(t;        |j<                        dk(  r|j?                         S ||j<                  v r|jA                  |      }tB        r%|jD                  tF        k7  r|jI                  d      }|jK                  ||      |d<   |r|jM                  |d         |d<   |jO                  |d|d         S |S |\  }} }!}"|d   jQ                         }tS        t;        |            D #ci c]  }#||#   |"|#    }$}#|r|jU                  | d      } nd} |j?                  |$||       }tW        |d   |j<                        D ](  \  }%}&|%jY                  d      st[        ||&         ||&<   * |!|s|S |jK                  |!|      }!|jO                  ||!|d         S # t0        $ r}t3        d      |d}~ww xY wc c}#w )a!  Read from an OGR data source to a GeoPandas GeoDataFrame or Pandas DataFrame.

    If the data source does not have a geometry column or ``read_geometry`` is False,
    a DataFrame will be returned.

    Requires ``geopandas`` >= 0.8.

    Parameters
    ----------
    path_or_buffer : pathlib.Path or str, or bytes buffer
        A dataset path or URI, raw buffer, or file-like object with a read method.
    layer : int or str, optional (default: first layer)
        If an integer is provided, it corresponds to the index of the layer
        with the data source.  If a string is provided, it must match the name
        of the layer in the data source.  Defaults to first layer in data source.
    encoding : str, optional (default: None)
        If present, will be used as the encoding for reading string values from
        the data source.  By default will automatically try to detect the native
        encoding and decode to ``UTF-8``.
    columns : list-like, optional (default: all columns)
        List of column names to import from the data source.  Column names must
        exactly match the names in the data source, and will be returned in
        the order they occur in the data source.  To avoid reading any columns,
        pass an empty list-like.  If combined with ``where`` parameter, must
        include columns referenced in the ``where`` expression or the data may
        not be correctly read; the data source may return empty results or
        raise an exception (behavior varies by driver).
    read_geometry : bool, optional (default: True)
        If True, will read geometry into a GeoSeries.  If False, a Pandas DataFrame
        will be returned instead.
    force_2d : bool, optional (default: False)
        If the geometry has Z values, setting this to True will cause those to
        be ignored and 2D geometries to be returned
    skip_features : int, optional (default: 0)
        Number of features to skip from the beginning of the file before
        returning features.  If greater than available number of features, an
        empty DataFrame will be returned.  Using this parameter may incur
        significant overhead if the driver does not support the capability to
        randomly seek to a specific feature, because it will need to iterate
        over all prior features.
    max_features : int, optional (default: None)
        Number of features to read from the file.
    where : str, optional (default: None)
        Where clause to filter features in layer by attribute values. If the data source
        natively supports SQL, its specific SQL dialect should be used (eg. SQLite and
        GeoPackage: `SQLITE`_, PostgreSQL). If it doesn't, the `OGRSQL WHERE`_ syntax
        should be used. Note that it is not possible to overrule the SQL dialect, this
        is only possible when you use the ``sql`` parameter.
        Examples: ``"ISO_A3 = 'CAN'"``, ``"POP_EST > 10000000 AND POP_EST < 100000000"``
    bbox : tuple of (xmin, ymin, xmax, ymax) (default: None)
        If present, will be used to filter records whose geometry intersects this
        box.  This must be in the same CRS as the dataset.  If GEOS is present
        and used by GDAL, only geometries that intersect this bbox will be
        returned; if GEOS is not available or not used by GDAL, all geometries
        with bounding boxes that intersect this bbox will be returned.
        Cannot be combined with ``mask`` keyword.
    mask : Shapely geometry, optional (default: None)
        If present, will be used to filter records whose geometry intersects
        this geometry.  This must be in the same CRS as the dataset.  If GEOS is
        present and used by GDAL, only geometries that intersect this geometry
        will be returned; if GEOS is not available or not used by GDAL, all
        geometries with bounding boxes that intersect the bounding box of this
        geometry will be returned.  Requires Shapely >= 2.0.
        Cannot be combined with ``bbox`` keyword.
    fids : array-like, optional (default: None)
        Array of integer feature id (FID) values to select. Cannot be combined
        with other keywords to select a subset (``skip_features``,
        ``max_features``, ``where``, ``bbox``, ``mask``, or ``sql``). Note that
        the starting index is driver and file specific (e.g. typically 0 for
        Shapefile and 1 for GeoPackage, but can still depend on the specific
        file). The performance of reading a large number of features usings FIDs
        is also driver specific and depends on the value of ``use_arrow``. The order
        of the rows returned is undefined. If you would like to sort based on FID, use
        ``fid_as_index=True`` to have the index of the GeoDataFrame returned set to the
        FIDs of the features read. If ``use_arrow=True``, the number of FIDs is limited
        to 4997 for drivers with 'OGRSQL' as default SQL dialect. To read a larger
        number of FIDs, set ``user_arrow=False``.
    sql : str, optional (default: None)
        The SQL statement to execute. Look at the sql_dialect parameter for more
        information on the syntax to use for the query. When combined with other
        keywords like ``columns``, ``skip_features``, ``max_features``,
        ``where``, ``bbox``, or ``mask``, those are applied after the SQL query.
        Be aware that this can have an impact on performance, (e.g. filtering
        with the ``bbox`` or ``mask`` keywords may not use spatial indexes).
        Cannot be combined with the ``layer`` or ``fids`` keywords.
    sql_dialect : str, optional (default: None)
        The SQL dialect the SQL statement is written in. Possible values:

          - **None**: if the data source natively supports SQL, its specific SQL dialect
            will be used by default (eg. SQLite and Geopackage: `SQLITE`_, PostgreSQL).
            If the data source doesn't natively support SQL, the `OGRSQL`_ dialect is
            the default.
          - '`OGRSQL`_': can be used on any data source. Performance can suffer
            when used on data sources with native support for SQL.
          - '`SQLITE`_': can be used on any data source. All spatialite_
            functions can be used. Performance can suffer on data sources with
            native support for SQL, except for Geopackage and SQLite as this is
            their native SQL dialect.

    fid_as_index : bool, optional (default: False)
        If True, will use the FIDs of the features that were read as the
        index of the GeoDataFrame.  May start at 0 or 1 depending on the driver.
    use_arrow : bool, optional (default: False)
        Whether to use Arrow as the transfer mechanism of the read data
        from GDAL to Python (requires GDAL >= 3.6 and `pyarrow` to be
        installed). When enabled, this provides a further speed-up.
        Defaults to False, but this default can also be globally overridden
        by setting the ``PYOGRIO_USE_ARROW=1`` environment variable.
    on_invalid : str, optional (default: "raise")
        The action to take when an invalid geometry is encountered. Possible
        values:

        - **raise**: an exception will be raised if a WKB input geometry is
          invalid.
        - **warn**: invalid WKB geometries will be returned as ``None`` and a
          warning will be raised.
        - **ignore**: invalid WKB geometries will be returned as ``None``
          without a warning.
        - **fix**: an effort is made to fix invalid input geometries (currently
          just unclosed rings). If this is not possible, they are returned as
          ``None`` without a warning. Requires GEOS >= 3.11 and shapely >= 2.1.

    arrow_to_pandas_kwargs : dict, optional (default: None)
        When `use_arrow` is True, these kwargs will be passed to the `to_pandas`_
        call for the arrow to pandas conversion.
    **kwargs
        Additional driver-specific dataset open options passed to OGR.  Invalid
        options will trigger a warning.

    Returns
    -------
    GeoDataFrame or DataFrame (if no geometry is present)

    .. _OGRSQL:

        https://gdal.org/user/ogr_sql_dialect.html#ogr-sql-dialect

    .. _OGRSQL WHERE:

        https://gdal.org/user/ogr_sql_dialect.html#where

    .. _SQLITE:

        https://gdal.org/user/sql_sqlite_dialect.html#sql-sqlite-dialect

    .. _spatialite:

        https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html

    .. _to_pandas:

        https://arrow.apache.org/docs/python/generated/pyarrow.Table.html#pyarrow.Table.to_pandas

    z5geopandas is required to use pyogrio.read_dataframe()r   NPYOGRIO_USE_ARROW0FTdatetime_as_string)layerencodingcolumnsread_geometryforce_2dskip_featuresmax_featureswherebboxmaskfidssqlsql_dialectreturn_fidsself_destruct)na_valuetypes_mapperzCThe file being read is not encoded in UTF-8; please use_arrow=False
fid_columnfidgeometry_namewkb_geometry)
on_invalidgeometrycrs)rP   rQ   fields)name)r<   indexdtypesdatetime ).r   ImportError	geopandasr%   shapelyr   boolintosenvirongetr   r   pyarrowr   r   r    StringDtypenpnanlarge_stringjson_update	to_pandasUnicodeDecodeErrorr	   	set_indexrT   nameslenr<   	DataFramepopr   r,   r   to_numpyfrom_wkbr>   GeoDataFrametolistrangeIndexzip
startswithr5   )'path_or_bufferr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   fid_as_index	use_arrowrO   arrow_to_pandas_kwargskwargsgpr2   rZ   	read_funcgdal_force_2dresultpametatabledfexrM   
wkb_valuesrT   rP   
field_dataidatar,   cs'                                          r   read_dataframer   O   sp   ` QRR$^4NRZZ^^,?EFG	'
TI&EHM (,#$##!   !F& e "4( !IIK!@OO%r~~rvv~'FHHJ ?* #	 ~& "-MM01	 *6*B d<01B#WBHHN_-?BJJ1 4<<>!bjj(.J
 0 0F :'00$0?
$--jZ-PBzN!(!1!1"Z.!A:??2
U?LLI(.%D%:8n##%G/4S\/BC!GAJ
1%CDCU+	dG5	9BX

3 /qJ''1.BqE/ }	Z@H??2d5k?BB] " 	!U	> Ds   L; 	M;	MMMc                    t         st        d      ddl}ddlm} t        | |j                        st        d      |	2t        t        t        j                  j                  dd                  }	t        |||      \  }}| j                  | j                  d	k(     }t!        |      d
kD  rt        d      t!        |      dkD  r|d   }| |   }nd}d}||j"                  }|^||Yd}d}| j$                  s8t'        j(                         5  t'        j*                  ddt,               |j/                         }ddd       ||j0                   z     j2                  }|j5                         }|j7                         }|t8        v r|r|st;        d|       |j=                  j?                               jA                         jB                  }t!        |      d
k(  r|d   }|rM|dv rId| }nCt!        |      dk(  r5d|v rd|v rd}nd|v rd|v rd}nd|v rd|v rd}nd}||
|tD        v rd}|r|}||}|r
|dk7  r| d}d}|I|jF                  r=|jF                  jI                         }|rd| }n|jF                  jK                  d      }|	rddl&}ddl'm(}  ||rddl)}!dk(  }"|dk(  }#|dk(  }$|"j5                         r,|!jU                  tW        jX                  ||"         d      ||"<   |#j5                         r,|!j[                  tW        jX                  ||#         d      ||#<   |$j5                         r,|!j]                  tW        jX                  ||$         d      ||$<    ||jB                        }| j_                  d       } |j                  | d!      } || |<   |j`                  jc                  | d"      }%te        |%jf                        D ]s  \  }&}'|'jh                  |jk                         k(  s$|%jm                  |&|'jo                  |jq                               |%|&   js                  |jq                                     }%u ||%jf                  ju                  |      }(|jv                  jy                  |(jh                        s|jv                  j{                  |(jh                        si|%jm                  |%jf                  j}                  |      |(jo                  |j                               |%|   js                  |j                                     }% | |%|f||||||||
||||d#| y|2 ||jB                        }| j                  D )cg c]
  })|)|k(  r	|) }*})nt        | j                        }*g }+g },i }-|*D ]  }.| |.   }/t        |/j                  |j                        r|/j                  j                  d      }0|0jB                  }1|0|/j                  j                  d$      j                  j                  d      z
  }2|2|j                  d%      z  d&z   }3|3jB                  |-|.<   n|/jB                  }1t        |1|j                  j                  j                        rdd'lJmK}4mL}5mM}6 t        |1|6|5|4f      r8|+j                  |1j                         |,j                  |1j                         5|+j                  tW        j                  |1             |,j                  tW        j                  |1j                                      |+j                  |1       |,j                  d        t        |fi d(|d)|d	|d*|+d+|,d,|*d-|d.|d/|d0|d1|d2|d3|
d4|d5|d6|d7|d8|-| y# 1 sw Y   BxY wc c})w )9a  Write GeoPandas GeoDataFrame to an OGR file format.

    Parameters
    ----------
    df : GeoDataFrame or DataFrame
        The data to write. For attribute columns of the "object" dtype,
        all values will be converted to strings to be written to the
        output file, except None and np.nan, which will be set to NULL
        in the output file.
    path : str or io.BytesIO
        path to output file on writeable file system or an io.BytesIO object to
        allow writing to memory.  Will raise NotImplementedError if an open file
        handle is passed; use BytesIO instead.
        NOTE: support for writing to memory is limited to specific drivers.
    layer : str, optional (default: None)
        layer name to create.  If writing to memory and layer name is not
        provided, it layer name will be set to a UUID4 value.
    driver : string, optional (default: None)
        The OGR format driver used to write the vector file. By default attempts
        to infer driver from path.  Must be provided to write to memory.
    encoding : str, optional (default: None)
        If present, will be used as the encoding for writing string values to
        the file.  Use with caution, only certain drivers support encodings
        other than UTF-8.
    geometry_type : string, optional (default: None)
        By default, the geometry type of the layer will be inferred from the
        data, after applying the promote_to_multi logic. If the data only contains a
        single geometry type (after applying the logic of promote_to_multi), this type
        is used for the layer. If the data (still) contains mixed geometry types, the
        output layer geometry type will be set to "Unknown".

        This parameter does not modify the geometry, but it will try to force the layer
        type of the output file to this value. Use this parameter with caution because
        using a non-default layer geometry type may result in errors when writing the
        file, may be ignored by the driver, or may result in invalid files. Possible
        values are: "Unknown", "Point", "LineString", "Polygon", "MultiPoint",
        "MultiLineString", "MultiPolygon" or "GeometryCollection".
    promote_to_multi : bool, optional (default: None)
        If True, will convert singular geometry types in the data to their
        corresponding multi geometry type for writing. By default, will convert
        mixed singular and multi geometry types to multi geometry types for drivers
        that do not support mixed singular and multi geometry types. If False, geometry
        types will not be promoted, which may result in errors or invalid files when
        attempting to write mixed singular and multi geometry types to drivers that do
        not support such combinations.
    nan_as_null : bool, default True
        For floating point columns (float32 / float64), whether NaN values are
        written as "null" (missing value). Defaults to True because in pandas
        NaNs are typically used as missing value. Note that when set to False,
        behaviour is format specific: some formats don't support NaNs by
        default (e.g. GeoJSON will skip this property) or might treat them as
        null anyway (e.g. GeoPackage).
    append : bool, optional (default: False)
        If True, the data source specified by path already exists, and the
        driver supports appending to an existing data source, will cause the
        data to be appended to the existing records in the data source.  Not
        supported for writing to in-memory files.
        NOTE: append support is limited to specific drivers and GDAL versions.
    use_arrow : bool, optional (default: False)
        Whether to use Arrow as the transfer mechanism of the data to write
        from Python to GDAL (requires GDAL >= 3.8 and `pyarrow` to be
        installed). When enabled, this provides a further speed-up.
        Defaults to False, but this default can also be globally overridden
        by setting the ``PYOGRIO_USE_ARROW=1`` environment variable.
        Using Arrow does not support writing an object-dtype column with
        mixed types.
    dataset_metadata : dict, optional (default: None)
        Metadata to be stored at the dataset level in the output file; limited
        to drivers that support writing metadata, such as GPKG, and silently
        ignored otherwise. Keys and values must be strings.
    layer_metadata : dict, optional (default: None)
        Metadata to be stored at the layer level in the output file; limited to
        drivers that support writing metadata, such as GPKG, and silently
        ignored otherwise. Keys and values must be strings.
    metadata : dict, optional (default: None)
        alias of layer_metadata
    dataset_options : dict, optional
        Dataset creation options (format specific) passed to OGR. Specify as
        a key-value dictionary.
    layer_options : dict, optional
        Layer creation options (format specific) passed to OGR. Specify as
        a key-value dictionary.
    **kwargs
        Additional driver-specific dataset or layer creation options passed
        to OGR. pyogrio will attempt to automatically pass those keywords
        either as dataset or as layer creation option based on the known
        options for the specific driver. Alternatively, you can use the
        explicit `dataset_options` or `layer_options` keywords to manually
        do this (for example if an option exists as both dataset and layer
        option).

    z6geopandas is required to use pyogrio.write_dataframe()r   N)to_wkbz('df' must be a DataFrame or GeoDataFramer7   r8   )appendrP      zj'df' must have only one geometry column. Multiple geometry columns are not supported for output using OGR.UnknownFr   zGeoSeries\.notnaz1Mixed 2D and 3D coordinates are not supported by )Point
LineStringPolygonMulti   r   MultiPolygonr   MultiLineStringr   
MultiPointTz ZzEPSG:	WKT1_GDAL)write_arrow)axis)deep)copy)preserve_index)r:   driverrM   geometry_typerQ   r;   r   dataset_metadatalayer_metadatametadatadataset_optionslayer_optionsUTC15md   )BooleanArrayFloatingArrayIntegerArrayr:   r   r   
field_maskrR   rQ   r   r;   promote_to_multinan_as_nullr   r   r   r   r   r   gdal_tz_offsets)Tr   rX   r%   geopandas.arrayr   r   rl   
ValueErrorr[   r\   r]   r^   r_   r   r<   rU   rk   	geom_typeemptyr&   r'   r(   UserWarningnotnais_emptyhas_zanyallr
   r	   Seriesuniquedropnavaluesr   rQ   to_epsgto_wktr`   pyogrio.rawr   rZ   multipointsrb   
atleast_2dmultilinestringsmultipolygonsr   Tablefrom_pandas	enumerateschematypenull
set_column	with_typer    castfieldtypes	is_binaryis_large_binaryget_field_indexbinarylistr,   DatetimeTZDtyper.   tz_localize
tz_convert	Timedeltaapi
extensionsExtensionArraypandas.arraysr   r   r   r   _data_maskasarrayisnar   )7r   r   r:   r   r;   r   r   r   r   rx   r   r   r   r   r   rz   r2   r   geometry_columnsgeometry_columnrP   geometry_types_alltmp_geometry_typer   geometry_notna	has_z_arrall_zgeometry_types
multi_typerQ   epsgr   r   rZ   mask_pointsmask_linestringsmask_polygonsr   field_indexr   
geom_fieldr   rR   r   r   r   rS   colnaiver   	tz_offsetgdal_offset_representationr   r   r   s7                                                          r   write_dataframer   v  s   ` RSS&b",,'CDDRZZ^^,?EFG	)$vFLD&zz"))z"9:
q P
 	

 q *1-o& "%//"!1!9% xx((* 2''2E{S!)!12 !H4E4E3E!FGMMIMMOEMMOE44u%GxP   YY'9'@'@'BCJJLSSN>"a'$21$5!#(9 > )
 +00A/B(C%^$).>^3S!/J N2)^;!2J.<>3Q!-J!%J )(0"&CC+/(',6) -M)3#0/ 4
C"x|| ||##%$.C,,%%k2C+&0G;#5#E  2i ???$,3,?,?h{&;<1 -@ -H[) $'')181I1Ih/?&@A 2J 2H-. !$$&.5.C.Ch}&=>Q /D /H]+ hoo.He$B bu-B"*B$$R$> #,ELL"9 	KzzRWWY&((OOBIIK0+&++BIIK8	 & ++O<J"":??388++JOO<((LL00A((5/*//		< 		
 )'-)+'	
 	
" 	 "(//*ZZDqO/C!DDbjj! JJO $hcii!3!34 FF&&t,E\\F 1 1% 8 ; ; G G MMI *3bll56I)IC)O&$>$E$EOD!ZZFfbff//>>?OO&<"MN!!&,,/!!&,,/!!"**V"45!!"**V[[]";<f%d#;$> 
  	
     $  *    * &  !" (#$ $%& (
)q2 2^ Es   ,];
^"^;^)NNNTFr   NNNNNNNFNraiseN)NNNNNTFNNNNNN)__doc__r]   r&   numpyrb   pyogrio._compatr   r   r   r   r   r   pyogrio.errorsr	   r   r
   r   r   r   r   r   r   r5   r   r   rW   r   r   <module>r      s    = 	    + 
$T 
			'dCT	 er   