Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. data_list_central = []
  2. endlist = []
  3.  
  4.  
  5. def get_data_form_doc():
  6.  
  7.  
  8.  
  9. #Getting zipcode number
  10. def get_postnumber_for_adress(adress):
  11.  
  12.  
  13.  
  14. #list of all zipcode for AArhus
  15. aarhuszipcodes = [8000,8200,8210,8220,8230,8240,8250,8260,8270,8310,8320,8330,8340,8355,8361,8380,8381,8462,8471,8520,8530,8541]
  16. if "Strandvejen" in adress or "Carl Nielsens Vej" in adress or "Møllegade" in adress or "Kirkegårdsvej" in adress:
  17. resul="8000"
  18.  
  19.  
  20. if "Ajstrup Strand" in adress:
  21. resul = "8340"
  22.  
  23.  
  24. if "Fløjstrup strand" in adress:
  25. resul = "8330"
  26.  
  27.  
  28. #for adress in format Moesgård Strand, Strandskovvej 10
  29. if "," in adress:
  30.  
  31.  
  32. adressespilt = adress.split(",")
  33. newsplit = adressespilt[1].split(" ")
  34.  
  35. streetname = newsplit[0]
  36. streetnumber = newsplit[1]
  37.  
  38.  
  39.  
  40. link = "https://dawa.aws.dk/adresser/autocomplete?q=" + streetname + "%20" + streetnumber
  41.  
  42. #for adress in format Ørneredevej 55
  43. if not "," in adress and " " in adress:
  44. adressespilt = adress.split(" ")
  45. streetname = adressespilt[0]
  46. streetnumber = adressespilt[1]
  47. link = "https://dawa.aws.dk/adresser/autocomplete?q=" + streetname + "%20" + streetnumber
  48.  
  49.  
  50. #for adress in format Ørnevænget
  51. if not "," in adress and not " " in adress:
  52. link = "https://dawa.aws.dk/adresser/autocomplete?q=" +adress
  53.  
  54.  
  55.  
  56. #Getting the data from API
  57. import urllib.request, json
  58.  
  59. #Sætter request link til utf_8, så den kan læses af json
  60.  
  61. #Change Ø to Ø
  62. link = link.replace("Ø", "Ø")
  63. link = link.replace("ø", "Ø")
  64.  
  65. link = link.replace("Æ", "æ")
  66. link = link.replace("æ", "æ")
  67.  
  68. link = link.replace("Å", "Å")
  69. link = link.replace("å", "Å")
  70.  
  71.  
  72. with urllib.request.urlopen(link) as url:
  73. data = json.loads(url.read().decode("Latin"))
  74.  
  75.  
  76. numberofresults = len(data)
  77.  
  78.  
  79.  
  80. #if result is 0
  81. if numberofresults == 0:
  82. resul="No results found"
  83.  
  84. #If result is only one. Return postnumber
  85. if numberofresults == 1:
  86. resul = data[0]['adresse']['postnr']
  87.  
  88.  
  89.  
  90.  
  91. if numberofresults > 1:
  92.  
  93.  
  94.  
  95. for i in range(len(data)):
  96. newdata=data[i]['adresse']['postnr']
  97.  
  98.  
  99. for g in range(len(aarhuszipcodes)):
  100.  
  101. if aarhuszipcodes[g] == int(newdata):
  102. resul=newdata
  103.  
  104. return(resul)
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. #Get data from "Toiletter ved parker og skove i Aarhus Kommune"
  114.  
  115.  
  116. #PARK AND WOODS TOTILETS
  117. def get_toilet_data_park_woods():
  118.  
  119. import urllib.request, json
  120.  
  121. with urllib.request.urlopen("https://portal.opendata.dk/dataset/c637921c-6ad8-4c1a-bd8d-9dac8605c2d4/resource/1bd17c11-f20f-4db6-ab2a-a56baaf62980/download/toiletwgs84.json") as url:
  122. data = json.loads(url.read().decode("Latin"))
  123.  
  124.  
  125.  
  126. return(data)
  127.  
  128. #Adds park and woods toilet to list
  129. def add_data_to_list_park_woods_tolist():
  130. newlist = []
  131.  
  132.  
  133. for b in range(len(get_toilet_data_park_woods()['features'])):
  134.  
  135.  
  136. #Getting adresse
  137. adresse = get_toilet_data_park_woods()['features'][b]['properties']['Adresse']
  138.  
  139.  
  140. #Getting postnumber
  141.  
  142. #If adress is Ajstrup Stand"
  143.  
  144.  
  145. zipcode = get_postnumber_for_adress(adresse)
  146.  
  147. newlist = [adresse,zipcode]
  148.  
  149.  
  150. endlist.append(newlist)
  151.  
  152. return(endlist)
  153.  
  154. #CENTAL TOILETS
  155.  
  156. #Get data from "Toiletter i Midtbyen, Aarhus Kommune"
  157. def get_toilet_data_central ():
  158.  
  159. import urllib.request, json
  160. try:
  161. with urllib.request.urlopen("https://portal.opendata.dk/dataset/cf1c6b95-3d1f-4cb7-a67b-e93e2de5299c/resource/065550c2-44b0-41db-94ce-009c47b0ba2b/download/bytoiletterwgs84.json") as url:
  162. data = json.loads(url.read().decode("Latin"))
  163. except:
  164. print("Get data request error for get_toilet_data_central ")
  165.  
  166. finally:
  167. return(data)
  168.  
  169.  
  170.  
  171. #Adds Central toilet to list
  172. def add_data_to_list_central_tolist():
  173.  
  174.  
  175.  
  176. for a in range(len(get_toilet_data_central()['features'])):
  177. #reset list
  178. newlist = []
  179.  
  180.  
  181. #Getting adresse
  182. adresse = get_toilet_data_central()['features'][a]['properties']['Adresse']
  183. #Add adresse to new list
  184.  
  185.  
  186. #Get zipcode
  187. getzipcode = str(get_toilet_data_central()['features'][a]['properties']['Postnr#'])
  188. zipsplit = getzipcode.split(".")
  189. zipcode = zipsplit[0]
  190. #Add zipcode to new list
  191. newlist = [adresse,zipcode]
  192.  
  193.  
  194. #Add newlist to end list
  195. endlist.append(newlist)
  196.  
  197.  
  198. #print count to see progress
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. return(endlist)
  212.  
  213. def get_data_from_list():
  214. add_data_to_list_central_tolist()
  215. add_data_to_list_park_woods_tolist()
  216.  
  217.  
  218. return(endlist)
  219.  
  220.  
  221.  
  222.  
  223. print(get_data_from_list())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement