-
Notifications
You must be signed in to change notification settings - Fork 63
4.5 Use H2GIS with Groovy
The following example demonstrates how to use H2GIS with the Groovy scripting language.
Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax. Please go to http://groovy-lang.org/ for more information.
Download and install Groovy from http://groovy-lang.org/download.html
The following script uses the @Grab annotation. With this annotation, we define the H2GIS dependency for the script and they will be automatically downloaded and added to the classpath when we run the script. Then the H2GIS database is created and a connection is opened on it.
@Grab(group='org.orbisgis', module='h2gis', version='1.5.0')
import java.sql.Connection;
import org.h2gis.functions.factory.H2GISDBFactory;
String DB_NAME = "file://"+ "/tmp/myH2GIS_database";
//Create the H2 database and load the spatial functions
Connection con = H2GISDBFactory.createSpatialDataBase(DB_NAME);
//Now you can play with the database
To execute this script, go in a Terminal and call
groovy thescriptFile.groovy
If you want to see what Grape is doing set the system property as
groovy -Dgroovy.grape.report.downloads=true thescriptFile.groovy
If you want to take profit of the last H2GIS features, you must download H2GIS from our maven repository.
@GrabResolver(name='orbisgis', root='https://oss.sonatype.org/content/repositories/snapshots/')
@Grab(group='org.orbisgis', module='h2gis', version='2.0.0-SNAPSHOT')
import java.sql.Connection;
import org.h2gis.functions.factory.H2GISDBFactory;
String DB_NAME = "file://"+"/tmp/myH2GIS_database";
//Create the H2 database and load the spatial functions
Connection con = H2GISDBFactory.createSpatialDataBase(DB_NAME);
//Now you can play with the database
//This script is used to retrieve all buildings from the French commune 56260 (Vannes).
@GrabResolver(name='orbisgis', root='http://repo.orbisgis.org/')
@Grab(group='org.orbisgis', module='h2gis', version='1.4.0-SNAPSHOT')
import java.sql.Connection;
import org.h2gis.functions.factory.H2GISDBFactory;
import groovy.sql.Sql
def tmpPath = System.getProperty("java.io.tmpdir")
def outputFolder = tmpPath + File.separator + "osm_demo"
//Create a new outputFolder each time
def tmpFolder = new File(outputFolder);
tmpFolder.deleteDir()
tmpFolder.mkdir()
String DB_NAME = "file://"+ outputFolder+File.separator+"myH2GIS_database";
//Create the H2 database and load the spatial functions
Connection con = H2GISDBFactory.createSpatialDataBase(DB_NAME);
//Set a path for the file
pathOfTheFile = outputFolder+ File.separator +"thefile.osm"
//Replace the query
queryURL ="area['ref:INSEE'='56260'][admin_level=8]->.searchArea;way [building](area.searchArea);(._;>;);out;"
apiUrl = "http://overpass-api.de/api/interpreter?data=";
def connection = new URL( apiUrl+URLEncoder.encode(queryURL) ).openConnection() as HttpURLConnection
connection.setRequestMethod("GET")
//Save the result in a file
if(connection.responseCode==200){
println "Downloading the OSM data from overpass api"
new File(pathOfTheFile) << connection.inputStream
}
else{
println "Cannot execute the query"
}
//Create the groovy sql wrapper
def sql = Sql.newInstance(con);
println "Importing the OSM file"
//Import the OSM file
sql.execute "CALL OSMREAD('"+ pathOfTheFile+"', 'OSM_BRETAGNE', true)";
//Now put your analyses here