Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygonize script #79

Merged
merged 6 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* OrbisWPS contains a set of libraries to build a Web Processing Service (WPS)
* compliant with the 2.0 specification.
*
* OrbisWPS is part of the OrbisGIS platform
*
* OrbisGIS is a java GIS application dedicated to research in GIScience.
* OrbisGIS is developed by the GIS group of the DECIDE team of the
* Lab-STICC CNRS laboratory, see <http://www.lab-sticc.fr/>.
*
* The GIS group of the DECIDE team is located at :
*
* Laboratoire Lab-STICC – CNRS UMR 6285
* Equipe DECIDE
* UNIVERSITÉ DE BRETAGNE-SUD
* Institut Universitaire de Technologie de Vannes
* 8, Rue Montaigne - BP 561 56017 Vannes Cedex
*
* OrbisWPS is distributed under GPL 3 license.
*
* Copyright (C) 2015-2018 CNRS (Lab-STICC UMR CNRS 6285)
*
*
* OrbisWPS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* OrbisWPS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* OrbisWPS. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, please consult: <http://www.orbisgis.org/>
* or contact directly:
* info_at_ orbisgis.org
*/
package org.orbisgis.orbiswps.scripts.scripts.Geometry2D.Convert

import org.orbisgis.orbiswps.groovyapi.input.*
import org.orbisgis.orbiswps.groovyapi.output.*
import org.orbisgis.orbiswps.groovyapi.process.*

/**
* This process converts geometries to polygons
*
* @author Erwan Bocher
*/
@Process(
title = "Polygonize",
description = "Convert a set of 2 or 3 dimensional geometries to polygons.\n\
Optionally, polygonize extracts points where lines intersect and uses them,together with points from the original lines, to construct polygons",
keywords = ["Vector","Geometry", "Convert"],
properties = ["DBMS_TYPE", "H2GIS","DBMS_TYPE", "POSTGIS"],
version = "1.0")
def processing() {
the_geom = geometricField[0]
String query = "CREATE TABLE ${outputTableName} AS SELECT "

if(isH2){
query+= " explod_id as id, the_geom from ST_EXPLODE('(SELECT "
if(node){
query += "st_polygonize(st_union(st_precisionreducer(st_node(st_accum(${the_geom})), 3))) as the_geom from ${inputJDBCTable} where st_dimension(${the_geom})>0)')"
}else{
query += "st_polygonize(st_accum(${the_geom})) as the_geom from ${inputJDBCTable} where st_dimension(${the_geom})>0)')"
}
}
else{
query+= " row_number() OVER () AS id, t.the_geom from "
if(node){
query += "(select (st_dump(st_polygonize(ST_UnaryUnion(ST_SnapToGrid(${the_geom}, 0.001)))).geom as the_geom from ${inputJDBCTable} where st_dimension(${the_geom})>0) as t"
}else{
query += "(select (st_dump(st_accum(${the_geom}))).geom as the_geom from ${inputJDBCTable} where st_dimension(${the_geom})>0) as t"
}
}

if(dropOutputTable){
sql.execute "drop table if exists ${outputTableName}".toString()
}

sql.execute(query)
if(dropInputTable){
sql.execute "drop table if exists ${inputJDBCTable}"
}
literalOutput = i18n.tr("Process done")
}


/****************/
/** INPUT Data **/
/****************/

@JDBCTableInput(
title = "Table to polygonize",
description = "The table to polygonize.",
dataTypes = ["GEOMETRY"])
String inputJDBCTable

/**********************/
/** INPUT Parameters **/
/**********************/

/** Name of the Geometric field of the JDBCTable inputJDBCTable. */
@JDBCColumnInput(
title = "Geometric column",
description = "The geometric column of the input table.",
jdbcTableReference = "inputJDBCTable",
dataTypes = ["GEOMETRY"])
String[] geometricField

@LiteralDataInput(
title = "Use line intersections ",
description = "Extracts points where lines intersect.")
Boolean node


@LiteralDataInput(
title = "Drop the output table if exists",
description = "Drop the output table if exists.")
Boolean dropOutputTable

@LiteralDataInput(
title = "Output table name",
description = "Name of the table containing the result of the process.")
String outputTableName


@LiteralDataInput(
title = "Drop the input table",
description = "Drop the input table when the script is finished.")
Boolean dropInputTable


/*****************/
/** OUTPUT Data **/
/*****************/

/** String output of the process. */
@LiteralDataOutput(
title = "Output message",
description = "The output message.")
String literalOutput
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ScriptTests {
/** {@link java.sql.Statement} object used to execute Sql queries. */
private Statement st;


@BeforeClass
public static void tearUp() throws Exception {
// Keep a connection alive to not close the DataBase on each unit test
Expand Down Expand Up @@ -564,4 +565,93 @@ public void testConcavityIndice1() throws Exception {
Assert.assertTrue(rs.getDouble(2)<1);
rs.close();
}

@Test
public void testPolygonize1() throws Exception {
String scriptPath = WPSScriptExecute.class.getResource("scripts/Geometry2D/Convert/polygonize.groovy").getPath();
//Prepare input and output values
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("inputJDBCTable", "geomForms");
inputMap.put("geometricField", new String[]{"the_geom"});
inputMap.put("dropOutputTable", true);
inputMap.put("outputTableName", "polygonize_res");
Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put("sql", sql);
propertyMap.put("isH2", true);

//Add data
st.execute("drop table if exists geomForms; create table geomForms (the_geom polygon); "
+ "INSERT INTO geomForms VALUES(ST_GeomFromText('POLYGON ((100 330, 220 330, 220 230, 100 230, 100 330))'))"
+ ",(ST_GeomFromText('POLYGON ((160 340, 290 340, 290 260, 160 260, 160 340))')),"
+ "(ST_GeomFromText('POLYGON ((60 270, 140 270, 140 180, 60 180, 60 270))'))");

//Execute
Map<String, Object> outputMap = WPSScriptExecute.run(groovyClassLoader, scriptPath, propertyMap, inputMap);
Assert.assertEquals("Process done", outputMap.get("literalOutput"));
ResultSet rs = st.executeQuery(
"SELECT count(*) FROM polygonize_res;");
assertTrue(rs.next());
Assert.assertEquals(3, rs.getInt(1));
rs.close();
}

@Test
public void testPolygonize2() throws Exception {
String scriptPath = WPSScriptExecute.class.getResource("scripts/Geometry2D/Convert/polygonize.groovy").getPath();
//Prepare input and output values
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("inputJDBCTable", "geomForms");
inputMap.put("geometricField", new String[]{"the_geom"});
inputMap.put("node", true);
inputMap.put("dropOutputTable", true);
inputMap.put("outputTableName", "polygonize_res");
Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put("sql", sql);
propertyMap.put("isH2", true);

//Add data
st.execute("drop table if exists geomForms; create table geomForms (the_geom polygon); "
+ "INSERT INTO geomForms VALUES(ST_GeomFromText('POLYGON ((100 330, 220 330, 220 230, 100 230, 100 330))'))"
+ ",(ST_GeomFromText('POLYGON ((160 340, 290 340, 290 260, 160 260, 160 340))')),"
+ "(ST_GeomFromText('POLYGON ((60 270, 140 270, 140 180, 60 180, 60 270))'))");

//Execute
Map<String, Object> outputMap = WPSScriptExecute.run(groovyClassLoader, scriptPath, propertyMap, inputMap);
Assert.assertEquals("Process done", outputMap.get("literalOutput"));
ResultSet rs = st.executeQuery(
"SELECT count(*) FROM polygonize_res;");
assertTrue(rs.next());
Assert.assertEquals(5, rs.getInt(1));
rs.close();
}

@Test
public void testPolygonize3() throws Exception {
String scriptPath = WPSScriptExecute.class.getResource("scripts/Geometry2D/Convert/polygonize.groovy").getPath();
//Prepare input and output values
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("inputJDBCTable", "geomForms");
inputMap.put("geometricField", new String[]{"the_geom"});
inputMap.put("node", true);
inputMap.put("dropOutputTable", true);
inputMap.put("outputTableName", "polygonize_res");
Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put("sql", sql);
propertyMap.put("isH2", true);
//Add data
st.execute("drop table if exists geomForms; create table geomForms (the_geom linestring); "
+ "INSERT INTO geomForms VALUES(ST_GeomFromText('LINESTRING (90 200, 240 330)'))"
+ ",(ST_GeomFromText('LINESTRING (150 340, 290 240)')),"
+" (ST_GeomFromText('LINESTRING (100 290, 190 200, 270 310)')),"
+ "(ST_GeomFromText('LINESTRING (220 360, 90 260, 140 180)'))");

//Execute
Map<String, Object> outputMap = WPSScriptExecute.run(groovyClassLoader, scriptPath, propertyMap, inputMap);
Assert.assertEquals("Process done", outputMap.get("literalOutput"));
ResultSet rs = st.executeQuery(
"SELECT count(*) FROM polygonize_res;");
assertTrue(rs.next());
Assert.assertEquals(3, rs.getInt(1));
rs.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public ProcessIdentifierImpl(ProcessOffering processOffering, URL sourceUrl){
this.i18n = I18nFactory.getI18n(ProcessIdentifierImpl.class);
}

@Override
public void setI18n(I18n i18n){
this.i18n = i18n;
}
Expand Down