Skip to content

DataSources

Palominos Sylvain edited this page Apr 13, 2019 · 2 revisions

H2GIS datasource

Allows to open, create an H2GIS database. The main entry is the H2GIS class that offers methods to get a Table or a SpatialTable.

Run the following groovy scripts in your favourite IDE or use the Groovy Console. More information at http://groovy-lang.org/groovyconsole.html

Groovy examples :

// Connect to an H2GIS file database. if the database doesn't exist a new one is created.
@GrabResolver(name='orbisgis', root='http://nexus-ng.orbisgis.org/')
@Grab(group='org.orbisgis', module='data-manager', version='1.0-SNAPSHOT')

import org.orbisgis.datamanager.h2gis.H2GIS

H2GIS.open([databaseName: './target/loadH2GIS'])
//How to query a spatial table
@GrabResolver(name='orbisgis', root='http://nexus-ng.orbisgis.org/')
@Grab(group='org.orbisgis', module='data-manager', version='1.0-SNAPSHOT')

import org.orbisgis.datamanager.h2gis.H2GIS
import org.orbisgis.datamanagerapi.dataset.ISpatialTable

def h2GIS = H2GIS.open([databaseName: './target/loadH2GIS'])
h2GIS.execute("""
                DROP TABLE IF EXISTS h2gis;
                CREATE TABLE h2gis (id int, the_geom point);
                INSERT INTO h2gis VALUES (1, 'POINT(10 10)'::GEOMETRY), (2, 'POINT(1 1)'::GEOMETRY);
        """)
def concat = ""
h2GIS.getSpatialTable "h2gis" eachRow { row -> concat += "$row.id $row.the_geom\n" }
println concat
//Returns 1 POINT (10 10) 2 POINT (1 1)
//How to display the metadata of a spatial table
@GrabResolver(name='orbisgis', root='http://nexus-ng.orbisgis.org/')
@Grab(group='org.orbisgis', module='data-manager', version='1.0-SNAPSHOT')

import org.orbisgis.datamanager.h2gis.H2GIS
import org.orbisgis.datamanagerapi.dataset.ISpatialTable

def h2GIS = H2GIS.open([databaseName: './target/loadH2GIS'])
h2GIS.execute("""
                DROP TABLE IF EXISTS h2gis;
                CREATE TABLE h2gis (id int, the_geom point);
                INSERT INTO h2gis VALUES (1, 'POINT(10 10)'::GEOMETRY), (2, 'POINT(1 1)'::GEOMETRY);
        """)

def concat = ""
h2GIS.getSpatialTable("h2gis").meta.each {row -> concat += "$row.columnLabel $row.columnType\n"}
println concat
//Returns ID 4 THE_GEOM 1111
//List column names from a shape file
@GrabResolver(name='orbisgis', root='http://nexus-ng.orbisgis.org/')
@Grab(group='org.orbisgis', module='data-manager', version='1.0-SNAPSHOT')


import org.orbisgis.datamanager.h2gis.H2GIS
import org.orbisgis.datamanagerapi.dataset.ITable

def  h2GIS = H2GIS.open([databaseName: '/tmp/loadH2GIS'])
println((h2GIS.link('/tmp/myshapeFile.shp') as ITable).columnNames)
//Execute a parametrized SQL file
// Let's imagine you have a sql file call myscript.sql that contains the following queries
// CREATE TABLE buffer_distance as SELECT ST_BUFFER(THE_GEOM, ${distance}) as the_geom FROM rivers;
// CREATE TABLE distance_risk as SELECT ST_ClosestPoint(a.the_geom, b.the_geom) as the_geom FROM buffer_distance as A, buildings as B WHERE ST_INTERSECTS(a.the_geom, b.the_geom);
@GrabResolver(name='orbisgis', root='http://nexus-ng.orbisgis.org/')
@Grab(group='org.orbisgis', module='data-manager', version='1.0-SNAPSHOT')


import org.orbisgis.datamanager.h2gis.H2GIS

def  h2GIS = H2GIS.open([databaseName: '/tmp/loadH2GIS'])
h2GIS.load('/tmp/rivers.shp')
h2GIS.load('/tmp/buildings.shp')
h2GIS.executeScript('/tmp/myscript.sql', [distance:12])
h2GIS.save("distance_risk","/tmp/distance_risk.shp");

PostGIS datasource

As for H2GIS a PostGIS datasource is available. The use is identical to H2GIS but some features are not supported such as linked files or tables.

Groovy examples :

// Connect to a PostGIS database
@GrabResolver(name='orbisgis', root='http://nexus-ng.orbisgis.org/')
@Grab(group='org.orbisgis', module='data-manager', version='1.0-SNAPSHOT')

import org.orbisgis.datamanager.postgis.POSTGIS

def dbProperties=  [databaseName: 'mydbName',
    user: 'myUserName',
    password: 'myPassword',
    url :'jdbc:postgresql://myHost'
    ]
def postgis = POSTGIS.open(dbProperties)
println(postgis.tablenames)
Clone this wiki locally