Skip to content

Instantly share code, notes, and snippets.

@villares
Created August 25, 2023 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save villares/a3a6f628b02e439c9a0822febe3024f3 to your computer and use it in GitHub Desktop.
Save villares/a3a6f628b02e439c9a0822febe3024f3 to your computer and use it in GitHub Desktop.
Experimentos com osmnx
import pickle
from pathlib import Path
import osmnx as ox
from download_from_gdrive import download_from_gdrive
# configure the place, network type, trip times, and travel speed
place = {"city": "São Paulo", "country": "Brazil"}
network_type = "walk"
trip_times = [5, 10, 15] # in minutes
travel_speed = 3.5 # walking speed in km/hour
pickle_file = 'SP_walk_graph.data'
google_drive_file_id = '1PvoGpXPnCJUGjtXfSHsb0EKxN1hqRi9G'
# # download the street network
# G = ox.graph_from_place(place, network_type=network_type)
#
# # SALVAR O GRAFO NUM PICKLE FILE
# import pickle
# with open(pickle_file, 'wb') as f:
# pickle.dump(G, f)
if Path(pickle_file).is_file():
print('Using local file found.')
else:
print('downloading from Google Drive...')
download_from_gdrive(google_drive_file_id, pickle_file)
print('downloading done.')
with open(pickle_file, 'rb') as f:
graph = pickle.load(f)
#ox.plot_graph(graph)
#
#
gdf_edges = ox.graph_to_gdfs(graph, nodes=False)
ipiranga = gdf_edges[gdf_edges['name'] == 'Avenida Ipiranga']
for linestring in ipiranga.geometry:
print(linestring)
# https://stackoverflow.com/a/39225272
import sys
import requests
def download_from_gdrive(id, destination):
URL = "https://docs.google.com/uc?export=download&confirm=1"
session = requests.Session()
response = session.get(URL, params={"id": id}, stream=True)
token = get_confirm_token(response)
if token:
params = {"id": id, "confirm": token}
response = session.get(URL, params=params, stream=True)
save_response_content(response, destination)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith("download_warning"):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
def main():
if len(sys.argv) >= 3:
file_id = sys.argv[1]
destination = sys.argv[2]
else:
file_id = "TAKE_ID_FROM_SHAREABLE_LINK"
destination = "DESTINATION_FILE_ON_YOUR_DISK"
print(f"dowload {file_id} to {destination}")
download_from_gdrive(file_id, destination)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment