First, why do this. While Shapefiles are good for distributing and looking at data, it is difficult to do any analysis. To do anything, you end up using OGR/GDAL to examine the shapefile and then do something with it. PostGIS takes the spatial and data components of the Shapefile and exposes them in database format, allowing anything that can be done with a database to be done with the Shapefile. In addition, it enables Simple Features for SQL which allows for using set operations on geographic objects (as opposed to only sets). Think of all the Venn diagram pictures back in High School. And other spatial operations that may come to mind when dealing with points, lines and shapes.
The key, of course, is to get the PostGIS database started. So, first, within PostgreSQL, create a database
CREATE DATABASE PA;
Next, load PostGIS onto the database. The first step is to load PL/PGSQL
CREATE LANGUAGE plpgsql;
Next, within the database, load the lwpostgis.sql and spatial_ref_sys.sql into the database. I used the pgAdmin III GUI.
Next, use the shp2pgsql tool The format is:
shp2pgsql [
This should be run from the directory with the .shp file.
So, for example, using the NHPN shapefile for Pennsylvania (FIPS: 42), which is to be imported into a database named PACities, in the 'public' schema as the table 'NHPN' with the projection NAG 83 (ft) (Pennsyvania South) EPSG=2272, I get:
shp2pgsql -s 2272 S42NHPN public.NHPN PACities > nhpnImport.sql
And now it is in.
No comments:
Post a Comment