def phase4(spread_coeff, z, excld, delta, slope, slope_weights, og):
        TimerUtility.start_timer('spr_phase4')
        nrows = IGrid.nrows
        ncols = IGrid.ncols
        neighbor_options = [(-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1),
                            (-1, 1), (-1, 0), (-1, -1)]

        # Loop over the interior pixels looking for urban from which to perform organic growth
        for row in range(1, nrows - 1):
            for col in range(1, ncols - 1):
                offset = row * ncols + col

                # Is this an urban pixel and do we pass the random spread coefficient test?
                if z[offset] > 0 and Random.get_int(0, 100) < spread_coeff:

                    # Examine the eight cell neighbors
                    # Spread at random if at least 2 are urban
                    # Pixel itself must be urban (3)
                    urb_count = Spread.count_neighbor(z, row, col)
                    if 2 <= urb_count < 8:
                        x_neigh, y_neigh = Random.get_element(neighbor_options)
                        row_neighbor = row + x_neigh
                        col_neighbor = col + y_neigh
                        success, og = Spread.urbanize(row_neighbor,
                                                      col_neighbor, z, delta,
                                                      slope, excld,
                                                      slope_weights,
                                                      UGMDefines.PHASE4G, og)
        TimerUtility.stop_timer('spr_phase4')
        return og
Beispiel #2
0
    def get_neighbor(i_in, j_in):
        neighbor_options = [(-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)]
        i_offset, j_offset = Random.get_element(neighbor_options)

        return i_in + i_offset, j_in + j_offset
    def phase5(road_gravity, diffusion_coeff, breed_coeff, z, delta, slope,
               excld, roads, slope_weights, rt):
        TimerUtility.start_timer('spr_phase5')
        nrows = IGrid.nrows
        ncols = IGrid.ncols
        total_pixels = nrows * ncols

        # Determine the total growth count and save the row and col locations of the new growth
        growth_tracker = []
        growth_count = 0
        for i in range(total_pixels):
            if delta[i] > 0:
                growth_tracker.append((int(i / ncols), i % ncols))
                growth_count += 1

        # Phase 5: Road Trips
        # If there is new growth, begin processing road trips
        if growth_count > 0:
            for i in range(1 + int(breed_coeff)):
                """Determine the Max Index into the Global_Road_Seach_Incices Array
                 for road_gravity of 1 we have 8 values
                 for road_gravity of 2 we have 16 values
                 for road_gravity of 3 we have 24 values
                    and so on...
                    
                if we need to cover N road_gravity values, then the total number of 
                indexed values woud be
                8 + 16 + 24 + ... + (8*N) = 8 *(1+2+3+...+N) = 8*(N(1+N))/2
                """

                int_road_gravity = Spread.get_road_gravity_val(road_gravity)
                max_search_index = 4 * (int_road_gravity *
                                        (1 + int_road_gravity))
                max_search_index = max(max_search_index, nrows)
                max_search_index = max(max_search_index, ncols)

                # Randomly select a growth pixel to start search for road
                growth_row, growth_col = Random.get_element(growth_tracker)

                # Search for road about this growth point
                road_found, i_road_start, j_road_start = Spread.road_search(
                    growth_row, growth_col, max_search_index, roads)

                # If there is a road found, then walk along it
                i_road_end = 0
                j_road_end = 0
                spread = False
                if road_found:
                    #print(roads)
                    spread, i_road_end, j_road_end = Spread.road_walk(
                        i_road_start, j_road_start, roads, diffusion_coeff)

                if spread:
                    urbanized, rt, i_neigh, j_neigh = Spread.urbanize_neighbor(
                        i_road_end, j_road_end, z, delta, slope, excld,
                        slope_weights, UGMDefines.PHASE5G, rt)
                    if urbanized:
                        max_tries = 3
                        for tries in range(3):
                            urbanized, rt, i_neigh_neigh, j_neigh_neigh = Spread.urbanize_neighbor(
                                i_neigh, j_neigh, z, delta, slope, excld,
                                slope_weights, UGMDefines.PHASE5G, rt)
        TimerUtility.stop_timer('spr_phase5')
        return rt