def rook_boolean_connectivity(areal_unit_qset): """ Boolean connectivity, two regions are connected if they share a line """ from django.contrib.gis.geos import Polygon names = [x.name for x in areal_unit_qset] W = DataFrame(dict([(x, 0.) for x in names]), index=names) for r in areal_unit_qset: qset = areal_unit_qset.filter(mpoly__intersects=r.mpoly) coords = r.mpoly.coords for polycoords in coords: # discard z axis poly = Polygon(polycoords[0]) for q in qset: if q.id == r.id: # don't consider self-overlaps continue other_coords = q.mpoly.coords for otherpolycoords in other_coords: other_poly = Polygon(otherpolycoords[0]) if isinstance(poly.union(other_poly), Polygon): # shared boundary W[r.name][q.name] = 1. W[q.name][r.name] = 1. for i in range(len(W)): try: W.iloc[i] /= sum(W.iloc[i]) except ZeroDivisionError: pass return W
def shape_to_geos(shape, srs, unit="m"): args = [] for point_index in range(0, len(shape.points)): if point_index in shape.parts: args.append([]) point = shape.points[point_index] point = srs_to_latlng(point[0], point[1], srs, unit) args[-1].append([point[1], point[0]]) polygon = Polygon(*args) try: polygon.union( polygon) #this will fail if the polygon is actual a MultiPolygon except GEOSException as e: polygon = MultiPolygon([Polygon(p) for p in polygon]) return polygon
def download_geometry(self) -> Optional[Polygon]: if not self.layer_elements: self.check(self.aoi) bboxes = self.get_bboxes(self.layer_elements) polygon = Polygon() for bbox in bboxes: polygon = polygon.union(Polygon.from_bbox(bbox)) if polygon.area: return polygon return None
def map_compare(request): id1 = request.POST.get("id1", False) id2 = request.POST.get("id2", False) interesting_area = request.POST.get("interesting_area", False) interesting_area_list = [] origin = Polygon() for ir in interesting_area: origin = origin.union(GEOSGeometry(ir)) mask1_building = Mask.objects.get(map=id1, type_id=1).mask.union(origin) mask1_farm = Mask.objects.get(map=id1, type_id=5).mask.union(origin) mask1_forest = Mask.objects.get(map=id1, type_id=7).mask.union(origin) mask1_shack = Mask.objects.get(map=id1, type_id=6).mask.union(origin) mask1_water = Mask.objects.get(map=id1, type_id=4).mask.union(origin) mask1_grass = Mask.objects.get(map=id1, type_id=3).mask.union(origin) mask1_road = Mask.objects.get(map=id1, type_id=2).mask.union(origin) mask2_building = Mask.objects.get(map=id2, type_id=1).mask.union(origin) mask2_rest = Mask.objects.get(map=id2, type_id=0).mask.union(origin) demolition_area = (((mask1_building.union(mask1_farm)).union(mask1_forest) ).union(mask1_shack)).intersection(mask2_rest) ibuild_area = (((mask1_grass.union(mask1_forest)).union(mask1_road) ).union(mask1_water)).intersection(mask2_building) demolition_area_geojson = { 'type': 'Feature', 'geometry': demolition_area.geojson, 'properties': { 'type': 'change' } } ibuild_area_geojson = { 'type': 'Feature', 'geometry': ibuild_area.geojson, 'properties': { 'type': 'change' } } return JsonResponse({ "demolition_area": demolition_area_geojson, "ibuild_area": ibuild_area_geojson })