2

Is it possible to use a geometry or other atributes from a Shapefile directly in a PostGIS SQL query, without the need of manually importing the Shapefile to the geodatabase first? I imagine something like:

SELECT ST_Area(tab.geometry) from "C:/data/polygon.shp" tab

or something similar. The shapefile always contains only one feature. I need to test some custom PL/pgSQL functions with some sample shapefile data, but I don´t want to load this sample data to the database.

1 Answer 1

1

With foreign data wrappers https://wiki.postgresql.org/wiki/Foreign_data_wrappers#Geo_Wrappers, yes. However, creating a fdw for a shapefile https://www.crunchydata.com/blog/querying-spatial-data-with-postgis-and-ogr_fdw is not any easier than importing the shapefile with ogr2ogr, for example. Single feature shapefiles are not the most reasonable use cases for fdw.

It seems that you will operate in PostGIS and then the alternatives that I can imagine are either importing data physically or using fdw. Otherwise you could use GDAL with SQLite dialect instead. All these functions are supported https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html and the usage is simple

ogrinfo -dialect SQLite -sql "SELECT ST_Area(geometry) from polygon"  "C:/data/polygon.shp"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.