Exemplo n.º 1
0
    def test_polygon_marker(self):
        """Test polygon additions."""

        self.m = folium.Map(location=[45.60, -122.8])
        polygon_templ = self.env.get_template('polygon.js')

        # Single PolygonMarker.
        locations = [[35.6636, 139.7634], [35.6629, 139.7664],
                     [35.6663, 139.7706], [35.6725, 139.7632],
                     [35.6728, 139.7627], [35.6720, 139.7606],
                     [35.6682, 139.7588], [35.6663, 139.7627]]
        self.m.add_child(PolygonMarker(locations=locations, popup='Hi'))
        marker = list(self.m._children.values())[-1]
        polygon_1 = polygon_templ.render({
            'PolygonMarker': marker.get_name(),
            'location': locations,
            'color': 'black',
            'fill_color': 'black',
            'fill_opacity': 0.6,
            'weight': 1
        })
        assert (''.join(polygon_1.split())[:-1]
                in ''.join(self.m.get_root().render().split()))

        # Second PolygonMarker.
        locations = [[35.5636, 138.7634], [35.5629, 138.7664],
                     [35.5663, 138.7706], [35.5725, 138.7632],
                     [35.5728, 138.7627], [35.5720, 138.7606],
                     [35.5682, 138.7588], [35.5663, 138.7627]]
        self.m.add_child(
            PolygonMarker(locations=locations,
                          color='red',
                          fill_color='red',
                          fill_opacity=0.7,
                          weight=3,
                          popup='Hi'))
        marker = list(self.m._children.values())[-1]
        polygon_2 = polygon_templ.render({
            'PolygonMarker': marker.get_name(),
            'location': locations,
            'color': 'red',
            'fill_color': 'red',
            'fill_opacity': 0.7,
            'weight': 3
        })
        assert (''.join(polygon_2.split())[:-1]
                in ''.join(self.m.get_root().render().split()))

        bounds = self.m.get_bounds()
        assert bounds == [[[35.5636, 138.7634], [35.5629, 138.7664]],
                          [[35.6636, 139.7634], [35.6629, 139.7664]]], bounds
Exemplo n.º 2
0
    def add_contour(self, contour='SC'):
        '''
        Add the contour information on the canvass
        Args:
            contour: allow three types of information:
                1.Statenames: like SC, NC or north carolina
                2.shapely polygon type
                3.list of coords
        '''
        print('-' * 20)
        print('allow two input types: 1. eg. DC, SC 2. a list of coordinates')

        if isinstance(contour, str):
            polygon = get_state_contours(contour)[-1]
            make_coords = True

        elif isinstance(contour, shapely.geometry.polygon.Polygon):
            polygon = contour
            make_coords = True

        elif isinstance(contour, list):
            make_coords = False

        else:
            raise TypeError('only support str, list and polygon type')

        if make_coords:
            longitudes, latitudes = polygon.exterior.coords.xy
            list_of_coords = list(zip(latitudes, longitudes))

        PolygonMarker(list_of_coords, color='blue', fill_opacity=0.2,
                      weight=1).add_to(self.canvass)

        return self