sosbrumadinho / brumadinho_location

Conjunto de ferramentas para ajudar no resgate e localização das vítimas atingidas pelo rompimento da barragem que ocorreu recentemente em Brumadinho/MG
MIT License
868 stars 129 forks source link

Initial stage postition modeling of objects drifted - translated from MATLAB #82

Closed jsmatias closed 5 years ago

jsmatias commented 5 years ago

Code translated from MATLAB Minor changes:

beneduzi commented 5 years ago

If you want some help guive a call

beneduzi commented 5 years ago

Sorry, I was trying to creeate apull request but I could not So I decide to add here somo code that might help in this part

x = np.linspace(-20.139558, -20.115769, 1000)
y = np.linspace(-44.141856, -44.099738, 1000)

def locate_closest_point(_lat, _lng):
    '''
    Returns the closest index based on input latitude/longitude tuple
    This serves to find index wich represents the closest position on
    regular grid
    _lat = interest point latitude
    _lon = interest point longitude
    '''
    rad_factor = np.pi / 180.0
    latvals = x * rad_factor
    lonvals = y * rad_factor
    lat_rad = _lat * rad_factor
    lon_rad = _lng * rad_factor
    ix = np.abs(latvals[:] - lat_rad).argmin()
    iy = np.abs(lonvals[:] - lon_rad).argmin()
    gc.collect()
    return ix, iy

def calc_distance(_lat0, _lon0, _lat1, _lon1):
    '''
    Returns the distance in kilometers between 2 points
    Guivem to coords tuples, it return aproximated distance
    in km
    '''
    rad_factor = np.pi / \
        180.0  # converts every thing to radians to avoid errors with 0:360 or -180:180 longitudes
    rad_dist_base = (111.32 * 180) / np.pi
    lat0 = _lat0 * rad_factor
    lon0 = _lon0 * rad_factor
    lat1 = _lat1 * rad_factor
    lon1 = _lon1 * rad_factor
    rad_distance = np.sqrt(((lat0 - lat1)**2 + (lon0 - lon1)**2))
    return rad_distance * rad_base

def gen_reg_matrix(e_lats, e_lons):
    '''
    Convert vectors of lat,lon locations to 2D grid
    Usefull for ploting a simple grid or to find distances
    This 'rasterize' lat lon tuples to a regular grid
    Inspired by vec2matrix matlab function

    '''
    Z = np.full((len(set(x)), len(set(y))), np.nan)
    ss_lats = np.searchsorted(x, np.sort(e_lats), side='right')
    ss_lons = np.searchsorted(y, np.sort(e_lons), side='right')
    # Z[(ss_lats, ss_lons)] = np.ones(len(x)) ## fails with multiple
    # repetitions
    for ss_lat, ss_lon in zip(ss_lats, ss_lons):
        Z[(ss_lat, ss_lon)] = 1
    return np.flipud(Z)
jsmatias commented 5 years ago

Thanks @beneduzi for your collaboration. I just have a question about the functions you've created. There's this module utm that converts lat/lon to x,y in the UTM coords.

Would it do the same if I convert lat/lon to x,y before using the triangle relation to calculate the distance? Just to make the code cleaner.

jsmatias commented 5 years ago

Hello @jsmatias. Thanks for your contribution!

Could you please, add a README file under the directory being created explaining how to use the new code/feature being added? Specifying inputs and outputs can be a good starting point.

Done!

caiocarrara commented 5 years ago

@jsmatias sorry for the inconvenience (again :flushed:). Could you please close this PR and open a new one with this code in a new repository that we've created[1]? We're trying to organize the efforts. It would be great and help us a lot.

[1] - https://github.com/sosbrumadinho/victims_location_prediction

jsmatias commented 5 years ago

PR moved to: https://github.com/sosbrumadinho/victims_location_prediction