-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng2json.php
58 lines (54 loc) · 1.75 KB
/
png2json.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
<?php
function getKMLbox($file) {
$kml = file_get_contents($file);
$p = xml_parser_create();
xml_parse_into_struct($p, $kml, $vals, $index);
xml_parser_free($p);
$box = array();
foreach($vals as $val) {
if ($val["tag"] == "NORTH") {
$box["north"] = $val["value"];
} elseif ($val["tag"] == "SOUTH") {
$box["south"] = $val["value"];
} elseif ($val["tag"] == "EAST") {
$box["east"] = $val["value"];
} elseif ($val["tag"] == "WEST") {
$box["west"] = $val["value"];
};
};
return $box;
};
//$tmp = tempnam("/tmp", "png").".png";
$cli = !isset($_FILES['ufile']['tmp_name']);
if ($cli) {
// if running from command line, get the -f parameter and set the file variables
$args = getopt("f:");
$kml = $args["f"] . ".kml";
$png = $args["f"] . ".png";
//copy($png, $tmp);
} else {
// if running from the web form, get the uploaded files
$kml = $_FILES['ufile']['tmp_name'][0];
$png = $_FILES['ufile']['tmp_name'][1];
move_uploaded_file($png, __DIR__ . "/temp.png");
$png = __DIR__ . "/temp.png";
header('Content-type: application/json');
header("Content-Disposition: attachment; filename=\"coverage.geojson\"");
};
$box = getKMLbox($kml);
$y = $box["south"] - $box["north"];
$x = $box["west"] - $box["east"];
$json = json_decode(shell_exec(__DIR__.'/convert.sh "'.$png.'"'));
$imgsize = getimagesize($png);
$y_factor = $y / $imgsize[1];
$x_factor = $x / $imgsize[0];
foreach ($json->features as &$feature) {
foreach ($feature->geometry->coordinates as &$array) {
foreach ($array as &$coord) {
$coord[0] = $box["east"]+(($imgsize[0]-$coord[0]) * $x_factor);
$coord[1] = $box["north"]+(($imgsize[1]-$coord[1]) * $y_factor);
};
};
};
echo json_encode($json);
?>