GeoServer¶
In this section, we are going to connect GeoServer to the tables we have loaded into PostGIS, style the data, and view it in the default GeoServer layer viewer.
GeoServer Data Concepts¶
GeoServer is a piece of middleware, that provides various data sources (shape files, database tables, images) to network users as internet services (WMS, WFS, etc). This conversion of file formats and database drivers into simple services makes web application development simpler – web developers no longer have to care what formats or projections the source data are in, because the service abstracts that complexity away.
The abstraction is built on some core concepts that all data sources and services in GeoServer share:
- Workspaces
- A workspace name-space is just a tag, a logical folder, under which feature types are grouped. The work-space in GeoServer affects how WMS/WFS layers are named, and the XML of the GML features emitted by GeoServer.
- Stores
- Each source data data, whether it be a directory full of shape files, or a database instance, is considered a “Stores”. File directories are configured by providing a path. Databases are configured by providing connection information.
- Layers
- Each individual shape file, or spatial database table, is considered a “Layer”.
- Layer Groups
- Multiple Layers can be bound into groups, that can then be referenced by a single name. Useful for creating base maps or other cartographic outputs that are composed of multiple layers.
- Styles
- A style is applied to a Layer to create a cartographic output. The output might be an image, or it might be a KML file, or it might be some other format (like SVG) that provides a way of viewing data for end-users.
We are going to configure our PostGIS database as a Store, add the spatial tables in the database as Layers, and finally associate those Layers with Styles for attractive rendering.
Logging into GeoServer¶
First, connect to GeoServer at http://localhost:8080/geoserver or click on the “Configure” link in the Dashboard under GeoServer. You’ll arrive at the “Welcome Page”:
At the top of the page is a log-in form. Use “admin” as the user and “geoserver” as the password.
The configuration screen includes most the options for managing GeoServer. As GeoServer is developed, occasionally obscure tuning options are added which are not added to the interface, and can only be altered by manually editing configuration files. But over time, mature functionality is added to the user interface.
Creating a Workspace¶
Strictly speaking, creating a new Workspaces is not necessary, but it makes it easier to distinguish the layers we are adding to GeoServer from the ones that are already there.
Click on the “Workspaces” link in the sidebar under the “Data” category to get to the Workspace management page.
Click on “Add new workspace”, and enter “postgis” as the namespace, and “http://postgis.org” as the URI.
This way, our layers will end up logically named “postgis:streets”, “postgis:taxlots”, “postgis:schools”. The URI will be used in the prefixing of XML in GML documents, so we just use the PostGIS web site URL.
Click Submit and you’re done.
Note
The OpenGeo Suite ships with data from the City of Medford already installed, so we are going to have access to the data twice: once through the tables we have loaded into PostGIS (which we will publish in the “postgis” workspace), and again through the shapefiles shipped in the Suite (which are in the “medford” workspace).
Importing Data¶
One of the features of the Suite is the “Importer” which allows you to quickly import multiple layers of data into GeoServer in just a few steps. This is actually very handy, since the alternative is individually creating each layer definition and each associated style. We’ll use the importer to bring in the three spatial tables from our PostGIS database.
First, click on the “Importer” in the sidebar. You’ll see the data source selection page. Click on the “PostGIS” link.
Now fill in the “Create PostGIS Connection” page.
- Set the Workspace to “postgis”
- Enter “PostGIS” as the Name
- Set the Database to “postgis”
- Set the User name to “postgres”
- Set the Password to “postgres”
- Then click the Next button!
GeoServer connects to PostGIS and reads the list of available spatial tables. Ensure that our three tables are checked, and click the Import data button.
That’s it! You can view the results of the import by clicking the “OpenLayers” link next to one of the layers (try “road_ln” or “school_pt”, since the initial view shows all the data at once, and the “taxlot_ply” table is quite large).
Note
We have used the Importer to quickly create and publish a store, three layers and three associated styles. For information on manually creating stores, layers and styles, see Manually Configuring GeoServer.
Viewing the Layers¶
Once the Layers are created, you can view them directly, using the Layer Preview page, which is linked from the sidebar, near the bottom of the page.
Click on the “Layer Preview” link to see a list of all the layers that are configured in GeoServer. Look for the postgis:road_ln, postgis:school_pt and postgis:taxlot_ply layers you configured. You can type “postgis” in the search field to quickly find the layers you are looking for.
If you click on the “OpenLayers” link you will get a web map windows, displaying the WMS service for that layer. Here is the what the postgis:road_ln layer looks like in the OpenLayers view.
It may take some time for the first image to load, as the default view shows the complete extent of the data, so all 17182 streets have to be rendered. You can click on the individual features to run a feature information query and see the attributes of the shape.
- If you click on the “Google Earth” link for postgis:road_ln, you will get a KML image overlay file you can load into Google Earth. As you zoom in, the streets image overlay will refresh.
- If you click on the “Google Earth” link for postgis:school_pt, you will get a KML vector overlay file. GeoServer senses the data source size, and provides an appropriate form of KML output. The streets are large so you get an image overlay. The schools are small so you get a vector overlay.
Here’s a zoomed in view of the postgis:road_ln (thin black lines) and postgis:school_pt (red dots).
When you click a school, you can see the attributes in a pop-up bubble.
More Attractive Styles¶
GeoServer uses the OGC SLD (Styled Layer Descriptor) standard for representing cartography styling information. Here is what the default style we are using for lines looks like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>Default Line</Name>
<UserStyle>
<Title>1 px blue line</Title>
<Abstract>Default line style, 1 pixel wide blue</Abstract>
<FeatureTypeStyle>
<Rule>
<Title>Blue Line</Title>
<Abstract>A 1 pixel wide black line</Abstract>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke">#000000</CssParameter>
</Stroke>
</LineSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
Note
You can see this style yourself by selecting the “Styles” link in the “Data” section of the sidebar, then clicking the “line” style.
Being a fully valid XML document, complete with external references to XML Schema Description documents, makes SLD a fairly verbose configuration document. However, is is also a powerful language.
SLDs are capable of representing all the configuration of a map in a single document, which can potentially lead to very large SLD documents. A full SLD document might include multiple <NamedLayer> entries. Similarly, any layer might have multiple <UserStyle> entries available. And each of those might have multiple <FeatureTypeStyle> elements if the data source included many different geometry representations.
Changing a Style¶
The importer created styles and associated them with our layers when it configured them, so the if you click on the “Styles” heading in the sidebar, you’ll see our new styles.
- Click on the “postgis_road_ln” entry.
- Paste the text below into the SLD editor panel.
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>Medford Streets</Name>
<UserStyle>
<Title>Road Style</Title>
<Abstract>By combining two feature type styles, we can create a pipe style of roads.</Abstract>
<FeatureTypeStyle>
<Rule>
<Title>Pipe Style Outline</Title>
<Abstract>A 4-pixel dark underlay</Abstract>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke">#888888</CssParameter>
<CssParameter name="stroke-width">4</CssParameter>
</Stroke>
</LineSymbolizer>
</Rule>
</FeatureTypeStyle>
<FeatureTypeStyle>
<Rule>
<Title>Pipe Style Fill</Title>
<Abstract>A 2-pixel lighter overlay</Abstract>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke">#eeeeee</CssParameter>
<CssParameter name="stroke-width">2</CssParameter>
</Stroke>
</LineSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
If you read the SLD code above carefully, you’ll see we are creating a “filled pipe” road style. We are drawing the lines twice to achieve our effect: once in a broad stoke with a dark gray; then in a narrower stroke with a light grey. This creates the “filled pipe” effect that is common in web map representations of streets.
- Click the Validate button. At the top of the window you should see a green bar saying “No validation errors”
- Click the Submit button to save the style.
Warning
It is not uncommon to get parsing errors when submitting new documents. Watch out for syntax errors, and make sure the <?xml block has no spaces in front of it.
Navigate to the Layer Preview page again, and open up the OpenLayers view of the postgis:road_ln layer, or just click this link.
Very Attractive Styles¶
Combining multiple scales, and multiple rules for different types of features, you can achieve some very attractive map output.
The workshop package contains three SLD files that contain complete styles for our layers:
- road_ln.sld
- taxlot_ply.sld
- school_pt.sld
The GeoServer style configuration page includes the option of loading an external file, so we will use that to import our style files.
- Navigate to the Style editor (under “Data” click “Styles”).
- Select “postgis_road_ln” from the list and click it.
- At the bottom of the form, click Choose File to find the external SLD file.
- Find the road_ln.sld file in the data directory of the workshop.
- Click the Upload... link to import it into the user interface.
- Click Submit to accept it.
Go back to the demo page and have a look at the new style.
Now repeat the process for the school_pt.sld and taxlot_ply.sld files.
Because the data table is so large, the taxlot_ply.sld file includes a scale dependency, so you will have to zoom in a little before the features show up.
Once you have added the fancy styles, you can view them using the Layer Preview page, either as OpenLayers maps, or as KML in Google Earth.
Here is the OpenLayers view of the schools superimposed on the roads, created by editing the WMS URL slightly to include both postgis:road_ln and postgis:school_pt in the layers list.
And here is the Google Earth view of the taxlot_ply (retrieved from the KML link on the demos page). The style we have loaded is scale-dependent, so you will have to zoom in in order to see the tax lots appear. If you zoom in far enough, GeoServer will automatically switch from displaying raster overlays to displaying vector overlays.
Performance Tips¶
There are a number of tricks to making GeoServer run fast when deploying into production.
- More recent Java versions have better speed. Java 1.6 is faster than 1.5 is faster than 1.4.
- The simplest trick is to install the Java Advanced Imaging (JAI) extensions (the OpenGeo Suite already includes these)
- You can also run GeoServer inside a production servlet container (eg Tomcat)
- Turn down the logging level.
There are more recommendations for production performance at Running In A Production Environment.
Table Of Contents
About OpenGeo
OpenGeo provides commercial open source software for internet mapping and geospatial application development. We are a social enterprise dedicated to the growth and support of open source software.
License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License. Feel free to use this material, but we ask that you please retain the OpenGeo branding, logos and style.

