#!/usr/bin/env python # coding: utf-8 # # Inset Map of Kotlin Island # # Kotlin island is situated in Gulf of Finland and is one of districts of the city of Saint Petersburg in Russia. # # This example shows how *Lets-Plot* *GeoPandas* integration can help to build an inset map of Kotlin island. # [The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL)](https://www.openstreetmap.org/copyright). # In[1]: import geopandas as gpd from shapely.geometry import box from lets_plot import * # In[2]: LetsPlot.setup_html() # ### Load boundaries of St.-Petersburg districts. # In[3]: spb_gdf = gpd.read_file("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/spb_districts.geojson") # ### Create a map showing all districts of St.-Petersburg. # # This map will become the **inset map**. The red rectangle indicates the bounds of the future **main map**. # In[4]: kotlin_bbox = [29.63, 59.965, 29.815, 60.035] kotlin_rect = dict(zip(['xmin', 'ymin', 'xmax', 'ymax'], kotlin_bbox)) # In[5]: spb_plot = ggplot() + \ geom_rect(map=gpd.GeoDataFrame(geometry=[box(*spb_gdf.geometry.total_bounds)]), \ color='black', fill='white') + \ geom_polygon(map=spb_gdf, color='#a1d99b', fill='#f7fcf5') + \ geom_rect(**kotlin_rect, color='red', alpha=0) + \ geom_text(label='Saint Petersburg', x=30.334445, y=59.934294, color='black', size=6) + \ theme_void() spb_plot # ### Create the main map with only Kotlin island on it. # # We use `xlim` and `ylim` parameters of the coordinate system to crop the entire map containing all districts of St.-Petersburg. # # In[6]: # GeoDataFrame containing names and coordinates of some tourist attractions to show on the main map. places_gdf = gpd.read_file("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/kotlin_places.geojson") # In[7]: # Cut-out the Kotlin area and add layes with text and points of interest. kotlin_plot = ggplot() + \ geom_rect(**kotlin_rect, fill='#aadaff', alpha=0.2) + \ geom_polygon(map=spb_gdf, color='#31a354', fill='#e5f5e0') + \ geom_point(aes(color='type', shape='type'), data=places_gdf, size=5) + \ geom_text(aes(label='name'), data=places_gdf, hjust='right', position=position_nudge(x=-.002)) + \ geom_text(label='Kotlin Isl.', x=29.725, y=60.011, color='#31a354', size=13, fontface='italic') + \ geom_text(label='Gulf of Finland', x=29.665, y=60.002, color='#578bcc', size=11, fontface='italic') + \ coord_cartesian(xlim=kotlin_bbox[0::2], ylim=kotlin_bbox[1::2]) + \ ggtitle('Tourist attractions on Kotlin island') + \ theme_void() + theme(legend_position=[.15, .2]) kotlin_plot # ### Finally, use *GGBunch* to show these two maps together. # In[8]: bunch = GGBunch() bunch.add_plot(kotlin_plot, 0, 0, 800, 600) bunch.add_plot(spb_plot, 600, 25, 200, 150) bunch.show()