/[drupal]/contributions/modules/geo/includes/geo.wkb.inc
ViewVC logotype

Contents of /contributions/modules/geo/includes/geo.wkb.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Wed Aug 5 02:57:39 2009 UTC (3 months, 3 weeks ago) by vauxia
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-ALPHA1
Changes since 1.10: +21 -8 lines
File MIME type: text/x-php
#429256 by bec - WKB byte order
1 <?php // $Id: geo.wkb.inc,v 1.10 2009/06/18 01:24:11 vauxia Exp $
2
3 function geo_wkb_types($key = NULL) {
4 $geo_types = array(
5 1 => 'point',
6 2 => 'linestring',
7 3 => 'polygon',
8 4 => 'multipoint',
9 5 => 'multilinestring',
10 6 => 'multipolygon',
11 );
12
13 if ($key) return $geo_types[$key];
14 return $geo_types;
15 }
16
17 function _geo_wkb_get_data($wkb = NULL, $format = 'text', $fp = NULL, $type = NULL, $byte_order = NULL) {
18 static $local_byte_order;
19 if (!isset($local_byte_order)) {
20 $local_byte_order = current(unpack('c', pack('S', 1)));
21 }
22
23 $data = array();
24
25 if (!$fp) {
26 if (empty($wkb)) return;
27 // Put our WKB data into memory so we can cruise around with fread.
28 $fp = fopen("php://memory", 'r+');
29 fputs($fp, $wkb);
30 fseek($fp, 0);
31
32 // Fetch the byte order (0 = Big Endian, 1 = Little Endian) and geo type.
33 $data = unpack('cbyte_order', fread($fp, 1));
34 $byte_order = $data['byte_order'];
35 $data = array_merge($data, unpack(($byte_order ? 'Vtype' : 'Ntype'), fread($fp, 4)));
36
37 // Set the geometry type
38 $data['type'] = geo_wkb_types($data['type']);
39 }
40
41 $func = 'geo_wkb_get_'. $format;
42
43 if (!isset($type)) $type = $data['type'];
44
45 switch($type) {
46
47 case 'point': // Contains x, y decimal values.
48 $tmp = fread($fp, 16);
49 // Check whether the data's byte order matches the machine's byte order.
50 // If not, we need to reverse the data before PHP can unpack it.
51 if ($byte_order != $local_byte_order) {
52 $tmp = strrev(substr($tmp, 0, 8)) . strrev(substr($tmp, 8));
53 }
54 $data = array_merge($data, unpack('dx/dy', $tmp));
55 $data['value'] = $func($data);
56 if (function_exists($post = $func .'_post')) $data = $post($data);
57 return $data;
58
59 case 'linestring': // Contains count * (point) values.
60 $data['count'] = current(unpack(($byte_order ? 'V' : 'N'), fread($fp, 4)));
61 for ($i = 1; $i <= $data['count']; $i++) {
62 $point = _geo_wkb_get_data(NULL, $format, $fp, 'point', $byte_order);
63 $data['value'] = $func($point, $data['value']);
64 }
65 if (function_exists($post = $func .'_post')) $data = $post($data);
66 return $data;
67
68 case 'polygon': // Contains count * (linestring) items.
69 $data['count'] = current(unpack(($byte_order ? 'V' : 'N'), fread($fp, 4)));
70 for ($i = 1; $i <= $data['count']; $i++) {
71 $line = _geo_wkb_get_data(NULL, $format, $fp, 'linestring', $byte_order);
72 $data['value'] = $func($line, $data['value']);
73 }
74 if (function_exists($post = $func .'_post')) $data = $post($data);
75 return $data;
76 }
77 }
78
79 /**
80 * WKT format.
81 */
82 function geo_wkb_get_wkt($data, $items = NULL) {
83 $out = '';
84 if (isset($data['x']) && isset($data['y'])) {
85 $out = $data['x'] .' '. $data['y'];
86 if (isset($data['z'])) $out .= ' '. $data['z'];
87 }
88 elseif (isset($data['value'])) {
89 $out = $data['value'];
90 }
91
92 if (isset($items)) {
93 return $items .= ', '. $out;
94 }
95 return $out;
96 }
97
98 function geo_wkb_get_wkt_post($data) {
99 if (isset($data['value'])) {
100 $value = '';
101 if (isset($data['type'])) $value = strtoupper($data['type']);
102 $data['value'] = $value .'('. $data['value'] .')';
103 }
104 return $data;
105 }
106
107 /**
108 * Text format is suitable for many outputs, such as SVG, GeoRSS, etc.
109 */
110 function geo_wkb_get_text($data, $items = NULL) {
111 $out = '';
112 if (isset($data['x']) && isset($data['y'])) {
113 $out = $data['y'] .' '. $data['x'];
114 if (isset($data['z'])) $out .= ' '. $data['z'];
115 }
116 elseif (isset($data['value'])) {
117 $out = $data['value'];
118 }
119
120 if (isset($items)) {
121 return $items .= ' '. $out;
122 }
123 return $out;
124 }
125
126 /**
127 * Array, in case that's all you wanted.
128 */
129 function geo_wkb_get_array($data, $items = NULL) {
130 if (isset($data['x']) && isset($data['y'])) {
131 $ret = array('lon' => $data['x'], 'lat' => $data['y']);
132 }
133 elseif (isset($data['value'])) {
134 $ret = $data['value'];
135 }
136 if (isset($items)) {
137 return array_merge($items, array($ret));
138 }
139 return array($ret);
140 }
141
142 function geo_wkb_get_array_post($data, $items = NULL) {
143 if ($data['type'] == 'point') {
144 $data['value'] = $data['value'][0];
145 $data['lat'] = $data['value']['lat'];
146 $data['lon'] = $data['value']['lon'];
147 }
148 return $data;
149 }
150
151 /**
152 * KML format.
153 * It's actually the same thing as WKT with no post-process.
154 */
155 function geo_wkb_get_kml($data, $items = NULL) {
156 $out = '';
157 if (isset($data['x']) && isset($data['y'])) {
158 $out = $data['x'] .','. $data['y'];
159 if (isset($data['z'])) $out .= ','. $data['z'];
160 }
161 elseif (isset($data['value'])) {
162 $out = $data['value'];
163 }
164
165 if (isset($items)) {
166 return $items .= "\n". $out;
167 }
168 return $out;
169 }

  ViewVC Help
Powered by ViewVC 1.1.2