def test_geometries(tmpdir): test_file = '{}/test_file.shp'.format(tmpdir) _create_test_input_files(test_file) df = GeoDataFrame.from_file(test_file) p = (ggplot(df) + aes(fill='geometry.bounds.miny') + geom_map() + geom_map(draw='Point', size=4) + geom_map(draw='LineString', size=2) + labs(fill='miny')) assert p + _theme == 'geometries'
def test_geometries(tmpdir): test_file = '{}/test_file.shp'.format(tmpdir) _create_test_input_files(test_file) df = GeoDataFrame.from_file(test_file) p = (ggplot(df) + aes(fill='geometry.bounds.miny') + geom_map() + geom_map(draw='Point', size=4) + geom_map(draw='LineString', size=2) + labs(fill='miny') ) assert p + _theme == 'geometries'
def test_geometries(tmpdir): point_file = '{}/test_file_point.shp'.format(tmpdir) polygon_file = '{}/test_file_polygon.shp'.format(tmpdir) polyline_file = '{}/test_file_polyline.shp'.format(tmpdir) _point_file(point_file) _polygon_file(polygon_file) _polyline_file(polyline_file) df_point = GeoDataFrame.from_file(point_file) df_polygon = GeoDataFrame.from_file(polygon_file) df_polyline = GeoDataFrame.from_file(polyline_file) p = (ggplot() + aes(fill='geometry.bounds.miny') + geom_map(df_polygon) + geom_map(df_point, size=4) + geom_map(df_polyline, size=2) + labs(fill='miny')) assert p + _theme == 'geometries'
def test_facet_wrap(tmpdir): polygon_file = '{}/test_file_polygon.shp'.format(tmpdir) _polygon_file(polygon_file) df_polygon = GeoDataFrame.from_file(polygon_file) df_polygon['shape'] = ['rectangle', 'triangle'] p = (ggplot() + aes(fill='geometry.bounds.miny') + geom_map(df_polygon) + facet_wrap('shape') + labs(fill='miny')) assert p + _theme == 'facet_wrap'
def plot_restaurants_per_neighborhood(filepath, restaurant_data_file, pittsburgh_shapefile): mexican_restaurants = pd.read_csv(filepath + restaurant_data_file) gdf = gpd.GeoDataFrame( mexican_restaurants, geometry=gpd.points_from_xy(mexican_restaurants.longitude, mexican_restaurants.latitude), ) restaurant_locations = gdf.filter(items=["geometry"]) # import Pittsburgh neighborhood shapefile neighborhood_polygons = gpd.read_file(pittsburgh_shapefile).filter( items=["hood", "hood_no", "geometry"]) # spatial join to figure out which neighborhood each restaurant is in restaurants_in_polys = gpd.sjoin(restaurant_locations, neighborhood_polygons, how="inner", op="intersects") restaurants_counted = restaurants_in_polys.groupby( "hood_no").count().reset_index() restaurants_in_hoods = restaurants_counted.filter( items=["hood_no", "hood"]) restaurants_in_hoods.rename(columns={"hood": "num_restaurants"}, inplace=True) restaurants_per_shape = gpd.GeoDataFrame( pd.merge(neighborhood_polygons, restaurants_in_hoods, how="left")) restaurant_map = (p.ggplot(restaurants_per_shape) + p.geom_map(p.aes(fill="num_restaurants")) + p.scale_colour_gradient(low="white", high="black") + p.theme( panel_background=p.element_rect(fill="white"), axis_text_x=p.element_blank(), axis_text_y=p.element_blank(), axis_ticks_major_x=p.element_blank(), axis_ticks_major_y=p.element_blank(), )) + p.scale_fill_gradient( low="#efefef", high="#073763", name="# Restaurants") restaurant_map.save("restaurant_map.png")