-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathparse.php
180 lines (157 loc) · 5.43 KB
/
parse.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
175
176
177
178
179
180
<?php
ini_set('memory_limit', '2048m');
include('twd97tolatlng.php');
class Parser
{
public function fetchByte($size)
{
$ret = substr($this->data, 0, $size);
$this->data = substr($this->data, $size);
return $ret;
}
/**
* 把 Well-Known-Binary 轉換成 GeoJSON,程式簡化自 https://github.com/phayes/geoPHP
*
* @param string $value 16進位字串
* @access public
* @return Object
*/
public function parseWKB($value)
{
$this->data = pack('H*', $value);
return $this->getGeometry();
}
public function getGeometry()
{
$base_info = unpack("corder/ctype/cz/cm/cs", $this->fetchByte(5));
$ret = new StdClass;
switch ($base_info['type']) {
case 1:
$ret->type = 'Point';
$ret->coordinates = $this->getPoint();
break;
case 2:
$ret->type = 'LineString';
$ret->coordinates = $this->getLinstring();
break;
case 3:
$ret->type = 'Polygon';
$ret->coordinates = $this->getPolygon();
break;
case 4:
$ret->type = 'MultiPoint';
$ret->coordinates = $this->getMulti('point');
break;
case 5:
$ret->type = 'MultiLineString';
$ret->coordinates = $this->getMulti('line');
break;
case 6:
$ret->type = 'MultiPolygon';
$ret->coordinates = $this->getMulti('polygon');
break;
case 7:
$ret->type = 'GeometryCollection';
$ret->coordinates = $this->getMulti('geometry');
break;
}
return $ret;
}
public function getPoint() {
$point_coords = unpack("d*", $this->fetchByte(32));
//return twd97_to_latlng($point_coords[1], $point_coords[2]);
return array($point_coords[1], $point_coords[2]);
}
public function getLinstring() {
$line_length = unpack('L', $this->fetchByte(4));
if (!$line_length[1]) {
return [];
}
$line_coords = unpack('d*', $this->fetchByte($line_length[1] * 32));
$components = array();
$i = 1;
$num_coords = count($line_coords);
while ($i <= $num_coords) {
//$components[] = twd97_to_latlng($line_coords[$i], $line_coords[$i + 1]);
$components[] = array($line_coords[$i], $line_coords[$i + 1]);
$i += 4;
}
return $components;
}
public function getPolygon() {
$poly_length = unpack('L', $this->fetchByte(4));
$components = array();
$i = 1;
while ($i <= $poly_length[1]) {
$components[] = $this->getLinstring();
$i++;
}
return $components;
}
public function getMulti($type) {
$multi_length = unpack('L', $this->fetchByte(4));
$components = array();
$i = 1;
while ($i <= $multi_length[1]) {
if ('geometry' == $type) {
$components[] = $this->getGeometry();
} else {
$components[] = $this->getGeometry()->coordinates;
}
$i++;
}
return $components;
}
public function combine($columns, $values)
{
$numeric = ['area', 'max_x', 'max_y', 'min_x', 'min_y', 'x', 'y', 'sort', 'shape_leng', 'shape_le_1', 'shape_area'];
$int = ['objectid', 'oorig_fid'];
$feature_obj = new StdClass;
$feature_obj->type = 'Feature';
$feature_obj->properties = new StdClass;
foreach (array_combine($columns, $values) as $k => $v) {
if (in_array($k, $numeric)) {
$feature_obj->properties->{$k} = doubleval($v);
} elseif (in_array($k, $int)) {
$feature_obj->properties->{$k} = intval($v);
} elseif ('the_geom' == $k) {
$feature_obj->geometry = $this->parseWKB($v);
} else {
$feature_obj->properties->{$k} = $v;
}
}
$feature_obj->id = $feature_obj->properties->villcode;
return $feature_obj;
}
public function main($file)
{
$fp = popen('shp2pgsql -W big5-2003 ' . escapeshellarg($file), 'r');
$villages = [];
while (false !== ($line = fgets($fp))) {
if (!preg_match('#^INSERT INTO "[^"]*" \(([^)]*)\) VALUES \((.*)\)#', $line, $matches)) {
continue;
}
$columns = array_map(function($r) {
return trim($r, '"');
}, explode(',', $matches[1]));
$values = array_map(function($r) {
return NULL == $r ? null : trim($r, '\'');
}, explode(',', $matches[2]));
$village = $this->combine($columns, $values);
$villages[] = $village;
}
$json = new StdClass;
$json->type = 'FeatureCollection';
$json->link = 'https://github.com/ronnywang/twgeojson';
$json->data_time = '2012';
$json->data_source = 'http://tgos.nat.gov.tw/tgos/Web/Metadata/TGOS_MetaData_View.aspx?MID=36646&SHOW_BACK_BUTTON=false';
$json->description = '101.10.30台澎金馬村里界';
$json->features = $villages;
file_put_contents('output.json', json_encode($json, JSON_UNESCAPED_UNICODE));
}
}
if (!$file = $_SERVER['argv'][1]) {
die("請用 parseshp.php [file.shp]\n");
}
$parser = new Parser;
$parser->main($file);