Пример #1
0
 def test_contains_point(self):
     """Test contain_point method"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     p = Point(0.5, 0.5)
     bbox = Bbox(4.0, 0.0, 4.0, 0.0)
     self.assertTrue(area.contains_point(p, bbox))
     self.assertTrue(area.contains_point(p))
Пример #2
0
 def test_init(self):
     """Test area __init__ and basic functions"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     self.assertEqual(1, area.id)
     self.assertTrue(area.is2D)
     self.assertTrue(area.alive())
     self.assertEqual(area.area(), 12.0)
Пример #3
0
    def test_centroid(self):
        """Test centroid access"""
        area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
        centroid = area.centroid()

        self.assertEqual(centroid.id, 18)
        self.assertEqual(centroid.area_id, 1)
        self.assertEqual(centroid.to_wkt(), 'POINT (3.5000000000000000 3.5000000000000000)')
Пример #4
0
    def test_boundaries_2(self):
        """Test boundary access"""
        area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
        boundaries = area.boundaries()
        boundary = boundaries[2]
        boundary.read_area_ids()
        self.assertEqual(boundary.left_area_id, 2)
        self.assertEqual(boundary.right_area_id, 1)

        self.assertEqual(boundary.left_centroid().to_wkt(), 'POINT (5.5000000000000000 3.5000000000000000)')
        self.assertEqual(boundary.right_centroid().to_wkt(), 'POINT (3.5000000000000000 3.5000000000000000)')
Пример #5
0
    def test_isles_2(self):
        """Test centroid access"""
        area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
        self.assertEqual(area.num_isles(), 1)

        isles = area.isles()
        isle = isles[0]
        self.assertEqual(isle.area_id(), 1)
        self.assertTrue(isle.alive())

        self.assertEqual(str(isle.bbox()), "Bbox(3.0, 1.0, 3.0, 1.0)")
Пример #6
0
    def test_isles_1(self):
        """Test centroid access"""
        area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
        self.assertEqual(area.num_isles(), 1)

        isles = area.isles()
        isle = isles[0]

        self.assertEqual(isle.area(), 4.0)
        self.assertEqual(isle.points().to_wkt(), "LINESTRING (1.0000000000000000 1.0000000000000000, "\
                                                             "3.0000000000000000 1.0000000000000000, "\
                                                             "3.0000000000000000 3.0000000000000000, "\
                                                             "1.0000000000000000 3.0000000000000000, "\
                                                             "1.0000000000000000 1.0000000000000000)")
Пример #7
0
    def test_boundaries_1(self):
        """Test boundary access"""
        area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
        boundaries = area.boundaries()
        self.assertEqual(len(boundaries), 4)

        string_list = []
        string_list.append("LINESTRING (0.0000000000000000 0.0000000000000000, 0.0000000000000000 4.0000000000000000)")
        string_list.append("LINESTRING (0.0000000000000000 4.0000000000000000, 4.0000000000000000 4.0000000000000000)")
        string_list.append("LINESTRING (4.0000000000000000 4.0000000000000000, 4.0000000000000000 0.0000000000000000)")
        string_list.append("LINESTRING (4.0000000000000000 0.0000000000000000, 0.0000000000000000 0.0000000000000000)")

        for boundary, i in zip(boundaries, range(4)):
            self.assertEqual(len(boundary.to_wkb()), 41)
            self.assertEqual(boundary.to_wkt(), string_list[i])
Пример #8
0
    def test_boundaries_2(self):
        """Test boundary access"""
        area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
        boundaries = area.boundaries()
        boundary = boundaries[2]
        boundary.read_area_ids()
        self.assertEqual(boundary.left_area_id, 2)
        self.assertEqual(boundary.right_area_id, 1)

        self.assertEqual(
            boundary.left_centroid().to_wkt(),
            "POINT (5.5000000000000000 3.5000000000000000)",
        )
        self.assertEqual(
            boundary.right_centroid().to_wkt(),
            "POINT (3.5000000000000000 3.5000000000000000)",
        )
Пример #9
0
    def areas(self, bbox, boxlist=None, bboxlist_only=False):
        """Find areas inside a boundingbox.

            :param bbox: The boundingbox to search in
            :type bbox: grass.pygrass.vector.basic.Bbox

            :param boxlist: An existing BoxList to be filled with
            :type_boxlist: grass.pygrass.vector.basic.BoxList

            :param bboxlist_only: If true the BoxList will be returned,
                                  no features are generated
            :type bboxlist_only: boolean

            :return: A list of areas or None if nothing was found

            This methods uses libvect.Vect_select_areas_by_box()

            Examples:

            >>> from grass.pygrass.vector import VectorTopo
            >>> from grass.pygrass.vector.basic import Bbox
            >>> test_vect = VectorTopo(test_vector_name)
            >>> test_vect.open('r')

            # Find areas in box
            >>> bbox = Bbox(north=5, south=-1, east=9, west=-1)
            >>> result = test_vect.find_by_bbox.areas(bbox=bbox)
            >>> [area for area in result]
            [Area(1), Area(2), Area(3), Area(4)]

            >>> bbox = Bbox(north=5, south=-1, east=9, west=-1)
            >>> result = test_vect.find_by_bbox.areas(bbox=bbox,
            ...                                       bboxlist_only=True)
            >>> result                   #doctest: +NORMALIZE_WHITESPACE
            Boxlist([Bbox(4.0, 0.0, 4.0, 0.0),
                     Bbox(4.0, 0.0, 6.0, 4.0),
                     Bbox(3.0, 1.0, 3.0, 1.0),
                     Bbox(4.0, 0.0, 8.0, 6.0)])

            >>> bbox = Bbox(north=20, south=18, east=20, west=18)
            >>> test_vect.find_by_bbox.areas(bbox=bbox)

            >>> test_vect.find_by_bbox.areas(bbox=bbox,
            ...                              bboxlist_only=True)

            >>> test_vect.close()
        """
        boxlist = boxlist if boxlist else BoxList()
        if libvect.Vect_select_areas_by_box(self.c_mapinfo, bbox.c_bbox,
                                            boxlist.c_boxlist):
            if bboxlist_only:
                return boxlist
            else:
                return (Area(v_id=a_id,
                             c_mapinfo=self.c_mapinfo,
                             table=self.table,
                             writeable=self.writeable) for a_id in boxlist.ids)
Пример #10
0
 def test_to_wkt(self):
     """Test to_wkt method"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     # Outer and inner ring!!
     string = ("POLYGON ((0.0000000000000000 0.0000000000000000, "
               "0.0000000000000000 4.0000000000000000, "
               "0.0000000000000000 4.0000000000000000, "
               "4.0000000000000000 4.0000000000000000, "
               "4.0000000000000000 4.0000000000000000, "
               "4.0000000000000000 0.0000000000000000, "
               "4.0000000000000000 0.0000000000000000, "
               "0.0000000000000000 0.0000000000000000), "
               "(1.0000000000000000 1.0000000000000000, "
               "3.0000000000000000 1.0000000000000000, "
               "3.0000000000000000 3.0000000000000000, "
               "1.0000000000000000 3.0000000000000000, "
               "1.0000000000000000 1.0000000000000000))")
     self.assertEqual(area.to_wkt(), string)
Пример #11
0
 def test_to_wkt(self):
     """Test to_wkt method"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     # Outer and inner ring!!
     string = "POLYGON ((0.0000000000000000 0.0000000000000000, "\
                        "0.0000000000000000 4.0000000000000000, "\
                        "0.0000000000000000 4.0000000000000000, "\
                        "4.0000000000000000 4.0000000000000000, "\
                        "4.0000000000000000 4.0000000000000000, "\
                        "4.0000000000000000 0.0000000000000000, "\
                        "4.0000000000000000 0.0000000000000000, "\
                        "0.0000000000000000 0.0000000000000000), "\
                        "(1.0000000000000000 1.0000000000000000, "\
                        "3.0000000000000000 1.0000000000000000, "\
                        "3.0000000000000000 3.0000000000000000, "\
                        "1.0000000000000000 3.0000000000000000, "\
                        "1.0000000000000000 1.0000000000000000))"
     self.assertEqual(area.to_wkt(), string)
Пример #12
0
 def areas(self, bbox, boxlist=None, bboxlist_only=False):
     """Find the nearest area. Vect_find_area"""
     boxlist = boxlist if boxlist else BoxList()
     if libvect.Vect_select_areas_by_box(self.c_mapinfo, bbox.c_bbox,
                                         boxlist.c_boxlist):
         if bboxlist_only:
             return boxlist
         else:
             return (Area(v_id=a_id,
                          c_mapinfo=self.c_mapinfo,
                          table=self.table,
                          writable=self.writable) for a_id in boxlist.ids)
     return []
Пример #13
0
    def test_boundaries_1(self):
        """Test boundary access"""
        area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
        boundaries = area.boundaries()
        self.assertEqual(len(boundaries), 4)

        string_list = []
        string_list.append(
            "LINESTRING (0.0000000000000000 0.0000000000000000, 0.0000000000000000 4.0000000000000000)"
        )
        string_list.append(
            "LINESTRING (0.0000000000000000 4.0000000000000000, 4.0000000000000000 4.0000000000000000)"
        )
        string_list.append(
            "LINESTRING (4.0000000000000000 4.0000000000000000, 4.0000000000000000 0.0000000000000000)"
        )
        string_list.append(
            "LINESTRING (4.0000000000000000 0.0000000000000000, 0.0000000000000000 0.0000000000000000)"
        )

        for boundary, i in zip(boundaries, range(4)):
            self.assertEqual(len(boundary.to_wkb()), 41)
            self.assertEqual(boundary.to_wkt(), string_list[i])
Пример #14
0
 def test_bbox(self):
     """Test contain_point method"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     self.assertEqual(str(area.bbox()), "Bbox(4.0, 0.0, 4.0, 0.0)")
Пример #15
0
 def test_to_wkb(self):
     """Test to_wkt method"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     self.assertEqual(len(area.to_wkb()), 225)
Пример #16
0
    def area(self, point):
        """Find the nearest area around a specific point.

        :param point: The point to search
        :type point: grass.pygrass.vector.geometry.Point

        :return: A grass.pygrass.vector.geometry.Area if found or None

        This methods uses libvect.Vect_find_area()

        Examples:

        >>> from grass.pygrass.vector import VectorTopo
        >>> from grass.pygrass.vector.geometry import Point
        >>> test_vect = VectorTopo(test_vector_name)
        >>> test_vect.open('r')

        # Find AREAS
        >>> points = (Point(0.5,0.5), Point(5,1), Point(7,1))
        >>> result = []
        >>> for point in points:
        ...     area = test_vect.find_by_point.area(point)
        ...     result.append(area)
        >>> result
        [Area(1), Area(2), Area(4)]
        >>> for area in result:
        ...     print(area.to_wkt())         #doctest: +NORMALIZE_WHITESPACE
        POLYGON ((0.0000000000000000 0.0000000000000000,
                  0.0000000000000000 4.0000000000000000,
                  0.0000000000000000 4.0000000000000000,
                  4.0000000000000000 4.0000000000000000,
                  4.0000000000000000 4.0000000000000000,
                  4.0000000000000000 0.0000000000000000,
                  4.0000000000000000 0.0000000000000000,
                  0.0000000000000000 0.0000000000000000),
                 (1.0000000000000000 1.0000000000000000,
                  3.0000000000000000 1.0000000000000000,
                  3.0000000000000000 3.0000000000000000,
                  1.0000000000000000 3.0000000000000000,
                  1.0000000000000000 1.0000000000000000))
        POLYGON ((4.0000000000000000 0.0000000000000000,
                  4.0000000000000000 4.0000000000000000,
                  4.0000000000000000 4.0000000000000000,
                  6.0000000000000000 4.0000000000000000,
                  6.0000000000000000 4.0000000000000000,
                  6.0000000000000000 0.0000000000000000,
                  6.0000000000000000 0.0000000000000000,
                  4.0000000000000000 0.0000000000000000))
        POLYGON ((6.0000000000000000 0.0000000000000000,
                  6.0000000000000000 4.0000000000000000,
                  6.0000000000000000 4.0000000000000000,
                  8.0000000000000000 4.0000000000000000,
                  8.0000000000000000 4.0000000000000000,
                  8.0000000000000000 0.0000000000000000,
                  8.0000000000000000 0.0000000000000000,
                  6.0000000000000000 0.0000000000000000))

        >>> test_vect.find_by_point.area(Point(20,20))

        >>> test_vect.close()
        """
        area_id = libvect.Vect_find_area(self.c_mapinfo, point.x, point.y)
        if area_id:
            return Area(
                v_id=area_id,
                c_mapinfo=self.c_mapinfo,
                table=self.table,
                writeable=self.writeable,
            )
Пример #17
0
 def test_to_wkb(self):
     """Test to_wkt method"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     self.assertEqual(len(area.to_wkb()), 225)
Пример #18
0
 def test_bbox(self):
     """Test contain_point method"""
     area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
     self.assertEqual(str(area.bbox()), "Bbox(4.0, 0.0, 4.0, 0.0)")