Skip to content

grid_construction

  • construct a grid from a bounding box
  • label a collection of points by grid cells

  • input: - points csv file with required columns lat,lon # see example input files (structures_households.csv)

  • output: - csv file of grid locations - csv with grid cell id added for each point record

construct(x_min, y_min, x_max, y_max)

Creating grid

Source code in emod_api/demographics/service/grid_construction.py
def construct(x_min, y_min, x_max, y_max):
    '''
    Creating grid
    '''

    print("Creating grid...")

    # get the centroid of the cell left-down from the grid min corner; that is the origin of the grid
    origin = geod.fwd(x_min, y_min, -135, cell_size / np.sqrt(2))

    # get the centroid of the cell right-up from the grid max corner; that is the final point of the grid
    final = geod.fwd(x_max, y_max, 45, cell_size / np.sqrt(2))

    (fwdax, _, dx) = geod.inv(origin[0], origin[1], final[0], origin[1])
    (fwday, _, dy) = geod.inv(origin[0], origin[1], origin[0], final[1])

    # construct grid
    x = origin[0]
    y = origin[1]

    current_point = (x, y)
    grid_id_2_cell_id = dict()

    idx = 0
    cell_id = 0
    grid_lons = list()
    grid_lats = list()
    gcids = list()

    while x < final[0]:
        y = origin[1]
        idy = 0

        while y < final[1]:
            y = geod.fwd(current_point[0], y, fwday, cell_size)[1]
            current_point = (x, y)

            grid_lats.append(current_point[1])
            grid_lons.append(current_point[0])

            grid_id = get_grid_cell_id(idx, idy)
            grid_id_2_cell_id[grid_id] = cell_id

            cell_id += 1
            gcids.append(cell_id)
            idy += 1

        x = geod.fwd(current_point[0], current_point[1], fwdax, cell_size)[0]
        current_point = (x, current_point[1])
        idx += 1

    grid_dict = {"lat": grid_lats, "lon": grid_lons, "gcid": gcids}

    print("Created grid of size")
    print(str(len(set(grid_lons))) + "x" + str(len(set(grid_lats))))

    return grid_dict, grid_id_2_cell_id, origin, final