root/geowiki/getcolourxml.rs.php

Revision 9, 3.5 kB (checked in by mikel, 3 years ago)

geowiki initial checkin

Line 
1 <?php
2 require("jsrsServer.php.inc");
3 //getColourXML($filename, $dataset, $nrColours, $rangeValues)
4 jsrsDispatch("getcolourxml");
5
6 //makes an array out of $string and trims it
7 function getTrimmedArray($separator, $string) {
8     $tmp = explode($separator, $string);
9     for ($i = 0; $i < count($tmp); $i++)
10         $tmp[$i] = trim($tmp[$i]);
11     return $tmp;
12 }
13
14 //$values is an array which contains ordered values that define ranges
15 //e.g. [32, 18, 7, 3.5] defining ranges of different levels of alcoholic drinks
16 //(.., 32] hard liquor, (32, 18], (18, 7] wine, (7, 3.5] beer, (3.5, ..) non alcoholic
17 //return value is the range's number in which $value falls
18 //e.g. 0 for anything >= 32, 1 below 32 down to 18, or 4 for $value<3.5
19 function getColourTag($values, $value) {
20     $i = 0;
21     while ((count($values) > $i) && ($value < $values[$i]))
22         $i++;
23     return "<category>colour$i</category>";
24 }
25
26 //TODO: make use of $nrColours
27 //reads the data from rss.xml or any file given in $filename
28 //$dataset: a string containing (uid,value) pairs, separated by newline
29 //$nrColours: number of predefined colours as categories in config.xml (or
30 //any smaller number); if < 1 will be set to 1
31 //$rangeValues: comma-separated values, defining the ranges for each colour
32 //if empty, the interval between maximum and minimum value in $dataset
33 //is separated into $nrColours equal ranges
34 //returns the name of a temporary file, which contains the items of the original
35 //file. Each of the items whose uids are within $dataset is tagged by the
36 //predefined colour according to the value in $dataset
37 //Example:
38 //rss = getColourXML("rss.xml","Sterni Export,5.5\nAbsolut,42",4,"32,18,7,3.5");
39 //rss will contain the content from rss.xml, with added categories <colour3>
40 //and <colour0> for the respective drinks
41 function getcolourxml($filename, $dataset, $nrColours, $rangeValues = '') {
42     //organise dataset, find max and min value
43     $tmp = getTrimmedArray("\n", $dataset);
44     foreach ($tmp as $dataPair) {
45         $pair = explode(",", $dataPair);
46         $uid = $pair[0];
47         $value = $pair[1];
48         $values[$uid] = $value;
49
50         //find min and max values
51         if (!isset($vMin)) {
52             $vMin = $value;
53             $vMax = $value;
54         }
55         if ($value < $vMin) $vMin = $value;
56         else if ($value > $vMax) $vMax = $value;
57     }
58     
59     var_dump($values);
60     
61     //TODO
62     //if $rangeValues empty create it by dividing (vMin,vMax) into $nrColours
63     //equal intervals
64     //TODO: init $rangeArray
65     if ($rangeValues == '') {
66         if ($nrColours < 1)
67             $nrColours = 1;
68         $step = ($vMax - $vMin) / $nrColours;
69         for ($i=1; $i <= $nrColours; $i++) {
70             $rangeArray[] = $vMax - $i * $step;
71         }
72     }
73     else {
74         $rangeArray = explode(",", $rangeValues);
75         rsort($rangeArray);
76     }
77     
78     //read file
79     $handle = fopen($filename,"r");
80     $contents = fread($handle, filesize($filename));
81     fclose($handle);
82     
83     //for each uid in dataset: read xml, add relevant colour tag
84     //TODO: use xml-parsing functions
85     foreach ($values as $uid => $value) {
86         //find uid
87         $pos = strpos($contents, "<guid>$uid</guid>");
88         echo "pos: ", $pos;
89         //=== is essential!
90         if ($pos === false) continue;
91         
92         $itemEndPos = strpos($contents, "</item>", $pos);
93
94         //define colour
95         $colourTag = getColourTag($rangeArray, $value);
96         
97         //add tag to item
98         $contents = substr_replace($contents, $colourTag, $itemEndPos, 0);
99     }
100
101     //write to temporary file
102     $tmpName = "temprss.xml";
103
104     $handle = fopen($tmpName, "w");
105     fwrite($handle, $contents);
106     fclose($handle);
107
108     return $tmpName;
109 }
110
111 echo getcolourxml("rss.xml","Ulsoor Lake, 4\nHAL, 10","4","100, 80, 30, 5");
112 echo getcolourxml("rss.xml","Ulsoor Lake, 4\nHAL, 10\nIndiranagar Park, 7","4","");
113
114 ?>
115
Note: See TracBrowser for help on using the browser.