[1] | 1 | .. _loading_data: |
---|
| 2 | |
---|
| 3 | Section 4: Loading spatial data |
---|
| 4 | =============================== |
---|
| 5 | |
---|
| 6 | Supported by a wide variety of libraries and applications, PostGIS provides many options for loading data. This section will focus on the basics -- loading shapefiles using the PostGIS shapefile loading tool. |
---|
| 7 | |
---|
| 8 | #. First, return to the Dashboard, and click on the **Import shapefiles** link in the PostGIS section. The GUI shapefile importer pgShapeLoader will launch. |
---|
| 9 | |
---|
| 10 | .. image:: ./screenshots/pgshapeloader_01.png |
---|
| 11 | |
---|
| 12 | #. Next, open the *Shape File* browser and navigate to the data directory, file:`\\postgisintro\\data`. Select the :file:`nyc_census_blocks.shp` file. |
---|
| 13 | |
---|
| 14 | #. Fill in the details for the *PostGIS Connection* section and click on the **Test Connection...** button. |
---|
| 15 | |
---|
| 16 | .. list-table:: |
---|
| 17 | |
---|
| 18 | * - **Username** |
---|
| 19 | - ``postgres`` |
---|
| 20 | * - **Password** |
---|
| 21 | - ``postgres`` |
---|
| 22 | * - **Server Host** |
---|
| 23 | - ``localhost`` ``54321`` |
---|
| 24 | * - **Database** |
---|
| 25 | - ``nyc`` |
---|
| 26 | |
---|
| 27 | .. note:: |
---|
| 28 | |
---|
| 29 | Setting the port number to **54321** is very important! The OpenGeo PostGIS runs on port 54321, not the default PostgreSQL port of 5432. |
---|
| 30 | |
---|
| 31 | #. Fill in the details for the *Configuration* section. |
---|
| 32 | |
---|
| 33 | .. list-table:: |
---|
| 34 | |
---|
| 35 | * - **Destination Schema** |
---|
| 36 | - ``public`` |
---|
| 37 | * - **SRID** |
---|
| 38 | - ``26918`` |
---|
| 39 | * - **Destination Table** |
---|
| 40 | - ``nyc_census_blocks`` |
---|
| 41 | * - **Geometry Column** |
---|
| 42 | - ``the_geom`` |
---|
| 43 | |
---|
| 44 | #. Click the **Options** button and select "Load data using COPY rather than INSERT." This will make the data load process a little faster. |
---|
| 45 | |
---|
| 46 | .. image:: ./screenshots/pgshapeloader_02.png |
---|
| 47 | |
---|
| 48 | #. Finally, click the **Import** button and watch the import process. It may take a few minutes to load, but this is the largest file in our test set. |
---|
| 49 | |
---|
| 50 | #. Repeat the import process for the remaining shapefiles in the data directory. Except for the input file and output table name, all the other fields in pgShapeLoader should remain the same: |
---|
| 51 | |
---|
| 52 | * ``nyc_streets.shp`` |
---|
| 53 | * ``nyc_neighborhoods.shp`` |
---|
| 54 | * ``nyc_subway_stations.shp`` |
---|
| 55 | |
---|
| 56 | #. When all the files are loaded, click the "Refresh" button in pgAdmin to update the tree view. You should see your four tables show up in the **Tables** section of the tree. |
---|
| 57 | |
---|
| 58 | .. image:: ./screenshots/refresh.png |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | Shapefiles? What's that? |
---|
| 62 | ------------------------ |
---|
| 63 | |
---|
| 64 | You may be asking yourself -- "What's this shapefile thing?" A "shapefile" commonly refers to a collection of files with ``.shp``, ``.shx``, ``.dbf``, and other extensions on a common prefix name (e.g., nyc_census_blocks). The actual shapefile relates specifically to files with the ``.shp`` extension. However, the ``.shp`` file alone is incomplete for distribution without the required supporting files. |
---|
| 65 | |
---|
| 66 | Mandatory files: |
---|
| 67 | |
---|
| 68 | * ``.shp`` â shape format; the feature geometry itself |
---|
| 69 | * ``.shx`` â shape index format; a positional index of the feature geometry |
---|
| 70 | * ``.dbf`` â attribute format; columnar attributes for each shape, in dBase III |
---|
| 71 | |
---|
| 72 | Optional files include: |
---|
| 73 | |
---|
| 74 | * ``.prj`` â projection format; the coordinate system and projection information, a plain text file describing the projection using well-known text format |
---|
| 75 | |
---|
| 76 | In order to analyze a shapefile in PostGIS, you need to convert a shapefile into a series SQL commands. By running pgShapeLoader, a shapefile converts into a table that PostgreSQL can understand. |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | SRID 26918? What's with that? |
---|
| 80 | ----------------------------- |
---|
| 81 | |
---|
| 82 | Most of the import process is self-explanatory, but even experienced GIS professionals can trip over an **SRID**. |
---|
| 83 | |
---|
| 84 | An "SRID" stands for "Spatial Reference IDentifier." It defines all the parameters of our data's geographic coordinate system and projection. An SRID is convenient because it packs all the information about a map projection (which can be quite complex) into a single number. |
---|
| 85 | |
---|
| 86 | You can see the definition of our workshop map projection by looking it up either in an online database, |
---|
| 87 | |
---|
| 88 | http://spatialreference.org/ref/epsg/26918/ |
---|
| 89 | |
---|
| 90 | or directly inside PostGIS with a query to the ``spatial_ref_sys`` table. |
---|
| 91 | |
---|
| 92 | .. code-block:: sql |
---|
| 93 | |
---|
| 94 | SELECT srtext FROM spatial_ref_sys WHERE srid = 26918; |
---|
| 95 | |
---|
| 96 | .. note:: |
---|
| 97 | |
---|
| 98 | The PostGIS ``spatial_ref_sys`` table is an OGC-standard table that defines all the spatial reference systems known to the database. The data shipped with PostGIS, lists over 3000 known spatial reference systems and details needed to transform/re-project between them. |
---|
| 99 | |
---|
| 100 | In both cases, you see a textual representation of the **26918** spatial reference system (pretty-printed here for clarity): |
---|
| 101 | |
---|
| 102 | :: |
---|
| 103 | |
---|
| 104 | PROJCS["NAD83 / UTM zone 18N", |
---|
| 105 | GEOGCS["NAD83", |
---|
| 106 | DATUM["North_American_Datum_1983", |
---|
| 107 | SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]], |
---|
| 108 | AUTHORITY["EPSG","6269"]], |
---|
| 109 | PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]], |
---|
| 110 | UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]], |
---|
| 111 | AUTHORITY["EPSG","4269"]], |
---|
| 112 | UNIT["metre",1,AUTHORITY["EPSG","9001"]], |
---|
| 113 | PROJECTION["Transverse_Mercator"], |
---|
| 114 | PARAMETER["latitude_of_origin",0], |
---|
| 115 | PARAMETER["central_meridian",-75], |
---|
| 116 | PARAMETER["scale_factor",0.9996], |
---|
| 117 | PARAMETER["false_easting",500000], |
---|
| 118 | PARAMETER["false_northing",0], |
---|
| 119 | AUTHORITY["EPSG","26918"], |
---|
| 120 | AXIS["Easting",EAST], |
---|
| 121 | AXIS["Northing",NORTH]] |
---|
| 122 | |
---|
| 123 | If you open up the ``nyc_neighborhoods.prj`` file from the data directory, you'll see the same projection definition. |
---|
| 124 | |
---|
| 125 | A common problem for people getting started with PostGIS is figuring out what SRID number to use for their data. All they have is a ``.prj`` file. But how do humans translate a ``.prj`` file into the correct SRID number? |
---|
| 126 | |
---|
| 127 | The easy answer is to use a computer. Plug the contents of the ``.prj`` file into http://prj2epsg.org. This will give you the number (or a list of numbers) that most closely match your projection definition. There aren't numbers for *every* map projection in the world, but most common ones are contained within the prj2epsg database of standard numbers. |
---|
| 128 | |
---|
| 129 | .. image:: ./screenshots/prj2epsg_01.png |
---|
| 130 | |
---|
| 131 | Data you receive from local agencies -- such as New York City -- will usually be in a local projection noted by "state plane" or "UTM". Our projection is "Universal Transverse Mercator (UTM) Zone 18 North" or EPSG:26918. |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | Things to Try: Spatially Enable an Existing Database |
---|
| 135 | ---------------------------------------------------- |
---|
| 136 | |
---|
| 137 | You have already seen how to create a database using the ``postgis_template`` in pgAdmin. However when installing from source or adding PostGIS functionality to an existing database, it is not always appropriate to create a fresh database from the PostGIS template. |
---|
| 138 | |
---|
| 139 | Your task in this section is to create a database and add PostGIS types and functions after the fact. The SQL scripts needed -- :file:`postgis.sql` and :file:`spatial_ref_sys.sql` -- can be found in the :file:`contrib` directory of your PostgreSQL install. For guidance, refer to the PostGIS documentation on installing from source [#PostGIS_Install]_. |
---|
| 140 | |
---|
| 141 | .. note:: |
---|
| 142 | |
---|
| 143 | Remember to include your username and port number when creating a database from the command line. |
---|
| 144 | |
---|
| 145 | Things to Try: View data using uDig |
---|
| 146 | ----------------------------------- |
---|
| 147 | |
---|
| 148 | `uDig <http://udig.refractions.org>`_, (User-friendly Desktop Internet GIS), is a desktop GIS viewer/editor for quickly looking at data. You can view a number of data formats including flat shapefiles and a PostGIS database. Its graphical interface allows for easy exploration of your data, as well as simple testing and fast styling. |
---|
| 149 | |
---|
| 150 | Use this software to connect your PostGIS database. The application is included in the ``software`` folder. |
---|
| 151 | |
---|
| 152 | .. rubric:: Footnotes |
---|
| 153 | |
---|
| 154 | .. [#PostGIS_Install] "Chapter 2.5. Installation" PostGIS Documentation. May 2010 <http://postgis.org/documentation/manual-1.5/ch02.html#id2786223> |
---|
| 155 | |
---|