Book Image

OpenLayers Cookbook

Book Image

OpenLayers Cookbook

Overview of this book

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.
Table of Contents (15 chapters)
OpenLayers Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

We live in the century of information and a lot of this information is susceptible to being represented geographically. This is probably the main feature that has made Geographic Information System (GIS) become one of the most important aspects of many companies. GIS-related technologies are a growing industry and, because of this, GIS has become a desired skill for many professionals.

The universality of the Web and the improvement on the browsers' performance has made it possible for the Web to become a main part of the current GIS and has subtracted the importance of desktop applications because of its capabilities: the browsers allow us to show data visualizations to the masses, create online data editors, and so on.

Nowadays, OpenLayers is the most complete and powerful open source JavaScript library to create any kind of web mapping application. In addition to offering a great set of components, such as maps, layers, or controls, OpenLayers offers access to a great number of data sources using many different data formats, implements many standards from Open Geospatial Consortium (OGC, http://www.opengeospatial.org), and is compatible with almost all browsers.

What this book covers

Chapter 1, Web Mapping Basics, introduces OpenLayers to the reader and describes the basics of how to create a map, how to manage the layers stack, how to work with controls, and how to add OpenLayers within your project.

Chapter 2, Adding Raster Layers, is centered on working and understanding the main raster layers. OpenLayers offers the opportunity to work with main service providers, such as Google or Bing, plus integrating with open source ones, such as OpenStreetMap, or working with WMS servers.

Chapter 3, Working with Vector Layers, explores the power of vector layers and explains how we can load data from different sources using different formats and how we can create features, markers, and popups.

Chapter 4, Working with Events, describes the importance of events and how we can react when they are triggered by the OpenLayers components, such as map or layers, each time a layer is added to the map, a layer is loaded, a feature is added, and so on.

Chapter 5, Adding Controls, explains how to manage controls and describes the most commonly used and important controls the OpenLayers offers to the users: adding or editing features, measuring distances, getting information about features, and so on.

Chapter 6, Theming, describes how OpenLayers is designed to control the appearance of its components. This chapter shows how we can change the position or the controls' look and introduces the basics of creating a complete new theme.

Chapter 7, Styling Features, is completely oriented to show how we can control the features' appearance: the different ways we have to style features, the concept of renderers, styling depending on feature attributes, and so on.

Chapter 8, Beyond the Basics, explores some advanced topics of OpenLayers: work with projections, request remote data, create new controls, and so on. It collects some recipes that show the possibilities OpenLayers offers to developers.

What you need for this book

The fact that OpenLayers is a JavaScript library, which must be integrated within HTML pages, implies that the user must be familiarized with these technologies.

To run the recipes you need to place the code bundle within a HTTP server that serves the pages. All library dependencies required by the recipes code, such as OpenLayers or Dojo toolkit (http://dojotoolkit.org), are included in the bundle itself.

Some recipes request data from PHP scripts included in the source bundle. The function of these scripts is to generate some random data to later integrate within the map. To run these recipes properly the reader needs a HTTP server with PHP support.

Solutions such as XAMPP (http://www.apachefriends.org/en/xampp.html) or Bitnami Stacks (http://bitnami.org/stack/lampstack) are an easy way to install the required stack.

For better user experience, we have created a main application that allows the desired recipe to run and show its source code. Supposing the reader has installed and configured a local web server, and the bundle code is copied within the HTTP server root content folder in the openlayers-cookbook folder, the user can run the main application by accessing the http://localhost/openlayers-cookbook/index.html URL in the browser.

We have made use of the Dojo toolkit in many of the recipes because it allows us to create rich components such as sliders or toggle buttons. Dojo's JavaScript library and CSS files are included in the index.html file, so the HTML recipe files do not have to include them. If the reader plans to run the recipes as standalone web pages, he/she will need to include Dojo's JavaScript library and CSS files in the recipe file, otherwise the recipe will not work properly.

Who this book is for

This book is ideal for GIS-related professionals who need to create web-mapping applications.

From basic to advanced topics, the recipes of this book cover in a direct way the most common issues a user can find in his/her day-to-day job.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The topLayer() and bottomLayer() actions are similar too, they move the specified layer to the top or bottom of the stack."

A block of code is set as follows:

<style> 
    html, body { 
        width: 100%; 
        height: 100%; 
        margin: 0; 
        padding: 0; 
    } 
</style>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The layer opacity is set to 50% in the following screenshot."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.