Saturday 27 May 2017

geoserver - Using WFS protocol in OpenLayers?


Here is my code for getting a WMS layer in OpenLayers from Geoserver.


var wfs2 = new OpenLayers.Layer.Vector("WFS", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.WFS({
version: "1.1.0",

url: "http://localhost:8080/geoserver/wfs",
featurePrefix: "topp",
featureType: "states",
featureNS: "www.openplans.org/topp",
srsName: "EPSG:4326",
geometryName: "position"
})
});

So i get this problem in developer tools for chrome :



XMLHttpRequest cannot load http://localhost:8080/geoserver/wfs. Origin null is not allowed by Access-Control-Allow-Origin. 

in developer tools for chrome


I saw that people recommend to set up a proxy, but my .html is in the geoserver/data_dir/www folder, and don't call external urls.


I however tried it, so I created the folder cgi-bin in /usr/local/geoserver/cgi-bin/, where i put the proxy code I found here :http://trac.osgeo.org/openlayers/wiki/FrequentlyAskedQuestions#ProxyHost


And then I added in the cript of my .html this : OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";


But I then get this error in developer tools :


Failed to load resource file://localhost/cgi-bin/proxy.cgi?url=http%3A%2F%2Flocalhost%3A8080%2Fgeoserver%2Fwfs 

Probably the proxy is not in the right place, but I don't think the first problem (without problem) comes from here.




Answer



You should access it through http protocol. file protocol is not allowed with a WFS server. see this StackOverflow question.


To test geoserver WFS try GetCapabilities request.


For your case try this:


http://localhost:8080/geoserver/wfs?service=wfs&version=1.1.0&request=GetCapabilities.


More info at geoserver manual


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...