Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying GeoServer Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
GeoServer Cookbook

GeoServer Cookbook

By : Iacovella
4.8 (6)
close
close
GeoServer Cookbook

GeoServer Cookbook

4.8 (6)
By: Iacovella

Overview of this book

This book is ideal for GIS experts, developers, and system administrators who have had a first glance at GeoServer and who are eager to explore all its features in order to configure professional map servers. Basic knowledge of GIS and GeoServer is required.
Table of Contents (11 chapters)
close
close
10
Index

Using WFS vendor parameters

The previous recipes used standard WFS requests. GeoServer also supports a few optional parameters that you can include in your requests. In this recipe, we will see how to ask GeoServer, which is reprojecting the data from the native SRS to another SRS, to use a vendor parameter.

Reprojection of data is a part of WFS 1.1.0 and 2.0.0, and GeoServer has provided support since 1.0.0 so that you can use it with any WFS version. The following screenshot is what we're targeting in this recipe:

Using WFS vendor parameters

Tip

You can find the full source code for this recipe in the code bundle available from the Packt site for this book; look for the ch01_wfsReprojection.html file.

How to do it…

  1. Copy the file used in the first recipe to the wfsReprojection.html file in the same folder. Insert a new parameter for the Map object:
          projection: "EPSG:3857",
  2. Then, alter the JavaScript part when creating the WFS layer:
            new OpenLayers.Layer.Vector("countries", {
              strategies: [new OpenLayers.Strategy.BBOX()],
              protocol: new OpenLayers.Protocol.WFS({
                url: "http://localhost/geoserver/wfs",
                featureType: "countries",
                featureNS: "http://www.naturalearthdata.com/",
                geometryName: "geom",
  3. Add a parameter to request data reprojection:
              srsName: new OpenLayers.Projection("EPSG:3857"),
              srsNameInQuery: true
            }),
  4. Save the file and point your browser to it. You should get a map that looks like the one shown in the introduction to this recipe.

How it works…

Using a vendor parameter is really straightforward; you just add it to your request. In our recipe, we want to use the countries' data that is stored in the EPSG:4326 projection, which is the geographical coordinates, and in EPSG:3857, which is the planar coordinates. First of all, we set the spatial reference system for the map:

  map = new OpenLayers.Map({
    div: "myMap",
    allOverlays: true,
    projection: "EPSG:3857",

Then, we create the WFS request for data by inserting the srsName parameter and assigning it the same projected coordinate system used for the map. The Boolean parameter srsNameInQuery is really important, as it defaults to false. If you don't set it, OpenLayers will not ask for reprojection when using WFS 1.0.0:

            srsName: new OpenLayers.Projection("EPSG:3857"),
            srsNameInQuery: true

Let's see what happens when you load the page in your browser and the OpenLayers framework creates the WFS request. Use Firebug to capture the XML code sent to GeoServer with the GetFeature request. The Query element contains the srsName parameter that forces GeoServer to project data:

<wfs:Query typeName="feature:countries_1" srsName="EPSG:3857" xmlns:feature="http://www.naturalearthdata.com/">
  <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
    <ogc:BBOX>
      <ogc:PropertyName>geom</ogc:PropertyName>
      <gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:3857">
        <gml:coordinates decimal="." cs="," ts=" ">-13325909.428711,-3545545.6572266 16025909.428711,14065545.657227</gml:coordinates>
      </gml:Box>
    </ogc:BBOX>
  </ogc:Filter>
</wfs:Query>
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY