39,59 €
Over 50 comprehensive recipes to help you create spectacular maps with OpenLayers 3
If you are a GIS-related professional with basic knowledge of web technologies and want to gain in-depth knowledge of creating web mapping applications, then this book is for you. The recipes will be appropriately mixed to suit JavaScript beginners or experts and cover basic to advanced topics on OpenLayers.
OpenLayers 3 is one of the most important and complete open source JavaScript mapping libraries today.
Throughout this book, you will go through recipes that expose various features of OpenLayers 3, allowing you to gain an insight into building complex GIS web applications.
You will get to grips with the basics of creating a map with common functionality and quickly advance to more complicated solutions that address modern challenges. You will explore into maps, raster and vector layers, and styling in depth. This book also includes problem solving and how-to recipes for the most common and important tasks.
This book teaches you how to create stunning maps that are highly interactive and visually appealing with the help of 50 handpicked recipes. Each recipe will address your need to visualize data on a map. Just follow the steps in the recipes to create maps of your choice in no time.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 343
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2012
Second edition: March 2016
Production reference: 1180316
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-775-6
www.packtpub.com
Authors
Peter J. Langley
Antonio Santiago Perez
Reviewer
Jorge Arévalo
Commissioning Editor
Veena Pagare
Acquisition Editor
Kirk D'costa
Content Development Editor
Rashmi Suvarna
Technical Editor
Kunal Chaudhari
Copy Editor
Priyanka Ravi
Project Coordinator
Judie Jose
Proofreader
Safis Editing
Indexer
Tejal Daruwale Soni
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
Peter J. Langley has been developing websites ever since he owned his first computer. He has been working professionally for many years as a lead web developer for various companies and industries as an employee & freelancer. As the influx of available technologies and capabilities in web browsers continues to increase, he has been fortunate enough to play a leading role in the software engineering of some sophisticated solutions, such as web-based GIS applications for Britain's mapping agency, Ordnance Survey.
Peter is passionate about the Internet, computing, and software engineering principles. He enjoys working on engaging projects in vibrant atmospheres that quickly deliver value to consumers. He has been sharing how-to guides on his website, www.codechewing.com, for many years. This is a demonstration of his personal desire to encourage people to passionately unite knowledge and thrive from each other's experiences, interests, and perspectives.
I would like to thank my partner, Alanna, for all her dependable support and positivity during the undertaking of this book.
Antonio Santiago Perez is a computer science professional with more than 10 years of experience in designing and implementing systems. Since the beginning of his professional life, his work has been always related to the world of meteorology while working for different companies as an employee or a freelancer. He has experience in development of systems that collect, store, transform, analyze, and visualize data, and he is actively interested in any GIS-related technology with a preference for data visualization. His main field of experience is the Java ecosystem, and he has also actively worked with many related web technologies while looking to improve the client side of web applications. He is a firm believer in software engineering practices and is a follower of agile methodologies, involving customers as the main key to the project's success.
Jorge Arévalo is a computer engineer from Universidad Autónoma de Madrid, UAM. He started developing web applications with JS, PHP, and Python in 2007. In 2010, he began collaborating with PostGIS and GDAL projects after participating in GSoC 2009, creating the PostGIS Raster GDAL driver. He currently works as a technology trainer and Python/Django developer. He also organizes hackathons with others at http://hackathonlovers.com/. He has cowritten the book Zurb Foundation 4 Starter, for Packt Publishing. He has also worked as a reviewer for the books, PostGIS 2.0 Cookbook, OpenLayers 3: Beginner's Guide, Memcached, Speed Up Your Web Application, and QGIS Cookbook. All of these were published by Packt Publishing.
I want to thank my wife, Elena, for her continuous love and support while reviewing this book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Geographical awareness is an exciting and reassuring concept in general for many people. If a friend were to ask you where you live, you'd normally have a genuine desire to describe where. Better yet, you could show them where you live on a (digital) map. Not only is this exciting, but it's also often extremely relevant and useful to include spatial data in products or services to reveal exactly where something is. We live in a time where information is in abundance, and a lot of this information is susceptible to being represented geographically.
The ubiquity of the Web and the improvement in various browsers' performance has made it possible for the Web to become a major player in the GIS field. It can rival desktop applications because of its capabilities. Browsers now allow us to show data visualizations to the masses, create online data editors, and so on.
Nowadays, OpenLayers isn't without its competing libraries. However, with the new API (v3), it's better focused for mobile, and it remains a comprehensive mapping 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 also offers access to a great number of data sources using many different data formats and implements many standards from the Open Geospatial Consortium (OGC) (http://www.opengeospatial.org).
Chapter 1, Web Mapping Basics, demonstrates how creating a simple full-screen map is made easy with OpenLayers. This chapter will take a look at some basic map options and controls that help a user get around the map and view different locations. You will gain an understanding of the basic concepts behind a web-based map.
Chapter 2, Adding Raster Layers, talks about integrating with external services for data and how mapping imagery is an integral part of any mapping application on the Web. You will learn how to utilize tiles and WMS mapping services from a variety of external providers and customize the URL requests being made to these third parties. You will also discover how to customize animation effects for map interactions and modify default layer properties, which include simple performance techniques to preload data.
Chapter 3, Working with Vector Layers, teaches you how to integrate detailed GIS data from external sources into your own map layers to build useful sets of information for users. You will learn how to create custom layers with performance strategies for high volumes of data, how to integrate with sources in a variety of different formats, how to package layer data for export, how to interact with features on these layers, and also some basic feature manipulation.
Chapter 4, Working with Events, explains how you need to know how to handle triggered events like in any other JavaScript Web application. You will learn how to create event handlers for a variety of different events that are sourced from map navigation, feature modifications, keyboard interaction, and more.
Chapter 5, Adding Controls, teaches you how to create capabilities for users to get on with their intended tasks around a customized mapping application. You will learn new ways to interact with the map and manipulate features on layers. You will also learn how to customize the mapping experience for your target users by adding helpful map controls to achieve known tasks.
Chapter 6, Styling Features, will help you create a personalized mapping application that can be fully customized to meet users' expectations. You will learn how to set static layer-wide styling rules, as well as dynamic styling that is based on geometry types or feature attributes. You will take advantage of styling features differently during specific user actions, such as on selection, when dragged, or while being drawn.
Chapter 7, Beyond the Basics, shows you how to take the foundational skills that you learned in the previous chapters to the next level by taking on new and advanced functionality to create full-featured mapping applications. You will learn what the canvas renderer is capable of, how to build a custom version of the library, how to create features in freehand mode directly on the map, and how to work with other projections.
The fact that OpenLayers is a JavaScript library, which must be integrated within HTML pages, implies that the user must be familiar with these technologies.
To successfully view all the recipes in this book in action, you need to have Node.js (https://nodejs.org) installed on your machine, as we use an HTTP server written in Node.js. Please follow the README.md instructions inside the bundle for more details, where you'll be instructed to install the required Node.js packages (these are listed in the package.json file).
Once you've installed these dependencies and initiated the server, you can access a chapter topic from any browser at http://localhost:3000/ch03/ch03-gml-layer.
All library dependencies required by the code of various recipes, such as OpenLayers or jQuery, are included in the bundle itself.
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 the most common issues a user may find during their career in a direct way.
In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in 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 at www.packtpub.com/authors.
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.
You can download the example code files for this book 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.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/OpenLayers3xCookbook_ColorImages.pdf.
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 could 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/submit-errata, 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.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted 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 <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
In this chapter, we cover the following topics:
This chapter shows us the basics and the important things that we need to know when we start creating our first web-mapping application with OpenLayers.
As we will see in this and the following chapters, OpenLayers is a big and complex framework, but at the same time, it is also very powerful and flexible.
Although we're now spoilt for choice when it comes to picking a JavaScript mapping library (as we are with most JavaScript libraries and frameworks), OpenLayers is a mature, fully-featured, and well-supported library.
In contrast to other libraries, such asLeaflet (http://leafletjs.com), which focuses on a smaller download size in order to provide only the most common functionality as standard, OpenLayers tries to implement all the required things that a developer could need to create a web Geographic Information System (GIS) application.
One aspect of OpenLayers 3 that immediately differentiates itself from OpenLayers 2, is that it's been built with theGoogle Closure library (https://developers.google.com/closure). Google Closure provides an extensive range of modular cross-browser JavaScript utility methods that OpenLayers 3 selectively includes.
OpenLayers 3 packs a smaller footprint than its predecessor and targets the latest HTML5 and CCS3 capabilities. The trade off, of course, is that legacy browsers will not be as fully featured (primarily, Internet Explorer lower than version 9). As the rate of modern browser adoption ever increases, this disadvantage will soon become a moot point.
The main concept in OpenLayers is, rightly, the map. It represents the view where information is rendered. The map can contain multiple layers, which can be raster or vector layers. Each layer has a data source that serves data with its own format: a .PNG image, a .KML file, and so on. In addition, the map can contain controls, which help interact with the map and its contents; these are pan, zoom, feature selection, and so on.
Let's get started with learning OpenLayers by examples.
An OpenLayers map allows us to visualize information from different kinds of layers, and it brings us methods to manage the layers that are attached to it.
In this recipe, we'll learn some techniques on how to control the layers: adding, grouping, managing the stack order, and other layer manipulation. Learning these very common operations is important because these types of tasks will be required on almost every web-mapping application.
The application will display a map on the left and a control panel on the right with a list of layers, which can be dragged, that you'll be able to sort. Here's what we'll end up with:
