[GIS] Is it possible to export spatial data from database to shapefile without writing SQL manually

exportoracle-dbmsshapefilespatial-database

Shapefile is being imported to Oracle DB. Is there a tool to export this spatial data as a shapefile without writing any SQL (like you have to do with SQL Developer and GeoRaptor)?

EDIT: from all your answers I understand that there is no way for me to do action: import shapefile X to my database and later use some tool to export same shapefile X without using SQL or SQL like filtering… Unless I want to export whole layer which is not my interest.

Best Answer

GDAL - specifically ogr2ogr is the proper way to go. The base syntax for exporting one table is like this:

$ ogr2ogr -f "ESRI Shapefile" us_cities.shp OCI:scott/tiger@localhost:1521/orcl122:us_cities

where localhost:1521/orcl122 is your database connection string. Note that you do not actually need to specify the output file format (as OGR assumes you want a shapefile by default):

$ ogr2ogr us_cities.shp OCI:scott/tiger@localhost:1521/orcl122:us_cities

There are many things you can do in addition.

Select the rows to export:

ogr2ogr us_cities.shp OCI:scott/tiger@orcl122:us_cities -where "pop90 > 500000" -progress -overwrite

Select the columns to export. Notice you can rename them.

ogr2ogr us_cities.shp OCI:scott/tiger@orcl122:us_cities -select "id, city as city_name, state_abrv" -where "pop90 > 500000" -progress -overwrite

Even use some pseudo-SQL syntax:

ogr2ogr us_cities.shp OCI:scott/tiger@orcl122:us_cities -sql "select id, location from us_cities where pop90 > 500000" -progress -overwrite

Finally, you can also export multiple tables:

ogr2ogr us_data OCI:scott/tiger@localhost:1521/orcl122:us_cities,us_counties,us_states

In this case, us_data is the name of a directory that will be created if it does not exist yet, and each table becomes a shapefile in that directory:

$ ls -l us_data/
total 6772
-rw-rw-r--. 1 spatial spatial   48748 Sep 21 10:16 us_cities.dbf
-rw-rw-r--. 1 spatial spatial     165 Sep 21 10:16 us_cities.prj
-rw-rw-r--. 1 spatial spatial    5560 Sep 21 10:16 us_cities.shp
-rw-rw-r--. 1 spatial spatial    1660 Sep 21 10:16 us_cities.shx
-rw-rw-r--. 1 spatial spatial 1218031 Sep 21 10:16 us_counties.dbf
-rw-rw-r--. 1 spatial spatial     165 Sep 21 10:16 us_counties.prj
-rw-rw-r--. 1 spatial spatial 4605788 Sep 21 10:16 us_counties.shp
-rw-rw-r--. 1 spatial spatial   25940 Sep 21 10:16 us_counties.shx
-rw-rw-r--. 1 spatial spatial  156889 Sep 21 10:16 us_states.dbf
-rw-rw-r--. 1 spatial spatial     165 Sep 21 10:16 us_states.prj
-rw-rw-r--. 1 spatial spatial  837388 Sep 21 10:16 us_states.shp
-rw-rw-r--. 1 spatial spatial     548 Sep 21 10:16 us_states.shx

And not specifying any table name will export all (spatial) tables from the schema you connect as:

ogr2ogr us_data OCI:scott/tiger@localhost:1521/orcl122

EDIT:

Forgot to mention that you can use the same selection techniques (columns, rows, pseudo-sql) also when you import from shapefile (or from any other format).