Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm using this PHP PostGIS to geoJSON script: https://gist.github.com/bmcbride/1913855

But I keep getting this error: 'missing required parameter: geotable'

This is my code:

  # Retrive URL variables
    if (empty($_GET['WTGs_All'])) {
        echo "missing required parameter: <i>geotable</i>";
        exit;
    } else
        $geotable = $_GET['WTGs_All'];

    if (empty($_GET['the_geom'])) {
        echo "missing required parameter: <i>geomfield</i>";
        exit;
    } else
        $geomfield = $_GET['the_geom'];

    if (empty($_GET['srid'])) {
        $srid = '4326';
    } else
        $srid = $_GET['srid'];

    if (empty($_GET['fields'])) {
        $fields = '*';
    } else
        $fields = $_GET['fields'];

    $parameters = $_GET['parameters'];

    $orderby    = $_GET['orderby'];

    if (empty($_GET['sort'])) {
        $sort = 'ASC';
    } else
        $sort = $_GET['sort'];

    $limit      = $_GET['limit'];

    $offset     = $_GET['offset'];

    # Connect to PostgreSQL database
    $conn = pg_connect("dbname='postgres' user='postgres' password='****' port='5432' host='localhost'");
    if (!$conn) {
        echo "Not connected : " . pg_error();
        exit;
    }
# Build SQL SELECT statement and return the geometry as a GeoJSON element in EPSG: 4326
$sql = "SELECT " . pg_escape_string($fields) . ", st_asgeojson(transform(" . pg_escape_string($geomfield) . ",$srid)) AS geojson FROM " . pg_escape_string($geotable);
if (strlen(trim($parameters)) > 0) {
    $sql .= " WHERE " . pg_escape_string($parameters);
}
if (strlen(trim($orderby)) > 0) {
    $sql .= " ORDER BY " . pg_escape_string($orderby) . " " . $sort;
}
if (strlen(trim($limit)) > 0) {
    $sql .= " LIMIT " . pg_escape_string($limit);
}
if (strlen(trim($offset)) > 0) {
    $sql .= " OFFSET " . pg_escape_string($offset);
}
//echo $sql;

# Try query or error
$rs = pg_query($conn, $sql);
if (!$rs) {
    echo "An SQL error occured.\n";
    exit;
}

# Build GeoJSON
$output    = '';
$rowOutput = '';

while ($row = pg_fetch_assoc($rs)) {
    $rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $row['geojson'] . ', "properties": {';
    $props = '';
    $id    = '';
    foreach ($row as $key => $val) {
        if ($key != "geojson") {
            $props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . escapeJsonString($val) . '"';
        }
        if ($key == "id") {
            $id .= ',"id":"' . escapeJsonString($val) . '"';
        }
    }

    $rowOutput .= $props . '}';
    $rowOutput .= $id;
    $rowOutput .= '}';
    $output .= $rowOutput;
}

$output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}';
echo $output;
?>

My table is called WTGs_All, I don't know what am I doing wrong. Thanks.

share|improve this question
    
Are you passing a WTGs_All parameter in your get request? if so, what is it? –  Devdatta Tengshe Jun 13 '13 at 14:51
    
Hm, I'm new with PHP so I'm not sure what you're thinking about. I put all code now so please check it out. Maybe I'm using this code in a wrong way. Thanks. –  againstflow Jun 13 '13 at 15:02
    
This php script is a service. You need to pass in parameters and you will get the results according to the parameters. Without these parameters, you will get the error that you are seeing. –  Devdatta Tengshe Jun 13 '13 at 15:18
    
Can you explain me a little bit more how can I do that? I need to convert postgis data to geojson so I can add it to Leaflet map. I worked with Leaflet hundred times but didn't work with postgis data when working with Leaflet. I would appreciate any help. –  againstflow Jun 13 '13 at 15:29

2 Answers 2

Since you do not know much of Php, I would suggest that you use the original Php script. Do not change anything except for the connection parameters on the Line 61. Then place the postgis_geojson.php in your webserver.

Call the geojson in your Leafleat JavaScript code with the following get parameters:

postgis_geojson.php?geotable=WTGs_All&geomfield=the_geom

This will return all the features, with all the attributes in EPSG:4326

share|improve this answer
    
Should I call the geojson this way? var geojson = L.geojson('conn.php?geotable=WTGs_All&geomfield=the_geom', { style: style, maxZoom: 6, onEachFeature: onEachFeature }).addTo(map); –  againstflow Jun 13 '13 at 16:37
    
Yes, have you tried that? –  Devdatta Tengshe Jun 14 '13 at 1:24

You have unnecessarily changed the keys ($_GET['...']) for parameters. They should be left in original state, as this script is designed to read their values from the query string. The code should be:

# Retrive URL variables
if (empty($_GET['geotable'])) {
    echo "missing required parameter: <i>geotable</i>";
    exit;
} else
    $geotable = $_GET['geotable'];

if (empty($_GET['geomfield'])) {
    echo "missing required parameter: <i>geomfield</i>";
    exit;
} else
    $geomfield = $_GET['geomfield'];

And the request:

postgis_geojson.php?geotable=WTGs_All&geomfield=the_geom
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.