forked from phayes/geoPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoPHP.inc
151 lines (128 loc) · 4.94 KB
/
geoPHP.inc
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
<?php
/*
* (c) Patrick Hayes
*
* This code is open-source and licenced under the Modified BSD License.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
include_once("lib/GeometryLoader.class.php");
include_once("lib/adapters/GeoAdapter.class.php");
include_once("lib/adapters/GeoJSON.class.php");
include_once("lib/adapters/WKT.class.php");
include_once("lib/adapters/KML.class.php");
include_once("lib/adapters/GoogleGeocode.class.php");
include_once("lib/geometry/Geometry.class.php");
include_once("lib/geometry/Point.class.php");
include_once("lib/geometry/Collection.class.php");
include_once("lib/geometry/LineString.class.php");
include_once("lib/geometry/MultiPoint.class.php");
include_once("lib/geometry/LinearRing.class.php");
include_once("lib/geometry/Polygon.class.php");
include_once("lib/geometry/MultiLineString.class.php");
include_once("lib/geometry/MultiPolygon.class.php");
include_once("lib/geometry/GeometryCollection.class.php");
class geoPHP {
function version() {
return '0.2';
}
// geoPHPLoad($data, $type, $other_args);
function load() {
$args = func_get_args();
$data = array_shift($args);
$type = array_shift($args);
$type_map = geoPHP::getAdapterMap();
$processor_type = $type_map[$type];
$processor = new $processor_type();
// @@TODO: Hack alert!
// There has got to be a better way to do this...
// Is there an equivilent to call_user_func for objects???
if (count($args) == 0) $result = $processor->read($data);
if (count($args) == 1) $result = $processor->read($data, $args[0]);
if (count($args) == 2) $result = $processor->read($data, $args[0],$args[1]);
if (count($args) == 3) $result = $processor->read($data, $args[0],$args[1],$args[2]);
if (count($args) == 4) $result = $processor->read($data, $args[0],$args[1],$args[2], $args[3]);
if (count($args) == 5) $result = $processor->read($data, $args[0],$args[1],$args[2], $args[3], $args[4]);
return $result;
}
function getAdapterMap() {
return array (
'wkt' => 'WKT',
'json' => 'GeoJSON',
'kml' => 'KML',
'google_geocode' => 'GoogleGeocode',
);
}
// Utility Functions
// --------------------------------
function geometryList() {
return array(
'point' => 'Point',
'linestring' => 'LineString',
'linearring' => 'LinearRing',
'polygon' => 'Polygon',
'multipoint' => 'MultiPoint',
'multilinestring' => 'MultiLineString',
'multipolygon' => 'MultiPolygon',
'geometrycollection' => 'GeometryCollection',
);
}
// Reduce a geometry, or an array of geometries, into their 'lowest' available common geometry.
// For example a GeometryCollection of only points will become a MultiPoint
// A multi-point containing a single point will return a point.
// An array of geometries can be passed and they will be compiled into a single geometry
function geometryReduce($geometry) {
// If it's an array of one, then just parse the one
if (is_array($geometry)) {
if (count($geometry) == 1) return geoPHP::geometryReduce($geometry[0]);
}
// If the geometry cannot even theoretically be reduced more, then pass it back
if (gettype($geometry) == 'object') {
$passbacks = array('Point','LineString','LinearRing','Polygon');
if (in_array(get_class($geometry),$passbacks)) {
return $geometry;
}
}
// If it is a mutlti-geometry, check to see if it just has one member
// If it does, then pass the member, if not, then just pass back the geometry
if (gettype($geometry) == 'object') {
$simple_collections = array('MultiPoint','MultiLineString','MultiPolygon');
if (in_array(get_class($geometry),$passbacks)) {
$components = $geometry->getComponents();
if (count($components) == 1) {
return $components[0];
}
else {
return $geometry;
}
}
}
// So now we either have an array of geometries, a GeometryCollection, or an array of GeometryCollections
if (!is_array($geometry)) {
$geometry = array($geometry);
}
$geometries = array();
$geom_types = array();
$collections = array('MultiPoint','MultiLineString','MultiPolygon','GeometryCollection');
foreach ($geometry as $item) {
if (in_array(get_class($item), $collections)) {
foreach ($item->getComponents() as $component) {
$geometries[] = $component;
$geom_types[] = get_class($component);
}
}
else {
$geometries[] = $item;
$geom_types[] = get_class($item);
}
}
$geom_types = array_unique($geom_types);
if (count($geom_types) == 1) {
$class = 'Multi'.$geom_types[0];
return new $class($geometries);
}
else {
return new GeometryCollection($geometries);
}
}
}