I am trying to select database table features within a certain distance from a leafleat generated geosjon using an sql view in geoserver. I first used a GET function and a user here suggested POST. I am getting this error:
GeoJSON.js:221
Uncaught Error: Invalid GeoJSON object.
at De (GeoJSON.js:221:9)
at i.addData (GeoJSON.js:117:15)
at i.initialize (GeoJSON.js:92:9)
at new i (Class.js:22:20)
at Object.Ke (GeoJSON.js:439:9)
at Object.success (app.js:289:29)
at i (jquery-3.2.0.min.js:2:28017)
at Object.fireWith [as resolveWith] (jquery-3.2.0.min.js:2:28783)
at A (jquery-3.2.0.min.js:4:14017)
at XMLHttpRequest.<anonymous> (jquery-3.2.0.min.js:4:16305)
Edit: I updated the function that generates the path to create a geojson object from the l.geojson class then stringified it:
function getRoute_Nature(callback) {
var url = `${geoserverUrl}/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=pgrouting:nature_path&outputformat=application/json&viewparams=source:${source};target:${target};`;
$.getJSON(url, function(data) {
pathLayerNature = L.geoJSON(data, {
style: {
color: '#4CAF50',
weight: 7,
opacity: 1
}
}).addTo(map);
natureGeoJSON = pathLayerNature.toGeoJSON();
natureGjsonString = JSON.stringify(natureGeoJSON);
console.log("geojson: " + natureGeoJSON);
console.log("string: " + natureGjsonString);
if (callback) {
callback();
}
});
}
The SQL view looks like this, not sure if my issue is in the SQL but looks like it should be working, path as a parameter, it will be the "stringified" geojson object:
SELECT
ST_AsGeoJSON(ST_Collect(way)) AS geometry
FROM (
SELECT way FROM planet_osm_polygon
WHERE leisure = 'park' AND ST_DWithin(way::geography, ST_GeomFromGeoJSON('%path%')::geography, 50)
) AS nature_features;
and the function I have right now using POST request is below, I am suspecting I have sonmething wrong here?:
function getNatureFeatures(path) {
// Create a new AJAX request to get the data from the server
$.ajax({
type: "POST",
url: "http://127.0.0.1:8080/geoserver/wfs",
data: {
service: "WFS",
version: "1.0.0",
request: "GetFeature",
typeName: "nature_poi",
outputFormat: "application/json",
cql_filter: "INCLUDE",
viewparams: "path:" + path
},
success: function(data) {
// Create a new geoJSON layer with the data returned from the server
console.log(data)
var natureLayer = L.geoJson(data, {
onEachFeature: function(feature, layer) {
layer.bindPopup(feature.properties.name);
}
});
// Add the layer to the map
natureLayer.addTo(map);
},
error: function(xhr, status, error) {
console.log("Error: " + error);
}
});
}
when I run the code, the console.log(data) returns an xml document error saying: (the number 25 changes depending on how big the path/input geojson is)
<ServiceExceptionReport xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2.0" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd">
<ServiceException code="org.geoserver.wfs.kvp.GetFeatureKvpRequestReader"> 1 feature types requested, but found 25 view params specified. </ServiceException>
</ServiceExceptionReport>
This is how I use all the functions, after a button is clicked:
$('.nature').on('click', function() {
clearAllInfo();
clearAllLayers();
getRoute_Nature(function() {
calculatePathDetails(pathLayerNature, "natureInfo");
getNatureFeatures(natureGjsonString);
});
I get an error saying "Uncaught Error: Invalid GeoJSON object.", I printed the stingified geojson from the getRoute_Nature function and it works fine, returns a valid geojson.
An example of an input geojson path would look like this (this is an example of natureGjsonString):
{"type":"FeatureCollection","features":[{"type":"Feature","id":"nature_path.fid-29856ddd_186d1675796_-678e","geometry":{"type":"MultiLineString","coordinates":[[[-75.691719,45.418201],[-75.691289,45.418379]]]},"geometry_name":"geom","properties":{"seq":3,"id":110550,"name":"Cooper Street","distance":39.00298642840114}},{"type":"Feature","id":"nature_path.fid-29856ddd_186d1675796_-678d","geometry":{"type":"MultiLineString","coordinates":[[[-75.691289,45.418379],[-75.691269,45.418387]]]},"geometry_name":"geom","properties":{"seq":2,"id":112670,"name":"Cooper Street","distance":1.856382386068106}},{"type":"Feature","id":"nature_path.fid-29856ddd_186d1675796_-678c","geometry":{"type":"MultiLineString","coordinates":[[[-75.691269,45.418387],[-75.691301,45.418424]]]},"geometry_name":"geom","properties":{"seq":1,"id":368750,"name":null,"distance":4.825816521280108}}]}
ST_GeomFromGeoJSON
accepts only a single geometry - both logically, as well as structurally: it works on isolated GeoJSON Geometry objects only. So a) your view/query expects a single geometric parameter only, and b) the GeoJSON syntax error is thrown byST_GeomFromGeoJSON
because it can't parse FeatureCollections.