Importing Shp Files To Postgis-creating Geoserver Feature Types

THL Toolbox > Developers' Zone > Gis Development > Adding Gis Datasets For Display In The Map Layers Section Of The Interactive Map > Importing SHP files to Postgis-Creating Geoserver Feature Types

Importing SHP files to Postgis/Creating Geoserver Feature Types

Contributor(s): Ron Bentley

When you want to add a GIS data set to the THL Interactive GIS Map, the first thing to do is the following set of steps.

This is (roughly) the process Ron Bentley used to import the Bellezza archaeological sites in Dec., 2009; it is taken from external link: Ron's comment to JIRA THLTOOLS-857.

Import the SHP file to PostGIS

  1. Upload the SHP files to SDS7 using SCP or an equivalent. There are four "essential" files for each set of SHP files. In this case, they were bellezza.shp, bellezza.shx, bellezza.dbf, and bellezza.prj. In practice, the .prj file is not used as I'm describing the process below. I am assuming that the SRID is 4326 (WGS84, lat/lon coordinates), and that may not always be a safe assumption.
  2. Import the SHP files to PostGIS with the following commands at a command prompt on SDS7:
    shp2pgsql -s 4326 bellezza.shp bellezza > bellezza.sql
    psql -h sds2.itc.virginia.edu -U postgres -f bellezza.sql -d thdl_places_production
    psql -h sds2.itc.virginia.edu -U postgres -d thdl_places_production 
      -c "GRANT ALL ON bellezza TO rubyuser"
  3. Delete the SHP files and bellezza.sql on SDS7.

At this point there is a table in PostGIS on SDS2 with one field for each column in the SHP file database. The geometry column is called "the_geom" by default.

Also see additional information from the external link: specific case of importing SHP files for Dbus monasteries

There are other options for importing the SHP file that might be easier for the GUI-centric. A reasonably recent installation of external link: Quantum GIS comes with the SPIT plugin, the Shapefile to PostGIS Import Tool, which can accomplish the same thing.

Create a feature type on GeoServer

  1. From the external link: GeoServer main page, click on Config.
  2. Enter username and password.
  3. Click on Data.
  4. Click on FeatureTypes.
  5. Click on New.
  6. Select the feature type from the drop down menu, in this case, thl:::bellezza.
  7. Click the New button.
  8. Select a default style: line, point, or polygon. (We'll come back to the style later.)
  9. Enter coordinates for the bounding box. Right now we often use: Min Long: 78.701, Min Lat.: 26.382, Max Long: 99.033, Max Lat.: 34.583
  10. Leave all other fields at their default value.
  11. Click the Submit button at the bottom of the page.
  12. Click the Apply button on the top left of the page.
  13. Click the Save button on the top left of the page.
  14. Go to the external link: Map Preview page and click on the OpenLayers preview map for the new feature type to verify that it works.

This process has to be repeated for each instance of GeoServer. Alternatively, you could copy catalog.xml, featureTypes/ and styles/ from the geoserver/data directory on SDS7 to another server and reload GeoServer from the Tomcat manager.

Create a custom style

This is the most time consuming part of the process, especially if we're trying to re-create the look of an existing map. GeoServer uses Styled Layer Descriptors or SLDs to govern how data looks when it's presented. The Open Geospatial Consortium promulgates the standards, which include the external link: current SLD specification and the external link: description of filter encoding that is an essential component for what we do. However, the Geoserver documentation has a external link: far gentler introduction to styling that includes some useful recipes.

I personally tend to take an existing SLD and edit it by hand to match the current data. (The SLD is an XML document.) However, external link: uDig is one tool that can help. It's open-source, administered by the same company that administers PostGIS, and it works very much like ArcGIS or Quantum GIS. The benefit of uDig is that it saves its layer descriptions as SLDs; it can be a fast way to mock up an SLD. It can also connect directly to a PostGIS database. (The down-side is it runs on Java and can be slow and cumbersome.)

Assuming one has prepared an SLD for the new feature type, post it on GeoServer this way:

  1. From the external link: GeoServer main page, click on Config.
  2. Enter username and password.
  3. Click on Data.
  4. Click on New.
  5. Type a name for the new style, and click the New button. We'll use thl_bellezza as the example here.
  6. Click the Browse… button at the bottom of the screen and select the SLD.
  7. Click Upload.
  8. Uncheck "Fully Validate against the SLD schema". (That just seems to create problems.)
  9. Click Submit at the bottom of the page.
  10. Click the Apply button on the top left of the page.
  11. Click the Save button on the top left of the page. The new style is now ready, but it has to be applied to the new feature type.
  12. Click Data.
  13. Click FeatureTypes.
  14. Select the new feature type from the drop down menu, in this case thl:bellezza, and click the Edit button.
  15. Change the style from the default to the new thl_bellezza style.
  16. Click Submit at the bottom of the page.
  17. Click the Apply button on the top left of the page.
  18. Click the Save button on the top left of the page.
  19. Go to the external link: Map Preview page and click on the OpenLayers preview map for the new feature type to verify that it works.

Again, these changes have to be applied to all instances of GeoServer.