Skip to content

Instantly share code, notes, and snippets.

@descilla
Last active September 13, 2015 11:46
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 descilla/83ba4d5fe67aa506b3ac to your computer and use it in GitHub Desktop.
Save descilla/83ba4d5fe67aa506b3ac to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -
#Imports:
import urllib, json, time
from pprint import pprint
from collections import OrderedDict
from geopy.geocoders import Nominatim
url = "https://freifunk-muensterland.de/map/data/nodes.json"
response = urllib.urlopen(url)
#response = open('nodes.json')
data = json.loads(response.read())
ruleset = OrderedDict()
# structure of adminlayer hierarchy
ruleset = {
'country' : {
'state' : {
'state_district' : {
'county': {
'town' : {
'suburb' : {}
},
'city' : {
'city_district' : {
'suburb' : {
'neighbourhood' : {},
'administrative' : {}
}
}
},
'city_district' : {
'suburb' : {
'neighbourhood' : {},
'administrative' : {}
}
},
'village' : {}
}
}
}
}
}
# dictionary for admin results
elements = {
'count' : 0
}
# dictionary for zip results
postal = {}
def reverseGeo(lat,lon):
print "do reverse"
geolocator = Nominatim()
location = geolocator.reverse([lat, lon], timeout=20)
if 'address' in location.raw:
return location.raw['address']
else:
return None
# recursively admin area counter
def hieraRec(hierarchie, ruleLayer, location):
hierarchie['count'] += 1
for key, val in location.iteritems():
if key in ruleLayer.keys():
#avoid extra layer in kreisfreien Städten
if key == 'city':
if location['city'] == location['county']:
hierarchie['count'] -= 1
hieraRec(hierarchie, ruleLayer[key], location)
break
#end avoid fix
hierarchie = hiera(hierarchie, location[key])
ruleLayer = ruleLayer[key]
hieraRec(hierarchie, ruleLayer, location)
break
elif key == 'postcode':
plz(val)
# handle dictionary hierarchy
def hiera(hiera, key):
if key in hiera.keys():
return hiera[key]
else:
hiera[key] = {
'count' : 0
}
return hiera[key]
# count nodes in zip code
def plz(key):
if key in postal.keys():
postal[key] += 1
else:
postal[key] = 1
# print administration levels
def hieraPrint(hiera, hieraTab, name):
print hieraTab+name+':',hiera['count']
for key, val in hiera.iteritems():
if isinstance(val, dict):
hieraPrint(val, hieraTab+' ',key)
#print PLZs/ZIP Codes
def plzPrint():
for key, val in postal.iteritems():
print key+':',val
i = 0
j = 0
# loop all nodes from nodes.json
for node in data['nodes']:
if i:
#if i < 2000:
print "-------"
print 'item:',i
knoten = data['nodes'][node]['nodeinfo']
print knoten['hostname']
if 'location' in knoten.keys():
if data['nodes'][node]['flags']['online']:
print "Geo: lat:", knoten['location']['latitude'], ", lon:", knoten['location']['longitude']
#set timeout for not spamming api
j += 1
if j > 2:
time.sleep(1)
j = 0
location = reverseGeo(knoten['location']['latitude'],knoten['location']['longitude'])
if location:
hieraRec(elements, ruleset, location)
#pprint(location)
else:
print "offline"
else:
print "Geo: no geolocation available."
i += 1
#print the results
print '\n\n----------\n----------\n'
print 'Administrative:'
hieraPrint(elements, '', 'Gesamt')
print '\n\n----------\n----------\nPostal:'
plzPrint()
#https://wiki.openstreetmap.org/wiki/Nominatim#Reverse_Geocoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment