Exemplo n.º 1
0
 def test_isvalid(self):
     valid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')
     invalid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))')
     State.objects.create(name='valid', poly=valid_geom)
     State.objects.create(name='invalid', poly=invalid_geom)
     valid = State.objects.filter(name='valid').annotate(isvalid=functions.IsValid('poly')).first()
     invalid = State.objects.filter(name='invalid').annotate(isvalid=functions.IsValid('poly')).first()
     self.assertIs(valid.isvalid, True)
     self.assertIs(invalid.isvalid, False)
Exemplo n.º 2
0
    def test_lookup_insert_transform(self):
        "Testing automatic transform for lookups and inserts."
        # San Antonio in 'WGS84' (SRID 4326)
        sa_4326 = 'POINT (-98.493183 29.424170)'
        wgs_pnt = fromstr(sa_4326, srid=4326)  # Our reference point in WGS84
        # San Antonio in 'WGS 84 / Pseudo-Mercator' (SRID 3857)
        other_srid_pnt = wgs_pnt.transform(3857, clone=True)
        # Constructing & querying with a point from a different SRID. Oracle
        # `SDO_OVERLAPBDYINTERSECT` operates differently from
        # `ST_Intersects`, so contains is used instead.
        if oracle:
            tx = Country.objects.get(mpoly__contains=other_srid_pnt)
        else:
            tx = Country.objects.get(mpoly__intersects=other_srid_pnt)
        self.assertEqual('Texas', tx.name)

        # Creating San Antonio.  Remember the Alamo.
        sa = City.objects.create(name='San Antonio', point=other_srid_pnt)

        # Now verifying that San Antonio was transformed correctly
        sa = City.objects.get(name='San Antonio')
        self.assertAlmostEqual(wgs_pnt.x, sa.point.x, 6)
        self.assertAlmostEqual(wgs_pnt.y, sa.point.y, 6)

        # If the GeometryField SRID is -1, then we shouldn't perform any
        # transformation if the SRID of the input geometry is different.
        m1 = MinusOneSRID(geom=Point(17, 23, srid=4326))
        m1.save()
        self.assertEqual(-1, m1.geom.srid)
Exemplo n.º 3
0
    def test05_Polygon(self):
        'Testing Polygon mutations'
        for pg in (Polygon(((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
                           ((5, 4), (6, 4), (6, 3), (5, 4))),
                   fromstr(
                       'POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))')):
            self.assertEqual(
                pg._get_single_external(0),
                LinearRing((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
                'Polygon _get_single_external(0)')
            self.assertEqual(pg._get_single_external(1),
                             LinearRing((5, 4), (6, 4), (6, 3), (5, 4)),
                             'Polygon _get_single_external(1)')

            # _set_list
            pg._set_list(2, (((1, 2), (10, 0), (12, 9), (-1, 15), (1, 2)),
                             ((4, 2), (5, 2), (5, 3), (4, 2))))
            self.assertEqual(pg.coords, (((1.0, 2.0), (10.0, 0.0), (12.0, 9.0),
                                          (-1.0, 15.0), (1.0, 2.0)),
                                         ((4.0, 2.0), (5.0, 2.0), (5.0, 3.0),
                                          (4.0, 2.0))), 'Polygon _set_list')

            lsa = Polygon(*pg.coords)
            for f in geos_function_tests:
                self.assertEqual(f(lsa), f(pg), 'Polygon ' + f.__name__)
Exemplo n.º 4
0
 def test03_PointApi(self):
     'Testing Point API'
     q = Point(4, 5, 3)
     for p in (Point(1, 2, 3), fromstr('POINT (1 2 3)')):
         p[0:2] = [4, 5]
         for f in geos_function_tests:
             self.assertEqual(f(q), f(p), 'Point ' + f.__name__)
Exemplo n.º 5
0
 def test_equals_lookups(self):
     "Testing the 'same_as' and 'equals' lookup types."
     pnt = fromstr('POINT (-95.363151 29.763374)', srid=4326)
     c1 = City.objects.get(point=pnt)
     c2 = City.objects.get(point__same_as=pnt)
     c3 = City.objects.get(point__equals=pnt)
     for c in [c1, c2, c3]:
         self.assertEqual('Houston', c.name)
Exemplo n.º 6
0
    def test_point_on_surface(self):
        # Reference values.
        if oracle:
            # SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_GEOM.SDO_POINTONSURFACE(GEOAPP_COUNTRY.MPOLY, 0.05))
            # FROM GEOAPP_COUNTRY;
            ref = {'New Zealand': fromstr('POINT (174.616364 -36.100861)', srid=4326),
                   'Texas': fromstr('POINT (-103.002434 36.500397)', srid=4326),
                   }
        else:
            # Using GEOSGeometry to compute the reference point on surface values
            # -- since PostGIS also uses GEOS these should be the same.
            ref = {'New Zealand': Country.objects.get(name='New Zealand').mpoly.point_on_surface,
                   'Texas': Country.objects.get(name='Texas').mpoly.point_on_surface
                   }

        qs = Country.objects.annotate(point_on_surface=functions.PointOnSurface('mpoly'))
        for country in qs:
            tol = 0.00001  # SpatiaLite might have WKT-translation-related precision issues
            self.assertTrue(ref[country.name].equals_exact(country.point_on_surface, tol))
Exemplo n.º 7
0
    def test_transform(self):
        # Pre-transformed points for Houston and Pueblo.
        ptown = fromstr('POINT(992363.390841912 481455.395105533)', srid=2774)
        prec = 3  # Precision is low due to version variations in PROJ and GDAL.

        # Asserting the result of the transform operation with the values in
        #  the pre-transformed points.
        h = City.objects.annotate(pt=functions.Transform('point', ptown.srid)).get(name='Pueblo')
        self.assertEqual(2774, h.pt.srid)
        self.assertAlmostEqual(ptown.x, h.pt.x, prec)
        self.assertAlmostEqual(ptown.y, h.pt.y, prec)
Exemplo n.º 8
0
    def test01_PointMutations(self):
        'Testing Point mutations'
        for p in (Point(1, 2, 3), fromstr('POINT (1 2 3)')):
            self.assertEqual(p._get_single_external(1), 2.0,
                             'Point _get_single_external')

            # _set_single
            p._set_single(0, 100)
            self.assertEqual(p.coords, (100.0, 2.0, 3.0), 'Point _set_single')

            # _set_list
            p._set_list(2, (50, 3141))
            self.assertEqual(p.coords, (50.0, 3141.0), 'Point _set_list')
Exemplo n.º 9
0
 def test_isvalid_lookup(self):
     invalid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))')
     State.objects.create(name='invalid', poly=invalid_geom)
     qs = State.objects.all()
     if oracle or (mysql and connection.mysql_version < (8, 0, 0)):
         # Kansas has adjacent vertices with distance 6.99244813842e-12
         # which is smaller than the default Oracle tolerance.
         # It's invalid on MySQL < 8 also.
         qs = qs.exclude(name='Kansas')
         self.assertEqual(
             State.objects.filter(name='Kansas',
                                  poly__isvalid=False).count(), 1)
     self.assertEqual(qs.filter(poly__isvalid=False).count(), 1)
     self.assertEqual(qs.filter(poly__isvalid=True).count(), qs.count() - 1)
Exemplo n.º 10
0
    def test_union(self):
        """Union with all combinations of geometries/geometry fields."""
        geom = Point(-95.363151, 29.763374, srid=4326)

        union = City.objects.annotate(union=functions.Union('point', geom)).get(name='Dallas').union
        expected = fromstr('MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)', srid=4326)
        self.assertTrue(expected.equals(union))

        union = City.objects.annotate(union=functions.Union(geom, 'point')).get(name='Dallas').union
        self.assertTrue(expected.equals(union))

        union = City.objects.annotate(union=functions.Union('point', 'point')).get(name='Dallas').union
        expected = GEOSGeometry('POINT(-96.801611 32.782057)', srid=4326)
        self.assertTrue(expected.equals(union))

        union = City.objects.annotate(union=functions.Union(geom, geom)).get(name='Dallas').union
        self.assertTrue(geom.equals(union))
Exemplo n.º 11
0
    def test06_Collection(self):
        'Testing Collection mutations'
        points = (
            MultiPoint(*map(Point, ((3, 4), (-1, 2), (5, -4), (2, 8)))),
            fromstr('MULTIPOINT (3 4,-1 2,5 -4,2 8)'),
        )
        for mp in points:
            self.assertEqual(mp._get_single_external(2), Point(5, -4),
                             'Collection _get_single_external')

            mp._set_list(3, map(Point, ((5, 5), (3, -2), (8, 1))))
            self.assertEqual(mp.coords, ((5.0, 5.0), (3.0, -2.0), (8.0, 1.0)),
                             'Collection _set_list')

            lsa = MultiPoint(*map(Point, ((5, 5), (3, -2), (8, 1))))
            for f in geos_function_tests:
                self.assertEqual(f(lsa), f(mp), 'MultiPoint ' + f.__name__)
Exemplo n.º 12
0
    def test04_LineStringMutations(self):
        'Testing LineString mutations'
        for ls in (LineString((1, 0), (4, 1),
                              (6, -1)), fromstr('LINESTRING (1 0,4 1,6 -1)')):
            self.assertEqual(ls._get_single_external(1), (4.0, 1.0),
                             'LineString _get_single_external')

            # _set_single
            ls._set_single(0, (-50, 25))
            self.assertEqual(ls.coords,
                             ((-50.0, 25.0), (4.0, 1.0), (6.0, -1.0)),
                             'LineString _set_single')

            # _set_list
            ls._set_list(2, ((-50.0, 25.0), (6.0, -1.0)))
            self.assertEqual(ls.coords, ((-50.0, 25.0), (6.0, -1.0)),
                             'LineString _set_list')

            lsa = LineString(ls.coords)
            for f in geos_function_tests:
                self.assertEqual(f(lsa), f(ls), 'LineString ' + f.__name__)
Exemplo n.º 13
0
    def test_relate_lookup(self):
        "Testing the 'relate' lookup type."
        # To make things more interesting, we will have our Texas reference point in
        # different SRIDs.
        pnt1 = fromstr('POINT (649287.0363174 4177429.4494686)', srid=2847)
        pnt2 = fromstr('POINT(-98.4919715741052 29.4333344025053)', srid=4326)

        # Not passing in a geometry as first param raises a TypeError when
        # initializing the QuerySet.
        with self.assertRaises(ValueError):
            Country.objects.filter(mpoly__relate=(23, 'foo'))

        # Making sure the right exception is raised for the given
        # bad arguments.
        for bad_args, e in [((pnt1, 0), ValueError),
                            ((pnt2, 'T*T***FF*', 0), ValueError)]:
            qs = Country.objects.filter(mpoly__relate=bad_args)
            with self.assertRaises(e):
                qs.count()

        # Relate works differently for the different backends.
        if postgis or spatialite:
            contains_mask = 'T*T***FF*'
            within_mask = 'T*F**F***'
            intersects_mask = 'T********'
        elif oracle:
            contains_mask = 'contains'
            within_mask = 'inside'
            # TODO: This is not quite the same as the PostGIS mask above
            intersects_mask = 'overlapbdyintersect'

        # Testing contains relation mask.
        self.assertEqual(
            'Texas',
            Country.objects.get(mpoly__relate=(pnt1, contains_mask)).name)
        self.assertEqual(
            'Texas',
            Country.objects.get(mpoly__relate=(pnt2, contains_mask)).name)

        # Testing within relation mask.
        ks = State.objects.get(name='Kansas')
        self.assertEqual(
            'Lawrence',
            City.objects.get(point__relate=(ks.poly, within_mask)).name)

        # Testing intersection relation mask.
        if not oracle:
            self.assertEqual(
                'Texas',
                Country.objects.get(mpoly__relate=(pnt1,
                                                   intersects_mask)).name)
            self.assertEqual(
                'Texas',
                Country.objects.get(mpoly__relate=(pnt2,
                                                   intersects_mask)).name)
            self.assertEqual(
                'Lawrence',
                City.objects.get(point__relate=(ks.poly,
                                                intersects_mask)).name)

        # With a complex geometry expression
        mask = 'anyinteract' if oracle else within_mask
        self.assertFalse(
            City.objects.exclude(
                point__relate=(functions.Union('point', 'point'), mask)))
Exemplo n.º 14
0
    def test_snap_to_grid(self):
        # Let's try and break snap_to_grid() with bad combinations of arguments.
        for bad_args in ((), range(3), range(5)):
            with self.assertRaises(ValueError):
                Country.objects.annotate(snap=functions.SnapToGrid('mpoly', *bad_args))
        for bad_args in (('1.0',), (1.0, None), tuple(map(str, range(4)))):
            with self.assertRaises(TypeError):
                Country.objects.annotate(snap=functions.SnapToGrid('mpoly', *bad_args))

        # Boundary for San Marino, courtesy of Bjorn Sandvik of thematicmapping.org
        # from the world borders dataset he provides.
        wkt = ('MULTIPOLYGON(((12.41580 43.95795,12.45055 43.97972,12.45389 43.98167,'
               '12.46250 43.98472,12.47167 43.98694,12.49278 43.98917,'
               '12.50555 43.98861,12.51000 43.98694,12.51028 43.98277,'
               '12.51167 43.94333,12.51056 43.93916,12.49639 43.92333,'
               '12.49500 43.91472,12.48778 43.90583,12.47444 43.89722,'
               '12.46472 43.89555,12.45917 43.89611,12.41639 43.90472,'
               '12.41222 43.90610,12.40782 43.91366,12.40389 43.92667,'
               '12.40500 43.94833,12.40889 43.95499,12.41580 43.95795)))')
        Country.objects.create(name='San Marino', mpoly=fromstr(wkt))

        # Because floating-point arithmetic isn't exact, we set a tolerance
        # to pass into GEOS `equals_exact`.
        tol = 0.000000001

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.1)) FROM "geoapp_country"
        # WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr('MULTIPOLYGON(((12.4 44,12.5 44,12.5 43.9,12.4 43.9,12.4 44)))')
        self.assertTrue(
            ref.equals_exact(
                Country.objects.annotate(
                    snap=functions.SnapToGrid('mpoly', 0.1)
                ).get(name='San Marino').snap,
                tol
            )
        )

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.05, 0.23)) FROM "geoapp_country"
        # WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr('MULTIPOLYGON(((12.4 43.93,12.45 43.93,12.5 43.93,12.45 43.93,12.4 43.93)))')
        self.assertTrue(
            ref.equals_exact(
                Country.objects.annotate(
                    snap=functions.SnapToGrid('mpoly', 0.05, 0.23)
                ).get(name='San Marino').snap,
                tol
            )
        )

        # SELECT AsText(ST_SnapToGrid("geoapp_country"."mpoly", 0.5, 0.17, 0.05, 0.23)) FROM "geoapp_country"
        # WHERE "geoapp_country"."name" = 'San Marino';
        ref = fromstr(
            'MULTIPOLYGON(((12.4 43.87,12.45 43.87,12.45 44.1,12.5 44.1,12.5 43.87,12.45 43.87,12.4 43.87)))'
        )
        self.assertTrue(
            ref.equals_exact(
                Country.objects.annotate(
                    snap=functions.SnapToGrid('mpoly', 0.05, 0.23, 0.5, 0.17)
                ).get(name='San Marino').snap,
                tol
            )
        )
Exemplo n.º 15
0
 def test_make_valid(self):
     invalid_geom = fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 1 1, 1 0, 0 0))')
     State.objects.create(name='invalid', poly=invalid_geom)
     invalid = State.objects.filter(name='invalid').annotate(repaired=functions.MakeValid('poly')).first()
     self.assertIs(invalid.repaired.valid, True)
     self.assertEqual(invalid.repaired, fromstr('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))', srid=invalid.poly.srid))