예제 #1
0
 def test_multipolygon(self):
     p = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])
     e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)]
     ph1 = geometry.Polygon(e, [i])
     mp = geometry.MultiPolygon([p, ph1])
     self.assertEqual(len(mp), 2)
     self.assertTrue(isinstance(mp.geoms[0], geometry.Polygon))
     self.assertTrue(isinstance(mp.geoms[1], geometry.Polygon))
     self.assertEqual(mp.bounds, (0.0, 0.0, 2.0, 2.0))
     mp = geometry.MultiPolygon([
         (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
          [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))])
     ])
     self.assertEqual(
         mp.__geo_interface__, {
             'type':
             'MultiPolygon',
             'bbox': (0.0, 0.0, 1.0, 1.0),
             'coordinates':
             ((((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
               ((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1),
                (0.1, 0.1))), )
         })
     self.assertEqual(len(mp.geoms), 1)
     self.assertTrue(isinstance(mp.geoms[0], geometry.Polygon))
     mp1 = geometry.MultiPolygon(mp)
     self.assertEqual(mp.__geo_interface__, mp1.__geo_interface__)
     mp2 = geometry.MultiPolygon(ph1)
     self.assertRaises(ValueError, geometry.MultiPolygon, 0)
     self.assertRaises(ValueError, geometry.MultiPolygon, [0, 0])
     pt = geometry.Point(0, 1)
     self.assertRaises(TypeError, geometry.MultiPolygon, pt)
예제 #2
0
 def test_multipolygon(self):
     e0 = ((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0))
     p = geometry.Polygon(e0)
     e1 = ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0))
     i1 = ((1.0, 0.0), (0.5, 0.5), (1.0, 1.0), (1.5, 0.5), (1.0, 0.0))
     ph1 = geometry.Polygon(e1, [i1])
     mp = geometry.MultiPolygon([p, ph1])
     self.assertEqual(mp.geoms[0].exterior.coords, e0)
     self.assertEqual(mp.geoms[1].exterior.coords, e1)
     self.assertEqual(list(mp.geoms[1].interiors)[0].coords, i1)
     mp._set_orientation(True)
     self.assertEqual(mp.geoms[0].exterior.coords, e0)
     self.assertEqual(mp.geoms[1].exterior.coords, e1)
     self.assertEqual(list(mp.geoms[1].interiors)[0].coords, i1)
     mp._set_orientation(False)
     self.assertEqual(mp.geoms[0].exterior.coords, e0[::-1])
     self.assertEqual(mp.geoms[1].exterior.coords, e1[::-1])
     self.assertEqual(list(mp.geoms[1].interiors)[0].coords, i1[::-1])
     mp._set_orientation(True, exterior=False)
     self.assertEqual(mp.geoms[0].exterior.coords, e0[::-1])
     self.assertEqual(mp.geoms[1].exterior.coords, e1[::-1])
     self.assertEqual(list(mp.geoms[1].interiors)[0].coords, i1)
     mp._set_orientation(False)
     mp._set_orientation(True, interiors=False)
     self.assertEqual(list(mp.geoms[1].interiors)[0].coords, i1[::-1])
     self.assertEqual(mp.geoms[0].exterior.coords, e0)
     self.assertEqual(mp.geoms[1].exterior.coords, e1)
예제 #3
0
 def test_geometrycollection(self):
     self.assertRaises(TypeError, geometry.GeometryCollection)
     self.assertRaises(TypeError, geometry.GeometryCollection, None)
     p = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])
     e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)]
     ph = geometry.Polygon(e, [i])
     p0 = geometry.Point(0, 0)
     p1 = geometry.Point(-1, -1)
     r = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])
     l = geometry.LineString([(0, 0), (1, 1)])
     gc = geometry.GeometryCollection([p, ph, p0, p1, r, l])
     self.assertEqual(len(list(gc.geoms)), 6)
     self.assertEqual(len(gc), 6)
     self.assertEqual(gc.bounds, (-1.0, -1.0, 2.0, 2.0))
     self.assertEqual(gc.__geo_interface__,
                      geometry.as_shape(gc).__geo_interface__)
     self.assertEqual(
         gc.__geo_interface__,
         geometry.as_shape(gc.__geo_interface__).__geo_interface__)
     f = geometry._Geometry()
     gc1 = geometry.GeometryCollection(
         [p.__geo_interface__, ph, p0, p1, r, l.__geo_interface__])
     self.assertEqual(gc.__geo_interface__, gc1.__geo_interface__)
     self.assertRaises(NotImplementedError, geometry.GeometryCollection,
                       [p, f])
     mp1 = geometry.MultiPoint([p0, p1])
     self.assertRaises(ValueError, geometry.GeometryCollection, [p, mp1])
     self.assertEqual([p, ph, p0, p1, r, l], [geom for geom in gc])
예제 #4
0
 def setUp(self):
     self.a = geometry.Polygon(
         ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.)))
     self.b = geometry.Polygon(
         ((0., 0.), (0., 2.), (2., 1.), (2., 0.), (0., 0.)))
     self.f1 = geometry.Feature(self.a)
     self.f2 = geometry.Feature(self.b)
     self.fc = geometry.FeatureCollection([self.f1, self.f2])
예제 #5
0
 def test_polygon(self):
     f = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])
     s = geometry.as_shape(f)
     self.assertEqual(f.__geo_interface__, s.__geo_interface__)
     e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)]
     f = geometry.Polygon(e, [i])
     s = geometry.as_shape(f)
     self.assertEqual(f.__geo_interface__, s.__geo_interface__)
     ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25),
              (0.5, 0.25)]
     int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)]
     f = geometry.Polygon(ext, [int_1, int_2])
     s = geometry.as_shape(f)
     self.assertEqual(f.__geo_interface__, s.__geo_interface__)
예제 #6
0
 def test_polygon(self):
     ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25),
              (0.5, 0.25)]
     int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)]
     p = geometry.Polygon(ext, [int_1, int_2])
     self.assertEqual(list(p.exterior.coords), ext)
     interiors = list(p.interiors)
     self.assertEqual(list(interiors[0].coords), int_1)
     self.assertEqual(list(interiors[1].coords), int_2)
     p._set_orientation(False)
     self.assertEqual(list(p.exterior.coords), ext[::-1])
     interiors = list(p.interiors)
     self.assertEqual(list(interiors[0].coords), int_1)
     self.assertEqual(list(interiors[1].coords), int_2)
     p._set_orientation(True)
     self.assertEqual(list(p.exterior.coords), ext)
     interiors = list(p.interiors)
     self.assertEqual(list(interiors[0].coords), int_1[::-1])
     self.assertEqual(list(interiors[1].coords), int_2[::-1])
     p._set_orientation(False, interiors=False)
     self.assertEqual(list(p.exterior.coords), ext[::-1])
     interiors = list(p.interiors)
     self.assertEqual(list(interiors[0].coords), int_1[::-1])
     self.assertEqual(list(interiors[1].coords), int_2[::-1])
     p._set_orientation(True)
     p._set_orientation(False, exterior=False)
     self.assertEqual(list(p.exterior.coords), ext)
     interiors = list(p.interiors)
     self.assertEqual(list(interiors[0].coords), int_1)
     self.assertEqual(list(interiors[1].coords), int_2)
예제 #7
0
 def testVertexPositions(self):
     v = czml.VertexPositions()
     l = geometry.LineString([(0, 0), (1, 1)])
     r = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])
     ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25), (0.5, 0.25)]
     int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)]
     p = geometry.Polygon(ext, [int_1, int_2])
     v.cartesian = l
     v.cartographicDegrees = r
     v.cartographicRadians = p
     self.assertEqual(v.data(),
         {'cartesian': [0.0, 0.0, 0, 1.0, 1.0, 0],
         'cartographicRadians':
         [0.0, 0.0, 0, 0.0, 2.0, 0, 2.0, 2.0, 0, 2.0, 0.0, 0, 0.0, 0.0, 0],
         'cartographicDegrees':
         [0.0, 0.0, 0, 1.0, 1.0, 0, 1.0, 0.0, 0, 0.0, 0.0, 0]})
     v2 = czml.VertexPositions()
     v2.loads(v.dumps())
     self.assertEqual(v.data(), v2.data())
     v.cartesian = None
     v.cartographicDegrees = None
     v.cartographicRadians = [0.0, 0.0, .0, 1.0, 1.0, 1.0]
     self.assertEqual(v.data(), {'cartographicRadians':
         [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]})
예제 #8
0
 def test_linestring(self):
     l = geometry.LineString([(0, 0), (1, 1)])
     self.assertEqual(l.coords, ((0.0, 0.0), (1.0, 1.0)))
     self.assertEqual(l.coords[:1], ((0.0, 0.0), ))
     self.assertEqual(l.bounds, (0.0, 0.0, 1.0, 1.0))
     self.assertEqual(
         l.__geo_interface__, {
             'type': 'LineString',
             'bbox': (0.0, 0.0, 1.0, 1.0),
             'coordinates': ((0.0, 0.0), (1.0, 1.0))
         })
     self.assertEqual(l.to_wkt(), 'LINESTRING (0.0 0.0, 1.0 1.0)')
     p = geometry.Point(0, 0)
     p1 = geometry.Point(1, 1)
     p2 = geometry.Point(2, 2)
     l1 = geometry.LineString([p, p1, p2])
     self.assertEqual(l1.coords, ((0.0, 0.0), (1.0, 1.0), (2.0, 2.0)))
     l2 = geometry.LineString(l1)
     self.assertEqual(l2.coords, ((0.0, 0.0), (1.0, 1.0), (2.0, 2.0)))
     l.coords = l2.coords
     self.assertEqual(l.__geo_interface__, l2.__geo_interface__)
     self.assertRaises(ValueError, geometry.LineString, [(0, 0, 0), (1, 1)])
     ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25),
              (0.5, 0.25)]
     int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)]
     po = geometry.Polygon(ext, [int_1, int_2])
     self.assertRaises(TypeError, geometry.LineString, po)
     pt = geometry.Point(0, 1)
     self.assertRaises(TypeError, geometry.LineString, pt)
     self.assertRaises(TypeError, geometry.LineString, 0)
     self.assertRaises(ValueError, setattr, l2, 'coords',
                       ((0, 0), (1, 1, 1)))
     self.assertRaises(ValueError, setattr, l2, 'coords', 0)
예제 #9
0
def test_contains_attr_polygon():
    result = parse('CONTAINS(geometry, POLYGON((1 1,2 2,0 3,1 1)))')
    assert result == ast.GeometryContains(
        ast.Attribute('geometry'),
        values.Geometry(
            geometry.Polygon([(1, 1), (2, 2), (0, 3),
                              (1, 1)]).__geo_interface__, ),
    )
예제 #10
0
def test_within_multipolygon_attr():
    result = parse('WITHIN(MULTIPOLYGON(((1 1,2 2,0 3,1 1))), geometry)')
    assert result == ast.GeometryWithin(
        values.Geometry(
            geometry.MultiPolygon(
                [geometry.Polygon([(1, 1), (2, 2), (0, 3),
                                   (1, 1)])]).__geo_interface__, ),
        ast.Attribute('geometry'),
    )
예제 #11
0
def test_dwithin_attr_polygon():
    result = parse('DWITHIN(geometry, POLYGON((1 1,2 2,0 3,1 1)), 5, feet)')
    assert result == ast.DistanceWithin(
        ast.Attribute('geometry'),
        values.Geometry(
            geometry.Polygon([(1, 1), (2, 2), (0, 3),
                              (1, 1)]).__geo_interface__, ),
        distance=5,
        units='feet',
    )
예제 #12
0
def test_relate_attr_polygon():
    result = parse(
        'RELATE(geometry, POLYGON((1 1,2 2,0 3,1 1)), \'1*T***T**\')')
    assert result == ast.Relate(
        ast.Attribute('geometry'),
        values.Geometry(
            geometry.Polygon([(1, 1), (2, 2), (0, 3),
                              (1, 1)]).__geo_interface__, ),
        pattern='1*T***T**',
    )
예제 #13
0
def test_beyond_attr_polygon():
    result = parse(
        'BEYOND(geometry, POLYGON((1 1,2 2,0 3,1 1)), 5, nautical miles)')
    assert result == ast.DistanceBeyond(
        ast.Attribute('geometry'),
        values.Geometry(
            geometry.Polygon([(1, 1), (2, 2), (0, 3),
                              (1, 1)]).__geo_interface__, ),
        distance=5,
        units='nautical miles',
    )
예제 #14
0
def test_intersects_attr_geometrycollection():
    result = parse('INTERSECTS(geometry, GEOMETRYCOLLECTION(POINT(1 1),'
                   'LINESTRING(1 1,2 2),'
                   'POLYGON((1 1,2 2,0 3,1 1))'
                   '))')
    assert result == ast.GeometryIntersects(
        ast.Attribute('geometry'),
        values.Geometry(
            geometry.GeometryCollection([
                geometry.Point(1, 1),
                geometry.LineString([(1, 1), (2, 2)]),
                geometry.Polygon([(1, 1), (2, 2), (0, 3), (1, 1)])
            ]).__geo_interface__),
    )
예제 #15
0
 def test_orient(self):
     ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25), (0.5, 0.25)]
     int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)]
     p = geometry.Polygon(ext, [int_1, int_2])
     p1 = geometry.orient(p, 1)
     self.assertEqual(list(p1.exterior.coords), ext[::-1])
     interiors = list(p1.interiors)
     self.assertEqual(list(interiors[0].coords), int_1[::-1])
     self.assertEqual(list(interiors[1].coords), int_2[::-1])
     p2 = geometry.orient(p, -1)
     self.assertEqual(list(p2.exterior.coords), ext)
     interiors = list(p2.interiors)
     self.assertEqual(list(interiors[0].coords), int_1)
     self.assertEqual(list(interiors[1].coords), int_2)
예제 #16
0
 def test_multipoint(self):
     p0 = geometry.Point(0, 0)
     p1 = geometry.Point(1, 1)
     p2 = geometry.Point(2, 2)
     p3 = geometry.Point(3, 3)
     mp = geometry.MultiPoint(p0)
     self.assertEqual(len(mp.geoms), 1)
     self.assertEqual(mp.geoms[0].x, 0)
     mp1 = geometry.MultiPoint([p0, p1, p2])
     self.assertEqual(len(mp1.geoms), 3)
     self.assertEqual(len(mp1), 3)
     self.assertEqual(mp1.geoms[0].x, 0)
     self.assertEqual(mp1.geoms[1].x, 1)
     self.assertEqual(mp1.geoms[2].x, 2)
     self.assertEqual(mp1.bounds, (0.0, 0.0, 2.0, 2.0))
     l1 = geometry.LineString([p0, p1, p2])
     mp2 = geometry.MultiPoint(l1)
     self.assertEqual(len(mp2.geoms), 3)
     self.assertEqual(mp2.geoms[2].x, 2)
     mp3 = geometry.MultiPoint([l1, p3])
     self.assertEqual(mp3.geoms[3].x, 3)
     self.assertRaises(TypeError, geometry.MultiPoint, [mp1, mp3])
     mp4 = geometry.MultiPoint([p0, p1, p0, p1, p2])
     self.assertEqual(len(mp4.geoms), 5)
     mp4.unique()
     self.assertEqual(len(mp4.geoms), 3)
     mp5 = geometry.MultiPoint([[0.0, 0.0], [1.0, 2.0]])
     self.assertEqual(len(mp5.geoms), 2)
     p = geometry.Polygon(
         (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
          ((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))))
     mp6 = geometry.MultiPoint(p)
     self.assertEqual(mp6.bounds, (0.0, 0.0, 1.0, 1.0))
     self.assertRaises(TypeError, geometry.MultiPoint, [0, 0])
     self.assertRaises(
         TypeError,
         geometry.MultiPoint,
         0,
     )
     self.assertEqual(
         mp1.__geo_interface__, {
             'type': 'MultiPoint',
             'bbox': (0.0, 0.0, 2.0, 2.0),
             'coordinates': ((0.0, 0.0), (1.0, 1.0), (2.0, 2.0))
         })
예제 #17
0
 def setUp(self):
     self.geometry = geometry._Geometry()
     self.point = geometry.Point(0, 1)
     self.linestring = geometry.LineString([[0, 0], [1, 0], [1, 1]])
     self.linearring = geometry.LinearRing([[0, 0], [1, 0], [1, 1], [0, 0]])
     self.coords_1 = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
     self.polygon = geometry.Polygon(self.coords_1)
     self.multipoint = geometry.MultiPoint([[0.0, 0.0], [1.0, 2.0]])
     self.multiline = geometry.MultiLineString([[[0.0, 0.0], [1.0, 2.0]]])
     self.multipoly = geometry.MultiPolygon([
         (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
          [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))])
     ])
     self.geo_collect = geometry.GeometryCollection(
         [self.point, self.linestring, self.linearring])
     self.feature = geometry.Feature(self.point, {'a': 1, 'b': 2})
     self.feature_list = [self.feature, self.feature]
     self.fc = geometry.FeatureCollection(self.feature_list)
예제 #18
0
def test_within_multipolygon_attr():
    result = parse([
        'within',
        {
            'type': 'MultiPolygon',
            'coordinates': [[[[1, 1], [2, 2], [0, 3], [1, 1]]]],
            'bbox': [0.0, 1.0, 2.0, 3.0]
        },
        ['geometry'],
    ])
    assert result == ast.GeometryWithin(
        values.Geometry(
            normalize_geom(
                geometry.MultiPolygon(
                    [geometry.Polygon([(1, 1), (2, 2), (0, 3),
                                       (1, 1)])]).__geo_interface__), ),
        ast.Attribute('geometry'),
    )
예제 #19
0
    def savePascalVocFormat(self,
                            filename,
                            shapes,
                            imagePath,
                            imageData,
                            lineColor=None,
                            fillColor=None,
                            databaseSrc=None):
        imgFolderPath = os.path.dirname(imagePath)
        imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFileName = os.path.basename(imagePath)
        #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]
        # Read from file path because self.imageData might be empty if saving to
        # Pascal format
        image = QImage()
        image.load(imagePath)
        imageShape = [
            image.height(),
            image.width(), 1 if image.isGrayscale() else 3
        ]
        writer = PascalVocWriter(imgFolderName,
                                 imgFileName,
                                 imageShape,
                                 localImgPath=imagePath)
        writer.verified = self.verified

        for shape in shapes:
            points = shape['points']
            label = shape['label']
            # Add Chris
            difficult = int(shape['difficult'])
            polygon = geometry.Polygon(points)
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(polygon, bndbox[0], bndbox[1], bndbox[2],
                             bndbox[3], label, difficult)

        writer.save(targetFile=filename)
        return
예제 #20
0
 def test_linearring(self):
     r = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])
     self.assertEqual(r.coords, ((0, 0), (1, 1), (1, 0), (0, 0)))
     self.assertEqual(r.bounds, (0.0, 0.0, 1.0, 1.0))
     l = geometry.LineString(r)
     self.assertEqual(l.coords, ((0, 0), (1, 1), (1, 0), (0, 0)))
     self.assertEqual(
         r.__geo_interface__, {
             'type': 'LinearRing',
             'coordinates': ((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0))
         })
     ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25),
              (0.5, 0.25)]
     int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)]
     p = geometry.Polygon(ext, [int_1, int_2])
     self.assertRaises(TypeError, geometry.LinearRing, p)
     # A LinearRing is self closing
     r2 = geometry.LinearRing([(0, 0), (1, 1), (1, 0)])
     self.assertEqual(r.__geo_interface__, r2.__geo_interface__)
     r3 = geometry.LinearRing(int_1)
     r3.coords = [(0, 0), (1, 1), (1, 0)]
     self.assertEqual(r.__geo_interface__, r3.__geo_interface__)
예제 #21
0
def test_contains_attr_polygon():
    result = parse({
        "contains": [
            {"property": "geometry"},
            {
                "type": "Polygon",
                "coordinates": [
                    [[1, 1], [2, 2], [0, 3], [1, 1]]
                ],
                'bbox': [0.0, 1.0, 2.0, 3.0]
            },
        ]
    })
    assert result == ast.GeometryContains(
        ast.Attribute('geometry'),
        values.Geometry(
            normalize_geom(
                geometry.Polygon(
                    [(1, 1), (2, 2), (0, 3), (1, 1)]
                ).__geo_interface__
            ),
        ),
    )
예제 #22
0
def test_within_multipolygon_attr():
    result = parse({
        "within": [
            {
                "type": "MultiPolygon",
                "coordinates": [
                    [[[1, 1], [2, 2], [0, 3], [1, 1]]]
                ],
                'bbox': [0.0, 1.0, 2.0, 3.0]
            },
            {"property": "geometry"},
        ]
    })
    assert result == ast.GeometryWithin(
        values.Geometry(
            normalize_geom(
                geometry.MultiPolygon([
                    geometry.Polygon([(1, 1), (2, 2), (0, 3), (1, 1)])
                ]).__geo_interface__
            ),
        ),
        ast.Attribute('geometry'),
    )
예제 #23
0
def polygon_from_bounds(bounds):
    W, S, E, N = bounds
    return geometry.Polygon(((W, N), (E, N), (E, S), (W, S), (W, N)))
예제 #24
0
 def test_polygon(self):
     p = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])
     self.assertEqual(p.exterior.coords,
                      ((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)))
     self.assertEqual(list(p.interiors), [])
     self.assertEqual(
         p.__geo_interface__, {
             'type': 'Polygon',
             'bbox': (0.0, 0.0, 1.0, 1.0),
             'coordinates':
             (((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)), )
         })
     self.assertEqual(p.bounds, (0.0, 0.0, 1.0, 1.0))
     r = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])
     p1 = geometry.Polygon(r)
     self.assertEqual(p1.exterior.coords, r.coords)
     e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)]
     ph1 = geometry.Polygon(e, [i])
     self.assertEqual(ph1.__geo_interface__,
                      geometry.as_shape(ph1).__geo_interface__)
     self.assertEqual(ph1.exterior.coords, tuple(e))
     self.assertEqual(list(ph1.interiors)[0].coords, tuple(i))
     self.assertEqual(
         ph1.__geo_interface__, {
             'type':
             'Polygon',
             'bbox': (0.0, 0.0, 2.0, 2.0),
             'coordinates':
             (((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)),
              ((1.0, 0.0), (0.5, 0.5), (1.0, 1.0), (1.5, 0.5), (1.0, 0.0)))
         })
     ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
     int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25),
              (0.5, 0.25)]
     int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75), (0.5, 1.25)]
     ph2 = geometry.Polygon(ext, [int_1, int_2])
     self.assertEqual(ph2.exterior.coords, tuple(ext))
     self.assertEqual(list(ph2.interiors)[0].coords, tuple(int_1))
     self.assertEqual(list(ph2.interiors)[1].coords, tuple(int_2))
     self.assertEqual(
         ph2.__geo_interface__, {
             'type':
             'Polygon',
             'bbox': (0.0, 0.0, 2.0, 2.0),
             'coordinates':
             (((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)),
              ((0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25),
               (0.5, 0.25)), ((0.5, 1.25), (1.0, 1.25), (1.0, 1.75),
                              (0.5, 1.75), (0.5, 1.25)))
         })
     ph3 = geometry.Polygon(ph2)
     self.assertEqual(ph2.__geo_interface__, ph3.__geo_interface__)
     # if a polygon is passed as constructor holes will be ignored
     # XXX or should holes be added to the polygon?
     ph4 = geometry.Polygon(ph2, [i])
     self.assertEqual(ph2.__geo_interface__, ph4.__geo_interface__)
     coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
     polygon = geometry.Polygon(coords)
     ph5 = geometry.Polygon(
         (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
          ((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))))
     r1 = geometry.LinearRing([(0, 0), (2, 2), (2, 0), (0, 0)])
     r2 = geometry.LinearRing([(0.5, 0.5), (1, 1), (1, 0), (0.5, 0.5)])
     p6 = geometry.Polygon(r1, [r2])
     pt = geometry.Point(0, 1)
     self.assertRaises(TypeError, geometry.Polygon, pt)
     self.assertRaises(TypeError, geometry.Polygon, 0)
     self.assertRaises(TypeError, geometry.Polygon, pt, [pt])