forked from geo6/drawing-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn.php
88 lines (72 loc) · 2.06 KB
/
fn.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
<?php
use Geo6\GDAL\ogr2ogr;
/**
*
*/
function export($source, $file, $format, $srs = 'EPSG:4326', $params = array())
{
global $warnings;
$dir = dirname($file);
$fname = basename($file);
switch ($format) {
case "ESRI Shapefile":
$file .= ".shp";
break;
case "GML":
$file .= ".gml";
break;
case "KML":
case "LIBKML":
$file .= ".kml";
break;
case "MapInfo File":
if (isset($params['dsco']) && in_array('FORMAT=MIF', $params['dsco'])) {
$file .= ".mif";
} else {
$file .= ".tab";
}
break;
default:
break;
}
try {
if (!file_exists($dir) || !is_dir($dir)) {
mkdir($dir, 0777, TRUE);
}
$ogr2ogr = new ogr2ogr($file, $source);
$ogr2ogr->setOption('f', $format === 'KML' ? 'LIBKML' : $format);
if ($srs !== 'EPSG:4326') {
$ogr2ogr->setOption('s_srs', 'EPSG:4326');
$ogr2ogr->setOption('t_srs', $srs);
}
foreach ($params as $key => $param) {
if (is_array($param)) {
foreach ($param as $p) {
$ogr2ogr->setOption($key, $p);
}
} else {
$ogr2ogr->setOption($key, $param);
}
}
$ogr2ogr->run();
} catch (Exception $e) {
$warnings[] = $e->getMessage();
}
}
/**
*
*/
function export_proximus($kml, $file) {
global $warnings;
if (!file_exists($kml) || !is_readable($kml)) {
$warnings[] = 'Can\'t open KML file to generate Proximus file.';
} else {
$xml = simplexml_load_string(file_get_contents($kml));
$coordinates = (string) $xml->Document->Document->Placemark->Polygon->outerBoundaryIs->LinearRing->coordinates;
$coordinates = explode(PHP_EOL, $coordinates);
$filter = array_filter(array_map(function($coords) { return trim($coords); }, $coordinates), function ($coords) { return strlen($coords) > 0; });
$transform = array_map(function ($coords) { $c = explode(',', $coords); return implode(',', [$c[1], $c[0]]); }, $filter);
$result = implode(' ', $transform);
file_put_contents($file, '<polygon>' . $result . '</polygon>');
}
}