-
Notifications
You must be signed in to change notification settings - Fork 2
/
powerlinestest.php
58 lines (53 loc) · 2.08 KB
/
powerlinestest.php
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
<?php
include('import/config.inc.php');
$dbconn = pg_connect('host=' . DB_HOST . ' dbname=' . DB_NAME . ' user=' . DB_USER . ' password=' . DB_PASS)
or die('Verbindungsaufbau fehlgeschlagen: ' . pg_last_error());
// find the transformed coordinates of the house
$query = "Select ST_Transform(ST_Centroid(way), 4326) as way from planet_osm_polygon where not building = '' and osm_id = 171867723";
$result = pg_query($dbconn, $query);
$house = '';
if (pg_num_rows($result) == 1) {
$house = pg_fetch_object($result);
} else {
echo "Error";
exit;
}
// find the transformed coordinates of th transformer
$query = "SELECT ST_Transform(way, 4326) as way FROM planet_osm_point where osm_id = 999999999990005;";
$result = pg_query($dbconn, $query);
$transformer = '';
if (pg_num_rows($result) == 1) {
$transformer = pg_fetch_object($result);
} else {
echo "Error";
exit;
}
pg_close($dbconn);
$dbconn = pg_connect('host=' . DB_HOST . ' dbname= pgrouting user=' . DB_USER . ' password=' . DB_PASS)
or die('Verbindungsaufbau fehlgeschlagen: ' . pg_last_error());
// Getting the route from house to transformer
$mainquery = "SELECT ST_MakeLine(route.geom) FROM ( SELECT geom"
." FROM pgr_fromAtoB('ways', ST_X('" . $house->way . "'),ST_Y('" . $house->way
. "'),ST_X('" . $transformer->way . "'),ST_Y('" . $transformer->way . "') ) ORDER BY seq) AS route";
// adding source and target
$source_target = "ST_AddPoint( ST_AddPoint((" . $mainquery . "), '" . $house->way . "', 0), '" . $transformer->way . "')";
//converting result to GeoJSON
$query = "SELECT ST_AsGeoJSON(" . $source_target . ") as way;";
$result = pg_query($dbconn, $query);
$i = 1;
$rows = pg_num_rows($result);
if ($rows > 0) {
echo '{ "type": "FeatureCollection",' . "\n" . ' "features": [' . "\n";
while ($data = pg_fetch_object($result)) {
echo '{ "type": "Feature",' . "\n" . ' "geometry": ';
echo $data->way;
echo '}';
if ($i != $rows) {
echo ',';
}
echo "\n";
//echo '},' . "\n";
$i++;
}
echo "\n" . ']' . "\n" . ' }';
}