Last active
December 9, 2023 19:48
-
-
Save samilkorkmaz/8e78b5d7d132cc0864a032dffc723b87 to your computer and use it in GitHub Desktop.
Pan/zoom shape file with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#You can download shape files (.sh and .shx) from https://www.naturalearthdata.com/downloads/ | |
#Put .shx files into the same folder as .sh file | |
#Şamil Korkmaz, December 9th 2023 | |
import geopandas as gpd | |
import matplotlib.pyplot as plt | |
def plot_shapefile(shapefile_path1, shapefile_path2, shapefile_path3): | |
# Load shape files | |
gdf1 = gpd.read_file(shapefile_path1) | |
gdf2 = gpd.read_file(shapefile_path2) | |
gdf3 = gpd.read_file(shapefile_path3) | |
# Create a plot | |
fig, ax = plt.subplots() | |
ax.set_facecolor('#000080') # Set the background color to blue | |
gdf1.plot(ax=ax, alpha=1, color='#D2B48C') # Adjust alpha for transparency if needed | |
gdf2.plot(ax=ax, alpha=1) | |
gdf3.plot(ax=ax, alpha=0.05, edgecolor='k') # Change edgecolor as needed | |
ax.set_xlim([20, 50]) | |
ax.set_ylim([30, 50]) | |
# Add a red dot at the specified coordinates | |
ax.plot(33, 39, marker='o', color='red', markersize=5) | |
# State for the pan functionality | |
state = {'press': None, 'x0': None, 'y0': None} | |
# Function to handle mouse wheel zoom | |
def zoom(event): | |
if event.button == 'up': | |
scale_factor = 1/1.5 | |
elif event.button == 'down': | |
scale_factor = 1.5 | |
else: | |
return | |
xlim = ax.get_xlim() | |
ylim = ax.get_ylim() | |
xdata = event.xdata | |
ydata = event.ydata | |
if xdata is None or ydata is None: # Event out of plot area | |
return | |
ax.set_xlim([xdata - (xdata-xlim[0]) * scale_factor, xdata + (xlim[1]-xdata) * scale_factor]) | |
ax.set_ylim([ydata - (ydata-ylim[0]) * scale_factor, ydata + (ylim[1]-ydata) * scale_factor]) | |
ax.figure.canvas.draw_idle() | |
# Mouse press event handler | |
def on_press(event): | |
if event.button == 3: # Right mouse button | |
state['press'] = True | |
state['x0'], state['y0'] = event.xdata, event.ydata | |
# Mouse release event handler | |
def on_release(event): | |
state['press'] = None | |
state['x0'] = None | |
state['y0'] = None | |
# Mouse motion event handler | |
def on_motion(event): | |
if state['press'] is None or event.xdata is None or event.ydata is None: | |
return | |
dx = event.xdata - state['x0'] | |
dy = event.ydata - state['y0'] | |
xlim, ylim = ax.get_xlim(), ax.get_ylim() | |
ax.set_xlim(xlim[0] - dx, xlim[1] - dx) | |
ax.set_ylim(ylim[0] - dy, ylim[1] - dy) | |
fig.canvas.draw_idle() | |
# Connect the event handlers | |
fig.canvas.mpl_connect('scroll_event', zoom) | |
fig.canvas.mpl_connect('button_press_event', on_press) | |
fig.canvas.mpl_connect('button_release_event', on_release) | |
fig.canvas.mpl_connect('motion_notify_event', on_motion) | |
# Show the plot | |
plt.show() | |
plot_shapefile('ne_10m_admin_0_countries.shp', 'ne_10m_lakes.shp', 'ne_10m_graticules_5.shp') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment