Completed
Push — master ( d8c574...a11679 )
by Yannick
07:12
created

airspace-geojson.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
require_once('require/class.Connection.php');
3
include_once('require/libs/geoPHP/geoPHP.inc');
4
5
if (isset($_GET['download']))
6
{
7
	header('Content-disposition: attachment; filename="airspace.geojson"');
8
}
9
header('Content-Type: text/javascript');
10
11
$Connection = new Connection();
12
13
if (!$Connection->tableExists('airspace')) {
14
    die;
15
}
16
17
if (isset($_GET['coord'])) 
18
{
19
	$coords = explode(',',$_GET['coord']);
20
        if ($globalDBdriver == 'mysql') {
21
		$query = "SELECT *, ST_AsWKB(SHAPE) AS wkb FROM airspace WHERE ST_Intersects(SHAPE, ST_Envelope(linestring(point(:minlon,:minlat), point(:maxlon,:maxlat))))";
22
		try {
23
			$sth = $Connection->db->prepare($query);
1 ignored issue
show
The method prepare cannot be called on $Connection->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
24
			$sth->execute(array(':minlon' => $coords[0],':minlat' => $coords[1],':maxlon' => $coords[2],':maxlat' => $coords[3]));
25
			//$sth->execute();
1 ignored issue
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
		} catch(PDOException $e) {
27
			echo "error";
28
		}
29
	} else {
30
		$query = "SELECT *, ST_AsBinary(wkb_geometry,'NDR') AS wkb FROM airspace WHERE wkb_geometry && ST_MakeEnvelope(".$coords[0].",".$coords[1].",".$coords[2].",".$coords[3].",4326)";
31
		try {
32
			$sth = $Connection->db->prepare($query);
1 ignored issue
show
The method prepare cannot be called on $Connection->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
33
			//$sth->execute(array(':minlon' => $coords[0],':minlat' => $coords[1],':maxlon' => $coords[2],':maxlat' => $coords[3]));
1 ignored issue
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
			$sth->execute();
35
		} catch(PDOException $e) {
36
			echo "error";
37
		}
38
	}
39
} else {
40
        if ($globalDBdriver == 'mysql') {
41
		$query = "SELECT *, ST_AsWKB(SHAPE) AS wkb FROM airspace";
42
	} else {
43
		$query = "SELECT *, ST_AsBinary(wkb_geometry,'NDR') AS wkb FROM airspace";
44
	}
45
	try {
46
		$sth = $Connection->db->prepare($query);
1 ignored issue
show
The method prepare cannot be called on $Connection->db (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
47
		$sth->execute();
48
	} catch(PDOException $e) {
49
		echo "error";
50
	}
51
}
52
53
$geojson = array(
54
    'type' => 'FeatureCollection',
55
    'features' => array()
56
);
57
58
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
59
{
60
		date_default_timezone_set('UTC');
61
		$properties = $row;
62
		unset($properties['wkb']);
63
		unset($properties['SHAPE']);
64
		if ($globalDBdriver == 'mysql') {
65
			$geom = geoPHP::load($row['wkb']);
66
		} else {
67
			$geom = geoPHP::load(stream_get_contents($row['wkb']));
68
		}
69
		$feature = array(
70
		    'type' => 'Feature',
71
		    'geometry' => json_decode($geom->out('json')),
72
		    'properties' => $properties
73
		);
74
		array_push($geojson['features'], $feature);
75
}
76
print json_encode($geojson, JSON_NUMERIC_CHECK);
77
78
?>
1 ignored issue
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...