Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
OpenLayers Cookbook
OpenLayers Cookbook

OpenLayers Cookbook: The best method to learn the many ways OpenLayers can be used to render data on maps is to dive straight into these recipes. With a mix of basic and advanced techniques, it's ideal for JavaScript novices and experts alike.

eBook
S$12.99 S$52.99
Paperback
S$66.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

OpenLayers Cookbook

Chapter 2. Adding Raster Layers

In this chapter we will cover the following:

  • Using Google Maps imagery

  • Using Bing imagery

  • Adding WMS layer

  • Wrapping the date line options

  • Changing the zoom effect

  • Changing the layer opacity

  • Using WMS with single tile mode

  • Buffering the layer data to improve the map navigation

  • Creating an image layer

  • Setting the tile size in WMS layers

Introduction


This chapter is all about working with raster layers. We have tried to summarize, with a set of recipes, the most common and important use cases you can find day-to-day when working with OpenLayers.

Imagery is one of the most important kinds of data to work with in a GIS system.

OpenLayers offers several classes to integrate with different imagery providers, from proprietary providers such as Google Maps and Bing Maps, to Open Source ones such as OpenStreetMap or even any WMS service provider.

The base class for any layer type is the OpenLayers.Layer class, which offers a set of common properties and defines the common behavior for any other classes.

In addition, many layers inherit from the OpenLayers.Layer.Grid class, which divides the layer into zoom levels. This way each zoom level covers the same area but uses a greater set of tiles. For example, at level zero a grid with one tile covers the whole world, at level one a grid with four tiles covers the whole world, and so on...

Using Google Maps imagery


Google Maps is probably the most known web map application around the world. Their imageries, in the way of tiled layers, are well known by people; they are accustomed to their layer style and because of this you may be interested in using them in your own web mapping project.

OpenLayers counts with the OpenLayers.Layer.Google class, which is in fact a wrapper code around the Google Maps API, that allows us to use the Google Maps tiles in a homogeneous way within the OpenLayers API.

Note

Do not confuse Google Maps API with the Google Maps imagery. Google Maps API is a bunch of JavaScript code, which is free to use, while the access to the Google Maps imagery has some usage restrictions and, depending on the number of hits, will be subject to some payments.

How to do it...

To use Google Maps imagery, perform the following steps:

  1. Create an HTML file and add the OpenLayers dependencies.

  2. Include the Google Maps API as follows:

    <script type="text/javascript" src="http://maps...

Using Bing imagery


Bing Maps, previously known as Virtual Earth, is the mapping service provided by Microsoft.

In the same way as Google Maps, OpenLayers offers an OpenLayers.Layer.Bing class, which brings us the possibility to add Bing imagery in our projects.

Getting ready

Bing Maps requires you to register as a consumer user. Once registered, you will get an API key needed to initialize the OpenLayers.Layer.Bing layer and that will be used with every request to authenticate you against the Bing Maps service.

Opposite to Google Maps, Bing does not require any JavaScript code and the OpenLayers.Layer.Bing class does not act as a wrapper. Bing Maps offer a REST service to directly access tiles using your API key.

Note

You can find out how to register as a user at http://msdn.microsoft.com/en-us/library/ff428642.aspx.

In addition you can learn about Bing Maps REST Services at http://msdn.microsoft.com/en-us/library/ff701713.aspx.

At this point, it is assumed that you have an API key to be used...

Adding WMS layer


Web Map Service (WMS),is a standard developed by the Open Geospatial Consortium (OGC) implemented by many geospatial servers, among which we can find the free and open source projects GeoServer (http://geoserver.org) and MapServer (http://mapserver.org). More information on WMS can be found at http://en.wikipedia.org/wiki/Web_Map_Service.

As a very basic summary, you can understand a WMS server as a normal HTTP web server that accepts request with some GIS-related parameters (such as projection, bounding box, and so on) and returns a map similar to the following screenshot:

Note

We are going to work with remote WMS servers, so it is not necessary you have one installed. As an advise, note that we are not responsible for these servers and that they may have problems, or are not available when you read this section.

Any other WMS server can be used, but the URL and layer name must be known.

How to do it...

To add a WMS layer, perform the following steps:

  1. Create an HTML file and...

Wrapping the date line options


There might be situations where you do not want your map ends at -180 or +180 longitude degrees as you are working in that area and need a continuous map. For example, imagine a map where on the left you can see the end of Russia and at the right Alaska, as shown in the following screenshot:

This property is a common attribute from base class OpenLayers.Layer and is called the wrapDateLine.

How to do it...

To wrap the date line options, perform the following steps:

  1. Create an HTML file and add the OpenLayers dependency.

  2. In the beginning, we have put a checkbox to activate/deactivate the wrap data line feature, as follows:

    Wrap date line: <input dojoType="dijit.form.CheckBox" checked onChange="wrapDateLine" /> <br/>

    Note

    Do not worry about the dojoType="dijit.form.CheckBox" attribute, it is because the Dojo Toolkit (http://dojotoolkit.org) is used in the sample.

    Think of it as a normal HTML input element.

  3. Next, we have added the DOM element used to render the...

Changing the zoom effect


The panning and zoom effects are very important actions related to the user navigation experience.

In Chapter 1, Web Mapping Basics, the recipe Moving around the map view shows how you can control and create the way the map can be panned.

In the same way you can control the transition effect between two zoom levels on the layers.

The OpenLayers.Layer class has a transitionEffect property, which determines the effect applied to the layer when the zoom level is changed. For the moment only two values are allowed: null and resize.

The null value means no transition effect will be applied, because when you change the zoom level you probably see how the layer disappears until the tiles at the new zoom level are loaded.

With the resize value when we zoom into a level, the current tiles are resized, adapting to the new zoom, until the tiles at the new level are loaded in background. This way images are always visible and we avoid the ugly effect of seeing a blank map for...

Changing the layer opacity


When you are working with many layers—both raster and vector layers—you will probably find situations where a layer that is on top of another layer hides the one below it. This is more common when working with raster WMS layers without the transparent property set to true or tiled layers such as, OpenStreetMaps, Google, and Bing. The layer opacity is set to 50% in the following screenshot:

The OpenLayers.Layer base class has an opacity property, implemented by concrete subclasses, that allows us to modify the opacity of the layers. It is a float value that can range from 0.0 (completely transparent) to 1.0 (completely opaque).

How to do it...

The opacity of the layers can be changed. To change the opacity of the layer, perform the following steps:

  1. Create an HTML file adding the required OpenLayers dependencies.

    Note

    We have intentionally omitted the HTML code required for the slider control. Here, we have focused on the code for OpenLayers. If interested in knowing...

Using WMS with single tile mode


Web Map Service (WMS) is a protocol to serve georeferenced map images.

The basic idea is that, given a bounding box and some other parameters, such as a layer name, the client makes an HTTP request to the WMS server, which computes and returns an image with all the data for the specified layers and within the specified bounding box.

In OpenLayers, when you add a WMS layer to your map, the OpenLayers.Layer.WMS instance is provided with some parameters, such as resolutions and tile size. The WMS server computes the right number of tiles for each zoom level and divides the layer in that number of tiles.

This way, when you add a WMS layer to the map, there is not only one request to the server, but one by each tile that forms the current zoom level.

Note

Dividing the WMS layer in tiles can be better from the server-side point of view when it is configured with a cache system. This way tiles are generated once and served many times.

If you have more than one web mapping...

Buffering the layer data to improve the map navigation


Map navigation is an important factor to take into account to make the user experience better.

When we pan the map, many times we get to see blank areas (meaning that the content is loading) and after a few seconds the image appears.

On gridded layers and WMS layers working in single tile mode, we can improve this at the cost of increasing the requests number or increasing the computation time at the server side.

Note

Most of the raster layers inherit from the base class OpenLayers.Layer.Grid, which is responsible for dividing each zoom level into tiles.

For WMS layers working in single tile mode, the grid is formed only by one tile, which fills the whole map view.

The idea behind improving map navigation is simple; load the tiles outside the map view so that they are loaded before the user pans the map view in that direction.

This recipe shows you how to preload content outside the map view, both for gridded layers and also for WMS layers...

Creating an image layer


Sometimes a tiled layer, such as Google Maps, OpenStreetMap, or WMS, is not what you need. It is quite possible that you have a georeferenced image, knowing its projection and bounding box, and want to render it on the map.

In these cases, OpenLayers offers the OpenLayers.Layer.Image class that allows us to create a layer based on a simple image. A georeferenced image is shown in the following screenshot:

How to do it...

To create an image layer, perform the following steps:

  1. Let's go and create an HTML file with the OpenLayers dependencies.

  2. First, add the div element that will hold the map, as follows:

    <!-- Map DOM element -->
    <div id="ch2_image" style="width: 100%; height: 100%;"></div>
  3. Next, initialize the map and add a WMS base layer, as follows:

    <!-- The magic comes here -->
    <script type="text/javascript">
        // Create the map using the specified DOM element
        var map = new OpenLayers.Map("ch2_image", {
            allOverlays: true
      ...

Setting the tile size in WMS layers


The OpenLayers.Layer.Grid class is a special kind of layer, which divides the layer in different zoom levels composed of a grid of tiles.

The OpenLayers.Layer.WMS class is a subclass of the preceding one and, in addition to working in single tile mode, it can work in tiled mode as well.

Of course, controlling the size of the tiles of the WMS request can affect the performance. By default, the tile size is 256 x 256 pixels, but we can set this to any desired value. Bigger tile sizes means less request to the server but more computation time to generate a bigger image. On the contrary, smaller tile sizes means more server requests and less time to compute smaller images.

How to do it...

To set the tile size, perform the following steps:

  1. Create an HTML file with OpenLayers library dependency.

  2. Add a div element that will hold the map, as follows:

    <!-- Map DOM element -->
    <div id="ch2_tilesize" style="width: 100%; height: 100%;"></div>
  3. Next, initialize...

Left arrow icon Right arrow icon

Key benefits

  • Understand the main concepts about maps, layers, controls, protocols, events etc
  • Learn about the important tile providers and WMS servers
  • Packed with code examples and screenshots for practical, easy learning

Description

Data visualization and analysis has become an important task for many companies. Understanding the basic concepts of GIS and knowing how to visualize data on a map is a required ability for many professionals today. OpenLayers is a JavaScript library to load, display, and render maps from multiple sources on web pages."OpenLayers Cookbook" teaches how to work with OpenLayers, one of the most important and complete open source JavaScript libraries.Through an extensive set of recipes, this book shows how to work with the main concepts required to build a GIS web applicationñ maps, raster and vector layers, styling, theming, and so on."OpenLayers Cookbook" includes problem solving and how-to recipes for the most common and important tasks. A wide range of topics are covered.The range of recipes includes: creating basic maps, working with raster and vector layers, understanding events and working with main controls, reading features from different data sources, styling features, and understanding the underlying architecture."OpenLayers Cookbook" describes solutions and optimizations to problems commonly found.

Who is this book for?

If you are a GIS-related professional with some basic knowledge of web technologies and want to start or gain in-depth knowledge of creating web mapping applications, this book is written for you The recipes are appropriately mixed to suit JavaScript newbies or experts and cover basic to advanced topics on OpenLayers.

What you will learn

  • Create maps, understand projection and other important properties
  • Add raster and vector layers
  • Work with important tile providers
  • Work with OGCs WMS and WFS compliant servers
  • Read/write features from/to different data sources
  • Style features to improve their visualization
  • Understand events and work with the main controls

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 24, 2012
Length: 300 pages
Edition : 1st
Language : English
ISBN-13 : 9781849517850
Vendor :
OpenLayers
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Aug 24, 2012
Length: 300 pages
Edition : 1st
Language : English
ISBN-13 : 9781849517850
Vendor :
OpenLayers
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just S$6 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just S$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total S$ 208.97
OpenLayers 2.10 Beginner's Guide
S$66.99
OpenLayers Cookbook
S$66.99
GeoServer Beginner's Guide
S$74.99
Total S$ 208.97 Stars icon
Banner background image

Table of Contents

8 Chapters
Web Mapping Basics Chevron down icon Chevron up icon
Adding Raster Layers Chevron down icon Chevron up icon
Working with Vector Layers Chevron down icon Chevron up icon
Working with Events Chevron down icon Chevron up icon
Adding Controls Chevron down icon Chevron up icon
Theming Chevron down icon Chevron up icon
Styling Features Chevron down icon Chevron up icon
Beyond the Basics Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(9 Ratings)
5 star 77.8%
4 star 22.2%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Mauro Barattin Feb 26, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Si tratta di un libro molto interessante e che pertanto consiglio a chi è interessato alla presentazione di dati geospaziali.
Amazon Verified review Amazon
NemoM Jun 16, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I work for a County government as the GIS (computer mapping) guy. Our County is very sparsely populated, so we don't have a large budget, but we still need to supply our residents with all that we can. I've been working on setting up web maps; parcels, timber sales, motorized and non-motorized trails, and land sales. A combination of Geoserver, OpenLayers, JavaScript, jQuery, and jQuery Mobile on a VPS running CentOS makes a wonderful map.
Amazon Verified review Amazon
Bette Lamore Sep 10, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Everything I expected.
Amazon Verified review Amazon
Carlos Bello Pauste Oct 23, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book! From the very beginning you can start solving common practical problems and increase your knowledge of the base concepts of OpenLayers progressively, recipe by recipe.If you have a little or no knowledge of OpenLayers and you need to start using it, or you are using OpenLayers and need to speed up your results, or simply you get bored with the technicality and want something more practical... this book is for you.Is it perfect? Of course no: I miss, for example, a recipe for reverse geocoding. However, the practical approximation of the concepts and the wide range of OpenLayers features covered, make of this one a great book.
Amazon Verified review Amazon
ElEucas Oct 16, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent Book!This book provides concise examples to build OpenLayers apps in no time, covering almost every aspect of the OpenLayers API.Only needs geoext examples.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.