Tuesday, 12 November 2019

openlayers 2 - WFS reprojection problems


In geoserver i have a layer in EPSG:4326. In OpenLayers i add it on map as WFS:


            markerLayer = new OpenLayers.Layer.Vector('Markers',{
strategies: [new OpenLayers.Strategy.BBOX()],
protocol: new OpenLayers.Protocol.WFS({

version: "1.1.0",
url: "/geoserver/wfs",
srsName:"EPSG:4326",
featureType: "markers",
featureNS: "http://www.opengeospatial.net/cite",
}),
projection:new OpenLayers.Projection("EPSG:900913"),
styleMap: new OpenLayers.StyleMap({
externalGraphic: url_servlet+'img/marker.png',
graphicWidth: 20, graphicHeight: 24, graphicYOffset: -24,

title: '${tooltip}'
})/*,
eventListeners: {
beforefeatureadded: function(e) {
e.feature.attributes.annotation =
window.prompt("annotation", "no comment");
}
}*/
});
app.mapPanel.map.addLayers([markerLayer]);


My map in OL in EPSG:900913. So when i says to WFS projection=900913 i exepcted that features reproject and will show on my map. But its show somewhere outside OSM extent. And in WFS layer i see features with coords like this:


    POINT(62.01759636987174 56.767130970984326)

So how reproject wfs correctly?


UPDATE


I change protocol to HTTP and get gml:


            protocol:new OpenLayers.Protocol.HTTP({
url: "/geoserver/ows?service=WFS&request=GetFeature&typeName=cite:markers&srsName=EPSG:900913&outputFormat=GML2",
format: new OpenLayers.Format.GML()

}),

And now i get something interesting: My layers and Google Base layer: enter image description here


Same place with OSM base layer:


enter image description here


You see that there is no more red markers. And i have only question What a hell going on?



Answer



The projection of the WFS vector layer needs to be set to the projection of the data. i.e 4326 and not 900913.


OpenLayers will reproject the data on-the-fly, if the projection of the Map and the Projection of the data have been set correctly.


Please see this working sample for more details: WFS Reprojection Example



The following sample code shows how you can have a simple map with just an OSM layer, and your WFS layer. Make sure that you point the proxy to your own properly configured proxy.


var map;

function init() {
var geographic = new OpenLayers.Projection("EPSG:4326");
var mercator = new OpenLayers.Projection("EPSG:3857");
OpenLayers.ProxyHost = "/cgi-bin/proxy_Open.cgi?url=";

map = new OpenLayers.Map('map', {
projection: mercator,

layers: [new OpenLayers.Layer.OSM()],
center: new OpenLayers.LonLat(21.2442, -28.464324)
// Map uses web mercator as projection, so we have to
// transform our coordinates
.transform('EPSG:4326', 'EPSG:3857'),
zoom: 10
});

var wfs = new OpenLayers.Layer.Vector("Points", {
strategies: [new OpenLayers.Strategy.BBOX()],

protocol: new OpenLayers.Protocol.WFS({
version: "1.0.0",
srsName: "EPSG:4326", // this is the default projection of the data
featurePrefix: 'za',
url: "http://demo.opengeo.org/geoserver/wfs",
featureType: "za_points"
}),
projection: geographic // specified because it is different than the map
});


map.addLayer(wfs);

}

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...