+
    Si                         R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
HtHtHtHtHt RtR tR tR t]R8X  d
   ]! 4        R# R# )	a  
Reverse-geocode properties with lat/lon but missing department/region.

Uses Nominatim (free, 1 req/sec). Adds: country, state (region),
county (department), city, postcode to each property.

Usage:
    python3 enrich_geocode.py              # Geocode all missing
    python3 enrich_geocode.py --limit 5    # Test on 5 properties
    python3 enrich_geocode.py --dry-run    # Show what would be geocoded
N)Path)loadsavepersistupsert	is_activeNOMINATIM_UAz+https://nominatim.openstreetmap.org/reversec           
        \         P                  P                  RV RVRRR^/4      p\         RV 2p\         P                  P                  VR\        /R7      p \         P                  P                  V^
R	7      ;_uu_ 4       p\        P                  ! VP                  4       4      pVP                  R
/ 4      uuRRR4       #   + '       g   i     R# ; i  \         d    p\        RT  RT RT 24        Rp?R# Rp?ii ; i)zACall Nominatim reverse-geocode API. Returns address dict or None.latlonformatjsonaddressdetails?z
User-Agent)headers)timeoutaddressNz  Geocode failed for ,z: )urllibparse	urlencodeNOMINATIM_URLrequestRequest
USER_AGENTurlopenr   loadsreadget	Exceptionprint)r
   r   paramsurlreqrespdataes   &&      V/Users/jonathan/Documents/Zakelijk/ClaudeOS/03_Lab/farmmatch/scraper/enrich_geocode.pyreverse_geocoder(      s    \\##sE3&2BA% F O1VH
%C
..
 
 |Z.H
 
IC^^##C#44::diik*D88Ir* 5444  %cU!C51#67s6   )C 6C:
C C	C C D$C>>Dc                    V P                  R4      '       d   V P                  R4      '       g   R# \        V 4      '       g   R# V P                  R4      '       * # )z-Property has coords but no county/department.r
   r   Fcounty)r   r   )props   &r'   needs_geocoder,   (   s<    88E??$((5//T??xx!!!    c            
          \         P                  ! R R7      p V P                  R\        ^ RR7       V P                  RRRR7       V P	                  4       p\        4       pVP                  4        UUu. uF  w  r4\        V4      '       g   K  VNK  	  pppVP                  '       d   VR	VP                   p\        R
\        V4       24       VP                  '       d   VR,           FF  pW#,          p\        RVP                  R4      R RVP                  R4      R RVR,           24       KH  	  \        V4      ^
8  d    \        R\        V4      ^
,
           R24       R	# ^ p\        V4       EFd  w  rsW#,          pVR,          VR,          r\        W4      pV'       g   K4  / pVP                  R4      '       d   VR,          VR&   VP                  R4      '       d   VR,          VR&   VP                  R4      '       d   VR,          VR&   VP                  R4      '       g/   VP                  R4      '       g   VP                  R4      '       dG   VP                  R4      ;'       g+    VP                  R4      ;'       g    VP                  R4      VR&   VP                  R4      '       d   VR,          VR&   VP                  R4      ;'       g    RpV'       d   VP                  4       R(9   dS   VP                  R4      VP                  R4      VP                  R4      .pRP!                  R V 4       4      VR&   RVR &   \#        W#V4       V^,          pV^,           ^
,          ^ 8X  d#   \        R!V^,            R"\        V4       R#24       V^,           ^,          ^ 8X  d   \%        V4       \        R$4       \&        P(                  ! R%4       EKg  	  \        R&V R"\        V4       R'24       V^ 8  d   \+        V4       R	# R	# u uppi ))zReverse-geocode properties)descriptionz--limitz!Max properties to geocode (0=all))typedefaulthelpz	--dry-run
store_truezShow what would be geocoded)actionr2   NzProperties needing geocode: :N
   Nz  r
   z.4fz, r   :N<   Nz
  ... and z morecountrystateregionr*   citytownvillagepostcodelocation c              3   8   "   T F  q'       g   K  Vx  K  	  R # 5i)N ).0ps   & r'   	<genexpr>main.<locals>.<genexpr>`   s     *Aeq11es   	
	nominatimlocation_sourcez  Geocoded /z...z  [saved progress]g?z

Geocoded z properties)unknownr?   )argparseArgumentParseradd_argumentint
parse_argsr   itemsr,   limitr    lendry_runr   	enumerater(   lowerjoinr   r   timesleepr   )parserargsstorer"   rC   
candidatesgeocodedir+   r
   r   addrfieldslocpartss                  r'   mainrb   1   sZ   $$1MNF
	Q=`a
L?\]DFE$)KKMFM&#]15E##MJFzzz,
	(Z(9
:;|||c??C
ABquuU|C(155<*<Bs3xjIJ # z?RJs:34E:;HJ'z;USs(88I $YF988G#G}F888H#H~F888Ftxx//488I3F3F!XXf-XX&1AXXTXXiEXF6N88J!%j!1F: hhz"((bciik_4ZZ'H)=vzz(?STE!%*Ae*A!AF:(3F$%u6"AER<1KAwaJ'8<=ER<1K&(

3I (L 
Kz3z?"3;
?@!| q Gs   2P
P
__main__)__doc__rJ   r   rV   urllib.requestr   urllib.parsepathlibr   rZ   r   r   r   r   r   r   r   r   r(   r,   rb   __name__rA   r-   r'   <module>ri      sN   
       T T= "@F zF r-   