예제 #1
0
def change_union_coordinate_direction(union_geom):
    logger.info("change_coordinate_direction")
    coordinates = union_geom["coordinates"]
    logger.info("Type of union polygon : %s of len %s" %
                (type(coordinates), len(coordinates)))
    for i in range(len(coordinates)):
        cord = coordinates[i]
        cord_area = util.get_area(cord)
        if not cord_area > 0:
            logger.info(
                "change_coordinate_direction : coordinates are not clockwise, reversing it"
            )
            cord = [cord[::-1]]
            logger.info(cord)
            cord_area = util.get_area(cord)
            if not cord_area > 0:
                logger.info(
                    "change_coordinate_direction. coordinates are STILL NOT  clockwise"
                )
            union_geom["coordinates"][i] = cord
        else:
            logger.info(
                "change_coordinate_direction: coordinates are already clockwise"
            )

    return union_geom
예제 #2
0
def change_coordinate_direction(cord):
    logger.info("change_coordinate_direction 1 cord: %s\n" % cord)
    cord_area = util.get_area(cord)
    if not cord_area > 0:
        logger.info(
            "change_coordinate_direction : coordinates are not clockwise, reversing it"
        )
        cord = [cord[::-1]]
        logger.info("change_coordinate_direction 2 : cord : %s" % cord)
        try:
            cord_area = util.get_area(cord)
        except:
            cord = cord[0]
            logger.info("change_coordinate_direction 3 : cord : %s" % cord)
            cord_area = util.get_area(cord)
        if not cord_area > 0:
            logger.info(
                "change_coordinate_direction. coordinates are STILL NOT  clockwise"
            )
    else:
        logger.info(
            "change_coordinate_direction: coordinates are already clockwise")

    logger.info("change_coordinate_direction 4 : cord : %s" % cord)
    return cord
예제 #3
0
    def move_terrans(self):
        self.terran_coords = np.array(self.get_positions()[1])
        # TODO: calculate area gradients for each terran, move accordingly
        for terran in self.terrans:
            txy = np.array([terran.x, terran.y])
            closest = self.get_closest_terran(txy)[0]
            closest_coords = (closest.x, closest.y)
            area = util.get_area(txy, self.terrain.size)
            grad = np.zeros(9)

            for i in range(len(area)):
                g_sust = self.terrain.sustenance[
                    area[i][0], area[i][1]] - self.terrain.sustenance[txy[0],
                                                                      txy[1]]
                g_cli = np.average(self.gradient_c[area[i][0], area[i][1]]
                                   ) - self.gradient_c[txy[0], txy[1]][4]
                grad[i] = (g_sust + g_cli) / 2

            dest = area[grad.tolist().index(max(grad))]
            dest = np.asarray(dest)

            if terran.social < 0.2:
                dest_soc = util.path(closest_coords, area)
                dest = (dest + dest_soc) / 2
            elif terran.social > 0.8:
                dest_soc = util.path_away(closest_coords, area)
                dest = (dest + dest_soc) / 2

            if len(self.weather.storms) > 0:
                if self.weather.weathermap[txy[0], txy[1]] > 0:
                    storm = self.weather.get_closest_storm(txy)[0][0]
                    g_storm = util.path_away(storm, area)
                    dest = (dest + g_storm) / 2

            dest = np.ndarray.astype(np.asarray(dest), 'int32')

            # find a spot near destination that isn't occupied or below sea level
            dest_area = util.get_area(dest, self.terrain.size)
            for i in range(len(dest_area)):
                if dest_area[i] not in self.terran_coords:
                    if self.terrain.heightmap[
                            dest_area[i][0],
                            dest_area[i][1]] > self.terrain.water_level:
                        terran.x = dest[0]
                        terran.y = dest[1]
                        break
예제 #4
0
    def get_area(self, pt, ints, region_idx, show=False):
        pt_hp = util.half_plane(pt, ints[0], ints[1])
        vertices = self.vertices[self.regions[region_idx], :]
        vertices_hp = [
            util.half_plane(vertex, ints[0], ints[1]) for vertex in vertices
        ]
        vertices_hp = np.array(vertices_hp)
        vertices_keep = vertices[vertices_hp * pt_hp > 0, :]
        vertices_keep = np.vstack([vertices_keep, ints])
        # print vertices_keep.shape
        area = util.get_area(vertices_keep)
        # conv = scipy.spatial.ConvexHull(vertices_keep)
        # conv.close()

        if show:
            # plt.ion()
            plt.gcf()
            plt.plot(vertices_keep[:, 0], vertices_keep[:, 1], '-r')
            plt.show()

        return area