-
Notifications
You must be signed in to change notification settings - Fork 0
/
guifi_gml.inc.php
174 lines (156 loc) · 6.63 KB
/
guifi_gml.inc.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
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
<?php
/**
* @file guifi_gml.inc.php
*/
function guifi_gml($zid,$action = "help",$type = 'gml') {
if ($action == "help") {
$zone = db_fetch_object(db_query('SELECT title, nick FROM {guifi_zone} WHERE id = %d',$zid));
drupal_set_breadcrumb(guifi_zone_ariadna($zid));
$output = '<div id="guifi">';
$output .= '<h2>'.t('Zone %zname%',array('%zname%' => $zone->title)).'</h2>';
$output .= '<p>'.t('You must specify which data do you want to export, the following options are available:').'</p>';
$output .= '<ol><li>'. l(t('Nodes'), "guifi/gml/".$zid."/nodes", array('title' => t('export zone nodes in gml format')) ).'</li>';
$output .= '<li>'. l(t('Links'), "guifi/gml/".$zid."/links", array('title' => t('export zone links in gml format')) ).'</li></ol>';
$output .= '<p>'.t('The <a href="http://opengis.net/gml/">GML</a> is a Markup Language XML for Geography described at the <a href="http://www.opengeospatial.org/">Open Geospatial Consortium</a>').'</p>';
$output .= '<p>'.t('<b>IMPORTANT LEGAL NOTE:</b> This network information is under the <a href="http://guifi.net/ComunsSensefils/">Comuns Sensefils</a> license, and therefore, available for any other network under the same licensing. If is not your case, you should ask for permission before using it.</a>').'</p>';
$output .= "</div>";
print theme('page',$output,t('export %zname% in GML format',array('%zname%' => $z->title)));
return;
}
switch ($action) {
case 'links':
guifi_gml_links($zid,$type);
break;
case 'nodes':
guifi_gml_nodes($zid,$type);
break;
}
} //EOF function guifi_gml
function guifi_gml_nodes($zid,$type) {
$minx = 180; $miny = 90; $maxx= -180; $maxy = -90;
$zchilds = guifi_zone_childs($zid);
$res = db_query(
"SELECT id,nick,lat,lon,zone_id,status_flag " .
"FROM {guifi_location}");
while ($row = db_fetch_object($res)) {
if (($row->zone_id != $zid) and (!in_array($row->zone_id,$zchilds)))
continue;
$rsql = db_query(
"SELECT mode " .
"FROM {guifi_radios} " .
"WHERE nid = %d",
$row->id);
$rcount = 0;
$node_type = 'N_A';
while ($r = db_fetch_object($rsql)) {
$rcount++;
if ($rcount == 1)
$node_type = $r->mode;
else {
$node_type = 'Supernode';
break;
}
}
if ($type == 'gml') {
$output .= '
<gml:featureMember>
<dnodes fid="'.$row->id.'">
<ogr:geometryProperty><gml:Point><gml:coordinates>'.$row->lon.','.$row->lat.'</gml:coordinates></gml:Point></ogr:geometryProperty>
<NODE_ID>'.$row->id.'</NODE_ID>
<NODE_NAME>'.$row->nick.'</NODE_NAME>
<NODE_TYPE>'.$node_type.'</NODE_TYPE>
<STATUS>'.$row->status_flag.'</STATUS>
</dnodes>
</gml:featureMember>';
} else {
$output .= $row->id.','.$row->lon.','.$row->lat.','.$row->nick.','.$node_type.','.$row->status_flag."\n";
}
if ($row->lon > $maxx) $maxx = $row->lon;
if ($row->lat > $maxy) $maxy = $row->lat;
if ($row->lon < $minx) $minx = $row->lon;
if ($row->lat < $miny) $miny = $row->lat;
} // while nodes
drupal_set_header('Content-Type: application/xml; charset=utf-8');
if ($type == 'gml') print '<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation=". dnodes.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>'.$minx.'</gml:X><gml:Y>'.$miny.'</gml:Y></gml:coord>
<gml:coord><gml:X>'.$maxx.'</gml:X><gml:Y>'.$maxy.'</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>';
print $output;
if ($type == 'gml') print '
</ogr:FeatureCollection>';
} // EOF function guifi_gml_nodes()
function guifi_gml_links($zid,$type) {
$oGC = new GeoCalc();
$minx = 180; $miny = 90; $maxx= -180; $maxy = -90;
$res = db_query(
"SELECT id,link_type,flag " .
"FROM {guifi_links} " .
"WHERE link_type != 'cable' " .
"GROUP BY 1,2 " .
"HAVING count(*) = 2");
$zchilds = guifi_zone_childs($zid);
$zchilds[$zid] = 'Top';
while ($row = db_fetch_object($res)) {
$resnode = db_query(
"SELECT n.id, n.zone_id, n.nick,n.lat, n.lon, n.status_flag " .
"FROM {guifi_links} l, {guifi_location} n " .
"WHERE l.id = %d AND l.nid=n.id",
$row->id);
$nl = array();
while ($n = db_fetch_object($resnode)) {
$nl[] = $n;
}
if (count($nl) == 2)
if ((in_array($nl[0]->zone_id,$zchilds)) || (in_array($nl[1]->zone_id,$zchilds))) {
$distance = round($oGC->EllipsoidDistance($nl[0]->lat,$nl[0]->lon, $nl[1]->lat, $nl[1]->lon),3);
$status = $row->flag;
if ($type == 'gml') $output .= '
<gml:featureMember>
<dlinks fid="'.$row->id.'">
<NODE1_ID>'.$nl[0]->id.'</NODE1_ID>
<NODE1_NAME>'.$nl[0]->nick.'</NODE1_NAME>
<NODE2_ID>'.$nl[1]->id.'</NODE2_ID>
<NODE2_NAME>'.$nl[1]->nick.'</NODE2_NAME>
<KMS>'.$distance.'</KMS>
<LINK_TYPE>'.$row->link_type.'</LINK_TYPE>
<STATUS>'.$status.'</STATUS>
<ogr:geometryProperty><gml:LineString><gml:coordinates>'.$nl[0]->lon.','.$nl[0]->lat.' '.$nl[1]->lon.','.$nl[1]->lat.'</gml:coordinates></gml:LineString></ogr:geometryProperty>
</dlinks>
</gml:featureMember>';
else
$output .= $row->id.','.$nl[0]->id.','.$nl[0]->nick.','.$nl[1]->id.','.$nl[1]->nick.','.$distance.','.$row->link_type.','.$status.','.$nl[0]->lon.','.$nl[0]->lat.','.$nl[1]->lon.','.$nl[1]->lat."\n";
if ($nl[0]->lon > $maxx) $maxx = $nl[0]->lon;
if ($nl[0]->lat > $maxy) $maxy = $nl[0]->lat;
if ($nl[0]->lon < $minx) $minx = $nl[0]->lon;
if ($nl[0]->lat < $miny) $miny = $nl[0]->lat;
if ($nl[1]->lon > $maxx) $maxx = $nl[1]->lon;
if ($nl[1]->lat > $maxy) $maxy = $nl[1]->lat;
if ($nl[1]->lon < $minx) $minx = $nl[1]->lon;
if ($nl[1]->lat < $miny) $miny = $nl[1]->lat;
}
}
drupal_set_header('Content-Type: application/xml; charset=utf-8');
if ($type == 'gml') print '<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation=". dlinks.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>'.$minx.'</gml:X><gml:Y>'.$miny.'</gml:Y></gml:coord>
<gml:coord><gml:X>'.$maxx.'</gml:X><gml:Y>'.$maxy.'</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>';
print $output;
if ($type == 'gml') print '</ogr:FeatureCollection>';
} // eof function guifi_gml_links()
?>