PostGIS: Difference between revisions
Brian Wilson (talk | contribs) mNo edit summary |
Brian Wilson (talk | contribs) mNo edit summary |
||
Line 5: | Line 5: | ||
==== GDAL ==== | ==== GDAL ==== | ||
Building GDAL is the most convoluted and time consuming step in installing PostGIS from sources. | |||
But it also gives you the best support for the files you need to use, and it is probably necessary | |||
if you want the newest version of PostGIS. | |||
'''TODO''' might want to add SFCGAL support but I don't need it yet. | '''TODO''' might want to add SFCGAL support but I don't need it yet. | ||
apt-get install postgresql-server-9.6 | apt-get install postgresql-server-dev-9.6 | ||
I always need features not yet released in packaged form, so install the subversion source code to track the latest. | I always need features not yet released in packaged form, so install the subversion source code to track the latest. | ||
Line 30: | Line 34: | ||
sudo apt-get install libcurl4-openssl-dev | sudo apt-get install libcurl4-openssl-dev | ||
'''ESRI FGDB support''': | '''ESRI FGDB support''': OpenFGDB gives access to version 9 fgdb files and read only, ESRI library only gives version 10 access and write access. | ||
If you want it anyway, download kit from http://resources.arcgis.com/content/geodatabases/10.0/file-gdb-download and unpack it | |||
cd ~/src/GIS | cd ~/src/GIS | ||
Line 39: | Line 43: | ||
sudo ldconfig -v | sudo ldconfig -v | ||
'''ESRI MDB support:''' Personal geodatabases are so rare | '''ESRI MDB support:''' Personal geodatabases are so rare these days I no longer bother to include this. | ||
See http://www.gdal.org/ogr/drv_mdb.html | See http://www.gdal.org/ogr/drv_mdb.html | ||
Install a Java JDK (I use version 7) and get the jackess library: http://jackcess.sourceforge.net/ and put it in this MDB folder | Install a Java JDK (I use version 7) and get the jackess library: http://jackcess.sourceforge.net/ and put it in this MDB folder | ||
Line 61: | Line 65: | ||
--with-openjpeg --with-geos --with-geotiff \ | --with-openjpeg --with-geos --with-geotiff \ | ||
--with-jpeg --with-png \ | --with-jpeg --with-png \ | ||
--with-expat --with-libkml --with-xerces-c | --with-expat --with-libkml --with-xerces-c | ||
--with-java=/opt/oracle/jdk1.7.0_75 | Optionally add | ||
--with-java=/opt/oracle/jdk1.7.0_75 | |||
--with-fgdb=/home/bwilson/src/GIS/FileGDB_API-64 | --with-fgdb=/home/bwilson/src/GIS/FileGDB_API-64 | ||
--with-mrsid=/home/bwilson/src/GIS/MrSID/Raster_DSDK | |||
Note the MrSID line requires a complete path for some reason. I don't use MrSID so I don't build it in right now. (2017-02) | Note the MrSID line requires a complete path for some reason. I don't use MrSID so I don't build it in right now. (2017-02) | ||
Probably my fault somehow, I think a relative path should be good enough. | Probably my fault somehow, I think a relative path should be good enough. | ||
Once configure is done make sure the options you selected show 'yes'. Else adjust and repeat... | |||
make -j 14 | make -j 14 |
Revision as of 16:52, 7 May 2017
Installing on Debian 8.7
Postgresql 9.6 installs with
apt-get install postgresql
GDAL
Building GDAL is the most convoluted and time consuming step in installing PostGIS from sources. But it also gives you the best support for the files you need to use, and it is probably necessary if you want the newest version of PostGIS.
TODO might want to add SFCGAL support but I don't need it yet.
apt-get install postgresql-server-dev-9.6
I always need features not yet released in packaged form, so install the subversion source code to track the latest.
Get GDAL sources
sudo apt-get install subversion cd ~/src/GIS svn checkout https://svn.osgeo.org/gdal/trunk/gdal gdal
After the initial checkout, later on to update just do
cd ~/src/GIS/gdal svn update
GDAL (installed into /usr/local): "poppler" packages are included for PDF support.
sudo apt-get install openjpeg-tools libopenjpeg-dev sudo apt-get install libpoppler-dev sudo apt-get install expat libexpat1-dev libkml-dev libxerces-c-dev sudo apt-get install libcurl4-openssl-dev
ESRI FGDB support: OpenFGDB gives access to version 9 fgdb files and read only, ESRI library only gives version 10 access and write access. If you want it anyway, download kit from http://resources.arcgis.com/content/geodatabases/10.0/file-gdb-download and unpack it
cd ~/src/GIS tar xzvf FileGDB_API_1_4-64.tar.gz cd FileGDB_API sudo cp lib/*.so /usr/local/lib sudo ldconfig -v
ESRI MDB support: Personal geodatabases are so rare these days I no longer bother to include this. See http://www.gdal.org/ogr/drv_mdb.html Install a Java JDK (I use version 7) and get the jackess library: http://jackcess.sourceforge.net/ and put it in this MDB folder
mkdir MDB cd MDB wget http://apache.deathculture.net//commons/lang/binaries/commons-lang3-3.1-bin.tar.gz wget http://apache.cs.utah.edu//commons/logging/binaries/commons-logging-1.1.1-bin.tar.gz wget http://downloads.sourceforge.net/project/jackcess/jackcess/1.2.9/jackcess-1.2.9.jar?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjackcess%2Ffiles%2F&ts=1355080366&use_mirror=superb-dca3 tar xzvf commons-lang3-3.1-bin.tar.gz tar xzvf commons-logging-1.1.1-bin.tar.gz sudo cp commons-logging-1.1.1/commons-logging-1.1.1.jar commons-lang3-3.1/commons-lang3-3.1.jar jackcess-1.2.9.jar /opt/oracle/jdk1.7.0_09/jre/lib/ext/
Do the configure and build.
cd ~/src/GIS/gdal ./configure \ --with-python --with-poppler \ --with-pg=/usr/bin/pg_config \ --with-curl \ --with-openjpeg --with-geos --with-geotiff \ --with-jpeg --with-png \ --with-expat --with-libkml --with-xerces-c
Optionally add
--with-java=/opt/oracle/jdk1.7.0_75 --with-fgdb=/home/bwilson/src/GIS/FileGDB_API-64 --with-mrsid=/home/bwilson/src/GIS/MrSID/Raster_DSDK
Note the MrSID line requires a complete path for some reason. I don't use MrSID so I don't build it in right now. (2017-02) Probably my fault somehow, I think a relative path should be good enough.
Once configure is done make sure the options you selected show 'yes'. Else adjust and repeat...
make -j 14 sudo make install sudo ldconfig -v
Confirm that the version you just built is really the one found. (There might be an older version for example in /usr/bin! Surprise!!)
gdalinfo --version
Confirm the formats you need are listed with these commands
gdalinfo --formats ogrinfo --formats
For example.
ogrinfo --formats | grep GDB -> "FileGDB" (read/write)
Confirm you can load it from Python, too.
$ python Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23) [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import osgeo.gdal >>> print osgeo.gdal.__version__ 2.20dev >>>
YAY!
PostGIS
Installing the prerequisites
Don't do this: sudo apt-get build-dep postgis
Ordinarily it is handy to do something like this to pick up all the build dependencies, but this downloads and installs too much!
It picks up binaries that we want to build from source which is bad because we want to get the most up-to-date versions. Then you have to wrestle with having 2 versions of various things (some in /usr and some in /usr/local) and it's too much work.
Instead I install the supporting package individually. I have already installed PostgreSQL and GEOS at this point so all I needed to do at this stage was this:
sudo apt-get install libxml2-dev
My (Debian/Ubuntu) systems already had JSON support but you might need it
sudo apt-get install libjson0-dev
PostGIS 2.4
Building from source -- not really enterprise production ready! But I do it anyway to get features and performance.
sudo apt-get install xsltproc
(either) Direct checkout to get the very latest including topology and raster
svn checkout http://svn.osgeo.org/postgis/trunk postgis-svn cd postgis-svn sh autogen.sh
(or) Do a traditional tarball download to get almost the latest
Building on Debian and Ubuntu By the time you've made it to this stage the actual PostGIS build seems trivial.
./configure --with-raster make -j 10 sudo make install
Building on Mac seemed promising but I failed. I use a Linux virtual machine for testing.
Server management
You have to create the initial databases for Postgres. See the INSTALL file in the sources for more information. Here is how I do it.
Create a user to own the postgres files
su adduser postgres
Create your data storage area(s) (what's the Postgres term for that? it's a "cluster".) This assumes you set PGDATA correctly for your system.
mkdir $PGDATA chown postgres $PGDATA
Create the initial database tables
su - postgres initdb -D $PGDATA exit
You probably want to edit the $PGDATA/pg_hba.conf and postgresql.conf files. I add lines to allow my computers to connect in pg_hba.conf and I tell the server to listen for network connections in postgresql.conf
Start the server. There is a sample startup script for Redhat/Centos in the source tree
sudo cp contrib/start-scripts/linux /etc/init.d/postgresql-9 sudo systemctl start postgresql
Create a generic database to test postgres.
createdb -U postgres test psql -U postgres test \d \q
If it works you can drop it.
dropdb -U postgres test
Tuning your system for PostGIS
Tips from a SOTM Mapnik doc. http://www.geofabrik.de/media/2010-07-10-rendering-toolchain-performance.pdf
Adjust settings in your postgresql.conf file
- shared_buffers 128MB -> 768MB
- work_mem 1MB -> 512MB
- maintenance_work_mem 16 -> 512MB
- fsync on -> off
Create another user and password
From command line... if you want to use database authentication
psql -U postgres CREATE USER gis_owner WITH PASSWORD 'jackalope';
To see the user table
SELECT * FROM pg_authid;
Install PostGIS Template
createdb -U postgres gis_data psql -U postgres gis_data # CREATE EXTENSION postgis; CREATE EXTENSION # CREATE EXTENSION postgis_topology; CREATE EXTENSION # CREATE EXTENSION address_standardizer; CREATE EXTENSION
Et voila! You should be able to do this now:
# SELECT postgis_full_version();
and you will see this result or something similar to this:
POSTGIS="2.4.0dev r15360" GEOS="3.5.0-CAPI-1.9.0 r4084" PROJ="Rel. 4.9.2, 08 September 2015" GDAL="GDAL 2.1.1, released 2016/07/07" LIBXML="2.9.4" LIBJSON="0.12" TOPOLOGY RASTER (1 row)
Some extras
Suggestions from the switch2osm page,
sudo apt-get install munin munin-plugins-extra munin-node iotop ptop
See also http://aouyar.github.io/PyMunin/plugins/postgresql.html which I don't have set up yet.
Loading data into PostGIS
I have moved everything that used to be here to its own page, because this is a pretty big topic. Go look at Loading data into PostGIS
Exporting data from PostGIS
If you want to go back to a plain old shapefile (the horror!), use pgsql2shp
Accessing data
ArcMap Desktop 10.4
I have moved this to its own page. ArcGIS + PostGIS is an awesome (and more affordable) combination.
QGIS
QGIS and PostGIS are made for each other, so any guide to QGIS will tell you how to use PostGIS as repository. You can use QGIS to read and write data stored in PostGIS.
Web sites
You can read data directly from PostGIS or you can try using an intermediary. This allows more flexibility. For example you can keep raw spatial data in PostGIS, and create layers in GeoServer complete with styles, and then build up a web map from your own and other services using OpenLayers or Leaflet.
I have been working with GeoServer but many people like MapServer. There are other choices.
Reprojection
CREATE TABLE "wa_counties_p" (gid serial PRIMARY KEY, "county_cod" int2,"county_fip" varchar(3),"county_nm" varchar(15), "ecy_region" varchar(4),"air_region" varchar(46)); SELECT AddGeometryColumn(,'wa_counties_s','the_geom','-1','MULTIPOLYGON',2); ALTER TABLE wa_counties_p DROP CONSTRAINT "enforce_srid_the_geom" RESTRICT; INSERT INTO wa_counties_p SELECT gid,county_cod, county_fip, county_nm, ecy_region, air_region, transform(the_geom, 102348) FROM wa_counties; ALTER TABLE wa_counties_p ADD CONSTRAINT "enforce_srid_the_geom"CHECK(SRID(the_geom)=102348);
Additional resources
Postgis wiki http://trac.osgeo.org/postgis/wiki/UsersWikiMain
An article of interest
http://www.bostongis.com/PrinterFriendly.aspx?content_name=sqlserver2008_postgis_mysql_compare
Other
From some forum or other
"574 post(s)
- 23-Mar-09 20:06
I suppose I could share a few things that I have found out through trial and error. I am certainly no expert.
I am running on windows server 2003 32 bit, so if you are running 64 bit or *nix versions, the experiences could be very different.
1. Make sure you have the latest version of postgres / postGIS. In another thread you mentioned that you have only just installed it, so I would think that this is the case. Version Postgres 8.3 has a nice easy windows installer that makes it a piece of cake to install. The application Stack Builder picks up the latest postGIS version, so there is no need to worry too much about installing postGIS seperately.
2. I found that installing the Tuning wizard that can be found in the Application Stack Builder helps to configure the memory setting quite well. For my purposes, I didn't need to tweak any of the memory setting into the postgresql.conf file.
3. If you are planning on doing spatial queries on any datasets that you export to postgres, then make sure you have spatial indexes on the geometry field. When you export from manifold using the Postgres type, then these indexes are created automatically. I would suggest reading the postgis user manual, it is pretty comprehensive, and fairly easy to follow. It will give you a good idea of what is possible with spatial queries. It is available http://postgis.refractions.net/download/postgis-1.3.5.pdf
4. The postGIS email list is very helpful. See http://postgis.refractions.net/mailman/listinfo/postgis-users to join.
5. If you are doing joins between tables in the database, then you will have to manually set up the indexes. If you are using pgAdmin III (which I like), then before you run queries, you can see if your indexes are being used by using explain.
--sql (for use within postgresql (or in the database manager console in manifold)
explain select * from test limit 1000;
This will tell you what type of scan is being done. In the above case it is always going to be a sequential scan, due to the nature of the query. If you are doing a query like:
select * from "PARCEL_MAPPING" INNER JOIN "100K index" ON st_intersects("PARCEL_MAPPING".geometry, "100K index".geometry)
where "100K index"."Sheet_Name" = 'DANYO'
There is an explain button in the query editor in pgAdmin III which shows this in a graphic form so you can see pretty quickly if you aren't taking advantage of your indexes.
6. Also as mentioned above, it is a good idea to do vaccuum analyzes often as these help indexes perform better.
7. If you are using large drawings that have lots of drawing objects, then make use of the AOI windowing when linking the drawings. Also see http://forum.manifold.net/forum/t64537.18 for a handy tool to help out.
8. Change the extents() aggregate function in postgres. Manifold uses this when linking drawings (for which reason I don't know). It is VERY slow on large datasets. If you use the tool mentioned above, you don't need the extents function at all. All I did was rename the function so I could still use it if I had to in code and postGIS, but Manifold wasn't automatically trying to use it.
--sql (in postgres)
DROP AGGREGATE extent(geometry);
CREATE AGGREGATE _extent(geometry) (
SFUNC=public.st_combine_bbox,
STYPE=box2d
);
That is about all I can think of for the moment, but I am sure others have more tips.
Hope this helps
James"