r7844 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r7843‎ | r7844 | r7845 >
Date:08:49, 24 March 2005
Author:egilk
Status:old
Tags:
Comment:
Sort neighbors according to distance
Modified paths:
  • /trunk/extensions/gis/geo.php (modified) (history)
  • /trunk/extensions/gis/neighbors.php (modified) (history)

Diff [purge]

Index: trunk/extensions/gis/geo.php
@@ -131,8 +131,8 @@
132132 }
133133
134134 /**
135 - * Local:
136 - * Get a set of coordinates from parameters
 135+ * Private:
 136+ * Get a set of coordinates from parameters
137137 */
138138 function get_coor( ) {
139139 if ($this->is_coor($this->pieces[1],$this->pieces[3])) {
@@ -201,7 +201,7 @@
202202 * Given decimal degrees, convert to
203203 * minutes, seconds and direction
204204 */
205 - function make_minsec( $deg )
 205+ function make_minsec( $deg )
206206 {
207207 if ( $deg >= 0) {
208208 $NS = "N";
Index: trunk/extensions/gis/neighbors.php
@@ -1,7 +1,8 @@
22 <?php
33 /** @file
44 *
5 - * Create a page which link to other articles in Wikipedia
 5+ * Create a page which link to other articles in Wikipedia which are
 6+ * in the neighborhood
67 *
78 * ----------------------------------------------------------------------
89 *
@@ -72,59 +73,92 @@
7374 $lat0 = $this->p->latdeg;
7475 $lon0 = $this->p->londeg;
7576
76 - $out .= "''Locations within approx. ".$this->d." km of ";
77 - if ($this->title != "") {
78 - $out .= $this->title . ", ";
79 - }
80 - $out .= "coordinates "
81 - . $lat0."&deg; ".$lon0 . "&deg;''<br>";
82 -
8377 $g = new gis_database();
8478 $g->select_radius_m( $lat0, $lon0, $this->d * 1000);
 79+ $all = array();
 80+ $all_pos = array();
8581
8682 while (($x = $g->fetch_position())) {
8783 $id = $x->gis_id;
88 - $type = $x->gis_type; /* BUG: */
 84+ $lat = ($x->gis_latitude_min+$x->gis_latitude_max)/2;
 85+ $lon = ($x->gis_longitude_min+$x->gis_longitude_max)/2;
 86+ $gc = new greatcircle($lat,$lon, $lat0, $lon0);
 87+ $all[$id] = $gc->distance;
 88+ $all_pos[$id] = array(
 89+ 'lat' => $lat,
 90+ 'lon' => $lon,
 91+ 'name' => $g->get_title($id),
 92+ 'type' => $x->gis_type,
 93+ 'octant' => $gc->octant(),
 94+ 'heading' => $gc->heading);
 95+ }
8996
90 - if ( $type == "") $type = "unknown";
 97+ /* Sort by distance */
 98+ asort($all, SORT_NUMERIC);
 99+ reset($all);
91100
92 - $name_text = $g->get_title( $id );
 101+ /* Output */
 102+ $out .= "''List of ". count($all)
 103+ . " locations within approx. ".$this->d." km of ";
 104+ if ($this->title != "") {
 105+ $out .= $this->title . ", ";
 106+ }
 107+ $out .= "coordinates "
 108+ . $this->show_position($lat0,$lon0)
 109+ . "''<br /><hr />\r\n";
93110
94 - $gc = new greatcircle(
95 - ($x->gis_latitude_min+$x->gis_latitude_max)/2,
96 - ($x->gis_longitude_min+$x->gis_longitude_max)/2,
97 - $lat0, $lon0);
98 - $d = $gc->distance;
 111+ while (list($id, $d) = each($all)) {
 112+ $out .= $this->show_location($id, $d, $all_pos[$id]);
 113+ }
 114+ $wgOut->addWikiText( $out );
 115+ }
 116+
 117+ function show_location( $id, $d, $pos )
 118+ {
 119+ $id = $pos->gis_id;
99120
100 - $out .= "'''[[".$name_text."]]''' ";
101 - if ($type != "" and $type != "unknown") {
102 - $out .= "(".$type .") ";
 121+ $out = "'''[[".$pos['name']."]]''' ";
 122+
 123+ $type = $pos['type'];
 124+ if ($type != "" and $type != "unknown") {
 125+ $out .= "(".$type .") ";
 126+ }
 127+ if ($d < 1000) {
 128+ $out .= round($d)." m ";
 129+ } elseif ($d < 10000) {
 130+ $out .= round($d/100)/10 ." km ";
 131+ } else {
 132+ $d = round($d/1000);
 133+ if (d >= 1000) {
 134+ $m = floor($d/1000);
 135+ $out .= $m.",";
 136+ $d -= $m*1000;
103137 }
104 - if ($d < 1000) {
105 - $out .= round($d)." m";
106 - } elseif ($d < 10000) {
107 - $out .= round($d/100)/10 ." km";
108 - } else {
109 - $d = round($d/1000);
110 - if (d >= 1000) {
111 - $m = floor($d/1000);
112 - $out .= $m.",";
113 - $d -= $m*1000;
114 - }
115 - $out .= $d." km";
 138+ $out .= $d." km ";
 139+ }
 140+ return $out . $pos['octant'] . ", bearing "
 141+ . round($pos['heading']) . "&deg; towards "
 142+ . $this->show_position($pos['lat'],$pos['lon'])
 143+ . "<br />\r\n";
 144+ }
 145+
 146+ function show_position( $lat, $lon )
 147+ {
 148+ $a = geo_param::make_minsec( $lat );
 149+ $b = geo_param::make_minsec( $lon );
 150+ $outa = intval(abs($a['deg'])) . "&deg;&nbsp;";
 151+ $outb = intval(abs($b['deg'])) . "&deg;&nbsp;";
 152+ if ($a['min'] != 0 or $b['min'] != 0
 153+ or $a['sec'] != 0 or $b['sec'] != 0) {
 154+ $outa .= intval($a['min']) . "&prime;&nbsp;";
 155+ $outb .= intval($b['min']) . "&prime;&nbsp;";
 156+ if ($a['sec'] != 0 or $b['sec'] != 0) {
 157+ $outa .= $a['sec']. "&Prime;&nbsp;";
 158+ $outb .= $b['sec']. "&Prime;&nbsp;";
116159 }
117 - $out .= " bearing ".round($gc->heading)."&deg; "
118 - . $gc->octant()."<br>";
119160 }
120 - $wgOut->addWikiText( $out );
 161+
 162+ return $outa . $a['NS'] . " " . $outb . $b['EW'];
121163 }
122164 }
123 -
124165 ?>
125 -
126 -
127 -
128 -
129 -
130 -
131 -

Status & tagging log