Commit fcc98bad authored by Luna Riegel's avatar Luna Riegel
Browse files

Refactor: Move method to Cache class

parent 786171a6
Pipeline #11752 failed with stage
in 1 minute and 7 seconds
......@@ -2,7 +2,9 @@ package de.hft.stuttgart.citydoctor2.database;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.datastructure.GmlId;
import de.hft.stuttgart.citydoctor2.utils.Pair;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.lang.ref.SoftReference;
......@@ -40,6 +42,17 @@ public class CityObjectCache {
return cityObject;
}
public static List<Pair<GmlId, List<GmlId>>> getCollidingObjectsList() {
List<Pair<GmlId, List<GmlId>>> collidingObjects = new ArrayList<>();
for (GmlId id : DatabaseHandler.getAllGmlIds()) {
List<GmlId> collidingIds = DatabaseHandler.getBBoxIntersectingObjects(id);
if (collidingIds != null && !collidingIds.isEmpty()) {
collidingObjects.add(new Pair<>(id, collidingIds));
}
}
return collidingObjects;
}
public void remove(GmlId id) {
cache.remove(id);
}
......
......@@ -125,6 +125,22 @@ public class DatabaseHandler {
return cos;
}
public static List<GmlId> getAllGmlIds() {
List<GmlId> cos = new ArrayList<>();
try (Connection con = new ConnectionWrapper(H2GISDBFactory.openSpatialDataBase(DB_NAME))) {
try (PreparedStatement ps = con.prepareStatement("SELECT gmlid FROM features")) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
cos.add(new GmlId(rs.getString("gmlid")));
}
}
} catch (SQLException e) {
logger.error("Connection to database failed while trying to get all CityObjects!");
logger.debug(e.getMessage());
}
return cos;
}
public static List<CityObject> getBufferIntersectingObjects(CityObject co, double bufferSize) {
List<CityObject> intersectingObjects = new ArrayList<>();
try (Connection con = new ConnectionWrapper(H2GISDBFactory.openSpatialDataBase(DB_NAME))) {
......@@ -161,12 +177,27 @@ public class DatabaseHandler {
return intersectingObjects;
}
public static List<Pair<CityObject, List<CityObject>>> getCollidingObjects() {
List<Pair<CityObject, List<CityObject>>> collidingObjects = new ArrayList<>();
// TODO: Implement query to return all Objects that have a collision and the list of whhich other objects collide
return collidingObjects;
public static List<GmlId> getBBoxIntersectingObjects(GmlId id) {
List<GmlId> intersectingObjects = new ArrayList<>();
try (Connection con = new ConnectionWrapper(H2GISDBFactory.openSpatialDataBase(DB_NAME))) {
try (PreparedStatement ps = con.prepareStatement("SELECT gmlid FROM features AS tab1" +
" WHERE ST_INTERSECTS(tab1.bbox, (SELECT bbox FROM features WHERE gmlid = ?)) AND tab1.gmlid <> ?")) {
ps.setString(1, id.toString());
ps.setString(2, id.toString());
ResultSet rs = ps.executeQuery();
while (rs.next()) {
intersectingObjects.add(new GmlId(rs.getString("gmlid")));
}
}
} catch (SQLException e) {
logger.error("Connection to database failed while trying to get bbox intersecting objects!");
logger.debug(e.getMessage());
}
return intersectingObjects;
}
public static int getFeatureCount() {
int count = -1;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment