U
    ‹HaJ  ã                   @   s>   d Z dd„ Zdd„ Zdd„ Zdd„ Zd	d
dddgfdd„ZdS )zCoordinate utility functions.c                 c   s    d| kr,| d D ]}t |ƒD ]
}|V  qqnpt| ttfƒr@| }n"d| krV| d d }n|  d| ¡}|D ]4}t|ttfƒr†t|ƒV   qœt |ƒD ]
}|V  qŽqfdS )a	  
    Yields the coordinates from a Feature or Geometry.

    :param obj: A geometry or feature to extract the coordinates from.
    :type obj: Feature, Geometry
    :return: A generator with coordinate tuples from the geometry or feature.
    :rtype: generator
    ÚfeaturesÚgeometryÚcoordinatesN)ÚcoordsÚ
isinstanceÚtupleÚlistÚgetÚfloatÚint)ÚobjÚfÚcr   Úe© r   ú1/tmp/pip-unpacked-wheel-kunv6awc/geojson/utils.pyr      s    

r   c                    s   ‡ fdd„}t ||ƒS )aL  
    Returns the mapped coordinates from a Geometry after applying the provided
    function to each dimension in tuples list (ie, linear scaling).

    :param func: Function to apply to individual coordinate values
    independently
    :type func: function
    :param obj: A geometry or feature to extract the coordinates from.
    :type obj: Point, LineString, MultiPoint, MultiLineString, Polygon,
    MultiPolygon
    :return: The result of applying the function to each dimension in the
    array.
    :rtype: list
    :raises ValueError: if the provided object is not GeoJSON.
    c                    s   ˆ | d ƒˆ | d ƒfS )Né    é   r   )Zcoord©Úfuncr   r   Ú
tuple_func4   s    zmap_coords.<locals>.tuple_func©Ú
map_tuples)r   r   r   r   r   r   Ú
map_coords#   s    r   c                    sÆ   |d dkrt ˆ |d ƒƒ}nš|d dkrB‡ fdd„|d D ƒ}nv|d dkrf‡ fdd„|d D ƒ}nR|d d	krŠ‡ fd
d„|d D ƒ}n.|d dkr¨t‡ fdd„|ƒS tdt|ƒ ƒ‚|d |dœS )a  
    Returns the mapped coordinates from a Geometry after applying the provided
    function to each coordinate.

    :param func: Function to apply to tuples
    :type func: function
    :param obj: A geometry or feature to extract the coordinates from.
    :type obj: Point, LineString, MultiPoint, MultiLineString, Polygon,
    MultiPolygon
    :return: The result of applying the function to each dimension in the
    array.
    :rtype: list
    :raises ValueError: if the provided object is not GeoJSON.
    ÚtypeÚPointr   )Ú
LineStringÚ
MultiPointc                    s   g | ]}t ˆ |ƒƒ‘qS r   ©r   ©Ú.0r   r   r   r   Ú
<listcomp>M   s     zmap_tuples.<locals>.<listcomp>)ÚMultiLineStringÚPolygonc                    s   g | ]}‡ fd d„|D ƒ‘qS )c                    s   g | ]}t ˆ |ƒƒ‘qS r   r   r   r   r   r   r    O   s    ú)map_tuples.<locals>.<listcomp>.<listcomp>r   ©r   Zcurver   r   r   r    O   s   þ
ÿÚMultiPolygonc                    s   g | ]}‡ fd d„|D ƒ‘qS )c                    s   g | ]}‡ fd d„|D ƒ‘qS )c                    s   g | ]}t ˆ |ƒƒ‘qS r   r   r   r   r   r   r    S   s    z4map_tuples.<locals>.<listcomp>.<listcomp>.<listcomp>r   r$   r   r   r   r    S   s   þ
ÿr#   r   )r   Úpartr   r   r   r    S   s   ý
þ)ÚFeatureÚFeatureCollectionÚGeometryCollectionc                    s
   t ˆ | ƒS ©Nr   )Úgr   r   r   Ú<lambda>X   ó    zmap_tuples.<locals>.<lambda>zInvalid geometry object %s)r   r   )r   Úmap_geometriesÚ
ValueErrorÚrepr)r   r   r   r   r   r   r   :   s     
þ
ýr   c                    sÄ   ddddddg}|d |kr$ˆ |ƒS |d dkrT‡ fd	d
„|d D ƒ}|d |dœS |d dkr€|d rtˆ |d ƒnd|d< |S |d dkr°‡ fdd
„|d D ƒ}|d |dœS t dt|ƒ ƒ‚dS )a›  
    Returns the result of passing every geometry in the given geojson object
    through func.

    :param func: Function to apply to tuples
    :type func: function
    :param obj: A geometry or feature to extract the coordinates from.
    :type obj: GeoJSON
    :return: The result of applying the function to each geometry
    :rtype: list
    :raises ValueError: if the provided object is not geojson.
    r   r   r   r!   r"   r%   r   r)   c                    s   g | ]}|rˆ |ƒnd ‘qS r*   r   )r   Zgeomr   r   r   r    w   s     z"map_geometries.<locals>.<listcomp>Ú
geometries)r   r1   r'   r   Nr(   c                    s   g | ]}t ˆ |ƒ‘qS r   )r.   )r   Zfeatr   r   r   r    }   s     r   )r   r   zInvalid GeoJSON object %s)r/   r0   )r   r   Zsimple_typesZgeomsZfeatsr   r   r   r.   ^   s&    ú	r.   é   g     €fÀg     €VÀg     €f@g     €V@c                    sÎ   ddl m‰m‰ m‰ ddl‰ddl‰	|d ‰|d ‰‡‡‡fdd„‰|d ‰|d ‰‡‡‡fd	d
„‰‡‡‡fdd„‰‡ ‡‡
fdd„}‡‡‡	‡
‡fdd„}dd„ ‰| dkr®ˆƒ S | dkr¼|ƒ S | dkrÊ|ƒ S dS )a§  
    Generates random geojson features depending on the parameters
    passed through.
    The bounding box defaults to the world - [-180.0, -90.0, 180.0, 90.0].
    The number of vertices defaults to 3.

    :param featureType: A geometry type
    :type featureType: Point, LineString, Polygon
    :param numberVertices: The number vertices that a linestring or polygon
    will have
    :type numberVertices: int
    :param boundingBox: A bounding box in which features will be restricted to
    :type boundingBox: list
    :return: The resulting random geojson object or geometry collection.
    :rtype: object
    :raises ValueError: if there is no featureType provided.
    r   )r   r   r"   Né   c                      s   ˆ  ˆˆ ¡S r*   ©Úuniformr   )ÚlonMaxÚlonMinÚrandomr   r   Ú	randomLonž   s    z"generate_random.<locals>.randomLonr   r2   c                      s   ˆ  ˆˆ ¡S r*   r4   r   )ÚlatMaxÚlatMinr8   r   r   Ú	randomLat¤   s    z"generate_random.<locals>.randomLatc                      s   ˆ ˆƒ ˆƒ fƒS r*   r   r   )r   r<   r9   r   r   ÚcreatePoint§   s    z$generate_random.<locals>.createPointc                      s   ˆ ‡fdd„t ˆƒD ƒƒS )Nc                    s   g | ]
}ˆ ƒ ‘qS r   r   )r   Zunused)r=   r   r   r    «   s     z7generate_random.<locals>.createLine.<locals>.<listcomp>)Úranger   )r   r=   ÚnumberVerticesr   r   Ú
createLineª   s    z#generate_random.<locals>.createLinec                     sX  d} d}d}ˆdddƒd ˆj  ˆ }ˆdddƒ|  }g }dˆj  ˆ | }dˆj  ˆ | }d}tˆƒD ]"}	ˆ ||¡}
| |
¡ ||
 }qj|dˆj   }tˆƒD ]}	||	 | ||	< q¤g }ˆ ddˆj  ¡}tˆƒD ]b}	ˆˆ | |¡dd|  ƒ}||ˆ |¡  }||ˆ |¡  }| t|ƒt|ƒf¡ |||	  }qØ|d }| |¡ ˆ |gƒS )Né<   gš™™™™™¹?gš™™™™™É?r   r   r3   g      à?)Úpir>   r5   ÚappendÚgaussÚcosÚsinr
   )Z	aveRadiusZctrXZctrYZirregularityZ
spikeynessZ
angleStepsÚlowerÚupperÚsumÚiÚtmpÚkZpointsZangleZr_iÚxÚyZfirstVal)r"   ÚclipÚmathr?   r8   r   r   Ú
createPoly­   s6    


z#generate_random.<locals>.createPolyc                 S   s,   ||kr| S | |k r|S | |kr$|S | S d S r*   r   )rM   ÚminÚmaxr   r   r   rO   Ï   s    zgenerate_random.<locals>.clipr   r   r"   )Zgeojsonr   r   r"   r8   rP   )ZfeatureTyper?   ZboundingBoxr@   rQ   r   )r   r   r"   rO   r=   r:   r;   r6   r7   rP   r?   r8   r<   r9   r   Úgenerate_randomƒ   s&    "
rT   N)Ú__doc__r   r   r   r.   rT   r   r   r   r   Ú<module>   s   $%
ÿ