Shapefiles - ConstantB/ontop-spatial GitHub Wiki

Querying shapefiles using ontop-spatial

Shapefiles is a very common ESRI-compliant format for geospatial data. It is a tabular format (like CSV, TSV, etc.) and files of that format can be opened with GIS. Many datasets with geospatial information come in shapefile format- among others. Ontop-spatial enables users to query those datasets almost transparently, handling them as additional tables in a database (and creating mappings for them). In this tutorial we present a roadmap of how one can import shapefiles to a geospatial database (i.e., PostGIS) and eventually pose GeoSPARQL queries.

First, for the sake of the example, we will use the following shapefiles about Open Street map data for Bremen, Germany.

  1. Download the data provided in the link above
  2. Make sure you have permissions to access the shapefiles in the folder you just extracted.
  3. Import the shpfile to the database using the shp2pgsql tool: shp2pgsql -I -s 4326 <path-to-shapefile> > <dump-name>.sql

The -I argument means that an R-tree index will be used on the geometry column of the table. The -s argument is the SRID of the geometries (ATTENTION: be sure that you provide the correct SRID or your geometries will not be represented correctly! 4326 is the srid for the world geodetic system (WGS84)). In most cases, the data provider mentions also the Coordinate reference system in which the geometries are represented.

This step assumes that the database already exists. If you do not want to import the shapefile as a table in an existing database (e.g., to, possibly, add it to an existing dataset), you should have created a database named before and it should be spatially enabled, i.e., the postgis extension should be created for that database (CREATE EXTENSION postgis;). Then you can perform the step described above.

Next, Run the produced sql script .sql as follows: psql -d <dbname> -f <dump-name>.sql

This will create a table in the database , with the same name as the shapefile name, and every column of that table will have the same name as the respective attribute of the shapefile. Note that an "extra" column is added: The geometry column where geometries are stored.

  1. Now add mappings! Now the ontology and the mappings can be created as in the default case of ontop
  2. Run geospatial queries! For example, Select the geometries of ports
select distinct ?geo where {
                ?x lgd:landUse lgd:port .
                ?x geo:asWKT ?geo .
                ?x1 geo:asWKT ?geo1 .
                FILTER(<http://www.opengis.net/def/function/geosparql/sfIntersects>(?geo,?geo1))};
⚠️ **GitHub.com Fallback** ⚠️