ó
|£*^c           @   sF   d  Z  d d l Z d Z d d g Z e d „ Z d „  Z d „  Z d S(	   s±  
*********
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 http://en.wikipedia.org/wiki/Shapefile for additional information.
iÿÿÿÿNs!   Ben Reilly (benwreilly@gmail.com)t   read_shpt	   write_shpc         C   sx  y d d l  m } Wn t k
 r3 t d ƒ ‚ n Xt |  t ƒ sG d St j ƒ  } | j |  ƒ } x| D]} g  | j D] } | j	 ƒ  ^ qy } xÜ| D]Ô} g  | D] } | j
 | j | ƒ ƒ ^ q¥ }	 | j ƒ  }
 t t | |	 ƒ ƒ } | j	 ƒ  | d <|
 j ƒ  d k r(| j |
 j d ƒ | ƒ n  |
 j ƒ  d k r˜ |
 j ƒ  d } | r¨|
 j ƒ  | d	 <|
 j ƒ  | d
 <|
 j ƒ  | d <| j |
 j d ƒ |
 j | ƒ | ƒ qlxÁ t | ƒ D]° } |
 j | ƒ } |
 j | d ƒ } | j | j ƒ } | j | d | d ƒ | j | d | d ƒ | j ƒ  | d	 <| j ƒ  | d
 <| j ƒ  | d <~ | j | | | ƒ qµWq˜ q˜ Wqi W| S(   s  Generates a networkx.DiGraph from shapefiles. 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.

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

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

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Shapefile
    iÿÿÿÿ(   t   ogrs+   read_shp requires OGR: http://www.gdal.org/Nt   ShpNamei   i    i   t   Wkbt   Wktt   Json(   t   osgeoR   t   ImportErrort
   isinstancet   strt   nxt   DiGrapht   Opent   schemat   GetNamet   GetFieldt   GetFieldIndext   geometryt   dictt   zipt   GetGeometryTypet   add_nodet   GetPoint_2Dt   GetPointCountt   ExportToWkbt   ExportToWktt   ExportToJsont   add_edget   ranget   Geometryt   wkbLineStringt   AddPoint_2D(   t   patht   simplifyR   t   nett   shpt   lyrt   xt   fieldst   ft   flddatat   gt
   attributest   lastt   it   pt1t   pt2t   segment(    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/nx_shp.pyR       sH    !"+("c            s{  y d d l  m ‰ Wn t k
 r3 t d ƒ ‚ n Xˆ j ƒ  ‡ f d †  } d ‡  ‡ f d † } ˆ j d ƒ } | j | ƒ } y | j d ƒ Wn n X| j d d ˆ j	 ƒ } x7 |  D]/ } |  j
 | } | | | ƒ ‰  | ˆ  | ƒ qº Wy | j d ƒ Wn n X| j d d ˆ j ƒ }	 i  }
 i  } i ˆ j t 6ˆ j t 6ˆ j t 6} x|  j d	 t ƒ D]} |  j | Œ  } | | | ƒ ‰  xÍ | d
 j ƒ  D]» \ } } | d k r˜| d k r˜| d k r˜| d k r˜| |
 k rFt | ƒ | k r	| t | ƒ |
 | <n ˆ j |
 | <ˆ j | |
 | ƒ } |	 j | ƒ | | | <qS| | | <q˜q˜W| ˆ  |	 | ƒ qcWd \ } }	 d S(   sÈ  Writes a networkx.DiGraph to two shapefiles, edges and nodes.
    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
    ----------
    outdir : directory path
       Output directory for the two shapefiles.

    Returns
    -------
    None

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

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Shapefile
    iÿÿÿÿ(   R   s,   write_shp requires OGR: http://www.gdal.org/c   	         sŠ  d | k r" ˆ  j  | d ƒ } ndd | k rD ˆ  j | d ƒ } nBt |  d ƒ j d k rˆ  j ˆ  j ƒ } |  d |  d } } y$ | j d | Œ | j d | Œ Wq†t k
 rg  | D] } t | ƒ ^ q¿ } g  | D] } t | ƒ ^ qÞ } | j d | Œ | j d | Œ q†Xni ˆ  j ˆ  j	 ƒ } y | j d |  Œ Wn@ t k
 r…g  |  D] } t | ƒ ^ qZ} | j d | Œ n X| S(   NR   R   i    t   tuplei   (
   t   CreateGeometryFromWkbt   CreateGeometryFromWktt   typet   __name__R   R   t   SetPointt	   TypeErrort   floatt   wkbPoint(	   t   keyt   datat   geomt   _fromt   _toR&   t   _ffromt   _ftot   fkey(   R   (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/nx_shp.pyt   netgeometry‡   s,    c            sy   ˆ j  | j ƒ  ƒ } | j ˆ  ƒ | d  k r^ x- | j ƒ  D] \ } } | j | | ƒ q; Wn  | j | ƒ | j ƒ  d  S(   N(   t   Featuret   GetLayerDefnt   SetGeometryt   Nonet   itemst   SetFieldt   CreateFeaturet   Destroy(   R   R%   R+   t   featuret   fieldR;   (   R*   R   (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/nx_shp.pyt   create_feature¤   s    s   ESRI Shapefilet   nodest   edgesR;   i   R   R   R   R   N(   NN(   R   R   R   t   UseExceptionsRF   t   GetDriverByNamet   CreateDataSourcet   DeleteLayert   CreateLayerR9   t   nodeR   t
   OFTIntegert   intt	   OFTStringR
   t   OFTRealR8   RO   t   Truet   get_edge_dataRG   R4   t	   FieldDefnt   CreateField(   t   Gt   outdirRB   RM   t   drvt   shpdirRN   t   nR;   RO   R'   R+   t   OGRTypest   eR:   t   newfield(    (   R*   R   sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/nx_shp.pyR   f   sT    

$$c         C   s:   d d l  m } y d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   OGR not available(   t   noseRf   R   (   t   moduleRf   R   (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/nx_shp.pyt   setup_moduleç   s
    (	   t   __doc__t   networkxR   t
   __author__t   __all__RZ   R    R   Ri   (    (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/nx_shp.pyt   <module>   s   L	�