Advertisement
Guest User

Executable Python-File (Hamburg)

a guest
Jul 7th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # https://github.com/brandonxiang/geojson-python-utils
  4. # https://gis.stackexchange.com/questions/73768/converting-geojson-to-python-objects
  5. import pygeoj
  6. import re
  7. from geopy.geocoders import Nominatim
  8. from geopy import distance
  9. import csv
  10. import time
  11. import overpass_api as opi
  12.  
  13. geolocator = Nominatim(user_agent="KulturInHambung")
  14.  
  15. complete_start_time = time.time()
  16.  
  17. hamburg_mitte_geocode = geolocator.geocode("Hamburg")
  18. hamburg_mitte = hamburg_mitte_geocode.point
  19.  
  20. hamburg_coords = pygeoj.load("hamburg_city_districts.geojson")
  21.  
  22. with open('skorna_datasafe.csv', 'w', encoding='utf-8') as csv_file: # Datei anlegen
  23.         writer = csv.writer(csv_file,lineterminator='\n')
  24.         writer.writerow(["Stadtteil","Stadtbezirk","Stadt","Land","Entfernung","Bars","Diskotheken","Kinos","Theater","Museeen"])
  25.  
  26. time.sleep(0.1) # Timeouthandling
  27.  
  28. for feature in hamburg_coords:
  29.     # Debug-Time-Measurement
  30.     startzeit = time.time() # returns time in seconds
  31.  
  32.     # ERZEUGT STADTTEILNAMEN
  33.     property_stadtteil = str(feature.properties).split(': ')
  34.     stadtteil = re.sub('[:\'}]', '', property_stadtteil[1])
  35.     stadtteil = stadtteil.replace('ü','ü')
  36.     stadtteil = stadtteil.replace('ö','ö')
  37.     stadtteil = stadtteil.replace('ä','ä')
  38.     stadtteil = stadtteil.replace('ß','ß')
  39.     stadtteil = stadtteil.replace('.','. ') # St.Pauli / St.Georg --> St. Pauli  / St. Georg
  40.     # print(stadtteil)
  41.  
  42.     # ERZEUGT STADTTEIL, STADTBEZIRK, STADT, LAND
  43.     koord = geolocator.geocode(stadtteil + ", Hamburg")
  44.     time.sleep(0.1) #Timeouthandling
  45.     koord_plz = koord
  46.     koord_plz = re.sub('[0-9]','',str(koord_plz))
  47.     koord_str = koord_plz.replace(' ,','')
  48.     # You Either Code Properly, Or You Code Long Enough To See Yourself Become Hated By Everyone On Your Team
  49.     print(koord_str) # print(koord)
  50.  
  51.     #Timeouthandling
  52.     time.sleep(0.1)
  53.     # DISTANZ VON GEOCODE ZU GEOCODE BERECHNEN
  54.     entfernung = distance.distance(koord.point, hamburg_mitte).kilometers # Zahl im Bereich 0-20 sind Kilometer
  55.     entfernung = (int(entfernung*100)/float(100))    # Runden.
  56.     # print(entfernung)
  57.  
  58.     # DATEN VORBEREITEN FÜR CSV
  59.     skorna_array = koord_str.split(', ')
  60.     skorna_array.append(entfernung)
  61.     print(skorna_array) # Super krasses Array mit allen wichtigen Informationen, yo.
  62.  
  63.     if len(skorna_array) > 5:
  64.             print("Removed: "+skorna_array[2])
  65.             del skorna_array[2]
  66.             print(skorna_array)
  67.  
  68.     # ERZEUGT POINTS OF INTERESTS
  69.     print("\r\n##### NOW PROCESSING: " + stadtteil + " #####\r\n")
  70.     # count them up in hamburg_POIs_count, assign them integers
  71.     '''
  72.    ### Laufzeit 7.5 bis 15 Stunden! Nicht optimal!
  73.    ### _fast reduziert das zu 30 Minuten. Maximal 'ne Stunde.
  74.    '''
  75.  
  76.     # Reduzierung Laufzeit auf 2-4 Stunden.
  77.     # Bitte _fast benutzen.
  78.     time.sleep(1)
  79.     POI_counts = opi.hamburg_POIs_count_fast("bar or pub or nightclub or cinema or theatre or museum in ", stadtteil)
  80.     time.sleep(1)
  81.     skorna_array.append(POI_counts[0]) # bars
  82.     skorna_array.append(POI_counts[1]) # disco
  83.     skorna_array.append(POI_counts[2]) # cinema
  84.     skorna_array.append(POI_counts[3]) # theatre
  85.     skorna_array.append(POI_counts[4]) # museum
  86.  
  87.     # Einbauen: Overpass-Turbo -- Bars, Diskotheken, etc. (mehrere Keywords)
  88.     # OverPass-Turbo:
  89.     # - Wizard: admin_level=10
  90.     # - Wizard: pubs or bars in Hamburg / "St. Pauli" etc.
  91.     # - "@id": "relation/28931" --> 36000xxxxx / 3600028931 --> Python-Requests an Server/API stellen
  92.  
  93.     # CSV SCHREIBEN
  94.     with open('skorna_datasafe.csv', 'a', encoding='utf-8') as csv_file: # append
  95.         writer = csv.writer(csv_file,lineterminator='\n',delimiter=",")
  96.         writer.writerow(skorna_array)
  97.  
  98.     # Zeitinfo
  99.     endzeit = time.time()
  100.     process_time = endzeit - startzeit
  101.     process_time = (int(process_time*100)/float(100))
  102.     print("\r\n##### FINISHED AFTER: "+str(process_time)+" SECONDS #####")
  103.     print("##### MOVING TO NEXT CITY DISTRICT #####\r\n")
  104.  
  105.     time.sleep(0.25) # Timeouthandling
  106.  
  107. # For-Loop-End
  108.  
  109. complete_end_time = time.time()
  110. complete_time_taken = complete_end_time - complete_start_time
  111. complete_time_taken_min = (complete_time_taken/60)
  112. complete_time_taken_min_round = (int(complete_time_taken_min*100)/float(100))
  113. complete_time_taken_h = (complete_time_taken_min/60)
  114. complete_time_taken_h_round = (int(complete_time_taken_h*100)/float(100))
  115. print("\r\n############")
  116. print("COMPLETED AFTER: "+str(complete_time_taken_h_round)+" HOURS. ("+str(complete_time_taken_min_round)+" MINUTES.)")
  117. print("############")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement