Created
May 27, 2021 15:30
-
-
Save Carreau/a6f635fae31239011752af1bcfb84730 to your computer and use it in GitHub Desktop.
PyShS demo Script
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
import sys | |
import geocoder | |
import matplotlib | |
matplotlib.use('agg') | |
import matplotlib.pyplot as plt | |
import argparse | |
import wikipedia | |
import wptools | |
import flask | |
import io | |
from flask import Flask, Response | |
app = Flask(__name__) | |
from functools import lru_cache | |
import geopandas as gpd | |
europe_file = "../Formation-Unil/Data/europe/european-union-countries.shp" | |
europe = gpd.read_file(europe_file) | |
suisse_file = "../Formation-Unil/Data/suisse/CHE_adm1.shp" | |
suisse = gpd.read_file(suisse_file) | |
suisse_p = suisse.dissolve(by="NAME_0",aggfunc='first') | |
europe_mod = europe[["name","geometry"]].set_index("name") | |
europe_mod = europe_mod.append(suisse_p[europe_mod.columns]) | |
@lru_cache | |
def _fig(location, radius): | |
print('...',location, radius) | |
req = geocoder.osm(location) | |
fig,ax = plt.subplots(figsize=(10,10)) | |
europe_mod.plot(ax=ax, linewidth=1, edgecolor='white', color='C0') | |
long, lat = req.latlng | |
ax.plot(lat, long, color='C1', marker="o") | |
ax.set_xlim(lat-radius,lat+radius) | |
ax.set_ylim(long-radius,long+radius) | |
ax.axis(False) | |
infobox = wptools.page(location).get_parse().data['infobox'] | |
elevation = infobox.get('elevation', None) | |
area = infobox.get('area') | |
ax.set_title(f'Carte de {location} | {elevation}m | {area}$km^2$') | |
return fig | |
def main(location, radius): | |
fig = _fig(location, radius) | |
fig.savefig(f"{location}.png") | |
nom_de_fichier = f"{location}.png" | |
print(f"saved as {nom_de_fichier}") | |
@app.route('/<path:location>.png') | |
def handle(location): | |
radius = 5 | |
buf = io.BytesIO() | |
fig = _fig(location, radius) | |
fig.savefig(buf, format='png') | |
buf.seek(0) | |
return Response(buf.read(), mimetype='image/png', headers={"Content-Disposition": "inline"}) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Save a PNG file of given location') | |
parser.add_argument('location', metavar='loc', type=str, nargs=1, | |
help='where') | |
parser.add_argument('--radius', dest='radius', | |
type=int, default=5, | |
help='Radius around location to draw') | |
parser.add_argument('--server', dest='server', action='store_true', | |
help='Start a falsk server') | |
args = parser.parse_args() | |
if args.server: | |
app.run() | |
else: | |
print(args.location) | |
print(args.radius) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment