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've got a function that takes an Openlayers mouse click and puts the click location into a SQL statement which then gets passed to a JSP script which runs the SQL statement on the PostGIS database and returns geojson. I've grown bleary eyed googling for possible reasons why the line var features = geojson.read(e.responseText) always returns features as null. I'm interested in getting an attribute/property of one of the features in the returned geojson. What am I not understanding about reading the returned geojson?

function getWatershed(e) {
    var geojson = new OpenLayers.Format.GeoJSON();
    // Get the mercator coordinates from the viewport
    var xy = map.getLonLatFromViewPortPx(e.xy);
        // Find the watershed ID.
        var sql  = "select st_asgeojson(the_geom), id, area_sqmi ";
            sql += "from medford.subshed2 ";
            sql += "where st_contains(the_geom,st_transform(st_setsrid(";
            sql += "st_makepoint(" + xy.lon + "," + xy.lat + ")";
            sql += ",900913),26918))";
        OpenLayers.Request.GET({
            async: false,
            url: "./04-sql-to-json.jsp",
            params: {sql: sql},
            success: function(e){
                var features = geojson.read(e.responseText);
                if(features) {
                    if(features.constructor != Array) {
                        features = [features];
                    }
                for(var i=0; i<features.length; ++i) {
                    watid = features[0].attributes.id;  
                }
                }
            }
        });

The geojson returned from the server looks like this (the coordinates were abbreviated):

{"type":"FeatureCollection",
"features":[{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[251083.32142683025,4381268.9660757259,0],[251139.94862688333,4381204.021947667,0]]]]},
"properties":{"area_sqmi":"344.0191",
                     "id":"PU2_3900_3750"
} }
]}

What am I not understanding about reading the returned geojson? Why is features always null?

share|improve this question
    
I'm assuming my problem is likely in the fact that my PostGIS geometry is putting out a z coordinate and my code can't handle that. I'll have to research how to remove the z coordinate from my geojson returned from PostGIS. Anyone know how to remove the z coordinate from a POSTGIS geometry? –  Jan Jun 4 '13 at 14:37
1  
a sidenote, but: you are aware of the Sql-injection risks here? –  atlefren Jun 4 '13 at 14:55
    
Unfortunately, there are so many tutorials on the web on how to accomplish complex tasks for beginners like me (on POSTGIS, javascript, etc), but ALL of them show you these tasks, then tell you "oh, but of course there are inherent SQL injection risks here", but then don't show you how to accomplish those same tasks to avoid SQL injection. No wonder it's so hard for beginners to learn anything secure - the examples we learn from aren't secure. If you can recommend a good source for learning secure javascript for beginners, please advise. Thank you. –  Jan Jun 4 '13 at 15:34
    
I see your problem. What I would suggest is to call your jsp with just the point, sanitize the point in jsp and build the SQL query there. –  atlefren Jun 4 '13 at 20:25

2 Answers 2

Try running your query again, in firefox with firebug og chrome with Developer tools. Have the console-window open and add some console.log-statements to your success-function:

    success: function(e){
        console.log("responseText: ", e.responseText);
        var features = geojson.read(e.responseText);
        console.log("features: ",features);
        if(features) {
            if(features.constructor != Array) {
                features = [features];
            }
            for(var i=0; i<features.length; ++i) {
                watid = features[0].attributes.id;   //btw: you know this is overwritten for each iteration?
            }
        }
    }
share|improve this answer
    
I've been using the firebug console for a while, but console shows the e.responseText similar to the geojson show above. The console log of features says it's null. I assume the failure to read the responseText is due to the z parameter present? –  Jan Jun 5 '13 at 13:37
    
I think I've made some progess: I was able to parse the returned geojson only after I changed my select statement from: var sql = "select st_asgeojson(the_geom), id, area_sqmi "; to var sql = "select st_asgeojson(ST_Force_2D(the_geom)), id, area_sqmi "; –  Jan Jun 5 '13 at 14:30

The solution to reading the geojson as returned from the PostGIS database was to change the Select statement from:

var sql = "select st_asgeojson(the_geom), id, area_sqmi ";

to

var sql = "select st_asgeojson(ST_Force_2D(the_geom)), id, area_sqmi ";

and forcing PostGIS to output only 2 dimensions when it returns the geojson.

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.