a
    ~b(|                     @   s   d dl mZmZ d dlZd dlmZ d dlZddlm	Z	 ddl
mZmZmZmZ ddlmZmZ dd	lmZmZ dd
lmZ G dd deZdd Zd#ddZd$ddZdd ZefddZdd Zg dfddZd%ddZdd  Z d!d" Z!e"e dS )&    )absolute_importdivisionN)defaultdict   )dimension_name)isscalarunique_iteratorpdunique_array   )	DataError	Interface)MultiInterfaceensure_ring)PandasInterfacec                   @   s  e Zd ZeZdZdZdZedd Z	edd Z
edd	 Zed
d Zedd Zedd Zedd ZedFddZedd Zedd Zedd ZedGddZedd Zedd  Zed!d" ZedHd$d%Zed&d' Zed(d) Zed*d+ Zeg fd,d-ZedId.d/Zed0d1 Zeg d#fd2d3Zed4d5 Z ed6d7 Z!ed8d9 Z"ed:d; Z#ed<d= Z$edJd>d?Z%ed@dA Z&edBdC Z'edDdE Z(dS )KSpatialPandasInterfacespatialpandasT c                 C   s
   dt jv S )Nr   )sysmodules)clsr   r   @lib/python3.9/site-packages/holoviews/core/data/spatialpandas.pyloaded   s    zSpatialPandasInterface.loadedc                 C   sP   |   sdS t||  }dtjv rLdtjvrLdd l}|t||j|jfO }|S )NF	geopandasZgeoviewsr   )r   
isinstance
data_typesr   r   r   GeoDataFrame	GeoSeries)r   objZis_sdfgpdr   r   r   applies   s    zSpatialPandasInterface.appliesc                 C   s   ddl m}m} ||fS )Nr   r   r   )r   r   r   )r   r   r   r   r   r   r   (   s    z!SpatialPandasInterface.data_typesc                 C   s   ddl m} |S )Nr   r   )r   r   )r   r   r   r   r   series_type-   s    z"SpatialPandasInterface.series_typec                 C   s   ddl m} |S )Nr   r   )r   r   )r   r   r   r   r   
frame_type2   s    z!SpatialPandasInterface.frame_typec                    sP   d}|   | v r&t | r&|S  fdd jD }|sHtd|d S )Ngeometryc                    s   g | ]}t  | r|qS r   r   .0cdataZstypesr   r   
<listcomp>=       z5SpatialPandasInterface.geo_column.<locals>.<listcomp>zXNo geometry column found in spatialpandas.GeoDataFrame, use the PandasInterface instead.r   )r#   r   columns
ValueError)r   r,   colcolsr   r+   r   
geo_column7   s    z!SpatialPandasInterface.geo_columnc                    sl  ddl m} |d u r|j}|d u r(|j}t||  r>| }dtjv rxdd l	}t||j
rd| }t||jrx||}t|trdtjv rt|}t|trt||||}n0t||  std| j nd|vr| | t|tjr|jjn|jjg}|d gkrdg}|| D ]D t   |jv r2qt fdd	|D r| } q\q|||d
i fS )Nr   r$   r   Zshapelyz)%s only support spatialpandas DataFrames.r&   indexc                 3   s"   | ]} |d u rdn|kV  qd S )Nr4   r   )r)   namekdr   r   	<genexpr>h   s   z.SpatialPandasInterface.init.<locals>.<genexpr>kdimsvdims)r   r   r:   r;   r   r#   to_framer   r   r   r   listfrom_shapely
from_multir%   r0   __name__r3   r	   Z	DataFramer4   namesr5   r   r/   anyZreset_index)r   eltyper,   r:   r;   r   r   Zindex_namesr   r6   r   initC   sF    






zSpatialPandasInterface.initc                    sr   |rdnd}|   tdkr>tdt jtf |  fdd |D }|rntdt| | d S )Nkeyallr   zExpected %s instance to declare two key dimensions corresponding to the geometry coordinates but %d dimensions were found which did not refer to any columns.c                    s&   g | ]}|vr|j  jvr|j qS r   r5   r,   r)   ddataset	geom_dimsr   r   r-   y   s   z3SpatialPandasInterface.validate.<locals>.<listcomp>z`Supplied data does not contain specified dimensions, the following dimensions were not found: %s)rL   lenr   typer@   
dimensionsrepr)r   rK   r;   Z	dim_typesZ	not_foundr   rJ   r   validateo   s    
zSpatialPandasInterface.validatec                 C   sD   |j |dd}|| |v r6| |j}|j| jjS |j|j jS )NT)strict)get_dimensionrL   r3   r,   dtypeZsubtyper5   )r   rK   	dimensiondimr1   r   r   r   rT      s
    zSpatialPandasInterface.dtypec           
      C   s   ddl m}m}m}m} | |j}|j| }t|j||fsBdS |D ]L}t||rht	|jdkrh dS t||rF|jD ]}	t	|	dkrx  dS qxqFdS )Nr   )MultiPolygonDtypePolygonDtypePolygonMultiPolygonFr   T)
spatialpandas.geometryrW   rX   rY   rZ   r3   r,   r   rT   rM   )
r   rK   rW   rX   rY   rZ   r1   seriesgeompr   r   r   	has_holes   s    


z SpatialPandasInterface.has_holesc                 C   s6   g }t |js|S | |j}|j| }dd |D S )Nc                 S   s   g | ]}t |qS r   )geom_to_holes)r)   r]   r   r   r   r-      r.   z0SpatialPandasInterface.holes.<locals>.<listcomp>)rM   r,   r3   )r   rK   holesr1   r\   r   r   r   ra      s    

zSpatialPandasInterface.holesNc                 K   s   |  |\}}||jd  ||jd  |j}|s8|S |d u rL| ||}| ||}|| }|rt|dkrt|jdkr||jd j jd S |S )Nr   r   )	rL   popr5   r,   select_maskindexedrM   r;   iloc)r   rK   Zselection_mask	selectionxdimydimZdfrd   r   r   r   select   s    zSpatialPandasInterface.selectc                 C   s   | j ||S N)base_interfacerc   )r   rK   rf   r   r   r   rc      s    z"SpatialPandasInterface.select_maskc                    s    fdd j  j D S )Nc                    s   g | ]}|j  jvr|qS r   rG   rH   rK   r   r   r-      s   z4SpatialPandasInterface.geom_dims.<locals>.<listcomp>r9   r   rK   r   rl   r   rL      s    z SpatialPandasInterface.geom_dimsc                 C   s   | |}| ||jS rj   )rS   rT   rN   )r   rK   rV   r   r   r   dimension_type   s    
z%SpatialPandasInterface.dimension_typeFc                 C   s\   | |}|| |v rdS |r:tdd |j|j D S | |}t|j|j  dkS )z?
        Tests if dimension is scalar in each subpath.
        Fc                 s   s*   | ]"}t |p ttt|d kV  qdS r   N)r   rM   r=   r
   )r)   vr   r   r   r8      s   z2SpatialPandasInterface.isscalar.<locals>.<genexpr>r   )rS   rL   rF   r,   r5   rM   unique)r   rK   rV   Zper_geomr   r   r   r      s    


zSpatialPandasInterface.isscalarc                 C   sz   | |}| |}||v rh| |j}||}|j| j}|dkrV|d |d fS |d |d fS n| j||S d S )Nr   r   r      )rS   rL   r3   r,   r4   Ztotal_boundsrk   range)r   rK   rV   rL   r1   idxZboundsr   r   r   rs      s    


zSpatialPandasInterface.rangec                    sD   |  | t fdd|D r*td| | jj||||fi |S )Nc                 3   s   | ]}| v V  qd S rj   r   rH   Zgeo_dimsr   r   r8      r.   z1SpatialPandasInterface.groupby.<locals>.<genexpr>zESpatialPandasInterface does not allow grouping by geometry dimension.)rL   rB   r   rk   groupby)r   rK   rO   Zcontainer_typeZ
group_typekwargsr   ru   r   rv      s    
zSpatialPandasInterface.groupbyc                 K   s   t d S rj   NotImplementedError)r   r/   rO   functionrw   r   r   r   	aggregate   s    z SpatialPandasInterface.aggregatec                 C   s   t d S rj   rx   )r   r/   Zsamplesr   r   r   sample   s    zSpatialPandasInterface.samplec                 C   s   |j S rj   r,   )r   rK   r:   r;   r   r   r   reindex   s    zSpatialPandasInterface.reindexc                 C   s   |  |t| fS rj   )lengthrM   rO   rm   r   r   r   shape   s    zSpatialPandasInterface.shapec                    s:   |  | t fdd|D r*td| | j|||S )Nc                 3   s   | ]}| v V  qd S rj   r   rH   ru   r   r   r8      r.   z.SpatialPandasInterface.sort.<locals>.<genexpr>zDSpatialPandasInterface does not allow sorting by geometry dimension.)rL   rB   r   rk   sort)r   rK   Zbyreverser   ru   r   r      s    
zSpatialPandasInterface.sortc           
      C   s   ddl m}m} | |j}|j| }| |}t|j|sP|dkrP| j	|S d}t
|D ].\}}	t|	|rx|d7 }q\|t|	jd 7 }q\|S )Nr   )MultiPointDtypePointr   r   r   )r[   r   r   r3   r,   	geom_typer   rT   rk   r   	enumeraterM   buffer_values)
r   rK   r   r   Zcol_namecolumnr   r   ir]   r   r   r   r      s    



zSpatialPandasInterface.lengthc                 C   s   t t|jdS Nr   )boolrM   r,   headrm   r   r   r   nonzero  s    zSpatialPandasInterface.nonzeroc                 C   s   | j ||S rj   )rk   redimr   rK   rO   r   r   r   r     s    zSpatialPandasInterface.redimc                 C   sP   |j  }| |j }|t|j|kr2|d8 }|j|vrL|||j| |S r   )r,   copyr3   r=   r/   r4   r5   insert)r   rK   rU   Zdim_posvaluesZvdimr,   geom_colr   r   r   add_dimension  s    

z$SpatialPandasInterface.add_dimensionc                    s6  ddl m} ddlm} |\} | | jd}tjjt	 t
rhdd  D    n:t rt|} jg nfdd|d D  t fd	d
D std| ttfdd D  t	j j|s2|rjj|d  d f S t|r"|g}jj| f S j }dg g  }}	t|D ]\}
}tt|jd t|rވ|  kr k r
n n@| d }|j||d  }|t|| |	|
  qn,t	|t
r|jd ur|j krqRn|jd ur*|jk r* q|jd u r:d nt|j dd }|jd u r^d nt|j d }|jd urj !d |j|| }t|r
|	|
 |t|| nJfdd|D }|r
|	|
 tj"|td}|t||j|  7 qRjj|	 # }|||< |S )Nr   r"   )r   Fc                 S   s   g | ]
}|j qS r   r5   rH   r   r   r   r-   *  r.   z/SpatialPandasInterface.iloc.<locals>.<listcomp>c                    s   g | ]}  |jqS r   )rS   r5   rH   rl   r   r   r-   /  r.   r   c                 3   s   | ]}| v V  qd S rj   r   rH   )r2   r   r   r8   0  r.   z.SpatialPandasInterface.iloc.<locals>.<genexpr>z[Cannot index a dimension which is part of the geometry column of a spatialpandas DataFrame.c                    s(   g | ] }|v r  n  |qS r   )r4   r(   )r/   r   rL   r   r   r-   3  s   r   zL.iloc step slicing currently not supported forthe multi-tabular data format.c                    sN   g | ]F}|  d  |  d  d fD ]$} |  kr@  k r$n q$|q$qS )r   r   r   )r)   rrp   )countr   r   r   r-   Y  s   (rT   )$r   r   r[   r   rL   r3   r,   r=   r/   r   slicerO   npr   rS   r5   rF   r   r   rT   re   r   intrM   r   appendrN   startstopmaxminstepZparamZwarningarrayr   )r   rK   r4   r   r   ZrowsscalarZgeomsZ	new_geomsZindexesr   r]   rt   r,   r   r   ZslicedZsub_rowsZidxsnewr   )r2   r/   r   rK   r   rL   r   r   re      sx    







 

$$


zSpatialPandasInterface.ilocc                 C   s   | |}|j|}|j}||v }	| |j}
| |dk}|	rN|rN||
 S |	st|rb||j jS t|||||
|S t	|st
g S | |}||}||
 }|rt|dr| }t|j|||S )Nr   compute)rS   	interfacerL   r,   r3   r   r5   r   get_value_arrayrM   r   r   r4   hasattrr   geom_array_to_array)r   rK   rU   expandedZflatr   
keep_indexrL   r,   Zisgeomr   	is_pointsr   r4   Zgeom_seriesr   r   r   r   e  s(    



zSpatialPandasInterface.valuesc                    s$  ddl m}m  ddlm} g }t|js.g S | |\fdd|j|j	 D }	|jj
d | |j}
| |}|d urt|
 |d}jjf|i}|fdd	|	D  |j|d
gd}| |r| |nd }|j D ]0\}|d u r.| fdd	|jjD }||| q|
 }|pFt|j|
}t||d}j|d d df j|d d df i}|fdd	|	D  |dv r|d ur|| ||j< ||d< || q||_|dkr|jf i |}n(|dkr|jf i |}ntd| || q|S )Nr   r!   rr   Polygonsc                    s   g | ]}| fvr|qS r   r   r)   rV   )rg   rh   r   r   r-     s   z0SpatialPandasInterface.split.<locals>.<listcomp>)r   c                    s   i | ]}|j  |j  qS r   r   r   rowr   r   
<dictcomp>  r.   z0SpatialPandasInterface.split.<locals>.<dictcomp>
dictionary)datatypec                    s.   i | ]&}||d kr  | gn| gqS )r&   r   r(   )r   r   r   r   r     s   r   c                    s   i | ]}|j  |j  qS r   r   r   r   r   r   r     r.   )r   r/   r   r   Z	dataframez%s datatype not support)r   r   r   elementr   rM   r,   rL   r:   r;   re   r3   r   geom_to_arrayr5   updateZcloner_   ra   Ziterrowsr/   r   get_geom_type	_hole_keyr   dframer0   )r   rK   r   endr   rw   r   r   ZobjsZ
value_dimsr1   r   arrrI   Zdsra   r   gdfr]   gtr   r   )r   r   rg   rh   r   split}  sT    


(




zSpatialPandasInterface.splitc                 C   s   |r|j | S |j  S d S rj   )r,   r   r   r   r   r   r     s    
zSpatialPandasInterface.dframec                 C   s   |j S rj   r}   rm   r   r   r   	as_dframe  s    z SpatialPandasInterface.as_dframe)T)N)F)NN)TTTF))r@   
__module____qualname__r   rk   r   multitypesclassmethodr   r    r   r#   r%   r3   rD   rQ   rT   r_   ra   ri   rc   rL   rn   r   rs   rv   r{   r|   r~   r   r   r   r   r   r   re   r   r   r   r   r   r   r   r   r      s   

	




+













	
D
1
r   c           
      C   s|   ddl m}m}m}m}m}m}m} | | }	t|	j	||fr@dS t|	j	||frTdS t|	j	||frhdS t|	j	|rxdS dS )zReturn the HoloViews geometry type string for the geometry column.

    Args:
        gdf: The GeoDataFrame to get the geometry from
        col: The geometry column

    Returns:
        A string representing the type of geometry
    r   )
PointDtyper   	LineDtypeMultiLineDtyperX   rW   	RingDtyper   LinerY   RingN)
r[   r   r   r   r   rX   rW   r   r   rT   )
r   r1   r   r   r   r   rX   rW   r   r   r   r   r   r     s    
$r   Fc                 C   s  ddl m}m}m}m}m}m}	 t| |rd|du rFt	| j
| jggS t	|rT| jn| j
gg}
n~t| |||frt| |r| jd n| j}t	| dd}t| ||frt|}|du r|n|dd|f g}
nt| |	r$|du r
t	| jddg}
nt	| j|dd g}
ng }
| jD ]}t| |rF|d n|}t	| dd}t| |rtt|}|
|du r|n|dd|f  |dkr.|
|du rtjtjggntjg q.|dkr|
dd }
|r|
S t|
dkr|
d S t|
S dS )zConverts spatialpandas geometry to an array.

    Args:
        geom: spatialpandas geometry
        index: The column index to return
        multi: Whether to concatenate multiple arrays or not

    Returns:
        Array or list of arrays.
    r   )r   rY   r   r   rZ   
MultiPointNr   r   r   )r[   r   rY   r   r   rZ   r   r   r   r   xyr,   as_pyreshaper   r   r   nanrM   concatenate)r]   r4   r   r   r   rY   r   r   rZ   r   arraysexteriorr   gr   r   r   r     s>     
"

$
*
r   c           
      C   s   ddl m}m} t| |r*|r$| jS | jS g }t| |p>|dk}| D ]@}t||||d}	|rz||	 |s|t	j
g qD||	 qD|r|s|dd }|rt	|S t	g S t	jt|td}	||	dd< |	S dS )zConverts spatialpandas extension arrays to a flattened array.

    Args:
        geom: spatialpandas geometry
        index: The column index to return

    Returns:
        Flattened array
    r   )
PointArrayMultiPointArrayr   )r   r   Nr   r   )r[   r   r   r   r   r   r   extendr   r   r   r   r   emptyrM   object)

geom_arrayr4   expandr   r   r   r   Zmulti_pointr]   r   r   r   r   r     s&    


r   c           	      C   s8  ddl m}m}m}m} t| |rdd}| jd }|d |d ksP|d |d krTd}t|d | S t| ||frd}| jD ]\}d}t| |r|}n,|d }|d |d ks|d |d krd}|t|d d | 7 }q||r|d S dS d}| j}t| |r(|d |d ks$|d |d kr(d}t|d S d S )Nr   )rY   r   rZ   	MultiLiner   r   r   )	r[   rY   r   rZ   r   r   r,   rM   r   )	r]   rY   r   rZ   r   offsetr   r   r   r   r   r   geom_length)  s.    

 

 0r   c                 C   s  | |j  }|r|S d}g g  }	}
t| | D ]\}}|r<dn||}|j| }t|}|rft|g}|stt|dkr|dd }d}||M }|
| |r|s|	| n|r|	t	|| |r,|s,|t| | d ks,|	ttj
g q,t| stg S |r2t|	dkr*t|	S |	d S |rR|	rRtdd |	D S tjt|	td}dd t|
|	D |dd< |S dS )	a  Returns an array of values from a GeoDataFrame.

    Args:
        data: GeoDataFrame
        dimension: The dimension to get the values from
        expanded: Whether to expand the value array
        keep_index: Whether to return a Series
        geom_col: The column in the data that contains the geometries
        is_points: Whether the geometries are points
        geom_length: The function used to compute the length of each geometry

    Returns:
        An array containing the values along a dimension
    Tr   Nr   c                 S   s   g | ]}|d  qS r   r   )r)   ar   r   r   r-   q  r.   z#get_value_array.<locals>.<listcomp>r   c                 S   s    g | ]\}}|r|d  n|qS r   r   )r)   sr   r   r   r   r-   t  r.   )r5   r   re   r   r   r   rM   r
   r   ZfullZNaNr   r   r   zip)r,   rU   r   r   r   r   r   r   Z
all_scalarr   Zscalarsr   r]   r   valr   r   r   r   r   r   E  s>    





 r   c           	      C   s   ddl m}m} t| |rfg }t| jD ]6\}}|dkr:q(tt|	 
dd}|| q(|gS t| |rg }| jD ]R}g }t|D ]6\}}|dkrqtt|	 
dd}|| q|| qz|S dt| jv rg gt|  S g gS dS )zExtracts holes from spatialpandas Polygon geometries.

    Args:
        geom: spatialpandas geometry

    Returns:
        List of arrays representing holes
    r   )rY   rZ   r   r   ZMultiN)r[   rY   rZ   r   r   r,   r   r   r   r   r   r   rN   r@   rM   )	r]   rY   rZ   ra   r   ZholepolyZ
poly_holesr   r   r   r   r`   x  s.    	


r`   Zpointc           *   	      s  ddl m}m} ddlm}m}m}	m}
m}m	}m
}m}m}m}m} ddlm  t fdd| D pl|dkr|	}|| }}n>|d	kr|}|| }}n&|d
kr|
}|| }}n|}|| }}d}g g  }}| D ]}t|}||vs||v rtd|||| }}t|t| }}|rJ|rJt|gt|g }}n&|r^t||}n|rpt||}t||g}||	|
fv rt|}tt|ddddf djddd }t|rt ||d n|g}| j!d}|dur*t|t|krt"dt#ndd |D }|$| |$| ||u rt|dksjtdd |D rp|}n|du r|}qt|r|}q|du r|}qt%t&} t'| ||D ]p\}}!}"g }#t(|!D ]\}$}%|$t|!d kr|%dd }%t|%r dndk r||urqʈr0|#$g  |#d }&n|#}&|&$|%ddddf  r|"dur|&dd |"|$ D 7 }&q|) D ]\}'}(| |' $|( q||u r|#d * }#nb||u rt+dd |#D }#nB||u rfdd|#D }#n$rdd |#d D n
|#d * }#| d $|# q| rl| d })||u rHt+|)})||)}r^|, }||| d< n||g | d< || dg| dS )a  Converts list of dictionary format geometries to spatialpandas line geometries.

    Args:
        data: List of dictionaries representing individual geometries
        xdim: Name of x-coordinates column
        ydim: Name of y-coordinates column
        columns: List of columns to add
        geom: The type of geometry

    Returns:
        A spatialpandas.GeoDataFrame version of the data
    r   )r   r   )r   r   rY   r   	LineArrayPolygonArrayr   MultiLineArrayMultiPolygonArrayr   	RingArrayrr   r   c                 3   s   | ]} j |v V  qd S rj   )r   rH   r   r   r   r8     r.   z#to_spatialpandas.<locals>.<genexpr>rY   r   r   Nz"Could not find geometry dimensionsr   floatr   )Zaxisz_Polygons with holes containing multi-geometries must declare a list of holes for each geometry.c                 S   s   g | ]}d d |D qS )c                 S   s   g | ]}t t|qS r   )r   r   asarrayr)   hr   r   r   r-     r.   /to_spatialpandas.<locals>.<listcomp>.<listcomp>r   )r)   Zhsr   r   r   r-     r.   z$to_spatialpandas.<locals>.<listcomp>c                 s   s   | ]}t |d kV  qdS ro   )rM   )r)   r   r   r   r   r8     r.   r   c                 S   s   g | ]}t |qS r   )r   r   r   r   r   r   r-     r.   c                 S   s   g | ]}|  qS r   flattenr)   spr   r   r   r-     r.   c                    s&   g | ]} rd d |D n|  qS )c                 S   s   g | ]}|  qS r   r   )r)   Zsspr   r   r   r-     r.   r   r   r   )r   r   r   r-     r.   c                 S   s   g | ]}t | qS r   )r   r   r   r   r   r   r   r-     r.   r&   )r/   )-r   r   r   r[   r   r   rY   r   r   r   r   r   r   r   r   r   r   rB   dictr0   rb   r   r   r   Z	full_likeZcolumn_stackr   whereZisnanZastypesumrM   r   r   r   r   r   r   r=   r   r   itemsr   r   Zoriented)*r,   rg   rh   r/   r]   r   r   r   r   rY   r   r   r   r   r   r   r   r   r   Zsingle_arrayZmulti_arrayZ
array_typeZhole_arraysZgeom_arraysZxsZysZxscalarZyscalarr   ZsplitsZsplit_geomsZsplit_holesZ	convertedr   ra   partsr   r   Zsubpartsr*   rp   Z
geometriesr   )r   r   r   to_spatialpandas  s    4


2 



"

"




$

r   c                 C   s   ddl m} dd |dd D \}}t|trFdd | D }|S ||||}|j|u rb|jS i }	| D ]H}
|
||fv rd	}n|j||
}|	|
| }|r|d
 n||	|
j
< qn|	S )aI  Converts data from any list format to a dictionary based format.

    Args:
        eltype: Element type to convert
        data: The original data
        kdims: The declared key dimensions
        vdims: The declared value dimensions

    Returns:
        A list of dictionaries containing geometry coordinates and values.
    r   )Datasetc                 s   s   | ]}|j V  qd S rj   r   r)   r7   r   r   r   r8     r.   zto_geom_dict.<locals>.<genexpr>Nr   c                 S   s(   i | ] \}}|t |r|nt|qS r   )r   r   r   )r)   krp   r   r   r   r   !  r.   z to_geom_dict.<locals>.<dictcomp>Fr   ) r   r   r   r   r   r,   rO   r   Zdimension_valuesr5   )rC   r,   r:   r;   r   r   xnameynameZnew_elnew_dictrI   r   Zvalsr   r   r   to_geom_dict  s     

r  c                    s*  ddl m} dd |dd D \ g g g   }}}|D ]l}|t| t| |||t}	d|	v r~|	d |vr~||	d  ||	 t|d ts:t|d |d< q:tt	|d	krt
d
|r|d |u rt|}nJ fdd|| D }
t|d	kr|d }n
t| }t| |
|}|S )a0  Converts list formats into spatialpandas.GeoDataFrame.

    Args:
        eltype: Element type to convert
        data: The original data
        kdims: The declared key dimensions
        vdims: The declared value dimensions

    Returns:
        A GeoDataFrame containing in the list based format.
    r   r$   c                 s   s   | ]}|j V  qd S rj   r   r   r   r   r   r8   ?  r.   zfrom_multi.<locals>.<genexpr>Nr   r   r   r   zMixed types not supportedc                    s   g | ]}| fvr|j qS r   r   rH   r   r  r   r   r-   O  r.   zfrom_multi.<locals>.<listcomp>)r   r   r   rN   r  r   r   r   rM   setr   r	   concatr   r   )rC   r,   r:   r;   r   new_datar   Z
geom_typesrI   r  r/   r]   r   r  r   r?   1  s*    


r?   c                    s   ddl m}m} ddlm  | s"nt fdd| D rF||  } nt fdd| D rdd | d D }| D ]@}| D ]2\}}|| t	|st
| r|nt| q~qr||d	 |d	< ||} | S )
zConverts shapely based data formats to spatialpandas.GeoDataFrame.

    Args:
        data: A list of shapely objects or dictionaries containing
              shapely objects

    Returns:
        A GeoDataFrame containing the shapely geometry data.
    r   r!   BaseGeometryc                 3   s   | ]}t | V  qd S rj   r'   rH   r  r   r   r8   h  r.   zfrom_shapely.<locals>.<genexpr>c                 3   s.   | ]&}t |to$d |v o$t |d   V  qdS )r&   N)r   r   rH   r  r   r   r8   j  s   c                 S   s   i | ]
}|g qS r   r   )r)   r1   r   r   r   r   l  r.   z from_shapely.<locals>.<dictcomp>r&   )r   r   r   Zshapely.geometry.baser	  rF   r<   r   r   r   r   r   r   )r,   r   r   r  rI   r1   r   r   r  r   r>   X  s     .r>   )NFN)FN)N)#Z
__future__r   r   r   collectionsr   Znumpyr   rU   r   utilr   r   r	   r
   r   r   r   Z	multipathr   r   Zpandasr   r   r   r   r   r   r   r`   r   r  r?   r>   registerr   r   r   r   <module>   s0      0
1
!
3#v
 '