Exemplo n.º 1
0
 def test_contains_point(self):
     p = Polygon([Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))], [Point((1, 2)), Point((2, 2)), Point((2, 1)), Point((1, 1))])
     self.assertEquals(p.contains_point((0, 0)), 1)
     self.assertEquals(p.contains_point((1, 1)), 0)
     self.assertEquals(p.contains_point((2, 2)), 1)
     self.assertEquals(p.contains_point((5, 5)), 1)
     self.assertEquals(p.contains_point((10, 10)), 0)
Exemplo n.º 2
0
    def test_area2(self):
        """
        Test holes.

        Test tag: <tc>#tests#Polygon.area</tc>
        """
        p = Polygon(
            [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                    holes=[Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))])
        self.assertEquals(p.area, 100 - 4)

        p = Polygon(
            [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                    holes=[[Point(
                        (2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
                        [Point((6, 6)), Point((6, 8)), Point((8, 8)), Point((8, 6))]])
        self.assertEquals(p.area, 100 - (4 + 4))

        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point(
                         (
                             30, 30)), Point(
                                 (40, 30)), Point((40, 40)), Point((30, 40))]],
                    holes=[[Point(
                        (2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
                        [Point((36, 36)), Point((36, 38)), Point((38, 38)), Point((38, 36))]])
        self.assertEquals(p.area, 200 - (4 + 4))
Exemplo n.º 3
0
 def test_contains_point(self):
     p = Polygon([Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))], [Point((1, 2)), Point((2, 2)), Point((2, 1)), Point((1, 1))])
     self.assertEquals(p.contains_point((0, 0)), 1)
     self.assertEquals(p.contains_point((1, 1)), 0)
     self.assertEquals(p.contains_point((2, 2)), 1)
     self.assertEquals(p.contains_point((5, 5)), 1)
     self.assertEquals(p.contains_point((10, 10)), 0)
Exemplo n.º 4
0
def parse_polygon_to_pysal(raw_feature):
    """
    get an OGC polygon from an input ESRI ring array.
    """
    pgon_type, ogc_nest = _get_polygon_type(raw_feature)
    from pysal.cg import Polygon
    if pgon_type == 'Polygon':
        return Polygon([(c[0], c[1]) for c in ogc_nest])
    elif pgon_type == 'MultiPolygon':
        polygons = []
        for p in ogc_nest:
            polygons.append([(c[0], c[1]) for c in p])
        return Polygon(polygons)
    elif pgon_type == 'Polygon with Holes':
        polygon = [(c[0], c[1]) for c in ogc_nest[0]]
        holes = []
        for hole in ogc_nest[1:]:
            holes.append([(c[0], c[1]) for c in hole])
        return Polygon(polygon, holes=holes)
    elif pgon_type == 'MultiPolygon with Holes':
        # return ogc_nest
        return Polygon(
            vertices=[ring[0] for ring in ogc_nest],
            holes=[list(hole) for ring in ogc_nest for hole in ring[1:]])
    else:
        raise Exception('Unexpected Polygon kind {} provided to'
                        ' parse_polygon_to_pysal'.format(pgon_type))
Exemplo n.º 5
0
    def test_area4(self):
        """
        Test polygons with vertices in both orders (cw, ccw).

        Test tag: <tc>#tests#Polygon.area</tc>
        """
        p = Polygon([Point(
            (0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))])
        self.assertEquals(p.area, 100)

        p = Polygon([Point(
            (0, 0)), Point((0, 10)), Point((10, 10)), Point((10, 0))])
        self.assertEquals(p.area, 100)
Exemplo n.º 6
0
 def setUp(self):
     import pandas as pd
     self.columbus = pdio.read_files(get_path('columbus.shp'))
     grid = [
         Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]),
         Polygon([(0, 1), (0, 2), (1, 2), (1, 1)]),
         Polygon([(1, 2), (2, 2), (2, 1), (1, 1)]),
         Polygon([(1, 1), (2, 1), (2, 0), (1, 0)])
     ]
     regime = [0, 0, 1, 1]
     ids = range(4)
     data = np.array((regime, ids)).T
     self.exdf = pd.DataFrame(data, columns=['regime', 'ids'])
     self.exdf['geometry'] = grid
Exemplo n.º 7
0
    def test_perimeter2(self):
        """
        Test with holes.

        Test tag: <tc>#tests#Polygon.perimeter</tc>
        """
        p = Polygon(
            [[Point((0, 0)),
              Point((10, 0)),
              Point((10, 10)),
              Point((0, 10))],
             [
                 Point((30, 30)),
                 Point((40, 30)),
                 Point((40, 40)),
                 Point((30, 40))
             ]],
            holes=[[
                Point((2, 2)),
                Point((4, 2)),
                Point((4, 4)),
                Point((2, 4))
            ], [Point((6, 6)),
                Point((6, 8)),
                Point((8, 8)),
                Point((8, 6))]])
        self.assertEquals(p.perimeter, 80 + 16)
Exemplo n.º 8
0
    def test_parts1(self):
        """
        Test for correct vertex values/order.

        Test tag: <tc>#tests#Polygon.parts</tc>
        """
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((40, 30)), Point((30, 40))]])
        self.assertEquals(len(p.parts), 2)

        part1 = [Point(
            (0, 0)), Point((0, 10)), Point((10, 10)), Point((10, 0))]
        part2 = [Point((30, 30)), Point((30, 40)), Point((40, 30))]
        if len(p.parts[0]) == 4:
            self.assertTrue(p.parts[0] in [part1, part1[-1:] + part1[:3],
                                           part1[-2:] + part1[:2], part1[-3:] + part1[:1]])
            self.assertTrue(p.parts[1] in [part2, part2[-1:] +
                                           part2[:2], part2[-2:] + part2[:1]])
        elif len(p.parts[0]) == 3:
            self.assertTrue(p.parts[0] in [part2, part2[-1:] +
                                           part2[:2], part2[-2:] + part2[:1]])
            self.assertTrue(p.parts[1] in [part1, part1[-1:] + part1[:3],
                                           part1[-2:] + part1[:2], part1[-3:] + part1[:1]])
        else:
            self.fail()
Exemplo n.º 9
0
def parse_polygon_to_pysal(raw_feature):
    """
    get an OGC polygon from an input ESRI ring array.
    """
    pgon_type, ogc_nest = _get_polygon_type(raw_feature)
    from pysal.cg import Polygon
    if pgon_type in ('Polygon', 'MultiPolygon'):
        return Polygon(ogc_nest)
    elif pgon_type == 'Polygon with Holes':
        return Polygon(ogc_nest[0], holes=ogc_nest[1:])
    elif pgon_type == 'MultiPolygon with Holes':
        # return ogc_nest
        return Polygon(
            vertices=[ring[0] for ring in ogc_nest],
            holes=[list(hole) for ring in ogc_nest for hole in ring[1:]])
    else:
        raise Exception('Unexpected Polygon kind {} provided to'
                        ' parse_polygon_to_pysal'.format(pgon_type))
Exemplo n.º 10
0
    def test_area1(self):
        """
        Test multiple parts.

        Test tag: <tc>#tests#Polygon.area</tc>
        """
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))]])
        self.assertEquals(p.area, 200)
Exemplo n.º 11
0
def parse_polygon_to_shapely(raw_feature, strict=False):
    pgon_type, ogc_nest = _get_polygon_type(raw_feature)
    from shapely.geometry import Polygon, MultiPolygon
    if pgon_type == 'Polygon':
        return Polygon(ogc_nest)
    elif pgon_type == 'Polygon with Holes':
        return Polygon(shell=ogc_nest[0], holes=ogc_nest[1:])
    elif pgon_type == 'MultiPolygon':
        return MultiPolygon(Polygon(s, holes=None) for s in ogc_nest)
    elif pgon_type == 'MultiPolygon with Holes':
        out = MultiPolygon(polygons=[
            Polygon(shell=ring[0], holes=ring[1:]) for ring in ogc_nest
        ])
        if not out.is_valid:
            out = fix_rings(out, strict=strict)
        return out
    else:
        raise Exception('Unexpected Polygon kind {} provided to'
                        ' parse_polygon_to_shapely'.format(pgon_type))
Exemplo n.º 12
0
    def test_perimeter1(self):
        """
        Test with multiple parts.

        Test tag: <tc>#tests#Polygon.perimeter</tc>
        """
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))]])
        self.assertEquals(p.perimeter, 80)
Exemplo n.º 13
0
    def test_vertices2(self):
        """
        Test for multiple parts.

        Test tag: <tc>#tests#Polygon.vertices</tc>
        """
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))]])
        self.assertEquals(len(p.vertices), 8)
Exemplo n.º 14
0
    def test_centroid1(self):
        """
        Test polygons with multiple parts of the same size.

        Test tag: <tc>#tests#Polygon.centroid</tc>
        """
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))]])
        c = p.centroid
        self.assertEquals(c[0], 20)
        self.assertEquals(c[1], 20)
Exemplo n.º 15
0
    def test_centroid2(self):
        """
        Test polygons with multiple parts of different size.

        Test tag: <tc>#tests#Polygon.centroid</tc>
        """
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((35, 30)), Point((35, 35)), Point((30, 35))]])
        c = p.centroid
        self.assertEquals(c[0], 10.5)
        self.assertEquals(c[1], 10.5)
Exemplo n.º 16
0
    def test_vertices1(self):
        """
        Test for correct values/order of vertices.

        Test tag: <tc>#tests#Polygon.vertices</tc>
        """
        p = Polygon([Point(
            (0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))])
        self.assertEquals(len(p.vertices), 4)
        e_verts = [Point(
            (0, 0)), Point((0, 10)), Point((10, 10)), Point((10, 0))]
        self.assertTrue(p.vertices in [e_verts, e_verts[-1:] + e_verts[:3],
                                       e_verts[-2:] + e_verts[:2], e_verts[-3:] + e_verts[:1]])
Exemplo n.º 17
0
    def test_holes2(self):
        """
        Test for multiple holes.

        Test tag: <tc>#tests#Polygon.holes</tc>
        """
        p = Polygon(
            [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                    holes=[[Point(
                        (2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
                        [Point((6, 6)), Point((6, 8)), Point((8, 8)), Point((8, 6))]])
        holes = p.holes
        self.assertEquals(len(holes), 2)
Exemplo n.º 18
0
    def test_holes1(self):
        """
        Test for correct vertex values/order.

        Test tag: <tc>#tests#Polygon.holes</tc>
        """
        p = Polygon(
            [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                    holes=[Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))])
        self.assertEquals(len(p.holes), 1)
        e_holes = [Point((2, 2)), Point((2, 4)), Point((4, 4)), Point((4, 2))]
        self.assertTrue(p.holes[0] in [e_holes, [e_holes[-1]] + e_holes[:3],
                                       e_holes[-2:] + e_holes[:2], e_holes[-3:] + [e_holes[0]]])
Exemplo n.º 19
0
    def test_bounding_box1(self):
        """
        Test polygons with multiple parts.

        Test tag: <tc>#tests#Polygon.bounding_box</tc>
        """
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))]])
        bb = p.bounding_box
        self.assertEquals(bb.left, 0)
        self.assertEquals(bb.lower, 0)
        self.assertEquals(bb.right, 40)
        self.assertEquals(bb.upper, 40)
Exemplo n.º 20
0
    def test___init__1(self):
        """
        Test various input configurations (list vs. lists of lists, holes)

        <tc>#tests#Polygon.__init__</tc>
        """
        # Input configurations tested (in order of test):
        # one part, no holes
        # multi parts, no holes
        # one part, one hole
        # multi part, one hole
        # one part, multi holes
        # multi part, multi holes
        p = Polygon([Point(
            (0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))])
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point((30, 30)), Point((40, 30)), Point((40, 40)), Point((30, 40))]])
        p = Polygon(
            [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                    holes=[Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))])
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point(
                         (
                             30, 30)), Point(
                                 (40, 30)), Point((40, 40)), Point((30, 40))]],
                    holes=[Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))])
        p = Polygon(
            [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                    holes=[[Point(
                        (2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
                        [Point((6, 6)), Point((6, 8)), Point((8, 8)), Point((8, 6))]])
        p = Polygon(
            [[Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
                     [Point(
                         (
                             30, 30)), Point(
                                 (40, 30)), Point((40, 40)), Point((30, 40))]],
                    holes=[[Point(
                        (2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))],
                        [Point((6, 6)), Point((6, 8)), Point((8, 8)), Point((8, 6))]])