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

TRIANGLES in Regular Grid #563

Merged
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
Expand Up @@ -24,6 +24,7 @@ import org.geotools.jdbc.JDBCDataStore
import org.h2gis.functions.spatial.crs.ST_SetSRID
import org.h2gis.functions.spatial.crs.ST_Transform
import org.h2gis.utilities.GeometryTableUtilities
import org.h2gis.utilities.JDBCUtilities
import org.h2gis.utilities.TableLocation
import org.locationtech.jts.geom.Geometry
import org.locationtech.jts.geom.GeometryFactory
Expand All @@ -41,6 +42,8 @@ inputs = [
buildingTableName : [
name : 'Buildings table name',
title: 'Buildings table name',
description: 'Buildings table, receivers that are into this tables geometries will be deleted',
min : 0, max: 1,
type : String.class
],
fence : [
Expand Down Expand Up @@ -87,6 +90,13 @@ inputs = [
description: 'Height of receivers in meters',
min : 0, max: 1,
type : Double.class
],
outputTriangleTable : [
name : 'Output triangle table',
title : 'Output triangle table',
description: 'Output a triangle table in order to be used to generate iso contours with Create_Isosurface',
min : 0, max: 1,
type : Boolean.class
]
]

Expand Down Expand Up @@ -127,6 +137,8 @@ def run(input) {

def exec(connection, input) {

Sql sql = new Sql(connection)

// output string, the information given back to the user
String resultString = null

Expand Down Expand Up @@ -155,39 +167,44 @@ def exec(connection, input) {
h = input['height'] as Double
}

String sources_table_name = "SOURCES"
boolean createTriangles = false
if(input['outputTriangleTable']) {
createTriangles = Boolean.parseBoolean(input['outputTriangleTable'] as String)
}

String sources_table_name = ""
if (input['sourcesTableName']) {
sources_table_name = input['sourcesTableName']
}
sources_table_name = sources_table_name.toUpperCase()

String building_table_name = "BUILDINGS"
String building_table_name = ""
if (input['buildingTableName']) {
building_table_name = input['buildingTableName']
}
building_table_name = building_table_name.toUpperCase()

int srid = GeometryTableUtilities.getSRID(connection, TableLocation.parse(building_table_name))

Sql sql = new Sql(connection)
//Delete previous receivers grid.
sql.execute(String.format("DROP TABLE IF EXISTS %s", receivers_table_name))
String queryGrid = null
// Try to find the best SRID for receivers table
int srid = 0
if(input['buildingTableName']) {
srid = GeometryTableUtilities.getSRID(connection, TableLocation.parse(building_table_name))
}

if (srid == 0 && input['sourcesTableName']) {
srid = GeometryTableUtilities.getSRID(connection, TableLocation.parse(sources_table_name) as String)
}

// Reproject fence
int targetSrid = GeometryTableUtilities.getSRID(connection, TableLocation.parse(building_table_name))
if (targetSrid == 0 && input['sourcesTableName']) {
targetSrid = GeometryTableUtilities.getSRID(connection, TableLocation.parse(sources_table_name))
if (srid == 0 && input['fenceTableName']) {
srid = GeometryTableUtilities.getSRID(connection, TableLocation.parse(input['fenceTableName'] as String))
}

Geometry fenceGeom = null
if (input['fence']) {
if (targetSrid != 0) {
if (srid != 0) {
// Transform fence to the same coordinate system than the buildings & sources
WKTReader wktReader = new WKTReader()
fence = wktReader.read(input['fence'] as String)
fenceGeom = ST_Transform.ST_Transform(connection, ST_SetSRID.setSRID(fence, 4326), targetSrid)
def fence = wktReader.read(input['fence'] as String)
fenceGeom = ST_Transform.ST_Transform(connection, ST_SetSRID.setSRID(fence, 4326), srid)
} else {
throw new Exception("Unable to find buildings or sources SRID, ignore fence parameters")
}
Expand All @@ -197,7 +214,10 @@ def exec(connection, input) {
fenceGeom = GeometryTableUtilities.getEnvelope(connection, TableLocation.parse(building_table_name), "THE_GEOM")
}

sql.execute("CREATE TABLE " + receivers_table_name + "(THE_GEOM GEOMETRY) AS SELECT ST_SETSRID(ST_UPDATEZ(THE_GEOM, " + h + "), " + srid + ") THE_GEOM FROM ST_MakeGridPoints(ST_GeomFromText('" + fenceGeom + "')," + delta + "," + delta + ");")
//Delete previous receivers grid.
sql.execute(String.format("DROP TABLE IF EXISTS %s", receivers_table_name))

sql.execute("CREATE TABLE " + receivers_table_name + "(THE_GEOM GEOMETRY, ID_COL INTEGER, ID_ROW INTEGER) AS SELECT ST_SETSRID(ST_UPDATEZ(THE_GEOM, " + h + "), " + srid + ") THE_GEOM, ID_COL, ID_ROW FROM ST_MakeGridPoints(ST_GeomFromText('" + fenceGeom + "')," + delta + "," + delta + ");")
sql.execute("ALTER TABLE " + receivers_table_name + " ADD COLUMN PK SERIAL PRIMARY KEY")

logger.info("Create spatial index on " + receivers_table_name)
Expand All @@ -216,6 +236,23 @@ def exec(connection, input) {
logger.info("Delete receivers near sources")
sql.execute("delete from " + receivers_table_name + " g where exists (select 1 from " + sources_table_name + " r where st_expand(g.the_geom, 1) && r.the_geom and st_distance(g.the_geom, r.the_geom) < 1 limit 1);")
}
if(createTriangles) {
sql.execute("DROP TABLE IF EXISTS TRIANGLES")
sql.execute("CREATE TABLE TRIANGLES(pk serial NOT NULL, the_geom geometry(POLYGON Z, "+srid+"), PK_1 integer not null," +
" PK_2 integer not null, PK_3 integer not null, cell_id integer not null, PRIMARY KEY (PK))")
sql.execute("INSERT INTO TRIANGLES(THE_GEOM, PK_1, PK_2, PK_3, CELL_ID) " +
"SELECT ST_ConvexHull(ST_UNION(A.THE_GEOM, ST_UNION(B.THE_GEOM, C.THE_GEOM))) THE_GEOM, " +
"A.PK PK_1, B.PK PK_2, C.PK PK_3, 0" +
" FROM "+receivers_table_name+" A, "+receivers_table_name+" B, "+receivers_table_name+" C " +
"WHERE A.ID_ROW = B.ID_ROW + 1 AND A.ID_COL = B.ID_COL AND " +
"A.ID_ROW = C.ID_ROW + 1 AND A.ID_COL = C.ID_COL + 1;")
sql.execute("INSERT INTO TRIANGLES(THE_GEOM, PK_1, PK_2, PK_3, CELL_ID) " +
"SELECT ST_ConvexHull(ST_UNION(A.THE_GEOM, ST_UNION(B.THE_GEOM, C.THE_GEOM))) THE_GEOM, " +
"A.PK PK_1, B.PK PK_2, C.PK PK_3, 0" +
" FROM "+receivers_table_name+" A, "+receivers_table_name+" B, "+receivers_table_name+" C " +
"WHERE A.ID_ROW = B.ID_ROW + 1 AND A.ID_COL = B.ID_COL + 1" +
" AND A.ID_ROW = C.ID_ROW AND A.ID_COL = C.ID_COL + 1;")
}

return [tableNameCreated: "Process done. Table of receivers " + receivers_table_name + " created !"]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,19 @@ class TestReceivers extends JdbcTestCase {

}

public void testRegularGridWithTriangleTable() {

def sql = new Sql(connection)

SHPRead.importTable(connection, TestReceivers.getResource("buildings.shp").getPath())
SHPRead.importTable(connection, TestReceivers.getResource("roads.shp").getPath())

new Regular_Grid().exec(connection, ["fenceTableName" : "BUILDINGS",
"delta" : 50,
"outputTriangleTable" : true])

assertEquals(2154, GeometryTableUtilities.getSRID(connection, TableLocation.parse("RECEIVERS")))

assertEquals(1920, sql.firstRow("SELECT COUNT(*) FROM TRIANGLES")[0] as Integer)
}
}