From 713ee09bab03ba77126572c2258aa2bf7cccec59 Mon Sep 17 00:00:00 2001 From: Dennis Date: Thu, 9 Jul 2020 11:17:22 +0200 Subject: [PATCH] added files --- analyzer_orig.py | 152 ++++++++++++++++++++++++++++++++++++++++++++++ analyzer_v2.py | 154 +++++++++++++++++++++++++++++++++++++++++++++++ config.ini | 11 ++++ xmlparser.py | 16 +++++ 4 files changed, 333 insertions(+) create mode 100755 analyzer_orig.py create mode 100755 analyzer_v2.py create mode 100755 config.ini create mode 100755 xmlparser.py diff --git a/analyzer_orig.py b/analyzer_orig.py new file mode 100755 index 0000000..73fa7e8 --- /dev/null +++ b/analyzer_orig.py @@ -0,0 +1,152 @@ +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 = open('osm_file_analysis_' + timestr + '_sq.txt', 'x') +elif squaremode is False: + resultfile = open('osm_file_analysis_' + timestr + '.txt', 'x') + +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 = {} +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 + + 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: + resultfile.write('Building ID: ' + str(x) + + '\nGebouwType:' + str(building_type) + + '\nZijdelengtes: ' + str(building_lengths[x]).strip('[]') + + '\nZijdelengtes afgerond: ' + str([round(y, 2) for y in building_lengths[x]]) + + '\nGoogle maps link: http://www.google.com/maps/place/' + str( + building_coordinates[x][0][1]) + ',' + str(building_coordinates[x][0][0]) + + '\nOpenstreetmaps node link: http://www.openstreetmap.com/node/' + str( + single_building[0]) + + '\n\n') + + skip = True + x = x + 1 diff --git a/analyzer_v2.py b/analyzer_v2.py new file mode 100755 index 0000000..2f3c09a --- /dev/null +++ b/analyzer_v2.py @@ -0,0 +1,154 @@ +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 diff --git a/config.ini b/config.ini new file mode 100755 index 0000000..3ee4e47 --- /dev/null +++ b/config.ini @@ -0,0 +1,11 @@ +[SHARED] +basepath = D:/openstreetmap/ +json_file = map1.json + +[XMLPARSER] +osm_file = map1.osm + +[ANALYZER] +lower_length = 70 +higher_length = 90 +squaremode = n diff --git a/xmlparser.py b/xmlparser.py new file mode 100755 index 0000000..dd86037 --- /dev/null +++ b/xmlparser.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 + +import osmnx as ox +import json +import configparser + +config = configparser.ConfigParser() +config.read('config.ini') +osm_file = config['SHARED']['basepath'] + config['XMLPARSER']['osm_file'] +json_file = config['SHARED']['basepath'] + config['SHARED']['json_file'] +print('converting osm file, this takes very long!') +osm_data = ox.overpass_json_from_file(osm_file) +print("writing to file, this will take some time!") +with open(json_file, 'w') as writefile: + json.dump(osm_data, writefile) +print("done")