o
    ˷e/                     @   s<   d Z ddlZddlZddgZd
ddZdddZd	d ZdS )a  
*********
Shapefile
*********

Generates a networkx.DiGraph from point and line shapefiles.

"The Esri Shapefile or simply a shapefile is a popular geospatial vector
data format for geographic information systems software. It is developed
and regulated by Esri as a (mostly) open specification for data
interoperability among Esri and other software products."
See https://en.wikipedia.org/wiki/Shapefile for additional information.
    Nread_shp	write_shpTc              
      s  d}t j|tdd zddlm} W n ty# } ztd|d}~ww t| ts+dS t	 }|
| }|du r?td|  |D ]}	d	d
 |	jD }
|	D ]r   }|du r_|r^tdqM fdd
|
D }tt|
|}|	 |d< | |jkr|j|dfi | qM| |j|jfv rt||||D ]}|\}}}||| || | | qqM|rtd|  dqMqA|S )a6  Generates a networkx.DiGraph from shapefiles.

    .. deprecated:: 2.6

       read_shp is deprecated and will be removed in NetworkX 3.0.
       See https://networkx.org/documentation/latest/auto_examples/index.html#geospatial.

    Point geometries are
    translated into nodes, lines into edges. Coordinate tuples are used as
    keys. Attributes are preserved, line geometries are simplified into start
    and end coordinates. Accepts a single shapefile or directory of many
    shapefiles.

    "The Esri Shapefile or simply a shapefile is a popular geospatial vector
    data format for geographic information systems software [1]_."

    Parameters
    ----------
    path : file or string
       File, directory, or filename to read.

    simplify:  bool
        If True, simplify line geometries to start and end coordinates.
        If False, and line feature geometry has multiple segments, the
        non-geometric attributes for that feature will be repeated for each
        edge comprising that feature.

    geom_attrs: bool
        If True, include the Wkb, Wkt and Json geometry attributes with
        each edge.

        NOTE:  if these attributes are available, write_shp will use them
        to write the geometry.  If nodes store the underlying coordinates for
        the edge geometry as well (as they do when they are read via
        this method) and they change, your geomety will be out of sync.

    strict: bool
        If True, raise NetworkXError when feature geometry is missing or
        GeometryType is not supported.
        If False, silently ignore missing or unsupported geometry in features.

    Returns
    -------
    G : NetworkX graph

    Raises
    ------
    ImportError
       If ogr module is not available.

    RuntimeError
       If file cannot be open or read.

    NetworkXError
       If strict=True and feature is missing geometry or GeometryType is
       not supported.

    Examples
    --------
    >>> G = nx.read_shp("test.shp")  # doctest: +SKIP

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Shapefile
    zread_shp is deprecated and will be removed in 3.0.See https://networkx.org/documentation/latest/auto_examples/index.html#geospatial.   
stacklevelr   ogrz+read_shp requires OGR: http://www.gdal.org/NzUnable to open c                 S   s   g | ]}|  qS  )GetName.0xr	   r	   P/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/readwrite/nx_shp.py
<listcomp>i       zread_shp.<locals>.<listcomp>z"Bad data: feature missing geometryc                    s   g | ]
}   |qS r	   )GetFieldGetFieldIndexr   fr	   r   r   q   s    ShpNamezGeometryType z not supported)warningswarnDeprecationWarningosgeor   ImportError
isinstancestrnxDiGraphOpenRuntimeErrorschemageometryNetworkXErrordictzipr
   GetGeometryTypewkbPointadd_nodeGetPoint_2DwkbLineStringwkbMultiLineStringedges_from_lineadd_edgeupdate)pathsimplify
geom_attrsstrictmsgr   errnetshplyrfieldsgflddata
attributesedgee1e2attrr	   r   r   r      sR   C




c              
   c   s   d}t j|tdd zddlm} W n ty$ } ztd|d}~ww |  |jkr|rZ| }| 	 d }|rL| 
 |d	< |  |d
< |  |d< | d| ||fV  dS td| 	 d D ]G}	| |	}
| |	d }| }|r||j}||
d |
d  ||d |d  |
 |d	< | |d
< | |d< ~|
||fV  qcdS |  |jkrt|  D ]}	| |	}t||||E dH  qdS dS )a  
    Generate edges for each line in geom
    Written as a helper for read_shp

    Parameters
    ----------

    geom:  ogr line geometry
        To be converted into an edge or edges

    attrs:  dict
        Attributes to be associated with all geoms

    simplify:  bool
        If True, simplify the line as in read_shp

    geom_attrs:  bool
        If True, add geom attributes to edge as in read_shp


    Returns
    -------
     edges:  generator of edges
        each edge is a tuple of form
        (node1_coord, node2_coord, attribute_dict)
        suitable for expanding into a networkx Graph add_edge call

    .. deprecated:: 2.6
    zedges_from_line is deprecated and will be removed in 3.0.See https://networkx.org/documentation/latest/auto_examples/index.html#geospatial.r   r   r   r   z2edges_from_line requires OGR: http://www.gdal.org/N   WkbWktJson)r   r   r   r   r   r   r&   r*   copyGetPointCountExportToWkbExportToWktExportToJsonr)   rangeGeometryAddPoint_2Dr+   GetGeometryCountGetGeometryRefr,   )geomattrsr0   r1   r3   r   r4   
edge_attrslastipt1pt2segmentgeom_ir	   r	   r   r,      sV   

r,   c              
      s  d}t j|tdd zddlm W n ty# } ztd|d}~ww   fdd	}dfd
d	}tjt	j
tji  fddd}||}z|d W n   Y |ddj}i }	fdd}
| D ]}| j| }||||
||	|\}}||| qpz|d W n   Y |ddj}i }| jddD ]}| j| }||||
|d ||\}}||| qd\}}dS )a  Writes a networkx.DiGraph to two shapefiles, edges and nodes.

    .. deprecated:: 2.6

       write_shp is deprecated and will be removed in 3.0.
       See https://networkx.org/documentation/latest/auto_examples/index.html#geospatial.

    Nodes and edges are expected to have a Well Known Binary (Wkb) or
    Well Known Text (Wkt) key in order to generate geometries. Also
    acceptable are nodes with a numeric tuple key (x,y).

    "The Esri Shapefile or simply a shapefile is a popular geospatial vector
    data format for geographic information systems software [1]_."

    Parameters
    ----------
    G : NetworkX graph
        Directed graph
    outdir : directory path
       Output directory for the two shapefiles.

    Returns
    -------
    None

    Examples
    --------
    nx.write_shp(digraph, '/shapefiles') # doctest +SKIP

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Shapefile
    zwrite_shp is deprecated and will be removed in 3.0.See https://networkx.org/documentation/latest/auto_examples/index.html#geospatial.r   r   r   r   z,write_shp requires OGR: http://www.gdal.org/Nc                    s>  d|v r  |d }|S d|v r |d }|S t| d jdkrr  j}| d | d }}z|jdg|R   |jdg|R   W |S  tyq   dd |D }dd |D }|jdg|R   |jdg|R   Y |S w   j}z|jdg| R   W |S  ty   d	d | D }|jdg|R   Y |S w )
NrA   rB   r   tupler@   c                 S      g | ]}t |qS r	   floatr   r	   r	   r   r     r   z2write_shp.<locals>.netgeometry.<locals>.<listcomp>c                 S   rX   r	   rY   r   r	   r	   r   r   	  r   c                 S   rX   r	   rY   r   r	   r	   r   r     r   )	CreateGeometryFromWkbCreateGeometryFromWkttype__name__rJ   r*   SetPoint	TypeErrorr'   )keydatarN   _from_to_ffrom_ftofkeyr   r	   r   netgeometry   s:   
zwrite_shp.<locals>.netgeometryc                    sT    | }|  |d ur| D ]
\}}||| q|| |  d S N)FeatureGetLayerDefnSetGeometryitemsSetFieldCreateFeatureDestroy)r"   r7   r;   featurefieldrb   )r9   r   r	   r   create_feature  s   

z!write_shp.<locals>.create_featurec                    sF   t | v r t | || < nj|| < | ||  }|| d S ri   )r]   	OFTString	FieldDefnCreateField)ra   valuer8   layernewfield)OGRTypesr   r	   r   add_fields_to_layer&  s
   
z&write_shp.<locals>.add_fields_to_layerzESRI Shapefilenodesc                    s\   i }|   D ]#\}}|dkr)|dkr)|dkr)|dkr)||vr% |||| |||< q||fS )NrC   rB   rA   r   )rm   )rb   r8   rx   r;   ra   rw   )r{   r	   r   create_attributes=  s    z$write_shp.<locals>.create_attributesedgesT)rb   )NNri   )r   r   r   r   r   r   UseExceptionsint
OFTIntegerr   rt   rZ   OFTRealGetDriverByNameCreateDataSourceDeleteLayerCreateLayerr'   r|   r*   r~   get_edge_data)Goutdirr3   r4   rh   rs   drvshpdirr|   node_fieldsr}   nrb   r;   r~   edge_fieldsr<   r	   )rz   r{   r9   r   r   r      sP   #






)TTT)TT)__doc__r   networkxr   __all__r   r,   r   r	   r	   r	   r   <module>   s    

pH