Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CityDoctor
CityDoctor2
Commits
3a45b8d9
Commit
3a45b8d9
authored
Jul 29, 2025
by
Luna Riegel
Browse files
Refactor: Rework handling of DatabaseHandler returns
parent
ee22bc29
Changes
2
Hide whitespace changes
Inline
Side-by-side
CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/database/DatabaseHandler.java
View file @
3a45b8d9
...
...
@@ -70,6 +70,10 @@ public class DatabaseHandler {
}
}
/**
* Saves a CityObject to the embedded database, updating an existing entry if it exists.
* @param co the CityObject
*/
public
static
void
marshallCityObject
(
CityObject
co
)
{
try
(
Connection
con
=
new
ConnectionWrapper
(
H2GISDBFactory
.
openSpatialDataBase
(
DB_NAME
)))
{
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"MERGE INTO features VALUES (?, ?, ?, ?)"
))
{
...
...
@@ -91,6 +95,11 @@ public class DatabaseHandler {
}
}
/**
* Retrieves a CityObject from the embedded database.
* @param id the gmlId of the requested Feature
* @return the Feature, or null if no entry was found
*/
public
static
CityObject
unmarshallCityObject
(
GmlId
id
)
{
try
(
Connection
con
=
new
ConnectionWrapper
(
H2GISDBFactory
.
openSpatialDataBase
(
DB_NAME
)))
{
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"SELECT gmlid, bbox, data, type FROM features WHERE gmlid = ?"
))
{
...
...
@@ -150,16 +159,22 @@ public class DatabaseHandler {
return
cos
;
}
public
static
List
<
CityObject
>
getBufferIntersectingObjects
(
CityObject
co
,
double
bufferSize
)
{
List
<
CityObject
>
intersectingObjects
=
new
ArrayList
<>();
public
static
List
<
GmlId
>
getBufferIntersectingObjects
(
CityObject
co
,
double
bufferSize
)
{
return
getBufferIntersectingObjects
(
co
.
getGmlId
(),
bufferSize
);
}
public
static
List
<
GmlId
>
getBufferIntersectingObjects
(
GmlId
gmlID
,
double
bufferSize
)
{
List
<
GmlId
>
intersectingObjects
=
new
ArrayList
<>();
try
(
Connection
con
=
new
ConnectionWrapper
(
H2GISDBFactory
.
openSpatialDataBase
(
DB_NAME
)))
{
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"SELECT gmlid
, bbox, data, type
FROM features AS tab1"
+
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"SELECT gmlid FROM features AS tab1"
+
" WHERE ST_INTERSECTS(ST_BUFFER(tab1.bbox,?), (SELECT bbox FROM features WHERE gmlid = ?)) AND tab1.gmlid <> ?"
))
{
ps
.
setDouble
(
1
,
bufferSize
);
ps
.
setString
(
2
,
co
.
getGmlId
()
.
toString
());
ps
.
setString
(
3
,
co
.
getGmlId
()
.
toString
());
ps
.
setString
(
2
,
gmlID
.
toString
());
ps
.
setString
(
3
,
gmlID
.
toString
());
ResultSet
rs
=
ps
.
executeQuery
();
intersectingObjects
.
addAll
(
retrieveCityObjects
(
rs
));
while
(
rs
.
next
())
{
intersectingObjects
.
add
(
new
GmlId
(
rs
.
getString
(
"gmlid"
)));
}
}
}
catch
(
SQLException
e
)
{
logger
.
error
(
"Connection to database failed while trying to get buffer intersecting objects!"
);
...
...
@@ -168,22 +183,8 @@ public class DatabaseHandler {
return
intersectingObjects
;
}
public
static
List
<
CityObject
>
getBBoxIntersectingObjects
(
CityObject
co
)
{
List
<
CityObject
>
intersectingObjects
=
new
ArrayList
<>();
try
(
Connection
con
=
new
ConnectionWrapper
(
H2GISDBFactory
.
openSpatialDataBase
(
DB_NAME
)))
{
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"SELECT gmlid, bbox, data, type FROM features AS tab1"
+
" WHERE ST_INTERSECTS(tab1.bbox, (SELECT bbox FROM features WHERE gmlid = ?)) AND tab1.gmlid <> ?"
))
{
ps
.
setString
(
1
,
co
.
getGmlId
().
toString
());
ps
.
setString
(
2
,
co
.
getGmlId
().
toString
());
ResultSet
rs
=
ps
.
executeQuery
();
intersectingObjects
.
addAll
(
retrieveCityObjects
(
rs
));
}
}
catch
(
SQLException
e
)
{
logger
.
error
(
"Connection to database failed while trying to get bbox intersecting objects!"
);
logger
.
debug
(
e
.
getMessage
());
}
return
intersectingObjects
;
public
static
List
<
GmlId
>
getBBoxIntersectingObjects
(
CityObject
co
)
{
return
getBBoxIntersectingObjects
(
co
.
getGmlId
());
}
public
static
List
<
GmlId
>
getBBoxIntersectingObjects
(
GmlId
id
)
{
...
...
CityDoctorParent/CityDoctorModel/src/test/java/de/hft/stuttgart/citydoctor2/database/DataBaseHandlerTest.java
View file @
3a45b8d9
...
...
@@ -7,6 +7,7 @@ import de.hft.stuttgart.citydoctor2.datastructure.ConcretePolygon;
import
de.hft.stuttgart.citydoctor2.datastructure.Geometry
;
import
de.hft.stuttgart.citydoctor2.datastructure.GeometryTestUtils
;
import
de.hft.stuttgart.citydoctor2.datastructure.GeometryType
;
import
de.hft.stuttgart.citydoctor2.datastructure.GmlId
;
import
de.hft.stuttgart.citydoctor2.datastructure.LinearRing
;
import
de.hft.stuttgart.citydoctor2.datastructure.Lod
;
import
de.hft.stuttgart.citydoctor2.datastructure.Vertex
;
...
...
@@ -144,11 +145,13 @@ public class DataBaseHandlerTest {
DatabaseHandler
.
marshallCityObject
(
b4
);
assertEquals
(
4
,
DatabaseHandler
.
getFeatureCount
());
List
<
CityObject
>
objects
=
DatabaseHandler
.
getBBoxIntersectingObjects
(
b
);
List
<
GmlId
>
objects
=
DatabaseHandler
.
getBBoxIntersectingObjects
(
b
);
assertEquals
(
1
,
objects
.
size
());
assertEquals
(
4
,
DatabaseHandler
.
getFeatureCount
());
assertCityObjectEquality
(
b2
,
objects
.
get
(
0
));
CityObject
co
=
DatabaseHandler
.
unmarshallCityObject
(
objects
.
get
(
0
));
assertNotNull
(
co
);
assertCityObjectEquality
(
b2
,
co
);
}
...
...
@@ -223,11 +226,13 @@ public class DataBaseHandlerTest {
DatabaseHandler
.
marshallCityObject
(
b4
);
assertEquals
(
4
,
DatabaseHandler
.
getFeatureCount
());
List
<
CityObject
>
objects
=
DatabaseHandler
.
getBufferIntersectingObjects
(
b
,
Math
.
sqrt
(
2
));
List
<
GmlId
>
objects
=
DatabaseHandler
.
getBufferIntersectingObjects
(
b
,
Math
.
sqrt
(
2
));
assertEquals
(
1
,
objects
.
size
());
assertEquals
(
4
,
DatabaseHandler
.
getFeatureCount
());
assertCityObjectEquality
(
b2
,
objects
.
get
(
0
));
CityObject
co
=
DatabaseHandler
.
unmarshallCityObject
(
objects
.
get
(
0
));
assertNotNull
(
co
);
assertCityObjectEquality
(
b2
,
co
);
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment