-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChargingStations2GeoJson.py
197 lines (154 loc) · 8.07 KB
/
ChargingStations2GeoJson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import argparse
import time
import json
import re
import requests
import logging
import os
import sys
import math
import xml.etree.cElementTree as ET
from utils.GeoJsonBuilder import GeoJsonBuilder
from collections import OrderedDict
ns = {}
ch = logging.StreamHandler(sys.stdout)
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
strict_mode = False
def is_valid_file(parser, arg):
if not os.path.exists(arg):
parser.error("The file %s does not exist!" % arg)
return arg
def get_namespace(element):
match = re.match(r"\{(.*?)\}", element.tag)
if match:
return match.group(1)
raise ValueError("Fatal: Couldn't identify the namespace!")
def process_charging_device(charging_point, station_name):
json_device = json.loads(charging_point.text)
sum_connectors = json_device["numberOfConnectors"]
charging_point_id = json_device["id"]
charging_point_name = json_device["name"].strip()
if re.search(r"\s", charging_point_name):
logger.warning("Charging point name '%s' probably contains some text, make sure to verify this before importing. (Belongs to charging station: '%s')" % (
charging_point_name, station_name))
if not charging_point_name.startswith("CP"):
logger.warning("Charging point name '%s' does not have a chargy Id (CPabcd), make sure to verify this before importing. (Belong to charging station: '%s')" %
(charging_point_name, station_name))
# Check contents of the charging points
charging_points_status = 0
charging_speeds_for_points = []
for chargingPoint in json_device["connectors"]:
charging_point_description = chargingPoint["description"].strip()
if charging_point_description.upper() != "OFFLINE":
charging_points_status += 1
charging_speeds_for_points.append(math.floor(chargingPoint["maxchspeed"]))
#TODO: Fix
#if math.floor(chargingPoint["maxchspeed"]) != output_wattage_value:
# logger.error("Power Output mismatch for '%s', was expecting '%s' and got '%s'." % (
# charging_point_name, output_wattage_value, chargingPoint["maxchspeed"]))
# if strict_mode:
# raise ValueError("Power Output Error!")
count_connectors_not_offline = charging_points_status
return sum_connectors, charging_point_id, charging_point_name, count_connectors_not_offline, charging_speeds_for_points
def process_charging_station(station):
properties = {}
station_name = ' '.join(station.find("ns:name", ns).text.split())
if not (station_name.startswith("Chargy Ok") or station_name.startswith("SuperChargy Ok")):
properties["operator"] = "Chargy"
visibility = int(station.find("ns:visibility", ns).text)
if visibility != 1:
logger.warning("Node '%s', visibility flag != 1." % station_name)
properties["operational_status"] = "closed"
properties["amenity"] = "charging_station"
properties['name'] = station_name
properties["brand"] = "Chargy"
properties["opening_hours"] = "24/7"
properties["motorcar"] = "yes"
properties["phone"] = "+352 80062020"
properties["authentication:membership_card"] = "yes"
# Each charging station can have multiple charging points
charging_devices = station.findall(
"ns:ExtendedData/ns:Data[@name='chargingdevice']/ns:value", ns)
properties["devices"] = len(charging_devices)
if(len(charging_devices) > 1):
logger.info("Charging Station '%s' contains '%s' charging points, tagging as 1 charging station." % (
station_name, len(charging_devices)))
# Process all the charging points
sum_connectors = 0
count_connectors_not_offline = 0
output_wattage_for_all_stations = []
for device in charging_devices:
r_sum_connectors, r_charging_point_id, charging_point_name, r_cnt_connectors_offline, output_wattages = process_charging_device(
device, station_name)
sum_connectors += r_sum_connectors
count_connectors_not_offline += r_cnt_connectors_offline
output_wattage_for_all_stations.extend(output_wattages)
if count_connectors_not_offline == 0:
logger.warning(
"Charging station '%s' is OFFLINE (All sockets are OFFLINE)" % station_name)
#properties["operational_status"] = "closed"
properties["socket:type2:output"] = ";".join(list(map(lambda wattage: "%s kW" % wattage, sorted(set(output_wattage_for_all_stations)))))
countChargingPoints = int(station.find(
"ns:ExtendedData/ns:Data[@name='CPnum']/ns:value", ns).text)
if countChargingPoints != sum_connectors:
logger.error("Charging point count mismatch for '%s'. Total reported count is %s, summed description count is %s." % (
charging_point_name, countChargingPoints, sum_connectors))
if strict_mode:
raise ValueError("Charging Point Count mismatch!")
properties["socket:type2"] = countChargingPoints
properties["capacity"] = countChargingPoints
lon, lat = station.find("ns:Point/ns:coordinates", ns).text.split(",")
return GeoJsonBuilder.create_feature(properties, float(lon), float(lat))
def download_data_from_opendata_portal():
logger.debug("File not provided, downloading from data.public.lu...")
downloaded_file_result = requests.get("https://data.public.lu/fr/datasets/r/22f9d77a-5138-4b02-b315-15f306b77034")
dataset_filename = "chargy_%s.kml" % time.strftime("%Y%m%d_%H%M%S")
dataset_full_path = "data_cache/%s" % dataset_filename
logger.debug("Saving to %s" % dataset_full_path)
if not os.path.exists(os.path.dirname(dataset_full_path)):
os.makedirs(os.path.dirname(dataset_full_path))
with open(dataset_full_path, "wb") as f:
f.write(downloaded_file_result.content)
return dataset_full_path
def extract_data_from_kml(input_file, output_file):
if input_file is None:
input_file = download_data_from_opendata_portal()
logger.debug("Reading File: %s" % input_file)
doc = ET.parse(input_file)
root = doc.getroot()
ns["ns"] = "%s" % get_namespace(root)
stations = root.findall(".//ns:Placemark", ns)
logger.debug("Found %s stations" % len(stations))
features = []
for station in stations:
computed_feature = process_charging_station(station)
if computed_feature is not None:
features.append(computed_feature)
export_artifact = GeoJsonBuilder.create_geojson(features)
if os.path.dirname(output_file) and not os.path.exists(os.path.dirname(output_file)):
os.makedirs(os.path.dirname(output_file))
with open(output_file, "w") as outfile:
logger.debug("Writing to: %s" % output_file)
json.dump(export_artifact, outfile)
logger.debug("Success! Output file contains %s points." % len(features))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert the Chargy KML Dataset into GeoJSON Points")
parser.add_argument("infile", metavar="INFILE", nargs="?",
default=None,
help="KML File from Chargy. If unset, the most recent file will be pulled from the OpenData Portal",
type=lambda x: is_valid_file(parser, x))
parser.add_argument("-o", "--outfile", metavar="OUTFILE", nargs="?",
default="results/charging_stations_%s.geojson" % time.strftime(
"%Y%m%d_%H%M%S"),
help="Overrides the default filename for the exported GeoJSON file")
parser.add_argument("-v", "--verbose", action="store_const", dest="loglevel",
help="Overrides the default LogLevel", const=logging.DEBUG)
parser.add_argument("-s", "--strict", action="store_const", dest="strict_mode",
help="Enables strict mode. Halt execution if any unexpected value is found.", default=strict_mode, const=True)
args = parser.parse_args()
if args.loglevel is not None:
logger.setLevel(args.loglevel)
strict_mode = args.strict_mode
extract_data_from_kml(args.infile, args.outfile)