Table of Contents for
Web Mapping Illustrated

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Web Mapping Illustrated by Tyler Mitchell Published by O'Reilly Media, Inc., 2005
  1. Web Mapping Illustrated
  2. Cover
  3. Web Mapping Illustrated
  4. A Note Regarding Supplemental Files
  5. Foreword
  6. Preface
  7. Youthful Exploration
  8. The Tools in This Book
  9. What This Book Covers
  10. Organization of This Book
  11. Conventions Used in This Book
  12. Safari Enabled
  13. Comments and Questions
  14. Acknowledgments
  15. 1. Introduction to Digital Mapping
  16. 1.1. The Power of Digital Maps
  17. 1.2. The Difficulties of Making Maps
  18. 1.3. Different Kinds of Web Mapping
  19. 2. Digital Mapping Tasks and Tools
  20. 2.1. Common Mapping Tasks
  21. 2.2. Common Pitfalls, Deadends, and Irritations
  22. 2.3. Identifying the Types of Tasks for a Project
  23. 3. Converting and Viewing Maps
  24. 3.1. Raster and Vector
  25. 3.2. OpenEV
  26. 3.3. MapServer
  27. 3.4. Geospatial Data Abstraction Library (GDAL)
  28. 3.5. OGR Simple Features Library
  29. 3.6. PostGIS
  30. 3.7. Summary of Applications
  31. 4. Installing MapServer
  32. 4.1. How MapServer Applications Operate
  33. 4.2. Walkthrough of the Main Components
  34. 4.3. Installing MapServer
  35. 4.4. Getting Help
  36. 5. Acquiring Map Data
  37. 5.1. Appraising Your Data Needs
  38. 5.2. Acquiring the Data You Need
  39. 6. Analyzing Map Data
  40. 6.1. Downloading the Demonstration Data
  41. 6.2. Installing Data Management Tools: GDAL and FWTools
  42. 6.3. Examining Data Content
  43. 6.4. Summarizing Information Using Other Tools
  44. 7. Converting Map Data
  45. 7.1. Converting Map Data
  46. 7.2. Converting Vector Data
  47. 7.3. Converting Raster Data to Other Formats
  48. 8. Visualizing Mapping Data in a Desktop Program
  49. 8.1. Visualization and Mapping Programs
  50. 8.2. Using OpenEV
  51. 8.3. OpenEV Basics
  52. 9. Create and Edit Personal Map Data
  53. 9.1. Planning Your Map
  54. 9.2. Preprocessing Data Examples
  55. 10. Creating Static Maps
  56. 10.1. MapServer Utilities
  57. 10.2. Sample Uses of the Command-Line Utilities
  58. 10.3. Setting Output Image Formats
  59. 11. Publishing Interactive Maps on the Web
  60. 11.1. Preparing and Testing MapServer
  61. 11.2. Create a Custom Application for a Particular Area
  62. 11.3. Continuing Education
  63. 12. Accessing Maps Through Web Services
  64. 12.1. Web Services for Mapping
  65. 12.2. What Do Web Services for Mapping Do?
  66. 12.3. Using MapServer with Web Services
  67. 12.4. Reference Map Files
  68. 13. Managing a Spatial Database
  69. 13.1. Introducing PostGIS
  70. 13.2. What Is a Spatial Database?
  71. 13.3. Downloading PostGIS Install Packages and Binaries
  72. 13.4. Compiling from Source Code
  73. 13.5. Steps for Setting Up PostGIS
  74. 13.6. Creating a Spatial Database
  75. 13.7. Load Data into the Database
  76. 13.8. Spatial Data Queries
  77. 13.9. Accessing Spatial Data from PostGIS in Other Applications
  78. 14. Custom Programming with MapServer’s MapScript
  79. 14.1. Introducing MapScript
  80. 14.2. Getting MapScript
  81. 14.3. MapScript Objects
  82. 14.4. MapScript Examples
  83. 14.5. Other Resources
  84. 14.6. Parallel MapScript Translations
  85. A. A Brief Introduction to Map Projections
  86. A.1. The Third Spheroid from the Sun
  87. A.2. Using Map Projections with MapServer
  88. A.3. Map Projection Examples
  89. A.4. Using Projections with Other Applications
  90. A.5. References
  91. B. MapServer Reference Guide for Vector Data Access
  92. B.1. Vector Data
  93. B.2. Data Format Guide
  94.  
  95. ESRI Shapefiles (SHP)
  96.  
  97. PostGIS/PostgreSQL Database
  98.  
  99. MapInfo Files (TAB/MID/MIF)
  100.  
  101. Oracle Spatial Database
  102.  
  103. Web Feature Service (WFS)
  104.  
  105. Geography Markup Language Files (GML)
  106.  
  107. VirtualSpatialData (ODBC/OVF)
  108.  
  109. TIGER/Line Files
  110.  
  111. ESRI ArcInfo Coverage Files
  112.  
  113. ESRI ArcSDE Database (SDE)
  114.  
  115. Microstation Design Files (DGN)
  116.  
  117. IHO S-57 Files
  118.  
  119. Spatial Data Transfer Standard Files (SDTS)
  120.  
  121. Inline MapServer Features
  122.  
  123. National Transfer Format Files (NTF)
  124. About the Author
  125. Colophon
  126. Copyright

Steps for Setting Up PostGIS

The PostGIS documentation is the best source for walking through all the steps of setting up PostGIS. This section provides a very brief run-through of steps. Depending on your skill and understanding of PostgreSQL and SQL scripts, these instructions may or may not be enough. If not, please refer to the main PostGIS documentation at http://postgis.refractions.net/docs.

Depending on the method you have used to get the PostgreSQL database set up and PostGIS installed, there will be different steps required to get started. Some installation packages include all the steps required to jump right into using PostGIS. Others don’t, which means you still need to set up the final stages of PostGIS functionality. If you compile PostGIS from source code, you will always have the following steps to walk through. In a nutshell, these steps involve:

  1. Getting PostgreSQL up and running

  2. Enabling pl/pgsql language support

  3. Loading the postgis.sql (or lwpostgis.sql) script

  4. Loading the spatial_ref_sys.sql script

  5. Creating a database

Other steps involving loading and accessing data are covered in later sections.

Getting PostgreSQL Up and Running

You must have a PostgreSQL database service running before setting up PostGIS. PostgreSQL runs in a similar fashion to other enterprise database services (MySQL, Oracle, DB2, SQL Server). It waits for requests to come from a client program and then responds by interacting with databases on the server that the service is running on.

To test that your database service is running, you can use the command-line tool psql with the list parameter to give you a list of available databases:

> psql -l
           List of databases
      Name      |  Owner   | Encoding
----------------+----------+-----------
 template0      | postgres | SQL_ASCII
 template1      | postgres | SQL_ASCII
(2 rows)

This shows two available databases that are part of every PostgreSQL system. The template1 database is the default used as a template for creating a new database. It is copied, and all settings in it are made available to any new database you create. To enable PostGIS functionality for a database, you load PostGIS settings and functions into that database. If you load them into template1, all the PostGIS functionality will be available in every subsequent database you create. Having every database loaded with PostGIS capabilities isn’t always desirable but may be reasonable in some cases.

If you are running the database service on a different computer than the one on which you are running the psql command, you will have to tell psql to connect to that computer. This is done by specifying some further parameters when running the psql command. The most important parameter will be the host name of the computer that is running the service. This is used to direct the psql command to look for the service on that remote computer. The -h option is used, followed by the name of the computer.

> psql -l -h myserver.com

This lists the databases available on the host myserver.com. If a PostgreSQL service isn’t running or isn’t accepting connections from you, you will get an error:

psql: could not connect to server:
   Is the server running on host "myserver.com" and accepting TCP/IP connections on port
5432?

Depending on how your database was installed, you may also need to specify a database username. In the previous example of the psql command, you can see that the user postgres owns the template databases. If you want to connect to the service as the postgres user, you use the -U parameter followed by the username:

> psql -l -h myserver.com -U postgres

There are other parameters that can be added to the command, as you will see in the following examples.

Enabling pl/pgsql Language Support

PostgreSQL has support for internal programming languages that can interact and be embedded within a database. This is a powerful feature of PostgreSQL and is used by PostGIS. Its functions are programmed using the pl/pgsql language. This language isn’t supported by default and needs to be explicitly enabled by running the command:

> createlang plpgsql template1

Specify template1 as the target database to add the language support to.

Loading the postgis.sql Script

With the language support enabled, you can now begin loading the PostGIS functions using the postgis.sql or lwpostgis.sql SQL script.

Tip

SQL scripts, such as postgis.sql, are text files with a list of SQL commands that are used to interact with the database. If you are curious, you can open this file in a text editor or in a web browser and have a look at what commands are being used.

Later you will have to type in some SQL commands to test the database. We’ll also use SQL to load geographic features into the database.

Again, the psql command is used to do this from the command line. Because postgis.sql is a file on the system, you need to know the path to the file or change directories so that it is in the same directory from which you run the command:

> psql -f postgis.sql -d template1

The first parameter specifies the file to load. The second parameter specifies which database to run the SQL commands in. Loading them into template1 makes the resulting database functions available in the new database that will be created.

Information notices and warnings scroll up the screen while this script is being loaded. There are warnings you can ignore, and several status statements acknowledging commands that ran successfully. An all-out failure of the script causes it to halt and provide a daunting error report similar to:

...
psql:postgis.sql:37: ERROR:  function "histogram2d_in" already exists with same argument
types
psql:postgis.sql:42: ERROR:  current transaction is aborted, commands ignored until end
of transaction block
...

These errors are saying that part of PostGIS has already been loaded into this database.

If the command runs properly, there will be many new functions, datatypes, and other settings in the template1 database. There will also be a table called geometry_columns, and another called spatial_ref_sys. You will check this in a moment, but first you need to load one more script.

Loading the spatial_ref_sys.sql Script

The other script is spatial_ref_sys.sql, which populates the spatial_ref_sys table with projection-related information. PostGIS has reprojection (or spatial reference system transformation) capabilities and relies on the data in this table.

You load this script the same way as the postgis.sql script:

> psql -f spatial_ref_sys.sql -d template1

The script inserts over 1,700 records into the spatial_ref_sys table. While this script is running, you will see several INSERT messages, followed by a number on your screen looking something like:

...
INSERT 797517 1
INSERT 797518 1
INSERT 797519 1
INSERT 797520 1
COMMIT

The final COMMIT and no error messages confirms that the table was populated successfully.

Tip

Depending on your version of PostGIS, the final message may actually be VACUUM.

You will get error messages if you haven’t already run postgis.sql/lwpostgis.sql or if you have already loaded spatial_ref_sys.sql.

Testing PostGIS Functionality

Before creating your first database, it is worthwhile to test the template1 database to make sure everything is installed properly. Because template1 is used to create your new databases, checking before you create them makes fixes and clean-up easier.

Some simple tests can be done using the psql command to connect to the template1 database. The psql command lists databases and runs scripts, as shown earlier, but it also provides an interface for typing in SQL commands. The program it launches allows interactive typing of SQL commands and shows query results. To do this, you start psql followed by the name of a database to connect to, in this case, template1:

               > psql template1
Welcome to psql 7.4.2, the PostgreSQL interactive terminal.
 
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help on internal slash commands
       \g or terminate with semicolon to execute query
       \q to quit
 
template1=#

When psql starts up, it tells you the version of the program, gives you a few lines of helpful tips for using the program, and then leaves you with a prompt. The prompt is the name of the connected database followed by =#.

The program is now ready for some SQL (or psql specific) commands. The first test checks to see that the expected PostGIS-related tables have been created. To list all the main objects in the database, type \d or use \dt to list just the tables (t):

template1=# \dt
             List of relations
 Schema |       Name       | Type  | Owner
--------+------------------+-------+---------
 public | geometry_columns | table | postgres
 public | spatial_ref_sys  | table | postgres
(2 rows)

The first script you ran created these tables. If they don’t exist, the script didn’t run properly.

If the spatial_ref_sys.sql script ran properly, there should be records in the spatial_ref_sys table. To check this, you can use an SQL command that counts the number of rows in the table:

template1=# SELECT count(*) FROM spatial_ref_sys;
 count
-------
  1785
(1 row)

Tip

To learn more about SQL commands, see the SQL entry in the WikiPedia at http://en.wikipedia.org/wiki/SQL.

Don’t be confused by the message: (1 row). This just describes the number of lines in the report output. The actual result of the query is 1785, which tells you the table isn’t empty. This looks good. Newer versions of PostGIS will show more entries in this table; for example, v1.0 has 2671 entries.

Tip

How do you know if it should be 1785 or something different? Just run a quick check from the operating system command line to see the number of times INSERT is used in spatial_ref_sys.sql:

> grep -c INSERT spatial_ref_sys.sql1785

The spatial functions added to the database are a large part of PostGIS. To do some further checks, peek at a list of the functions available in template1 to ensure they were loaded properly. Using \df describes all the functions in the database. Supplying a wildcard to search will help show only functions that start with the name postgis. PostGIS functions don’t all start with the word postgis, but this is a simple way to check that the database has some PostGIS-related functions properly loaded. Newer versions of PostGIS will show different results.

template1=# \df postgis*
                 List of functions
 Result data type | Schema |          Name           |
------------------+--------+-------------------------+
 double precision | public | postgis_gist_sel        |
 text             | public | postgis_version         |
 opaque           | public | postgisgistcostestimate |
(3 rows)

The function names are listed in the right column under Name. If one of these functions can be run, PostGIS has probably been installed properly. The postgis_version function is a good one to run as a test. It returns information about the PostGIS installation that is available:

template1=# SELECT postgis_version();
            postgis_version
---------------------------------------
 0.8 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
(1 row)

The output tells what version of PostGIS is being used (0.8). It also shows that it can use the GEOS and PROJ libraries as part of PostGIS. These supporting libraries are used by certain PostGIS functions. Proj libraries are used to reproject or transform coordinates, and GEOS is an advanced geometry engine that allows a whole suite of manipulations and analysis of geometry data types.

To learn more about GEOS, including installation instructions, see the GEOS web site at http://geos.refractions.net/.