Beispiel #1
0
    def getBoundary(self, size_x=512, size_y=512, force=False):
        # Return the img if already loaded
        if (self.img != "N/A"):
            return self.img

        # Find the "shape" of the boundary, if we haven't already
        if (self.boundary == "N/A"):
            self.boundary = self.getFeature()

        # If the "shape" of the boundary cannot be found, that's all we can do
        if (self.boundary == "N/A"):
            return "N/A"

        # Check if the "img" has already been cached, and just open it if it is there
        os.makedirs("cached_property_images", exist_ok=True)

        cache_file = os.path.join(
            "cached_property_images", self.boundary['properties']['LOT'] +
            "_" + self.boundary['properties']['PLAN'] + ".png")

        if (os.path.exists(cache_file) and (force == False)):
            self.img = Image.open(cache_file)
            return self.img

        print("Isolating farm", end='')

        with open(
                os.path.join(
                    "geometries", "geo-x" + str(self.tile_x) + "-y" +
                    str(self.tile_y) + ".geojson")) as f1:
            geo_json_features = json.load(f1)["features"]

        tile = GeometryCollection([
            shape(feature["geometry"]).buffer(0)
            for feature in geo_json_features
        ])

        self.img = Image.new('RGBA', (size_x, size_y))

        count_hit = 0

        result = GeometryCollection()

        if shape(self.boundary["geometry"]).intersects(tile):
            result = result.union(
                shape(self.boundary["geometry"]).intersection(tile))

        if tile.intersects(result):
            for y in range(size_y):
                if (y % 15 == 0):
                    #print("y="+  str(y) + ", hits=" + str(count_hit))
                    print(".", end="")
                for x in range(size_x):
                    lat_long = self.GetLatLongForCoords(y, x)
                    if result.intersects(Point(lat_long)):
                        self.img.putpixel((y, x), (0, 0, 255, 255))  # Blue
                        count_hit += 1

        #print("Hits: " + str(count_hit))

        # Find the border
        for y in range(size_y):
            for x in range(size_x):
                # It is a border if it's not blue...
                if (self.img.getpixel((y, x)) != (0, 0, 255, 255)):
                    # And it has a neighbour that is blue
                    for j in range(y - 1, y + 2):
                        for i in range(x - 1, x + 2):
                            # Check for neighbour out of bounds, don't compare pixel to itself
                            if ((j >= 0) and (j < size_y) and (i >= 0)
                                    and (i < size_x)
                                    and ((j != y) or (i != x))):
                                if (self.img.getpixel(
                                    (j, i)) == (0, 0, 255, 255)):
                                    # This is part of the border!
                                    self.img.putpixel((y, x), (255, 0, 0, 255))

        # Save the image to the cache
        self.img.save(cache_file)

        print(" done")

        return self.img