def update_sheds(self): from maps import get_sheds sheds = get_sheds(self.id) shed_05 = GEOSGeometry(sheds[0.5]) shed_10 = GEOSGeometry(sheds[1.0]) shed_15 = GEOSGeometry(sheds[1.5]) shed_20 = GEOSGeometry(sheds[2.0]) def coerce_to_multipolygon(geometry): if geometry.area == 0: return None if type(geometry) == MultiPolygon: return geometry elif type(geometry) == Polygon: return MultiPolygon(geometry) elif type(geometry) == GeometryCollection: polys = [] for g in geometry: if type(g) == Polygon: polys.append(g) elif type(g) == MultiPolygon: polys += list(g) if any(polys): return MultiPolygon(polys) else: return None else: return geometry shed_20_ring = shed_20.difference(shed_15) self.shed_20 = coerce_to_multipolygon(shed_20_ring) shed_15_ring = shed_15.difference(shed_10) self.shed_15 = coerce_to_multipolygon(shed_15_ring) shed_10_ring = shed_10.difference(shed_05) self.shed_10 = coerce_to_multipolygon(shed_10_ring) self.shed_05 = coerce_to_multipolygon(shed_05)
def test_relations_creation(self): polygon_low = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 0,10 0,10 10,0 10,0 0)))') polygon_high = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 10,10 10,10 20,0 20,0 10)))') polygon_full = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 0,10 0,10 10,10 20,0 20,0 10, 0 0)))') polygon_default = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 0,1 0,1 2,0 2,0 0)))') area_low = Area.objects.create(label=u'Test low', polygon=polygon_low, area_type=self.area_types[0]) area_high = Area.objects.create(label=u'Test high', polygon=polygon_high, area_type=self.area_types[0]) area_full = Area.objects.create(label=u"Test full", update_auto=True, polygon=polygon_default, area_type=self.area_types[0]) with self.assertRaises(ValidationError): area_full.add_parent(area_full) with self.assertRaises(ValidationError): area_full.add_child(area_full) area_full.add_child(area_low) area_full.add_child(area_high) self.assertEqual( AreaRelations.objects.filter(parent=area_full).count(), 2) self.assertEqual( AreaRelations.objects.filter(child=area_full).count(), 0) self.assertEqual( AreaRelations.objects.filter(child=area_low).count(), 1) self.assertEqual(area_full.polygon.difference(polygon_full).area, 0) self.assertEqual(polygon_full.difference(area_full.polygon).area, 0) area_low.polygon = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 -10,10 -10,10 10,0 10,0 -10)))') area_low.save() area_full = Area.objects.get(pk=3) polygon_full_2 = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 -10,10 -10,10 20,0 20,0 -10)))') self.assertEqual(area_full.polygon.difference(polygon_full_2).area, 0) self.assertEqual(polygon_full_2.difference(area_full.polygon).area, 0)
def test_relations_creation(self): polygon_low = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 0,10 0,10 10,0 10,0 0)))') polygon_high = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 10,10 10,10 20,0 20,0 10)))') polygon_full = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 0,10 0,10 10,10 20,0 20,0 10, 0 0)))') polygon_default = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 0,1 0,1 2,0 2,0 0)))') area_low = Area.objects.create(label=u'Test low', polygon=polygon_low, area_type=self.area_types[0]) area_high = Area.objects.create(label=u'Test high', polygon=polygon_high, area_type=self.area_types[0]) area_full = Area.objects.create(label=u"Test full", update_auto=True, polygon=polygon_default, area_type=self.area_types[0]) with self.assertRaises(ValidationError): area_full.add_parent(area_full) with self.assertRaises(ValidationError): area_full.add_child(area_full) area_full.add_child(area_low) area_full.add_child(area_high) self.assertEqual(AreaRelations.objects.filter( parent=area_full).count(), 2) self.assertEqual(AreaRelations.objects.filter( child=area_full).count(), 0) self.assertEqual(AreaRelations.objects.filter( child=area_low).count(), 1) self.assertEqual(area_full.polygon.difference(polygon_full).area, 0) self.assertEqual(polygon_full.difference(area_full.polygon).area, 0) area_low.polygon = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 -10,10 -10,10 10,0 10,0 -10)))') area_low.save() area_full = Area.objects.get(pk=3) polygon_full_2 = GEOSGeometry('SRID=4326;MULTIPOLYGON((('\ '0 -10,10 -10,10 20,0 20,0 -10)))') self.assertEqual(area_full.polygon.difference(polygon_full_2).area, 0) self.assertEqual(polygon_full_2.difference(area_full.polygon).area, 0)
def get_dtours(tracks, polys): """ Dada una lista de tracks y de bidegorris (polys) --> devuelve multilinestring[] con properties[dtour_length, ratio dtour/track] Parameters ---------- tracks : <TrackLikeModel>[] (Track, Dtour, BikeLane) Lista de elementos tipo track de la base de datos polys : MultiPolygon[] Lista de polígonos representando los carriles bici Returns ------- geojson_d GeoJSON con los dtours y sus properties asociadas """ intersected = False gj_dtours = [] # Abrimos los GeoJSON con sus Features gj_dtours.append("{\"type\": \"FeatureCollection\",\"features\": [") if polys: for t in tracks: if t.distance: track_length = t.distance # length del track en m track = GEOSGeometry(t.mlstring, srid=4326) for i in polys: # sacamos cada poly de la lista if track.intersects( i): # if they intersect --> then do the .difference() intersected = True track = track.difference( i ) # Guardamos el resultante como nuevo track y guardamos solo al final if intersected: gj_dtours.append("{\"type\": \"Feature\",") # Abrimos Feature dtour = track # Difference = Track - Poly gj_dtours.append("\"geometry\": " + dtour.geojson) gj_dtours.append(",") # PROPERTIES # Calcular distancia dtours dtour.transform(3035) dtour_length = 0 for lstring in dtour: dtour_length += lstring.length ratio_dtour_to_track = (dtour_length / track_length) * 100 prop_dict = { 'length': str(dtour_length), 'ratio': str(ratio_dtour_to_track) } gj_dtours = addProperties(gj_dtours, prop_dict) gj_dtours.append("},") # Cerramos Feature intersected = False if len(gj_dtours) > 1: gj_dtours = gj_dtours[:-1] # Quitamos la coma para el último track gj_dtours.append("}]}") # Cerramos el GeoJSON geojson_d = ''.join(gj_dtours) else: geojson_d = empty_geojson return geojson_d