Logo

Store Spatial Data in PostGIS and Publish with GeoServer

 

How to Store Spatial Data in PostGIS and Publish with GeoServer for Web Mapping with OpenLayers



In modern GIS workflows, particularly when working with web-based applications, PostGIS, GeoServer, and OpenLayers form a powerful trio for managing and visualizing geospatial data. But when setting up this system, one key decision is the coordinate reference system (CRS) to use for storing and serving your data. This blog will guide you through storing your spatial data in EPSG:4326 and publishing it as EPSG:3857 for seamless web mapping with OpenLayers.

Why EPSG:4326 for Storing Data?

What is EPSG:4326?

EPSG:4326, also known as WGS 84, is a geographic coordinate system based on latitude and longitude. This CRS is widely used because it's the default system for GPS and most global datasets. It’s ideal for storing geospatial data because:

  • Coordinates are in degrees of latitude and longitude.

  • It’s globally recognized and used by GPS devices, mapping tools, and data exchange standards.

  • It has minimal distortion at global scales (important for global analysis).

In simple terms, EPSG:4326 gives you a spherical representation of Earth, which works well for global data storage.

Why Use EPSG:4326 for Storage?

  1. Standardization: It’s the default for most data sources, so storing your data in EPSG:4326 ensures compatibility with external datasets, GPS systems, and other GIS platforms.

  2. Accuracy: Since it represents actual latitudes and longitudes, it’s excellent for storing raw data, like GPS coordinates or satellite imagery.

  3. Interoperability: Data stored in EPSG:4326 is easy to exchange between different systems and platforms (like QGIS, ArcGIS, and others).


How to Store Your Shapefile in PostGIS (EPSG:4326)

Step 1: Install PostGIS

If you haven’t already installed PostGIS (the spatial extension for PostgreSQL), follow the steps to install and configure it:

  • Install PostgreSQL

  • Install the PostGIS extension for PostgreSQL

Step 2: Import Your Shapefile to PostGIS

Once you have PostGIS set up, use the shp2pgsql tool to import your shapefile into a PostGIS-enabled database. Make sure your shapefile is in EPSG:4326 (if not, you can reproject it during the import).

Here’s the command to import a shapefile into PostGIS:

bash

shp2pgsql -s 4326 my_shapefile.shp my_schema.my_table | psql -d my_database
  • -s 4326: Specifies that the shapefile is in EPSG:4326.

  • my_schema.my_table: The schema and table where the data will be stored.

  • my_database: Your PostGIS-enabled PostgreSQL database.

This command will import your shapefile into PostGIS, storing it in EPSG:4326, ready for further use.


Publishing with GeoServer

GeoServer is an open-source server that enables you to serve geospatial data over the web using open standards like WMS, WFS, and WCS. When working with GeoServer, you’ll typically store your data in PostGIS and publish it for web mapping applications.

Step 3: Configure GeoServer to Use EPSG:4326 for Data Storage

  1. Set up your PostGIS Store in GeoServer:

    • In GeoServer, create a PostGIS data store that connects to the PostGIS database where your shapefile is stored.

    • Make sure GeoServer is configured to use EPSG:4326 for storing and reading data. GeoServer can read data stored in EPSG:4326 without any issues.

  2. Create a Layer in GeoServer:

    • Once your PostGIS store is set up, you can create a layer in GeoServer using the data from your PostGIS store.

    • Ensure that the SRS (Spatial Reference System) of the layer is set to EPSG:4326 to match your stored data.

Step 4: Configure GeoServer to Reproject Data for Web Use (EPSG:3857)

For web mapping, most clients, including OpenLayers and Leaflet, use EPSG:3857 (Web Mercator) as their default projection. This is because Web Mercator is designed for fast, tile-based map rendering in web applications.

GeoServer makes it easy to reproject your data to EPSG:3857 on the fly.

  1. Configure a Web Map Service (WMS): When creating a WMS layer in GeoServer, ensure that:

    • Native SRS: The data is stored in EPSG:4326.

    • Declared SRS: The layer will be published in EPSG:3857 (Web Mercator).

GeoServer will handle the projection transformation automatically when serving the layer.

  1. Enable Tiling: For performance, you might want to enable tiled WMS or WMS-C (Web Map Service - Cached). This will ensure fast map loading in OpenLayers by serving pre-rendered tiles.


Displaying Your Data in OpenLayers (EPSG:3857)

OpenLayers is a powerful, open-source JavaScript library for building web-based maps. It supports many CRS, but EPSG:3857 is the default because most base maps (like Google Maps and OpenStreetMap) use this projection.

Step 5: Set Up OpenLayers to Use EPSG:3857

Once your data is published in GeoServer and projected in EPSG:3857, you can load it into OpenLayers.

Here’s a basic example of how to set up an OpenLayers map with EPSG:3857 and load a WMS layer served by GeoServer:

js

var map = new ol.Map({ target: 'map', // the id of the HTML element to hold the map layers: [ new ol.layer.Tile({ source: new ol.source.OSM() // OpenStreetMap as the base map }), new ol.layer.Image({ source: new ol.source.ImageWMS({ url: 'http://your-geoserver-url/geoserver/your_workspace/ows', params: {'LAYERS': 'your_layer_name', 'SRS': 'EPSG:3857'} }) }) ], view: new ol.View({ projection: 'EPSG:3857', center: ol.proj.fromLonLat([-122.4194, 37.7749]), // San Francisco coordinates zoom: 10 }) });
  • ol.source.OSM(): Adds OpenStreetMap as a base map in EPSG:3857.

  • ol.source.ImageWMS(): Fetches the WMS layer from GeoServer, ensuring it's projected to EPSG:3857.

  • ol.proj.fromLonLat(): Converts latitude/longitude (EPSG:4326) to Web Mercator (EPSG:3857) for map centering.


Conclusion: Why Store in EPSG:4326 and Publish as EPSG:3857?

EPSG:4326 (WGS 84) for Data Storage:

  • Provides geodetic accuracy for global datasets.

  • Works well with GPS, global data, and analysis.

  • Ensures interoperability with other platforms.

EPSG:3857 (Web Mercator) for Web Mapping:

  • The default for web mapping applications, including OpenLayers and Leaflet.

  • Tiling and fast rendering make it perfect for web-based maps.

  • Aligns with major base maps like Google Maps, Bing Maps, and OpenStreetMap.

By storing your data in EPSG:4326 and publishing it as EPSG:3857, you get the best of both worlds: accurate, geodetic data storage with optimized web map rendering.


With this workflow, you’ll be able to store, serve, and visualize geospatial data seamlessly across various systems and platforms. Happy mapping! 🌍