³ò
ûž»Fc           @   s¥  d  Z  d Z d Z y e Wn d> \ Z Z n Xyž d d k Z d d k Z d d k Z d d k Z d d k	 Z	 e i
 i e i
 i e	 i e i ƒ  ƒ d d d	 ƒ ƒ e e e i
 ƒ Z d d
 k l Z l Z Wn1 y e i i d e ƒ Wn e GHn X‚  n Xe i
 i e i d ƒ d Z e i
 i e ƒ d Z e i d ƒ i e ƒ i ƒ  Z d „  Z d „  Z d „  Z d d k Td d k  Td d k! Z! d d k" Z" h  g  d <h  d <d d <d d <Z# e i d ƒ Z$ e i d ƒ Z% e i d ƒ Z& d Z' d Z( d Z) d d d d g Z* d  „  Z+ d! „  Z, d" „  Z- d# „  Z d$ „  Z. d% „  Z/ d& „  Z0 d' „  Z1 d( „  Z2 d) „  Z3 d* „  Z4 d+ „  Z5 d, „  Z6 d- „  Z7 d. „  Z8 d/ „  Z9 d0 „  Z: d1 „  Z; d2 „  Z< d3 „  Z= d4 „  Z> d5 „  Z? d6 „  Z@ d7 „  ZA d8 „  ZB eC d9 j ož e ZD e i d ZE e e j oR yD eF eE ƒ o3 eG d: eE d ƒ o e i d ZD e i d; ZE n WququXn eG d< eD e eH eE ƒ ƒ f ƒ p d= GHn d S(?   s$  Open Street Map file parser and editor

   understands the format of osm gpx files and allows command-line editing
   actually only made to work with data imported from TIGER using the
   tiger-to-osm.sh script
   partial glossary:
    CFCC: Census Feature Class Code
    TLID: Tiger Line ID
s/  
    osm - OpenStreetMap.org file parser and editor
    Copyright (C) 2007  John Comeau <jc.unternet.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    s7   Not all needed libraries found, upgrade or check path: i   i    iÿÿÿÿNi   t   libt   python(   t   gplt	   jclicenses   %s
s   [0-9A-Za-z]+c          G   s   t  S(   N(   t   False(   t   whatever(    (    s   ./osm.pyt
   DebugPrint8   s    c          G   s´   |  \ } } t  | ƒ t i j o t | ƒ } n t t d ƒ o | i | ƒ Sn^ d } xP t d t | ƒ ƒ D]9 } | | | } | t | ƒ d j o | | } qo qo W| Sd S(   s   for pythons without str.joint   joint    i    i   N(	   t   typet   typest
   StringTypet   evalt   hasattrt   strR   t   ranget   len(   t   argst   stringt   arrayt   joinedt   index(    (    s   ./osm.pyR   ;   s    & c          G   sg   |  \ } } t  | ƒ p
 d } n t d d ƒ o | i | ƒ Sn  t i t i | ƒ ƒ i | ƒ Sd S(   s   for pythons without str.splitR   t   splitN(   R   t   NoneR   R   t   ret   compilet   escape(   R   R   t   string_to_split(    (    s   ./osm.pyR   I   s
    (   t   *t   stackt   dictt   bytess   www.openstreetmap.orgt   servers   </?([a-z]+)([^>]*(?:/|></\1)?>)s   \s+([a-z]+)=(['"])(.*?)(\2)s   .*?(?:/|</[a-z]+)>\s*$s   	<%s%s/>s   <%s%s>s   </%s>t   nodet   segmentt   wayt   osmc          G   s   t  i i d t t ƒ ƒ d  S(   Ns   %s
(   t   syst   stderrt   writet   reprt   osmdata(   R   (    (    s   ./osm.pyt   dumpd   s    c          G   s\   t  i d ƒ t t t ƒ t |  ƒ d j  o t  i t d <n t |  d ƒ } | t d <d  S(   Ni'  i   t   sourcei    (	   R%   t   setrecursionlimitt   signalt   SIGUSR1R*   R   t   stdinR)   t   open(   R   t   file(    (    s   ./osm.pyt   osminitg   s    c           C   s’   y2 t  d d i t  d d ƒ t  d i d ƒ WnY t t  d ƒ d j p t  d d d d j o" t i i d t t  d ƒ ƒ qŽ n Xd  S(   NR   iþÿÿÿiÿÿÿÿi   i    R$   s   bad stack: %s
(   R)   t   appendt   popR   R%   R&   R'   R(   (    (    (    s   ./osm.pyt   stackadjustp   s    0c          G   s’   t  t d ƒ p# t |  d ƒ |  d }  t ƒ  n t i i d d ƒ t t d „  t d d d ƒ ƒ } t	 d | ƒ t
 t t d ƒ ƒ d S(	   sw   write out an OSM GPX file

    takes as arguments the tags osm|way|segment|node, or IDs of nodes,
    segments, or waysR   i    i   s   %s
s&   <?xml version='1.0' encoding='UTF-8'?>c         S   s   |  d  S(   i    (    (   t   a(    (    s   ./osm.pyt   <lambda>ƒ   s    s	   stack: %sN(   R   R)   R2   t   loadR%   t   stdoutR'   R(   t   mapR   t	   dumpstackt   list(   R   t   stackpicture(    (    s   ./osm.pyR'   y   s    
$c         C   s   t  d d |  ƒ S(   sp   nodefile must be in the correct OSM 0.4 XML format

    alternatively, the data itself can be in the commandlinet   PUTs   /api/0.4/node/create(   t   httprequest(   t   nodefile(    (    s   ./osm.pyt   newnode‡   s    c         C   s   t  d d |  d ƒ S(   sd   find out the nodenumber by downloading data from OSM

    make sure no segments depend on that node!t   DELETEs   /api/0.4/node/%sN(   R?   R   (   t
   nodenumber(    (    s   ./osm.pyt
   deletenode   s    c          G   s«  t  d |  ƒ x" t |  ƒ d j  o |  d 7}  q Wy t |  d ƒ i ƒ  } Wn |  d } n Xt  d | ƒ t i t d ƒ } | i |  d |  d | h  t	 t d ƒ d <ƒ | i
 ƒ  } t | i ƒ  ƒ } t  | i | i | ƒ t o´ d	 | i ƒ  j o¡ t i d
 ƒ i | d	 ƒ } t d „  | ƒ } t  | ƒ | i |  d |  d |  d ƒ | i d t | ƒ | d d ƒ | i d t	 t d ƒ ƒ | i ƒ  n | i ƒ  S(   NR   i   i   s   sending data '%s'R    i    i   t   authorizations
   set-cookies   \s*;\s*c         S   s   |  i  d  ƒ S(   t   =(   R   (   t   s(    (    s   ./osm.pyR7   ¥   s    t   cookie(   N(   R   R   R   R0   t   readt   httplibt   HTTPConnectionR)   t   requestt   authinfot   getresponseR   t
   getheaderst   statust   reasonR   t   keysR   R   R   R:   t
   putrequestt	   putheadert
   endheaders(   R   t   contentt
   connectiont   responset   responseheaderst
   components(    (    s   ./osm.pyR?   “   s.    
"c         C   sš   t  t i i t i d ƒ d ƒ ƒ } | i ƒ  } | i ƒ  t i d |  ƒ } t	 d „  t
 | i | ƒ ƒ d i ƒ  \ } } d t i | d | ƒ i ƒ  S(   Nt   HOMEs   .netrcs4   ^machine\s+%s\s+login\s+(\S+)\s+password\s+(\S+)\s*$c         S   s   |  S(    (    (   t   result(    (    s   ./osm.pyR7   ´   s    i    s   Basic t   :(   R0   t   ost   pathR   t   getenvt	   readlinest   closeR   R   t   filterR:   t   matcht   groupst   base64t   encodestringt   strip(   R    t   authfilet   authdatat   patternt   usernamet   password(    (    s   ./osm.pyRM   ­   s    $
+c          G   si  t  t d ƒ p# t |  d ƒ |  d }  t ƒ  n x.t d d D]} t d | d t d „  | d ƒ ƒ | d d j oä xF | d D]: } | d d
 j o# d t t | d ƒ ƒ | d <qˆ qˆ Wt  | ƒ d j o„ x | d D]q } t d | ƒ | d d j oM xJ | d D]: } | d d
 j o# d t t | d ƒ ƒ | d <qqWqä qä WqaqC qC Wd S(   sO   negate all IDs in the records on the stack

    otherwise OSM might reject themR   i    i   t   recordt   fieldsc         S   s   |  d  S(   i    (    (   t   item(    (    s   ./osm.pyR7   À   s    R!   R"   R#   t   ids   %di   s   record[2] fieldt   segN(   s   nodes   segments   way(   R   R)   R2   R8   R   R:   t   abst   int(   R   Rn   t   fieldt
   properties(    (    s   ./osm.pyt	   negateids·   s*    
 # '  c       
   G   sí   t  t d ƒ p# t |  d ƒ |  d }  t ƒ  n x² t d d D]¢ } | d d j o‹ t d | d t d	 „  | d ƒ ƒ xe | d
 D]U } t d | ƒ | d d j o1 | d d d d g j o d | d d d <qˆ qˆ WqC qC Wd S(   sK   set all 'tiger:reviewed' tags to 'yes'

    otherwise OSM might reject themR   i    i   R!   R"   R#   Rn   Ro   c         S   s   |  d  S(   i    (    (   Rp   (    (    s   ./osm.pyR7   ×   s    i   Ru   t   tagt   ks   tiger:reviewedt   yesN(   s   nodes   segments   way(   R   R)   R2   R8   R   R:   (   R   Rn   Ru   (    (    s   ./osm.pyt   setreviewedÍ   s    
 # ,c          G   s&   t  t t |  ƒ ƒ } t d d | ƒ S(   sŽ   fetch all nodes, segments, and ways within a particular bounding box

    from the OSM servers. args are: min_lon, min_lat, max_lon, max_lat
 t   GETs   /api/0.4/map?bbox=%f,%f,%f,%f(   t   tupleR:   t   floatR?   (   R   t   coordinates(    (    s   ./osm.pyt   getbboxÝ   s    c             s$  t  t d ƒ p# t |  d ƒ |  d }  t ƒ  n g  g  g  } ‰  } t t |  ƒ \ } } } } t d ƒ x)t d d D]} t d } }	 | d d j oõ xÉ | d D]½ }
 |
 d d j o8 t |
 d ƒ } | | j  p | | j o
 t } qhq« |
 d d j o8 t |
 d ƒ } | | j  p | | j o
 t } qhq« |
 d d j o t	 |
 d ƒ }	 q« q« W| o |	 d j o | i
 |	 ƒ q•q| q| Wt d	 ƒ xí t d d D]Ý } d }	 | d d
 j oÀ x | d D]u }
 |
 d d j o t |
 d ƒ } qÚ|
 d d j o t |
 d ƒ } qÚ|
 d d j o t	 |
 d ƒ }	 qÚqÚW| | j o+ | | j o |	 d j o ˆ  i
 |	 ƒ qq²q²Wt d ƒ x€ t d d D]p } | d d j oY t d | ƒ t d „  | d ƒ } t d | ƒ t ‡  f d †  | ƒ } t d | ƒ q¬q¬Wd S(   sÂ   get the subset of a TIGER file within a particular bounding box

    use the same syntax as in bbox in the OSM API:
    minimum_longitude, minimum_latitude, maximum_longitude, maximum_latitude
 R   i    i   s-   first getting the nodes within the boundariesR!   t   latt   lonRq   s4   now getting the segments containing ONLY those nodesR"   t   fromt   tos;   now removing unneeded segs from ways, and ways without segsR#   Rn   c         S   s   |  d  d j S(   i    Rr   (    (   Ru   (    (    s   ./osm.pyR7     s    t   segsc            s   t  t |  d  ƒ d ƒ ˆ  j S(   i   Rq   (   Rt   R   (   Rr   (   t   segments(    s   ./osm.pyR7     s    s   segs after filterN(   R   R)   R2   R8   R:   R~   R   t   TrueR   Rt   R3   Rc   (   R   t   nodest   wayst   min_lont   min_latt   max_lont   max_latRn   t   addnodeRq   Ru   R   R‚   t   seg_fromt   seg_toR…   (    (   R†   s   ./osm.pyt	   tigerbboxå   sb    

  
  '
 c       
   G   sË   t  t d ƒ p# t |  d ƒ |  d }  t ƒ  n x‰ t d d D]y } | d d j ob t d | d t d „  | d ƒ ƒ d	 t d
 „  | d ƒ j o | d i d	 t ƒ  g ƒ q¼ qC qC Wt ƒ  d S(   s]   add timestamp fields to all 'node' and 'segment' records

    otherwise OSM might reject themR   i    i   R!   R"   Rn   Ro   c         S   s   |  d  S(   i    (    (   Rp   (    (    s   ./osm.pyR7   )  s    t	   timestampc         S   s   |  d  S(   i    (    (   Rp   (    (    s   ./osm.pyR7   *  s    N(   s   nodes   segment(	   R   R)   R2   R8   R   R:   R3   R’   R'   (   R   Rn   (    (    s   ./osm.pyt   addtimestamps  s    
 #&c    
   
      sl  t  t d ƒ p# t |  d ƒ |  d }  t ƒ  n t t d ƒ d d d !g g  } } xŒ|  D]„} t i i d | ƒ xgt d „  t d d ƒ D]K} d d d	 g d
 | g g g | j o | d i	 | ƒ xt d „  | ƒ D]ô } t
 d | d d d ƒ | d d d d j o¼ | d d d ‰  t ‡  f d †  t d d ƒ } t
 d | ƒ | d i d | d ƒ t d „  | d d ƒ d d } t d „  | d d ƒ d d } | | | g 7} t
 d | ƒ qä qä Wq• q• Wq` Wx^ t t d | | ƒ ƒ i ƒ  D]> ‰ t ‡ f d †  t d d ƒ }	 | d i d |	 d ƒ qW| t d <t ƒ  t ƒ  t ƒ  d S(   s\   extract a single road (from "name" tag) from an OSM file

    write it out to a new OSM fileR   i    i   i   s   extracting road: %s
c         S   s   |  d  d j S(   i    R#   (    (   Rp   (    (    s   ./osm.pyR7   9  s    Rx   Ry   t   namet   vc         S   s   |  d  d j S(   i    Rr   (    (   Rp   (    (    s   ./osm.pyR7   <  s    R"   Rq   c            s%   |  d  d j o d ˆ  g |  d j S(   i    R"   Rq   i   (    (   Rp   (   Rq   (    s   ./osm.pyR7   @  s   s   found segmentc         S   s   |  d  d j S(   i    Rƒ   (    (   t   property(    (    s   ./osm.pyR7   E  s    c         S   s   |  d  d j S(   i    R„   (    (   R–   (    (    s   ./osm.pyR7   G  s    Rˆ   c            s%   |  d  d j o d ˆ  g |  d j S(   i    R!   Rq   i   (    (   Rp   (   RC   (    s   ./osm.pyR7   K  s   N(   R   R)   R2   R8   R<   R%   R&   R'   Rc   R3   R   t   insertR   R:   R   RR   Rw   R{   (
   R   t   newstackRˆ   t   roadnameR#   Rr   R"   t	   from_nodet   to_nodeR!   (    (   Rq   RC   s   ./osm.pyt   getroad.  sH    
%  % ! 
c         G   s™   t  d | ƒ | d } d } t | ƒ d j oe xb | D]V } | \ } } d } |  d j p d | j o
 d } n | d | | | | f 7} q7 Wn | S(   NRv   i    R   t   'Rx   t   "s
    %s=%s%s%s(   R   R   (   Rx   Rv   R\   R–   t   keyt   valuet	   delimiter(    (    s   ./osm.pyt   formatpropertiesS  s    
 
"c    	      G   sÔ   |  d d } } t  d t | d ƒ ƒ | d d d !\ } } t | | ƒ xw | t | d ƒ j  o_ | d | } | d | d | d } } } t  d | | | ƒ t | | | ƒ | d 7} qP Wt | GHd S(   sf   write out stack in order it was created

    ideally this should give a file identical to the originali    i   s   stack: %d elementsi   s   dumping item:N(   R   R   t   dumptoplevelt   dumpmultiplet	   formatend(	   R   R   R   t   topleveltagt   toplevelpropertiesRp   Rx   Rv   t   other(    (    s   ./osm.pyR;   `  s     c         C   s"   t  } | |  t |  | ƒ f GHd  S(   N(   t   formatmultipleR¢   (   Rx   Rv   t   formatstring(    (    s   ./osm.pyR£   p  s    c           C   s   t  d t ƒ  ƒ S(   Ns   %Y-%m-%dT%H:%M:%SZ(   t   strftimet   gmtime(    (    (    s   ./osm.pyR’   t  s    c         C   s…   t  } t | ƒ d j o# | t |  t |  | ƒ |  f GHnF | |  t |  | ƒ f GHx! | D] \ } } t | | ƒ q[ Wt |  GHd  S(   Ni    (   R©   R   R¥   R¢   t
   dumpsingle(   Rx   Rv   R¨   Rª   t	   singletagt   singleproperties(    (    s   ./osm.pyR¤   w  s    
 c         C   s"   t  } | |  t |  | ƒ f GHd  S(   N(   t   formatsingleR¢   (   Rx   Rv   Rª   (    (    s   ./osm.pyR­   ‚  s    c          C   sn   t  ƒ  p` t t d „  t d ƒ ƒ }  d t d |  f } | d d t | ƒ } t i i d | ƒ n d  S(   Nc         S   s   |  d  S(   i    (    (   R6   (    (    s   ./osm.pyR7   ˆ  s    R   s   bytes read: %d; stack: %sR   t    iN   s   %s(   R   R(   R:   R)   R   R%   R&   R'   (   R=   t   message(    (    s   ./osm.pyt   progress†  s
    
c           C   sj   t  t d d d ƒ t  t d d d ƒ t t d d d ƒ t d t d d d <t  t d ƒ d S(   sE   turn stack into a dict(ionary) of IDs to make property lookups easierR   i    i   R   N(   R   R)   R   (    (    (    s   ./osm.pyt   makedict  s    ,c          C   sr  xkt  oct d i ƒ  }  t d c t |  ƒ 7<t i |  ƒ } | o÷ | i ƒ  d } | i ƒ  d j o9 |  d | i ƒ  !i ƒ  o t	 d |  d | i ƒ  !ƒ n | i
 ƒ  i d ƒ o t ƒ  np t d i | g ƒ t d „  t i | i ƒ  d ƒ ƒ } t d d	 i | ƒ t i | i
 ƒ  ƒ o t ƒ  n t ƒ  q |  i d
 ƒ o q q t i i d |  ƒ Pq Wd  S(   NR+   R   i    t   skippeds   </R   c         S   s   |  d  |  d g S(   i    i   (    (   R6   (    (    s   ./osm.pyR7   ¡  s    i   iÿÿÿÿs   <?xmls   Quitting at line: %s
(   R‡   R)   t   readlineR   t   element_patternt   searchRe   t   startt   isspaceR   t   groupt
   startswithR5   R3   R:   t   property_patternt   findallt
   endpatternR³   R%   R&   R'   (   t   linet   elementt   osmtypeRv   (    (    s   ./osm.pyR8   ”  s*    
.	c          G   s'   t  |  d ƒ t ƒ  t ƒ  t ƒ  d S(   sA   dumps an OSM .gpx file

    shows all nodes, segments, and ways
 i    N(   R2   R8   R´   R*   (   R   (    (    s   ./osm.pyR$   ²  s    c          C   s(   d }  t  i d |  ƒ t  i d ƒ d  S(   Ns;   /cygdrive/c/Program Files/VMWare/VMware Player/vmplayer.exes   "%s" osm.vmxs   rm osm.vm?? osm.vmx.lck vmware*(   R^   t   system(   t   vmware(    (    s   ./osm.pyt   osmboot¼  s    t   __main__s   type(%s) == types.FunctionTypei   s   %s%sR   (   i   i    (I   t   __doc__t	   Copyrightt   errormessageR‡   R   R%   R^   R
   R   t   pwdR_   R3   R   t   getpwuidt   geteuidR(   t   com.jcomeauR   R   R&   R'   R   t   argvt   myselft   splitextt   commandR   R¸   R»   t   originalselfR   R-   t   timeRJ   Rf   R)   R·   R½   R¿   R°   R©   R¥   t	   multiplesR*   R2   R5   RA   RD   R?   RM   Rw   R{   R€   R‘   R“   Rœ   R¢   R;   R£   R’   R¤   R­   R³   R´   R8   R$   RÅ   t   __name__t   functionR   R   R   R}   (    (    (    s   ./osm.pys   <module>
   s   <5				


													
				:		%										
	"",