|
@@ -1,9 +1,11 @@
|
|
#!/usr/bin/env python3
|
|
#!/usr/bin/env python3
|
|
''' plan_gen main functions '''
|
|
''' plan_gen main functions '''
|
|
|
|
|
|
-import time
|
|
|
|
-import numpy as np
|
|
|
|
import lxml.etree as etree
|
|
import lxml.etree as etree
|
|
|
|
+import numpy as np
|
|
|
|
+import pandas
|
|
|
|
+import time
|
|
|
|
+import utm
|
|
|
|
|
|
# constants
|
|
# constants
|
|
# ------------------
|
|
# ------------------
|
|
@@ -30,7 +32,7 @@ def parse_params(param_str):
|
|
if param_str:
|
|
if param_str:
|
|
for key_value_str in param_str.split(','):
|
|
for key_value_str in param_str.split(','):
|
|
key, value = key_value_str.split('=')
|
|
key, value = key_value_str.split('=')
|
|
- if key in ['hc', 'wc']:
|
|
|
|
|
|
+ if key in ['hc', 'hw', 'wc', 'ww']:
|
|
coords = value.split('|')
|
|
coords = value.split('|')
|
|
dict_params[key] = [np.fromstring(str(x), dtype=int, sep=':') for x in coords]
|
|
dict_params[key] = [np.fromstring(str(x), dtype=int, sep=':') for x in coords]
|
|
elif key in ['hr', 'wr']:
|
|
elif key in ['hr', 'wr']:
|
|
@@ -39,6 +41,34 @@ def parse_params(param_str):
|
|
dict_params[key] = parse_value(value)
|
|
dict_params[key] = parse_value(value)
|
|
return dict_params
|
|
return dict_params
|
|
|
|
|
|
|
|
+def parse_latlon(csv):
|
|
|
|
+ ''' parse lat and lon from a csv '''
|
|
|
|
+ df = pandas.read_csv(csv, sep=';')
|
|
|
|
+ lat = df.lat.tolist()
|
|
|
|
+ lon = df.lon.tolist()
|
|
|
|
+ return lat, lon
|
|
|
|
+
|
|
|
|
+def parse_weights(csv):
|
|
|
|
+ ''' parse weights from a csv '''
|
|
|
|
+ df = pandas.read_csv(csv, sep=';')
|
|
|
|
+ weights = df.nbpeople.tolist()
|
|
|
|
+ return weights
|
|
|
|
+
|
|
|
|
+def latlon_to_utm(lat, lon):
|
|
|
|
+ ''' convert lat lon to utm coordinates '''
|
|
|
|
+ utms = [utm.from_latlon(a, b) for a, b in zip(lat, lon)]
|
|
|
|
+ xutm = [utm_coords[0] for utm_coords in utms]
|
|
|
|
+ yutm = [utm_coords[1] for utm_coords in utms]
|
|
|
|
+ return xutm, yutm
|
|
|
|
+
|
|
|
|
+def xy_to_ij(x, y, dx, dy, nb_clusters):
|
|
|
|
+ i, j = (int(x/dx), int(y/dy))
|
|
|
|
+ if i >= nb_clusters:
|
|
|
|
+ i -= 1
|
|
|
|
+ if j >= nb_clusters:
|
|
|
|
+ j -= 1
|
|
|
|
+ return i, j
|
|
|
|
+
|
|
def get_seconds(time_str):
|
|
def get_seconds(time_str):
|
|
''' returns seconds from a time string '''
|
|
''' returns seconds from a time string '''
|
|
h, m, s = time_str.split(':')
|
|
h, m, s = time_str.split(':')
|
|
@@ -60,29 +90,34 @@ def make_gaussian(size, center=None, radius=10):
|
|
|
|
|
|
def make_clusters(nb_clusters, nodes):
|
|
def make_clusters(nb_clusters, nodes):
|
|
''' make a grid of (nb_clusters*nb_clusters) from a nodes list '''
|
|
''' make a grid of (nb_clusters*nb_clusters) from a nodes list '''
|
|
- xmin, xmax, ymin, ymax = get_extrem_nodes(nodes)
|
|
|
|
- dx = (xmax - xmin) / nb_clusters
|
|
|
|
- dy = (ymax - ymin) / nb_clusters
|
|
|
|
|
|
+ xmin, xmax, ymin, ymax, dx, dy = get_min_max_steps(nodes, nb_clusters)
|
|
clusters = np.empty((nb_clusters, nb_clusters), dtype=object)
|
|
clusters = np.empty((nb_clusters, nb_clusters), dtype=object)
|
|
for node in nodes:
|
|
for node in nodes:
|
|
x, y = (float(node.get('x')) - xmin, float(node.get('y')) - ymin)
|
|
x, y = (float(node.get('x')) - xmin, float(node.get('y')) - ymin)
|
|
- i, j = (int(x/dx), int(y/dy))
|
|
|
|
- if i >= nb_clusters:
|
|
|
|
- i -= 1
|
|
|
|
- if j >= nb_clusters:
|
|
|
|
- j -= 1
|
|
|
|
|
|
+ i, j = xy_to_ij(x, y, dx, dy, nb_clusters)
|
|
if clusters[i][j] is None:
|
|
if clusters[i][j] is None:
|
|
clusters[i][j] = []
|
|
clusters[i][j] = []
|
|
clusters[i][j] += [node]
|
|
clusters[i][j] += [node]
|
|
return clusters
|
|
return clusters
|
|
|
|
|
|
-def make_densities(nb_clusters, centers=None, radius=None):
|
|
|
|
|
|
+def make_centers(csv, nb_clusters, nodes):
|
|
|
|
+ ''' make centers from a csv file '''
|
|
|
|
+ lat, lon = parse_latlon(csv)
|
|
|
|
+ xutm, yutm = latlon_to_utm(lat, lon)
|
|
|
|
+ xmin, xmax, ymin, ymax, dx, dy = get_min_max_steps(nodes, nb_clusters)
|
|
|
|
+ centers = []
|
|
|
|
+ for x, y in zip(xutm, yutm):
|
|
|
|
+ i, j = xy_to_ij(x - xmin, y - ymin, dx, dy, nb_clusters)
|
|
|
|
+ centers.append([i, j])
|
|
|
|
+ return centers
|
|
|
|
+
|
|
|
|
+def make_densities(nb_clusters, centers=None, radius=None, weights=None):
|
|
''' make a list of gaussian probability densities '''
|
|
''' make a list of gaussian probability densities '''
|
|
if centers is None:
|
|
if centers is None:
|
|
return make_gaussian(nb_clusters, radius=nb_clusters/2)
|
|
return make_gaussian(nb_clusters, radius=nb_clusters/2)
|
|
densities = np.zeros((nb_clusters, nb_clusters))
|
|
densities = np.zeros((nb_clusters, nb_clusters))
|
|
- for n, c in enumerate(centers):
|
|
|
|
- densities += make_gaussian(nb_clusters, center=c, radius=radius[n])
|
|
|
|
|
|
+ for n, (c, w) in enumerate(zip(centers, weights)):
|
|
|
|
+ densities += w * make_gaussian(nb_clusters, center=c, radius=radius[n])
|
|
return densities
|
|
return densities
|
|
|
|
|
|
def clean_densities(densities, clusters):
|
|
def clean_densities(densities, clusters):
|
|
@@ -160,4 +195,11 @@ def get_extrem_nodes(nodes):
|
|
''' returns extremum coordinates of a nodeslist '''
|
|
''' returns extremum coordinates of a nodeslist '''
|
|
x = [float(node.get('x')) for node in nodes]
|
|
x = [float(node.get('x')) for node in nodes]
|
|
y = [float(node.get('y')) for node in nodes]
|
|
y = [float(node.get('y')) for node in nodes]
|
|
- return min(x), max(x), min(y), max(y)
|
|
|
|
|
|
+ return min(x), max(x), min(y), max(y)
|
|
|
|
+
|
|
|
|
+def get_min_max_steps(nodes, nb_clusters):
|
|
|
|
+ ''' returns min max steps '''
|
|
|
|
+ xmin, xmax, ymin, ymax = get_extrem_nodes(nodes)
|
|
|
|
+ dx = (xmax - xmin) / nb_clusters
|
|
|
|
+ dy = (ymax - ymin) / nb_clusters
|
|
|
|
+ return xmin, xmax, ymin, ymax, dx, dy
|