Esempio n. 1
0
    def _almost_equals(self, geom, other_geom):
        if geom is None and other_geom is None:
            return True
        elif geom is not None and other_geom is None:
            return False
        elif geom is None and other_geom is not None:
            return False

        g1 = None
        proj1 = None
        if isinstance(geom, geoalchemy2.WKBElement):
            g1 = wkb_to_shape(geom)
            proj1 = geom.srid
        else:
            # WKT are used in the tests.
            split1 = str.split(geom, ';')
            proj1 = int(str.split(split1[0], '=')[1])
            str1 = split1[1]
            g1 = wkt.loads(str1)

        g2 = None
        proj2 = None
        if isinstance(other_geom, geoalchemy2.WKBElement):
            g2 = wkb_to_shape(other_geom)
            proj2 = other_geom.srid
        else:
            # WKT are used in the tests.
            split2 = str.split(other_geom, ';')
            proj2 = int(str.split(split2[0], '=')[1])
            str2 = split2[1]
            g2 = wkt.loads(str2)

        # https://github.com/Toblerity/Shapely/blob/
        # 8df2b1b718c89e7d644b246ab07ad3670d25aa6a/shapely/geometry/base.py#L673
        decimals = None
        if proj1 != proj2:
            # Should never occur
            raise HTTPInternalServerError('Incompatible projections')
        elif proj1 == 3857:
            decimals = -0.2  # +- 0.8m = 0.5 * 10^0.2
        elif proj1 == 4326:
            decimals = 7  # +- 1m
            # 5178564 740093 | gdaltransform -s_srs EPSG:3857 -t_srs EPSG:4326
            # 46.5198319099112 6.63349924965325 0
            # 5178565 740093 | gdaltransform -s_srs EPSG:3857 -t_srs EPSG:4326
            # 46.5198408930641 6.63349924965325 0
            # 46.5198408930641 - 46.5198319099112 = 0.0000089 -> 7 digits
        else:
            raise HTTPInternalServerError('Bad projection')

        return g1.almost_equals(g2, decimals)
Esempio n. 2
0
    def _almost_equals(self, geom, other_geom):
        if geom is None and other_geom is None:
            return True
        elif geom is not None and other_geom is None:
            return False
        elif geom is None and other_geom is not None:
            return False

        g1 = None
        proj1 = None
        if isinstance(geom, geoalchemy2.WKBElement):
            g1 = wkb_to_shape(geom)
            proj1 = geom.srid
        else:
            # WKT are used in the tests.
            split1 = str.split(geom, ';')
            proj1 = int(str.split(split1[0], '=')[1])
            str1 = split1[1]
            g1 = wkt.loads(str1)

        g2 = None
        proj2 = None
        if isinstance(other_geom, geoalchemy2.WKBElement):
            g2 = wkb_to_shape(other_geom)
            proj2 = other_geom.srid
        else:
            # WKT are used in the tests.
            split2 = str.split(other_geom, ';')
            proj2 = int(str.split(split2[0], '=')[1])
            str2 = split2[1]
            g2 = wkt.loads(str2)

        # https://github.com/Toblerity/Shapely/blob/
        # 8df2b1b718c89e7d644b246ab07ad3670d25aa6a/shapely/geometry/base.py#L673
        decimals = None
        if proj1 != proj2:
            # Should never occur
            raise HTTPInternalServerError('Incompatible projections')
        elif proj1 == 3857:
            decimals = -0.2  # +- 0.8m = 0.5 * 10^0.2
        elif proj1 == 4326:
            decimals = 7  # +- 1m
            # 5178564 740093 | gdaltransform -s_srs EPSG:3857 -t_srs EPSG:4326
            # 46.5198319099112 6.63349924965325 0
            # 5178565 740093 | gdaltransform -s_srs EPSG:3857 -t_srs EPSG:4326
            # 46.5198408930641 6.63349924965325 0
            # 46.5198408930641 - 46.5198319099112 = 0.0000089 -> 7 digits
        else:
            raise HTTPInternalServerError('Bad projection')

        return g1.almost_equals(g2, decimals)
Esempio n. 3
0
 def test_wkb_to_shape_point(self):
     wkb = wkbelement_from_geojson(json.loads(
         '{"type": "Point", "coordinates": [1.0, 2.0, 3.0, 4.0]}'), 3857)
     point = wkb_to_shape(wkb)
     self.assertFalse(point.has_z)
     self.assertAlmostEquals(point.x, 1.0)
     self.assertAlmostEquals(point.y, 2.0)
Esempio n. 4
0
    def test_wkb_to_shape_multipolygon(self):
        wkb = wkbelement_from_geojson(json.loads(
            '{"type": "MultiPolygon", "coordinates": ' +
            '[[[[100.0, 0.0, 1200], [101.0, 0.0, 1200], [101.0, 1.0, 1200], '
            '[100.0, 1.0, 1200], [100.0, 0.0, 1200]]]]}'), 3857)
        multi_polygon = wkb_to_shape(wkb)
        self.assertFalse(multi_polygon.has_z)

        self.assertEqual(len(multi_polygon.geoms[0].exterior.coords), 5)
        self.assertEqual(len(multi_polygon.geoms[0].exterior.coords[0]), 2)
Esempio n. 5
0
    def test_wkb_to_shape_polygon(self):
        wkb = wkbelement_from_geojson(
            '{"type": "Polygon", "coordinates": ' + "[[[100.0, 0.0, 1200], [101.0, 0.0, 1200], [101.0, 1.0, 1200], "
            "[100.0, 1.0, 1200], [100.0, 0.0, 1200]]]}",
            3857,
        )
        polygon = wkb_to_shape(wkb)
        self.assertFalse(polygon.has_z)

        self.assertEqual(len(polygon.exterior.coords), 5)
        self.assertEqual(len(polygon.exterior.coords[0]), 2)
Esempio n. 6
0
    def test_wkb_to_shape_linestring(self):
        wkb = wkbelement_from_geojson(
            '{"type": "LineString", "coordinates": ' + "[[635956, 5723604, 1200], [635966, 5723644, 1210]]}", 3857
        )
        line = wkb_to_shape(wkb)
        self.assertFalse(line.has_z)

        self.assertEqual(len(line.coords), 2)
        self.assertEqual(len(line.coords[0]), 2)
        self.assertCoodinateEquals(line.coords[0], [635956.0, 5723604.0])
        self.assertCoodinateEquals(line.coords[1], [635966.0, 5723644.0])
Esempio n. 7
0
    def test_wkb_to_shape_linestring(self):
        wkb = wkbelement_from_geojson(
            json.loads('{"type": "LineString", "coordinates": ' +
                       '[[635956, 5723604, 1200], [635966, 5723644, 1210]]}'),
            3857)
        line = wkb_to_shape(wkb)
        self.assertFalse(line.has_z)

        self.assertEqual(len(line.coords), 2)
        self.assertEqual(len(line.coords[0]), 2)
        self.assertCoodinateEquals(line.coords[0], [635956.0, 5723604.0])
        self.assertCoodinateEquals(line.coords[1], [635966.0, 5723644.0])
Esempio n. 8
0
 def test_wkb_to_shape_point(self):
     wkb = wkbelement_from_geojson('{"type": "Point", "coordinates": [1.0, 2.0, 3.0, 4.0]}', 3857)
     point = wkb_to_shape(wkb)
     self.assertFalse(point.has_z)
     self.assertAlmostEquals(point.x, 1.0)
     self.assertAlmostEquals(point.y, 2.0)