2

I am trying to show layer from Geoserver as WFS with OpenLayers, but when I open http://localhost:8080/geoserver/www/Geoinformatika2/index.html, it gives me only OSM map as a base, but there is no polygon that is defined in layer

var osnova = new ol.layer.Tile({ title: 'OSM', type: 'base', visible: true, source: new ol.source.OSM({ layer: 'OSM' })}); var logori = new ol.layer.Vector({ url: 'http://localhost:8080/geoserver/mydb/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=mydb:geoserver&maxFeatures&outputFormat=application/json', format: new ol.format.GeoJSON() }) }); var map = new ol.Map({ target: 'map', controls: ol.control.defaults().extend([ new ol.control.ScaleLine(), new ol.control.ZoomSlider() ]), layers: [ new ol.layer.Group({ 'title':'Osnova', layers:[ osnova ]}), new ol.layer.Group({ title:'Adrse', layers:[logori]}) ], view: new ol.View({ center: ol.proj.fromLonLat([17.86249816417694, 44.60232779122055]), zoom: 8 }) }); 

as the response I get

 {"type":"FeatureCollection","totalFeatures":1,"features":[{"type":"Feature","id":"geoserver.2","geometry" :{"type":"Polygon","coordinates":[[[6414273.98269,5015670.1361],[6414000,5015000.1361],[6416273.98269 ,5017670.1361],[6414273.98269,5015670.1361]]]},"geometry_name":"koordinate","properties":{"stratisteid" :2,"naziv":"s","tip":"logor","zrtve_broj":6000,"tipgeometrije":"Polygon"}}],"crs":{"type":"name" ,"properties":{"name":"urn:ogc:def:crs:EPSG::31276"}}} 

If I try to get it like this

var osnova = new ol.layer.Tile({ title: 'OSM', type: 'base', visible: true, source: new ol.source.OSM({ layer: 'OSM' })}); var logori = new ol.layer.Vector({ source: new ol.source.Vector({ format:new ol.format.WFS({ version:"1.1.0", url: "http://localhost:8080/geoserver/mydb/wfs", featurePrefix: "mydb", featureType: "mydb:geoserver", featureNS: "http://localhost:8080/geoserver/mydb", geometryName: "koordinate", srsName: "EPSG:4326" }) }) }); var map = new ol.Map({ target: 'map', controls: ol.control.defaults().extend([ new ol.control.ScaleLine(), new ol.control.ZoomSlider() ]), layers: [ new ol.layer.Group({ 'title':'Osnova', layers:[ osnova ]}), new ol.layer.Group({ title:'Adrse', layers:[logori]}) ], view: new ol.View({ center: ol.proj.fromLonLat([17.86249816417694, 44.60232779122055]), zoom: 10 }) }); 

it also gives me the only OSM map, but I can't see GET request for WFS layer. I can't figure what is wrong URI of workspace is http://localhost:8080/geoserver/mydb layer name is mydb:geoserver if I try to open layer from the link: http://demo.opengeo.org/geoserver/topp/ows?service=wfs&version=2.0.0&request=GetFeature&typeNames=topp:states&maxFeatures&outputFormat=application/json it shows it with no problem, so it looks like openlayers is not getting layer from geoserver

1
  • Is you geoserver instance on the same machine as your browser? If not a URL pointing to localhost will fail. Commented Dec 6, 2016 at 8:48

1 Answer 1

2

Your data seems to be stored in epsg:31276 (based on the response you do get) but you make no attempt to reproject it into the map projection (epsg:3857) in the first case. So it probably is drawn, just somewhere away from where you expect it.

In the second case, you request the data in lon/lat (epsg:4326) and again don't reproject it so I suspect it is drawn down near Null Island.

I think that if you take out the SRSName line from your WFS request, OL3 will correctly request the data and display it for you.

4
  • You are right I needed to reproject my data, but now it shows it somewhere at 16.7806061097;45.1889317415, but instead it should be at 45.1889317415;16.7806061097 I got this result when I added this line format: new ol.format.GeoJSON({defaultDataProjection:"ESPG:31276"}) Same result I get when I transform with mygeodata.cloud/cs2cs Commented Dec 6, 2016 at 20:47
  • I was unable to get the second case to even show layer at all Commented Dec 6, 2016 at 20:54
  • to change the axis order you can use WFS v 1.1.0 instead of 2.0 Commented Dec 8, 2016 at 11:00
  • the easiest way I found couple minutes ago is to reproject data in GeoServer to ESPG:4326, and then it works great Commented Dec 8, 2016 at 12:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.