osm_analyzer/analyzer_v2.py

155 lines
5.9 KiB
Python
Executable File

import json
from distutils.util import strtobool
import geopy.distance
import time
import configparser
import csv
def filter_buildings(overpass_json):
# Functie voor het filteren van alle beschikbare elementen naar gebouwen
buildings = []
for i in overpass_json['elements']:
if 'building' in i['tags']:
building_corner_ids = []
for j in i['nodes']:
building_corner_ids.append(j)
buildings.append(building_corner_ids)
return buildings
def filter_building_types(overpass_json):
# Functie voor het filteren van alle beschikbare elementen naar gebouwen
building_types = []
for i in overpass_json['elements']:
if 'building' in i['tags']:
building_types.append(i['tags']['building'])
return building_types
def filter_nodes(overpass_json):
# Functie voor het filteren van alle beschikbare elementen naar nodes
nodes = {}
for i in overpass_json['elements']:
if i['type'] == 'node':
nodes[i["id"]] = (i['lon'], i['lat'])
return nodes
def get_building_coords(nodes_list, requested_building, buildingnr):
# Functie voor het ophalen van de coordinaten van de gefilterde gebouwen
building_coords = {}
coords = []
for i in requested_building:
if i in nodes_list:
coords.append(nodes_list[i])
building_coords[buildingnr] = coords
return building_coords
def sq_filter(unfiltered_building_coords, building_id):
# Functie voor het filteren van een gecompliceerd gebouw naar een simpel vierkant.
new_coordinates = {}
coords_list = []
lon_list = []
lat_list = []
for i in unfiltered_building_coords:
lon_list.append(i[0])
lat_list.append(i[1])
max_lon = max(lon_list)
min_lon = min(lon_list)
max_lat = max(lat_list)
min_lat = min(lat_list)
coords_list.append((max_lon, (lat_list[lon_list.index(max_lon)])))
coords_list.append(((lon_list[lat_list.index(max_lat)]), max_lat))
coords_list.append((min_lon, (lat_list[lon_list.index(min_lon)])))
coords_list.append(((lon_list[lat_list.index(min_lat)]), min_lat))
new_coordinates[building_id] = coords_list
return new_coordinates
def plot_building(building, building_id):
# Functie voor het plotten van de lengtes van de zijden van het gebouw
building_edge_lengths = {}
distance = []
coords_1 = 0
coords_3 = ""
length_building = len(building)
for count, i in enumerate(building):
if coords_1 == 0:
coords_1 = i
coords_3 = i
elif count == (length_building - 1):
coords_2 = i
distance.append(geopy.distance.distance(coords_1, coords_2).meters)
distance.append(geopy.distance.distance(coords_3, coords_2).meters)
else:
coords_2 = i
distance.append(geopy.distance.distance(coords_1, coords_2).meters)
coords_1 = coords_2
building_edge_lengths[building_id] = distance
return building_edge_lengths
config = configparser.ConfigParser()
config.read('config.ini')
timestr = time.strftime("%Y%m%d-%H%M%S")
lower_length = float(input("aantal meter benedenwaarde: ") or config['ANALYZER']['lower_length'])
higher_length = float(input("aantal meter bovenwaarde: ") or config['ANALYZER']['higher_length'])
squaremode = bool(strtobool(input("Gebruik square mode: ") or config['ANALYZER']['squaremode']))
resultfile = ''
if squaremode is True:
resultfile = 'osm_file_analysis_' + timestr + '_sq.csv'
elif squaremode is False:
resultfile = 'osm_file_analysis_' + timestr + '.csv'
with open(config['SHARED']['basepath'] + config['SHARED']['json_file']) as f:
json_file = json.load(f)
# Filter de Json File naar de belangrijke elementen
all_nodes = filter_nodes(json_file)
all_buildings = filter_buildings(json_file)
all_buildings_types = filter_building_types(json_file)
# Bereken de lengtes
x = 0
building_lengths_filtered = {}
with open(resultfile, mode='x') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['id', 'type', 'google maps', 'openstreetmaps', 'afmetingen afgerond', 'afmetingen'])
for single_building in all_buildings:
building_coordinates = get_building_coords(all_nodes, single_building, x)
if squaremode is True:
# Filter de volledige set coordinaten naar: Kleinste X, Grootste Y, Grootste X, Kleinste Y
if not building_coordinates[x]:
pass
else:
building_coordinates = sq_filter(building_coordinates[x], x)
elif squaremode is False:
pass
# Haal het gebouwtype op
building_type = all_buildings_types[x]
for z in building_coordinates:
building_lengths = plot_building(building_coordinates[z], x)
# Filter de lengtes weg
skip = False
for single_length in building_lengths[x]:
if skip is True:
break
if single_length >= lower_length:
if single_length <= higher_length:
csv_writer.writerow([str(x),
str(building_type),
'http://www.google.com/maps/place/' + str(
building_coordinates[x][0][1]) + ',' + str(
building_coordinates[x][0][0]),
'http://www.openstreetmap.com/node/' + str(
single_building[0]),
str([round(y, 2) for y in building_lengths[x]]).strip('[]'),
str(building_lengths[x]).strip('[]')
])
skip = True
x = x + 1