-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdescription.php
More file actions
98 lines (86 loc) · 2.44 KB
/
Copy pathdescription.php
File metadata and controls
98 lines (86 loc) · 2.44 KB
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
<?php
function createDescription( MongoCollection $c, &$r )
{
$tags = Functions::split_tags( $r[TAGS] );
/* Find closest street */
$query = [ LOC => [ '$near' => $r[LOC] ], TAGS => new MongoRegex('/^highway=(trunk|pedestrian|service|primary|secondary|tertiary|residential|unclassified)/' ) ];
$road = $c->findOne( $query );
$roadTags = Functions::split_tags( $road[TAGS] );
$roadName = array_key_exists( 'name', $roadTags ) ? $roadTags['name'] : "Unknown " . $roadTags['highway'];
$s[] = $road;
/* Find all roads that intersect with the $road */
$q = $c->find( [
LOC => [ '$geoIntersects' => [ '$geometry' => $road[LOC] ] ],
TAGS => new MongoRegex('/^highway=(trunk|pedestrian|service|primary|secondary|tertiary|residential|unclassified)/' ),
'_id' => [ '$ne' => $road['_id'] ],
] );
$intersectingWays = array();
foreach ( $q as $crossRoad )
{
$crossTags = Functions::split_tags( $crossRoad[TAGS] );
if ( !in_array( "name={$roadName}", $crossRoad ) && array_key_exists( 'name', $crossTags ) )
{
$intersectingWays[] = $crossRoad['_id'];
}
}
/* Find closest road to the point, only using $intersectingWay roads */
$res = $c->aggregate( array(
'$geoNear' => array(
'near' => $r[LOC],
'distanceField' => 'distance',
'distanceMultiplier' => 1,
'maxDistance' => 5000,
'spherical' => true,
'query' => array( '_id' => [ '$in' => $intersectingWays ], TAGS => [ '$ne' => "name={$roadName}" ] ),
'limit' => 1,
)
) );
$intersectingRoad = false;
if ( array_key_exists( 'result', $res ) && ( count( $res['result'] ) > 0 ) )
{
$intersectingRoad = $res['result'][0];
$roadTags = Functions::split_tags( $intersectingRoad[TAGS] );
if ( array_key_exists( 'name', $roadTags ) )
{
$intersectRoadName = $roadTags['name'];
}
else if ( array_key_exists( 'ref', $roadTags ) )
{
$intersectRoadName = $roadTags['ref'];
}
else
{
$intersectRoadName = "???";
}
$s[] = $intersectingRoad;
}
/* If there is a ref, use it, otherwise set ??? */
if ( array_key_exists( 'ref', $tags ) )
{
$pbref = $tags['ref'];
}
else
{
$pbref = '???';
}
/* Add name tag */
if ( ! $intersectingRoad )
{
$desc = "On $roadName";
}
else
{
if ( $intersectingRoad['distance'] < 20 )
{
$desc = "On $roadName, on the corner with $intersectRoadName";
}
else
{
$desc = "On $roadName, near $intersectRoadName";
}
}
$r['desc'] = $desc;
$r['ref'] = $pbref;
$r[TAGS][] = "name={$pbref}<br/>{$desc}";
return $desc;
}