ó
ú£Õ\c           @   s‘   d  Z  d d l Z d d l Z d d l Z d d l Z d „  Z d „  Z e d „ Z	 d d d „  ƒ  YZ
 d d d	 „  ƒ  YZ d
 d d „  ƒ  YZ d S(   sU  
Read graphs in Open Street Maps osm format

Based on osm.py from brianw's osmgeocode
http://github.com/brianw/osmgeocode, which is based on osm.py from
comes from Graphserver:
http://github.com/bmander/graphserver/tree/master and is copyright (c)
2007, Brandon Martin-Anderson under the BSD License
2012, edits for PRISM by Clemen Wiltsche
iÿÿÿÿNc         C   s€  d } | j  |  j  t j d } | j |  j t j d } |  j  t j d } | j  t j d } t j | d ƒ t j | d ƒ t j | d ƒ t j | d ƒ t j | ƒ t j | ƒ } d t j t j | ƒ t j d | ƒ ƒ } | | }	 t j | ƒ t j | ƒ }
 t j | ƒ t j | ƒ t j | ƒ t j | ƒ t j | ƒ } t j |
 | ƒ d t j } |	 | f S(   NiÁ6a i´   i   i   (   t   latt   matht   pit   lont   sint   cost   atan2t   sqrt(   t   node1t   node2t   Rt   dLatt   dLont   lat1t   lat2t   at   ct   distancet   yt   xt   bearing(    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   calculateDistanceAndBearing   s    `,
Cc         C   s0   d d l  m } | d |  | | | f ƒ } | S(   s,    Return a filehandle to the downloaded data.iÿÿÿÿ(   t   urlopens9   http://api.openstreetmap.org/api/0.5/map?bbox=%f,%f,%f,%f(   t   urllibR   (   t   leftt   bottomt   rightt   topR   t   fp(    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   download_osm)   s    c         C   sª   t  |  ƒ } t j ƒ  } xQ | j j ƒ  D]@ } | rI d | j k rI q( n  | j | j d | j d | ƒq( Wx7 | j	 ƒ  D]) } | j
 | } t d | ƒ | j | <qy W| S(   s  Read graph in OSM format from file specified by name or by stream object.

    Parameters
    ----------
    filename_or_stream : filename or stream object

    Returns
    -------
    G : Graph

    Examples
    --------
    >>> G=nx.read_osm(nx.download_osm(-122.33,47.60,-122.31,47.61))
    >>> plot([G.node[n]['data'].lat for n in G], [G.node[n]['data'].lon for n in G], ',')

    t   highwayt   idt   data(   t   OSMt   networkxt   DiGrapht   wayst
   itervaluest   tagst   add_patht   ndsR   t
   nodes_itert   nodest   dictt   node(   t   filename_or_streamt
   only_roadst   osmt   Gt   wt   n_idt   n(    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   read_osm/   s    #t   Nodec           B   s   e  Z d  „  Z RS(   c         C   s(   | |  _  | |  _ | |  _ i  |  _ d  S(   N(   R   R   R    R&   (   t   selfR   R   R    (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   __init__N   s    			(   t   __name__t
   __module__R7   (    (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyR5   M   s   t   Wayc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s(   | |  _  | |  _ g  |  _ i  |  _ d  S(   N(   R/   R   R(   R&   (   R6   R   R/   (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyR7   U   s    			c            s„   ‡  f d †  ‰  ˆ  |  j  | ƒ } g  } d } xP | D]H } t j |  ƒ } | j d | 7_ | | _  | j | ƒ | d 7} q4 W| S(   Nc            sq   xg t  d t |  ƒ d ƒ D]L } | |  | d k r |  | d  } |  | } ˆ  | | ƒ } | g | Sq W|  g S(   Ni   (   t   ranget   len(   t   art   dividerst   iR   R   t   rightsliced(   t   slice_array(    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyRA   ]   s     
i    s   -%di   (   R(   t   copyR   t   append(   R6   R>   t   slicest   retR?   t   slicet	   littleway(    (   RA   sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   split[   s    	(   R8   R9   R7   RH   (    (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyR:   T   s   	R!   c           B   s   e  Z d  „  Z RS(   c   
         s=  i  ‰  i  ‰ |  ‰ d t  j j f ‡  ‡ ‡ f d †  ƒ  Y} t  j j | | ƒ ˆ  |  _ ˆ |  _ t j |  j j ƒ  d ƒ } x` |  j j	 ƒ  D]O } t
 | j ƒ d k  rµ |  j | j =qŠ x! | j D] } | | c d 7<q¿ WqŠ Wi  } xJ |  j j ƒ  D]9 \ } } | j | ƒ } x | D] }	 |	 | |	 j <qWqó W| |  _ d S(   s5    File can be either a filename or stream/file object.t
   OSMHandlerc              sq   e  Z e d  „  ƒ Z e d „  ƒ Z e d „  ƒ Z e ‡ f d †  ƒ Z e ‡  ‡ f d †  ƒ Z e d „  ƒ Z RS(   c         S   s   d  S(   N(    (   R6   t   loc(    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   setDocumentLocator‚   s    c         S   s   d  S(   N(    (   R6   (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   startDocument†   s    c         S   s   d  S(   N(    (   R6   (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   endDocumentŠ   s    c            s²   | d k r< t  | d t | d ƒ t | d ƒ ƒ |  _ nr | d k ra t | d ˆ  ƒ |  _ nM | d k rˆ | d |  j j | d <n& | d	 k r® |  j j j | d
 ƒ n  d  S(   NR,   R   R   R    t   wayt   tagt   vt   kt   ndt   ref(   R5   t   floatt   currElemR:   R&   R(   RC   (   R6   t   namet   attrs(   t	   superself(    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   startElementŽ   s    0c            sH   | d k r" |  j  ˆ  |  j  j <n" | d k rD |  j  ˆ |  j  j <n  d  S(   NR,   RN   (   RU   R   (   R6   RV   (   R*   R$   (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt
   endElement™   s    c         S   s   d  S(   N(    (   R6   t   chars(    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt
   characters    s    (	   R8   R9   t   classmethodRK   RL   RM   RY   RZ   R\   (    (   R*   RX   R$   (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyRI      s   i    i   i   N(   t   xmlt   saxt   ContentHandlert   parseR*   R$   R+   t   fromkeyst   keyst   valuesR<   R(   R   t	   iteritemsRH   (
   R6   R-   RI   t   node_histogramRN   R,   t   new_waysR   t
   split_wayst	   split_way(    (   R*   RX   R$   sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyR7   z   s&    (#		(   R8   R9   R7   (    (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyR!   y   s   (    (    (    (   t   __doc__t   xml.saxR^   RB   R"   R   R   R   t   TrueR4   R5   R:   R!   (    (    (    sA   /Users/dxp/prism/prism-games/prism-examples/smgs/car/osm2graph.pyt   <module>
   s   		%N