4

I'm trying to read some geo-data from PostGIS database using PHP, creating a GeoJSON file and showing it on a base map using OpenLayers.

The PHP code works just fine. The generated data in GeoJSON format is valid (I checked it with jsonlint). When I load the page, the developer tools shows no errors, but also doesn't load any data on the map.

I have just started learning OpenLayers and don't know much about it but saw some examples for this and tried to do the same.

Am I doing something wrong in the Openlayers code?

The code is:

var map = new OpenLayers.Map({
    div: "map",
    layers: [
        new OpenLayers.Layer.WMS(
            "WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0",
            {layers: "basic"}
        ),
        new OpenLayers.Layer.Vector("GJ", {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "gj/DB2GeoJson.php",
                format: new OpenLayers.Format.GeoJSON({
                    extractStyles: true, 
                    extractAttributes: true,
                    maxDepth: 2
                })
            })
        })
    ],
    center: new OpenLayers.LonLat(-112.169, 36.099),
    zoom: 11
});

and the GeoJson file generated is:

{
"type": "FeatureCollection",
"features": [{
    "type": "feature",
    "id": "97",
    "geometry": null,
    "properties": {
        "popn_total": ["97"],
        "boroname": ["Staten Island"]
    }
}, {
    "type": "feature",
    "id": "66",
    "geometry": null,
    "properties": {
        "popn_total": ["66"],
        "boroname": ["Staten Island"]
    }
}, {
    "geometry": ["{\"type\":\"MultiPolygon\",\"coordinates\":[[[[578620.717363209,4495974.81786636],[578535.357633272,4495915.80999248],[578524.626146389,4495908.44992811],[578475.151658836,4495981.94016235],[578550.53822566,4496033.27562186],[578571.6880873,4496047.75223892],[578620.717363209,4495974.81786636]]]]}"]
}]

}

  • 1
    I presume the php script is hosted on a webserver. Had you checked already the webserver's log? You must to assure yourself the script execution, in the OpenLeayers context, was ok or not. Was the generated GeoJson sent to the browser? If not, what is the cause? – Sorin Călinică Feb 23 '14 at 17:26
  • Yes, the PHP script is hosted on a webserver. when I load the Php page myself the code generated in GeoJSON format appears in the browser, so I guess it is functioning correctly... – Catlover Feb 23 '14 at 17:31
  • You really must to check the webserver log and to see if it works when loading the OpenLayers code! – Sorin Călinică Feb 23 '14 at 17:33
  • I'm a beginner here. Could you kindly tell me how I can check the webserver log? I'm using IIS on Windows server 2013 – Catlover Feb 23 '14 at 17:50
  • Unfortunately, I've switched to Apache during the dot-coms boom! Let's hope someone else will guide you. – Sorin Călinică Feb 23 '14 at 18:30
7

Your generated GeoJSON is valid JSON, no problem. But, it's not valid GeoJson, as you can see here: http://geojsonlint.com/

The problem seems to be that the features-array in your featurecollection contains three features, two with geometry:none and one with just geometry (which is also string-encoded).

So, in short: Your OpenLayers-code seems fine, the trouble is your PHP-code that generates the GeoJSON.

  • I agree with you. I'm too lazy to see so deeply ;-) ID:97 and ID:66 – Magno C Feb 25 '14 at 11:34
  • @atlefren: As you mentioned that the generated file is not valid GeoJson. May I ask what is the problem with it? or in another way my question is how does a valid GeoJson file look like? I want to compare and see where exactly is the problem to fix the php code.... – Catlover Feb 25 '14 at 11:48
  • 2
    geojson.org/geojson-spec.html – atlefren Feb 25 '14 at 11:49
  • @atlefren: Thanks. Ok. Now I see lots of differences. btw, is the "id":"97" also a problem that should be removed? – Catlover Feb 25 '14 at 12:06
  • 2
    not exactly sure if it's case sensitive, but in most sane computing environments 'A' and 'a' is two different things, so I would guess. id should be fine. Your problem is the geometry: none fields and the feature with just a geometry (quoted as a string) – atlefren Feb 25 '14 at 12:08
3

Try here: Integrate GeoJSON file with openlayers.protocol.http

See my answer (the accepted one). The code will solve your problem for sure.

After all, try to access your URL (gj/DB2GeoJson.php) using the browser and check it here : http://json.parser.online.fr/

Check for blank spaces and strange characters (PHP is the winner in messing html things). Don't forget to see the page SOURCE, not only what the browser shows.

  • 1
    You're welcome. @atlefren have the correct answer. Your 97 and 66 registers have no geometry, so the OpenLayers have no way to show it. "id":"97","geometry":null – Magno C Feb 25 '14 at 13:46
  • You're right.... – Catlover Feb 25 '14 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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