
    &h|                         S r SSKrSSKrSSKrSSKJrJrJrJ	r	J
r
Jr  SSKJr  SSKJrJrJrJrJrJr  S rS r                 S
S jr             SS	 jrg)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                 v    [        U [        5      (       a  U $ [        U S5      (       a  U R                  5       $ U $ )zFConvert path-like to a string if possible, pass-through other objects.
__fspath__)
isinstancestrhasattrr   )paths    dC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\pyogrio/geopandas.py_stringify_pathr      s7    $ t\""   K    c                 \   SS K n[        (       a  SS0nO[        (       a  SSS.nOSS0n[        R                  " 5          [        R
                  " SS[        5         UR                  " U 40 UD6nS S S 5        WR                  S	;   a   UR                  " U 4S
S0UD6nUR                  R                  S:X  aA  [        (       a  UR                  R                  S5      nU$ UR                  R                  SS9nU$ ! [         a    U n Nf = f! , (       d  f       N= f! [         a     Nf = f)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;   DC;	D ;D
D	D

D
D
D+*D+c                0   [         (       d  [        S5      eSSKnSSKnSSKn[        U 5      n Uc2  [        [        [        R                  R                  SS5      5      5      nU(       a  [        O[        nU(       a  SOUnU(       d  SUS'   U" U 4UUUUUUUUU	U
UUUUS	.UD6nU(       Ga  SSKnUu  nnS
S0n[        (       a  [        (       d  UR!                  5       UR#                  [$        R&                  S9UR)                  5       UR#                  [$        R&                  S9UR+                  5       UR#                  [$        R&                  S90R                  US'   Ub  UR-                  U5         UR.                  " S0 UD6nAU(       a&  UR5                  US   5      nS/UR6                  l        US   =(       d    SnU(       d)  [;        UR<                  5      S:X  a  UR?                  5       $ UUR<                  ;   a  URA                  U5      n[B        (       a#  URD                  [F        :w  a  URI                  SS9nURK                  UUS9US'   U(       a  URM                  US   5      US'   URO                  USUS   S9$ U$ Uu  nn n!n"US   RQ                  5       n[S        [;        U5      5       V#s0 s H  n#UU#   U"U#   _M     n$n#U(       a  URU                  U SS9n OSn UR?                  U$UU S9n[W        US   UR<                  5       H/  u  n%n&U%RY                  S5      (       d  M  [[        UU&   5      UU&'   M1     U!b  U(       d  U$ URK                  U!US9n!URO                  UU!US   S9$ ! [0         a  n[3        S5      UeSnAff = fs  sn#f )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   sw   ` =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/B!GAJ
1%/BDCU+	dG5	9BX

3qJ'''1.BqE 4 }	Z@H??2d5k?BB] " 	!U	> Ds   5M5 9N5
N?NNc                 ,   [         (       d  [        S5      eSSKnSSKJn  [        U UR                  5      (       d  [        S5      eU	c2  [        [        [        R                  R                  SS5      5      5      n	[        XUS9u  pU R                  U R                  S	:H     n[!        U5      S
:  a  [        S5      e[!        U5      S:  a  US   nU U   nOSnSnUb  UR"                  nUGb  Ub  UGc  SnSnU R$                  (       Gdd  [&        R(                  " 5          [&        R*                  " SS[,        5        UR/                  5       nSSS5        UWUR0                  ) -     R2                  nUR5                  5       nUR7                  5       nU[8        ;   a  U(       a  U(       d  [;        SU 35      eUR=                  WR?                  5       5      RA                  5       RB                  n[!        U5      S
:X  a  US   nU(       a  US;   a  SU 3nOY[!        U5      S:X  aJ  SU;   a	  SU;   a  SnO SU;   a	  SU;   a  SnOSU;   a	  SU;   a  SnOSnUb  Uc  U[D        ;   a  SnU(       a  UnUc  UnU(       a  US:w  a  U S3nSnUbS  URF                  (       aB  URF                  RI                  5       nU(       a  SU 3nOURF                  RK                  S5      nU	(       Ga  SSK&nSSK'J(n   UGb  U(       a  SSK)n!WS:H  n"US:H  n#US:H  n$U"R5                  5       (       a*  U!RU                  [V        RX                  " UU"   5      SS9UU"'   U#R5                  5       (       a*  U!R[                  [V        RX                  " UU#   5      SS9UU#'   U$R5                  5       (       a*  U!R]                  [V        RX                  " UU$   5      SS9UU$'   U" URB                  5      nU R_                  SS 9n UR                  U SS!9n UU U'   UR`                  Rc                  U SS"9n%[e        U%Rf                  5       Hv  u  n&n'U'Rh                  URk                  5       :X  d  M&  U%Rm                  U&U'Ro                  URq                  5       5      U%U&   Rs                  URq                  5       5      5      n%Mx     Ub  U%Rf                  Ru                  U5      n(URv                  Ry                  U(Rh                  5      (       d  URv                  R{                  U(Rh                  5      (       di  U%Rm                  U%Rf                  R}                  U5      U(Ro                  UR                  5       5      U%U   Rs                  UR                  5       5      5      n%U " U%U4UUUUUUUU
UUUUS#.UD6  gUb7  U" URB                  5      nU R                   V)s/ s H  n)U)U:X  a  M  U)PM     n*n)O[        U R                  5      n*/ n+/ n,0 n-U* GH  n.U U.   n/[        U/R                  UR                  5      (       a  U/R                  R                  S5      n0U0RB                  n1U0U/R                  R                  S$5      R                  R                  S5      -
  n2U2UR                  S%5      -  S&-   n3U3RB                  U-U.'   OU/RB                  n1[        U1UR                  R                  R                  5      (       a  SS'KJJKn4JLn5JMn6  [        U1U6U5U445      (       a9  U+R                  U1R                  5        U,R                  U1R                  5        GME  U+R                  [V        R                  " U15      5        U,R                  [V        R                  " U1R                  5       5      5        GM  U+R                  U15        U,R                  S5        GM     [        U40 S(U_S)U_S	U_S*U+_S+U,_S,U*_S-U_S.U_S/U_S0U_S1U_S2U_S3U
_S4U_S5U_S6U_S7U_S8U-_UD6  g! , (       d  f       GN= fs  sn)f )9aU  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% xxx((*''2E{S!)!1 + !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388++JOO<<((LL00A((5/*//		< 		
 )'-)+'	
 	
" 	 "(//*ZZDZqO/C!ZDbjj! JJOhcii!3!344 FF&&t,E\\F 1 1% 8 ; ; G G MMI *3bll56I)IC)O&$>$E$EOD!ZZFfbff//>>??OO&<"MNN!!&,,/!!&,,/!!"**V"45!!"**V[[]";<f%d#; > 
  	
     $  *    * &  !" (#$ $%& (
)q +*^ 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   