Exemplo n.º 1
0
def test_footprints():
    import json
    import pytest
    from shapely.geometry import Polygon

    # download footprints and plot them
    gdf = ox.footprints_from_place(place='Emeryville, California, USA')
    gdf = ox.footprints_from_polygon(
        Polygon([(17.574, -4.145), (17.575, -4.144), (17.576, -4.145)]))
    gdf = ox.footprints_from_address(
        address='600 Montgomery St, San Francisco, California, USA',
        distance=300)
    fig, ax = ox.plot_footprints(gdf)

    # new_river_head.json contains a relation with 1 outer closed way and 2 inner closed ways
    # inner way 665593284 is directly tagged as a building and should create its own polygon
    with open("tests/input_data/new_river_head.json", "r") as read_file:
        new_river_head_responses = [json.load(read_file)]
    new_river_head_gdf = ox.create_footprints_gdf(
        responses=new_river_head_responses)
    assert 665593284 in new_river_head_gdf.index
    assert new_river_head_gdf.loc[9246394]['geometry'].type == 'Polygon'
    assert len(new_river_head_gdf.loc[9246394, 'geometry'].interiors) == 2

    # clapham_common.json contains a relation with 5 outer rings and 1 inner ring. One of the outer rings is a chain of open ways
    with open("tests/input_data/clapham_common.json", "r") as read_file:
        clapham_common_responses = [json.load(read_file)]
    clapham_common_gdf = ox.create_footprints_gdf(
        footprint_type='leisure', responses=clapham_common_responses)
    assert clapham_common_gdf.loc[1290065]['geometry'].type == 'MultiPolygon'

    # relation_no_outer.json contains a relation with 0 outer rings and 1 inner ring
    with open("tests/input_data/relation_no_outer.json", "r") as read_file:
        relation_no_outer_responses = [json.load(read_file)]
    ox.create_footprints_gdf(responses=relation_no_outer_responses)

    # inner_chain.json contains a relation with 1 outer rings and several inner rings one of which is a chain of open ways
    with open("tests/input_data/inner_chain.json", "r") as read_file:
        inner_chain_responses = [json.load(read_file)]
    ox.create_footprints_gdf(responses=inner_chain_responses)

    # mis_tagged_bus_route.json contains a relation with out 'inner' or 'inner' rings
    with open("tests/input_data/mis_tagged_bus_route.json", "r") as read_file:
        mis_tagged_bus_route_responses = [json.load(read_file)]
    ox.create_footprints_gdf(responses=mis_tagged_bus_route_responses)

    # test plotting multipolygon
    fig, ax = ox.plot_footprints(clapham_common_gdf)

    # should raise an exception
    # polygon or -north, south, east, west- should be provided
    with pytest.raises(ValueError):
        ox.create_footprints_gdf(polygon=None,
                                 north=None,
                                 south=None,
                                 east=None,
                                 west=None)

    gdf = ox.footprints_from_place(place='kusatsu, shiga, japan',
                                   which_result=2)
Exemplo n.º 2
0
    def draw_map(self, padx=0.001, pady=0.001):
        """
        Generate a 2d matplotlib plot with buildings and the drone flight path

        Args:
            padx: the number of meters to add either side of the drone data.
                TODO: Actually make this meters, not latitude
            pady: the number of meters to add top and bottom of the drone data.
                TODO: Actually make this meters, not longitude

        Returns:
            fig: A matplotlib figure containing the drone map
        """
        north = max(self.drone_data["OSD.latitude"]) + pady
        south = min(self.drone_data["OSD.latitude"]) - pady
        east = max(self.drone_data["OSD.longitude"]) + padx
        west = min(self.drone_data["OSD.longitude"]) - padx

        # Fetch the OSM data
        place = "Åkersberga, Sweden"
        graph = ox.graph_from_bbox(north, south, east, west)
        area = ox.gdf_from_place(place)
        buildings = ox.create_footprints_gdf(
            "shapely Polygon", north, south, east, west)
        _, edges = ox.graph_to_gdfs(graph)

        # Plot the different map features
        self.fig, self.ax = plt.subplots(figsize=(10, 7))
        area.plot(ax=self.ax, facecolor='black')
        edges.plot(ax=self.ax, linewidth=1, edgecolor='#BC8F8F')
        buildings.plot(ax=self.ax, facecolor='khaki', alpha=0.7)
        try:
            leisure = ox.create_footprints_gdf(
                "shapely Polygon", north, south, east, west, "leisure")
            parks = leisure[leisure["leisure"].isin(["park","playground"])]
            parks.plot(ax=self.ax, facecolor='limegreen')
        except:
            print("No parks within bbox")

        self.ax.axis("equal")
        self.ax.set_xlim(west, east)
        self.xlim_diff = east - west
        self.ax.set_ylim(north, south)
        plt.tight_layout()

        # Event handlers
        def on_xlims_change(axes):
            # Update the reference scale for arrow drawing
            # TODO: Find some way to update the map on zoom action, rather than
            #       having to wait for the slider to be updated
            x, xmax = self.ax.get_xlim()
            self.xlim_diff = xmax - x

        # Set callback, to update arrow size when screen is zoomed
        self.ax.callbacks.connect('xlim_changed', on_xlims_change)