|  | 
| 37 | 37 | #from make_eddy_track import * | 
| 38 | 38 | from py_eddy_tracker_classes import * | 
| 39 | 39 | 
 | 
|  | 40 | +#import find_closest_point_on_leg as fcpl | 
| 40 | 41 | 
 | 
| 41 | 42 | def haversine_distance_vector(lon1, lat1, lon2, lat2): | 
| 42 | 43 |     ''' | 
| @@ -135,32 +136,34 @@ def strcompare(str1, str2): | 
| 135 | 136 | 
 | 
| 136 | 137 | 
 | 
| 137 | 138 | 
 | 
| 138 |  | -def _find_closest_point_on_leg(p1, p2, p0): | 
| 139 |  | -    """find closest point to p0 on line segment connecting p1 and p2""" | 
|  | 139 | +#def _find_closest_point_on_leg(p1, p2, p0): | 
|  | 140 | +    #"""find closest point to p0 on line segment connecting p1 and p2""" | 
| 140 | 141 | 
 | 
| 141 |  | -    # handle degenerate case | 
| 142 |  | -    if np.all(p2 == p1): | 
| 143 |  | -        d = p0 - p1 | 
| 144 |  | -        d **= 2 | 
| 145 |  | -        return d.sum() | 
|  | 142 | +    ##print type(p1), type(p2), type(p0) | 
| 146 | 143 | 
 | 
| 147 |  | -    d21 = p2 - p1 | 
| 148 |  | -    d01 = p0 - p1 | 
|  | 144 | +    ## handle degenerate case | 
|  | 145 | +    #if np.all(p2 == p1): | 
|  | 146 | +        #d = p0 - p1 | 
|  | 147 | +        #d **= 2 | 
|  | 148 | +        #return d.sum() | 
|  | 149 | + | 
|  | 150 | +    #d21 = p2 - p1 | 
|  | 151 | +    #d01 = p0 - p1 | 
| 149 | 152 | 
 | 
| 150 |  | -    # project on to line segment to find closest point | 
| 151 |  | -    proj = np.dot(d01, d21)  | 
| 152 |  | -    proj /= np.dot(d21, d21) | 
| 153 |  | -    if proj < 0: | 
| 154 |  | -        proj = 0 | 
| 155 |  | -    elif proj > 1: | 
| 156 |  | -        proj = 1 | 
| 157 |  | -    pc = p1 + proj * d21 | 
|  | 153 | +    ## project on to line segment to find closest point | 
|  | 154 | +    #proj = np.dot(d01, d21)  | 
|  | 155 | +    #proj /= np.dot(d21, d21) | 
|  | 156 | +    #if proj < 0: | 
|  | 157 | +        #proj = 0 | 
|  | 158 | +    #elif proj > 1: | 
|  | 159 | +        #proj = 1 | 
|  | 160 | +    #pc = p1 + proj * d21 | 
| 158 | 161 | 
 | 
| 159 |  | -    # find squared distance | 
| 160 |  | -    d = pc - p0 | 
| 161 |  | -    d **= 2 | 
|  | 162 | +    ## find squared distance | 
|  | 163 | +    #d = pc - p0 | 
|  | 164 | +    #d **= 2 | 
| 162 | 165 | 
 | 
| 163 |  | -    return d.sum()#, pc | 
|  | 166 | +    #return d.sum()#, pc | 
| 164 | 167 | 
 | 
| 165 | 168 | 
 | 
| 166 | 169 | #def _find_closest_point_on_leg(p1, p2, p0): | 
| @@ -222,73 +225,107 @@ def _find_closest_point_on_leg(p1, p2, p0): | 
| 222 | 225 | 
 | 
| 223 | 226 |     #return dmin#, xcmin, legmin) | 
| 224 | 227 | 
 | 
| 225 |  | -def _find_closest_point_on_path(lc, point): | 
| 226 |  | -    """ | 
| 227 |  | -    lc: coordinates of vertices | 
| 228 |  | -    point: coordinates of test point | 
| 229 |  | -    """ | 
| 230 |  | -    #import matplotlib.mlab as mlab; print 'delete me' | 
| 231 |  | -    # Find index of closest vertex for this segment | 
| 232 |  | -    ds = np.sum((lc - point[None, :])**2, 1) | 
| 233 |  | -    imin = ds.argmin() | 
| 234 |  | - | 
| 235 |  | -    dmin = np.inf | 
| 236 |  | -    closed = np.alltrue([lc[0,0] == lc[-1,0], lc[0,1] == lc[-1,1]]) | 
| 237 |  | -    #closedd = mlab.is_closed_polygon(lc) | 
|  | 228 | +#def _find_closest_point_on_path(lc, point): | 
|  | 229 | +    #""" | 
|  | 230 | +    #lc: coordinates of vertices | 
|  | 231 | +    #point: coordinates of test point | 
|  | 232 | +    #""" | 
|  | 233 | +    ##import matplotlib.mlab as mlab; print 'delete me' | 
|  | 234 | +    ## Find index of closest vertex for this segment | 
|  | 235 | +    #ds = np.sum((lc - point[None, :])**2, 1) | 
|  | 236 | +    #imin = ds.argmin() | 
|  | 237 | + | 
|  | 238 | +    #dmin = np.inf | 
|  | 239 | +    #closed = np.alltrue([lc[0,0] == lc[-1,0], lc[0,1] == lc[-1,1]]) | 
|  | 240 | +    ##closedd = mlab.is_closed_polygon(lc) | 
| 238 | 241 | 
 | 
| 239 |  | -    #assert closed == closedd, 'should be same' | 
| 240 |  | -    #print '***************************' | 
|  | 242 | +    ##assert closed == closedd, 'should be same' | 
|  | 243 | +    ##print '***************************' | 
| 241 | 244 | 
 | 
| 242 |  | -    # Build list of legs before and after this vertex | 
| 243 |  | -    legs = [] | 
| 244 |  | -    if imin > 0 or closed: | 
| 245 |  | -        legs.append(((imin-1) % len(lc), imin)) | 
| 246 |  | -    if imin < len(lc) - 1 or closed: | 
| 247 |  | -        legs.append((imin, (imin+1) % len(lc))) | 
| 248 |  | - | 
| 249 |  | -    for leg in legs: | 
| 250 |  | -        #d, xc = _find_closest_point_on_leg(lc[leg[0]], lc[leg[1]], point) | 
| 251 |  | -        d = _find_closest_point_on_leg(lc[leg[0]], lc[leg[1]], point) | 
| 252 |  | -        if d < dmin: | 
| 253 |  | -            dmin = d | 
|  | 245 | +    ## Build list of legs before and after this vertex | 
|  | 246 | +    #legs = [] | 
|  | 247 | +    #if imin > 0 or closed: | 
|  | 248 | +        #legs.append(((imin-1) % len(lc), imin)) | 
|  | 249 | +    #if imin < len(lc) - 1 or closed: | 
|  | 250 | +        #legs.append((imin, (imin+1) % len(lc))) | 
| 254 | 251 | 
 | 
| 255 |  | -    return dmin | 
|  | 252 | +    #for leg in legs: | 
|  | 253 | +        ##d, xc = _find_closest_point_on_leg(lc[leg[0]], lc[leg[1]], point) | 
|  | 254 | +        ##dd = _find_closest_point_on_leg(lc[leg[0]], lc[leg[1]], point) | 
|  | 255 | +        ##print type(point), type(lc[leg[0]]), type(lc[leg[1]]) | 
|  | 256 | +        #d = fcpl.find_closest_point_on_leg(lc[leg[0]], lc[leg[1]], point) | 
|  | 257 | +         | 
|  | 258 | +        ##assert dd == d, 'not the same' | 
|  | 259 | +         | 
|  | 260 | +        #if d < dmin: | 
|  | 261 | +            #dmin = d | 
| 256 | 262 | 
 | 
|  | 263 | +    #return dmin | 
| 257 | 264 | 
 | 
| 258 |  | -def find_nearest_contour(contour_obj, x, y, indices): | 
| 259 |  | -    """ | 
| 260 |  | -    Finds contour that is closest to a point. | 
| 261 | 265 | 
 | 
| 262 |  | -    Returns a tuple containing the contour, segment of minimum point. | 
|  | 266 | +#def find_nearest_contour(contour_obj, x, y, indices): | 
|  | 267 | +    #""" | 
|  | 268 | +    #Finds contour that is closest to a point. | 
| 263 | 269 | 
 | 
| 264 |  | -    Call signature:: | 
|  | 270 | +    #Returns a tuple containing the contour, segment of minimum point. | 
| 265 | 271 | 
 | 
| 266 |  | -      conmin, segmin = find_nearest_contour(contour_obj, x, y, indices) | 
| 267 |  | -    """ | 
| 268 |  | -    dmin = np.inf | 
| 269 |  | -    #conmin = None | 
| 270 |  | -    segmin = None | 
|  | 272 | +    #Call signature:: | 
|  | 273 | + | 
|  | 274 | +      #conmin, segmin = find_nearest_contour(contour_obj, x, y, indices) | 
|  | 275 | +    #""" | 
|  | 276 | +    #dmin = np.inf | 
|  | 277 | +    ##conmin = None | 
|  | 278 | +    #segmin = None | 
| 271 | 279 | 
 | 
| 272 |  | -    point = np.array([x, y]) | 
|  | 280 | +    #point = np.array([x, y]) | 
| 273 | 281 | 
 | 
| 274 |  | -    for icon in indices: | 
| 275 |  | -        con = contour_obj.collections[icon] | 
| 276 |  | -        paths = con.get_paths() | 
|  | 282 | +    #for icon in indices: | 
|  | 283 | +        #con = contour_obj.collections[icon] | 
|  | 284 | +        #paths = con.get_paths() | 
| 277 | 285 | 
 | 
| 278 |  | -        for segNum, linepath in enumerate(paths): | 
| 279 |  | -            lc = linepath.vertices | 
| 280 |  | -            d = _find_closest_point_on_path(lc, point) | 
|  | 286 | +        #for segNum, linepath in enumerate(paths): | 
|  | 287 | +            #lc = linepath.vertices | 
|  | 288 | +            ##d = _find_closest_point_on_path(lc, point) | 
|  | 289 | +            #d = fcpl.find_closest_point_on_path(lc, point) | 
| 281 | 290 | 
 | 
| 282 |  | -            if d < dmin: | 
| 283 |  | -                dmin = d | 
| 284 |  | -                #conmin = icon | 
| 285 |  | -                segmin = segNum | 
|  | 291 | +            #if d < dmin: | 
|  | 292 | +                #dmin = d | 
|  | 293 | +                ##conmin = icon | 
|  | 294 | +                #segmin = segNum | 
|  | 295 | + | 
|  | 296 | +    #return segmin | 
|  | 297 | + | 
|  | 298 | + | 
| 286 | 299 | 
 | 
| 287 |  | -    return segmin | 
|  | 300 | +def find_nearest_contour(contcoll, x, y): | 
|  | 301 | +    """ | 
|  | 302 | +    Finds contour that is closest to a point. | 
| 288 | 303 | 
 | 
|  | 304 | +    Returns a tuple containing the contour & segment. | 
| 289 | 305 | 
 | 
|  | 306 | +    Call signature:: | 
| 290 | 307 | 
 | 
|  | 308 | +      segmin = find_nearest_contour(contcoll, x, y) | 
| 291 | 309 | 
 | 
|  | 310 | +    """ | 
|  | 311 | +    dmin = 1e10 | 
|  | 312 | +    segmin = None | 
|  | 313 | +    linepathmin = None | 
|  | 314 | + | 
|  | 315 | +    paths = contcoll.get_paths() | 
|  | 316 | +    for segNum, linepath in enumerate(paths): | 
|  | 317 | +        lc = linepath.vertices | 
|  | 318 | +        ds = lc[:,0] - x | 
|  | 319 | +        ds **= 2 | 
|  | 320 | +        dss = lc[:,1] - y | 
|  | 321 | +        dss **= 2 | 
|  | 322 | +        ds += dss | 
|  | 323 | +        d = ds.min() | 
|  | 324 | +        if d < dmin: | 
|  | 325 | +            dmin = d | 
|  | 326 | +            segmin = segNum | 
|  | 327 | +            linepathmin = linepath | 
|  | 328 | +    return (segmin, linepathmin) | 
| 292 | 329 | 
 | 
| 293 | 330 | 
 | 
| 294 | 331 | 
 | 
|  | 
0 commit comments