def test_prepared(self): "Testing PreparedGeometry support." # Creating a simple multipolygon and getting a prepared version. mpoly = GEOSGeometry("MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))") prep = mpoly.prepared # A set of test points. pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)] covers = [True, True, False] # No `covers` op for regular GEOS geoms. for pnt, c in zip(pnts, covers): # Results should be the same (but faster) self.assertEqual(mpoly.contains(pnt), prep.contains(pnt)) self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) self.assertEqual(c, prep.covers(pnt)) if geos_version_info()["version"] > "3.3.0": self.assertTrue(prep.crosses(fromstr("LINESTRING(1 1, 15 15)"))) self.assertTrue(prep.disjoint(Point(-5, -5))) poly = Polygon(((-1, -1), (1, 1), (1, 0), (-1, -1))) self.assertTrue(prep.overlaps(poly)) poly = Polygon(((-5, 0), (-5, 5), (0, 5), (-5, 0))) self.assertTrue(prep.touches(poly)) poly = Polygon(((-1, -1), (-1, 11), (11, 11), (11, -1), (-1, -1))) self.assertTrue(prep.within(poly)) # Original geometry deletion should not crash the prepared one (#21662) del mpoly self.assertTrue(prep.covers(Point(5, 5)))
def test_prepared(self): "Testing PreparedGeometry support." # Creating a simple multipolygon and getting a prepared version. mpoly = GEOSGeometry( 'MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))' ) prep = mpoly.prepared # A set of test points. pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)] covers = [True, True, False] # No `covers` op for regular GEOS geoms. for pnt, c in zip(pnts, covers): # Results should be the same (but faster) self.assertEqual(mpoly.contains(pnt), prep.contains(pnt)) self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) self.assertEqual(c, prep.covers(pnt)) if geos_version_info()['version'] > '3.3.0': self.assertTrue(prep.crosses(fromstr('LINESTRING(1 1, 15 15)'))) self.assertTrue(prep.disjoint(Point(-5, -5))) poly = Polygon(((-1, -1), (1, 1), (1, 0), (-1, -1))) self.assertTrue(prep.overlaps(poly)) poly = Polygon(((-5, 0), (-5, 5), (0, 5), (-5, 0))) self.assertTrue(prep.touches(poly)) poly = Polygon(((-1, -1), (-1, 11), (11, 11), (11, -1), (-1, -1))) self.assertTrue(prep.within(poly)) # Original geometry deletion should not crash the prepared one (#21662) del mpoly self.assertTrue(prep.covers(Point(5, 5)))
def test_prepared(self): "Testing PreparedGeometry support." # Creating a simple multipolygon and getting a prepared version. mpoly = GEOSGeometry('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))') prep = mpoly.prepared # A set of test points. pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)] for pnt in pnts: # Results should be the same (but faster) self.assertEqual(mpoly.contains(pnt), prep.contains(pnt)) self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) self.assertEqual(mpoly.covers(pnt), prep.covers(pnt)) self.assertTrue(prep.crosses(fromstr('LINESTRING(1 1, 15 15)'))) self.assertTrue(prep.disjoint(Point(-5, -5))) poly = Polygon(((-1, -1), (1, 1), (1, 0), (-1, -1))) self.assertTrue(prep.overlaps(poly)) poly = Polygon(((-5, 0), (-5, 5), (0, 5), (-5, 0))) self.assertTrue(prep.touches(poly)) poly = Polygon(((-1, -1), (-1, 11), (11, 11), (11, -1), (-1, -1))) self.assertTrue(prep.within(poly)) # Original geometry deletion should not crash the prepared one (#21662) del mpoly self.assertTrue(prep.covers(Point(5, 5)))
def test_prepared(self): "Testing PreparedGeometry support." # Creating a simple multipolygon and getting a prepared version. mpoly = GEOSGeometry('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))') prep = mpoly.prepared # A set of test points. pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)] covers = [True, True, False] # No `covers` op for regular GEOS geoms. for pnt, c in zip(pnts, covers): # Results should be the same (but faster) self.assertEqual(mpoly.contains(pnt), prep.contains(pnt)) self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) self.assertEqual(c, prep.covers(pnt))
def clean_location(self): # Check if the location is within an area we cover. currently # just Brooklyn CB1; If not, set a warning. We can't do this in # RackForm because there's nowhere to put a message other than # RackForm._errors, which would prevent saving. location = self.cleaned_data.get('location') if location: from django.contrib.gis.geos import GEOSGeometry point = GEOSGeometry(location) try: bk = Borough.brooklyn() except Borough.DoesNotExist: self.warnings.append(RACK_NOT_IN_COVERED_AREA) else: cb_qs = list(CommunityBoard.objects.filter(borough=bk, board=1)) if not (cb_qs and point.intersects(cb_qs[0].the_geom)): self.warnings.append(RACK_NOT_IN_COVERED_AREA) return location
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