Guest User

Overpass-API-Skript

a guest
Jul 7th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # https://stackoverflow.com/questions/42753745/how-can-i-parse-geojson-with-python
  4. import pygeoj
  5. import re
  6. import requests
  7. import geojson
  8. import json
  9. import time
  10.  
  11. def hamburg_POIs_count_fast ( keywordString , district ):
  12.     '''
  13.    # keywordString =   "bar or pub or nightclub or cinema or theatre or museum in"
  14.  
  15.    # district      =   "St. Pauli"
  16.  
  17.    # How to use:
  18.        # define keyword to look for, iterate over set of districts in for-loop
  19.        # words will be quoted automatically, if necessary
  20.    '''
  21.     keyword_count = [] # count occurrences
  22.  
  23.     # get district id to use in data
  24.     district_id = 0
  25.     # district = district.replace('.','. ') # St.Pauli / St.Georg --> St. Pauli  / St. Georg
  26.    
  27.     certain_district = "" # geojson-element
  28.     with open("district_ids.geojson", "r") as districts_geojson:
  29.         city_districts = geojson.load(districts_geojson)
  30.        
  31.         for current_district in city_districts['features']:
  32.             if 'name' in current_district.properties:
  33.                 # ERROR IN UMLAUTE!!!
  34.                 searched_name = current_district.properties['name']
  35.  
  36.                 searched_name = searched_name.replace('ü','ü')
  37.                 searched_name = searched_name.replace('ö','ö')
  38.                 searched_name = searched_name.replace('ä','ä')
  39.                 searched_name = searched_name.replace('ß','ß')
  40.  
  41.                 if district == searched_name:
  42.                     certain_district = current_district
  43.                     district_id = certain_district.id
  44.                     # print(certain_district) # ist der richtige Distrikt
  45.                     break
  46.  
  47.         # gehe über alle Distrikte drüber
  48.         # hast du Distrikt mit übereinstimmendem Namen gefunden
  49.         # dann ist ein bestimmter Distrikt = der, den du gefunden hast.
  50.         # Ist OK, breche also ab.
  51.        
  52.         # Lese Ergebnisobjekt aus und mache zu einer Zahl, die für Request benötigt wird.
  53.         district_id = str(district_id).replace('relation/','')
  54.         print(district_id + " " + district)
  55.        
  56.     # construct keys
  57.     keywords = [''] # initialize array
  58.     query_request = ""
  59.  
  60.     keywordString = keywordString.replace(' in ','')
  61.     keywords = keywordString.split(' or ')
  62.     print(keywords)
  63.  
  64.     areacode = 3600000000 # len(areacode) --> 10
  65.     district_id_int = int(district_id)
  66.     areacode += district_id_int # Sag was de willst, aber das ist clever, ne?
  67.     areacode = str(areacode)
  68.  
  69.     # if query len != 6
  70.     if len(keywords) != 6:
  71.         print("Six Keywords. No more, no less.")
  72.         quit()
  73.     #if query len 6
  74.     if len(keywords) == 6:
  75.         query_request = ("[out:json][timeout:25];\r\n"
  76.                         "area("+areacode+")->.searchArea;\r\n"
  77.                         "// Hochschule Mannheim, GDV-Lecture: Project Hamburg.\r\n"
  78.                         "(\r\n"
  79.                         "  node[\"amenity\"=\""+ keywords[0] +"\"](area.searchArea);\r\n"
  80.                         ""
  81.                         "  node[\"amenity\"=\""+ keywords[1] +"\"](area.searchArea);\r\n"
  82.                         ""
  83.                         "  node[\"amenity\"=\""+ keywords[2] +"\"](area.searchArea);\r\n"
  84.                         ""
  85.                         "  node[\"amenity\"=\""+ keywords[3] +"\"](area.searchArea);\r\n"
  86.                         ""
  87.                         "  node[\"amenity\"=\""+ keywords[4] +"\"](area.searchArea);\r\n"
  88.                         ""
  89.                         "  node[\"tourism\"=\""+ keywords[5] +"\"](area.searchArea);\r\n" # Tourism Required for Museum
  90.                         ");\r\n"
  91.                         "// print results\r\n"
  92.                         "out body;\r\n"
  93.                         ">;\r\n"
  94.                         "out skel qt;\r\n")
  95. ###
  96.     time.sleep(0.2)
  97.     payload = dict(data=query_request) # requires appropiate payload
  98.     time.sleep(12) # HTTP 429 Avoidance Clause. --> https://overpass-api.de/api/status
  99.     # maximum required sleep on its own:        90 sec
  100.     # maximum required sleep in total process:  75 sec
  101.     # absolute minimum required sleep for 429:  10 sec
  102.     # "2" sec worked fine for a long period.
  103.  
  104.     r = requests.post('https://overpass-api.de/api/interpreter', data=payload)
  105.  
  106.     time.sleep(0.25) #Timeouthandling
  107.  
  108.     print(r.status_code)
  109.  
  110.     if r.status_code != 200:
  111.         print("ERROR IN HTTP CODE: " + str(r.status_code) + " at " + district)
  112.         print(r.headers)
  113.         print(r.text)
  114.         quit() # Quit on Error. We don't want incomplete databases.
  115.  
  116.     # print(r.text)  
  117.     # es werden Elemente ausgegeben. # Jetzt müssen die nur zusammengezählt werden.
  118.  
  119.     # (POI_counts[0]) # bars
  120.     # (POI_counts[1]) # nightclub (disco)
  121.     # (POI_counts[2]) # cinema
  122.     # (POI_counts[3]) # theatre
  123.     # (POI_counts[4]) # museum
  124.  
  125.     # bar or pub or nightclub or cinema or theatre or museum in
  126.    
  127.     keyword_text = r.text
  128.     # Calculate Occurences
  129.     bars_and_pubs = keyword_text.count('\"amenity\": \"pub\",') + keyword_text.count('\"amenity\": \"bar\",')
  130.     # Append Keyword_Count (Occurences) Array
  131.     keyword_count.append(bars_and_pubs)
  132.     keyword_count.append(keyword_text.count('\"amenity\": \"nightclub\",'))
  133.     keyword_count.append(keyword_text.count('\"amenity\": \"cinema\",'))
  134.     keyword_count.append(keyword_text.count('\"amenity\": \"theatre\",'))
  135.     keyword_count.append(keyword_text.count('\"tourism\": \"museum\",')) # Tourism Required for Museum, whyever
  136.    
  137.     printcount = keyword_count
  138.     print("Keywords appear:")
  139.     print("Bars/Pubs: " +str(printcount[0]) + " times in "+district)
  140.     print("Nightclub: " +str(printcount[1]) + " times in "+district)
  141.     print("Cinema: " +str(printcount[2]) + " times in "+district)
  142.     print("Theatre: " +str(printcount[3]) + " times in "+district)
  143.     print("Museum: " +str(printcount[4]) + " times in "+district)
  144.  
  145.     # requests.get('https://overpass-api.de/api/kill_my_queries')
  146.     return keyword_count
  147.  
  148. #######################
  149.  
  150. #POST:
  151. #
  152. #   >>> payload = dict(key1='value1', key2='value2')
  153. #   >>> r = requests.post('https://httpbin.org/post', data=payload)
  154. #   >>> print(r.text)
  155. #   {
  156. #     ...
  157. #     "form": {
  158. #       "key2": "value2",
  159. #       "key1": "value1"
  160. #     },
  161. #     ...
  162. #   }
  163.  
  164. # Relevant Info:
  165. # "features"{
  166. #  "properties": {
  167.         # "@id": "relation/28970",
  168.         # "admin_level": "10",
  169.         # "name": "Finkenwerder",
  170.  
  171.     # Einbauen: Overpass-Turbo -- Bars, Diskotheken, etc. (mehrere Keywords)
  172.     # OverPass-Turbo:
  173.     # - Wizard: admin_level=10
  174.     # - Wizard: pubs or bars in Hamburg / "St. Pauli" etc.
  175.     # - "@id": "relation/28931" --> 36000xxxxx / 3600028931 --> Python-Requests an Server/API stellen
  176.     # - Geo-Koordinaten ggf. auch aus Overpass ziehen oder GeoDatenJSON aus _polygon-Datei einbinden
  177.     # s.o.
  178.  
  179. ''''
  180. # Node representing POI
  181. # Elements --> type --> "node"
  182.  
  183. 200
  184. {
  185.  "version": 0.6,
  186.  "generator": "Overpass API 0.7.55.7 8b86ff77",
  187.  "osm3s": {
  188.    "timestamp_osm_base": "2019-06-06T12:38:02Z",
  189.    "timestamp_areas_base": "2019-06-06T12:19:03Z",
  190.    "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
  191.  },
  192.  "elements": [
  193.  
  194.    {
  195.        "type": "node",
  196.        "id": 724276519,
  197.        "tags": {
  198.            "amenity": "pub",
  199.            "name": "Roxie"
  200.        }
  201.    },
  202.    {
  203.        "type": "node",
  204.        "id": 1926030069,
  205.        "tags": {
  206.            "amenity": "pub",
  207.            "name": "Heimbar"
  208.        }
  209.    },
  210.    {
  211.    "type": "way",
  212.    "id": 61414966,
  213.    "nodes": [ n ],
  214.    "tags": {
  215.        "amenity": "pub",
  216.        "name": "Roxie",    # Different ID, Same Location.
  217.    }
  218.    },
  219.  ]
  220. }
  221. '''
Advertisement
Add Comment
Please, Sign In to add comment