Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
22a6fee
Improving implementation of idCoordinateSource.
staudtMarius Oct 27, 2022
116d351
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Oct 27, 2022
acf9a5d
Improving code.
staudtMarius Oct 28, 2022
e424f0d
Merge remote-tracking branch 'origin/ms/#689-improving-implementation…
staudtMarius Oct 28, 2022
6e8025c
fmt
staudtMarius Oct 28, 2022
d879543
Adding some documentation.
staudtMarius Oct 28, 2022
a2d2faf
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Oct 31, 2022
d4d3156
Implementing some tests.
staudtMarius Oct 31, 2022
00c21f2
Adding missing file for SqlIdCoordinateSource test.
staudtMarius Oct 31, 2022
a19ff9f
fmt
staudtMarius Oct 31, 2022
354fc4a
fmt
staudtMarius Oct 31, 2022
9b59372
fmt
staudtMarius Oct 31, 2022
344d6fb
Some refactoring.
staudtMarius Nov 2, 2022
2644931
Implementing changes in CsvIdCoordinateSource.
staudtMarius Nov 3, 2022
c83873b
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Nov 7, 2022
f0ca780
Adding some tests.
staudtMarius Nov 7, 2022
529bee2
Adding requested changes.
staudtMarius Nov 8, 2022
9613ba9
Adding requested changes.
staudtMarius Nov 8, 2022
42ff465
``SqlIdCoordinateSource`` uses ``PostGis`` now.
staudtMarius Nov 15, 2022
975fa09
Fixing test problems.
staudtMarius Nov 15, 2022
d196187
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Nov 21, 2022
c98ed65
Implementing requested changes.
staudtMarius Nov 24, 2022
69b58f4
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Nov 24, 2022
44d1f70
Fixing ``Codacy`` error.
staudtMarius Nov 24, 2022
e3f5fee
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Nov 28, 2022
46476de
Updating code to use new method of ``PSU``.
staudtMarius Nov 28, 2022
ab993c2
Merge remote-tracking branch 'origin/ms/#689-improving-implementation…
staudtMarius Nov 28, 2022
d359718
Updating code to use new method of ``PSU``.
staudtMarius Nov 28, 2022
c7fa204
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
sebastian-peter Dec 9, 2022
ca6a2f7
Implementing requested changes.
staudtMarius Jan 9, 2023
c6768ec
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Jan 9, 2023
6c2c789
fmt
staudtMarius Jan 9, 2023
fbcfed4
Fixing failing test.
staudtMarius Jan 10, 2023
613ce64
Improving IdCoordinateSource documentation
staudtMarius Jan 12, 2023
f9b8c2f
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Jan 12, 2023
cd0993b
Fixing failing test.
staudtMarius Jan 12, 2023
4070b13
fmt
staudtMarius Jan 12, 2023
aac4301
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
sebastian-peter Jan 20, 2023
f956731
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
sebastian-peter Jan 25, 2023
b4bd815
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Feb 2, 2023
e5e5d15
Improving documentation.
staudtMarius Feb 2, 2023
e6860e4
Adding markdown support to docs.
staudtMarius Feb 7, 2023
7e0dc92
Fix conf.py
staudtMarius Feb 7, 2023
b46a15a
Fixing markdown table rendering.
staudtMarius Feb 8, 2023
9732383
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Feb 14, 2023
54d6e45
Merging `dev` changes.
staudtMarius Feb 14, 2023
f3ed621
Fixing ``readthedoc``.
staudtMarius Feb 14, 2023
2dbb6e2
Fixing ``readthedoc``.
staudtMarius Feb 15, 2023
34faee4
Fixing ``readthedoc``.
staudtMarius Feb 15, 2023
76d9f49
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Feb 17, 2023
b7cee23
Some documentation improvements.
staudtMarius Feb 17, 2023
b6c2029
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Feb 20, 2023
bf23b8c
Resolving sonatype issues.
staudtMarius Feb 20, 2023
b0a0ec2
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Feb 20, 2023
eaa51f0
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Mar 9, 2023
d873354
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Apr 11, 2023
b93afed
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Apr 14, 2023
45d10ab
Implementing requested changes.
staudtMarius Apr 14, 2023
71e02e3
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Apr 18, 2023
bc1c713
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Apr 24, 2023
5e19cd6
fmt
staudtMarius Apr 24, 2023
13bbc45
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Apr 24, 2023
a3455c0
Indentation
sebastian-peter Apr 24, 2023
377cc32
Updating readthedocs online build config
sebastian-peter Apr 24, 2023
cabc9f0
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius Apr 27, 2023
64f17b3
Implementing requested changes.
staudtMarius Apr 27, 2023
6885b36
Fixing documentation issues.
staudtMarius May 2, 2023
5f124b0
Fixing documentation issues.
staudtMarius May 2, 2023
78e3aa2
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius May 4, 2023
85ae7f0
Implementing requested documentation changes.
staudtMarius May 4, 2023
6841c3e
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius May 9, 2023
ee15cf3
Merge branch 'dev' into ms/#689-improving-implementation-of-ÍdCoordin…
staudtMarius May 15, 2023
3a1142c
Implementing requested changes.
staudtMarius May 16, 2023
7e9c6e6
fmt
staudtMarius May 16, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `ThermalGrid` as a container for a completely connected thermal grid
- `EmResult` and `FlexOptionsResult` for Energy Management Systems [#651](https://github.com/ie3-institute/PowerSystemDataModel/issues/651)
- `EvcsInput` now has a parameter for enabling and disabling vehicle to grid support [#681](https://github.com/ie3-institute/PowerSystemDataModel/issues/681)
- Adding SQL id coordinate sources (``IdCoordinateSource``) [#689](https://github.com/ie3-institute/PowerSystemDataModel/issues/689)

### Fixed
- Reduced code smells [#492](https://github.com/ie3-institute/PowerSystemDataModel/issues/492)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class CosmoIdCoordinateFactory extends IdCoordinateFactory {
private static final String LAT_GEO = "latgeo";
private static final String LONG_ROT = "longrot";
private static final String LAT_ROT = "latrot";
private static final String Point = "point";

@Override
protected Pair<Integer, Point> buildModel(SimpleFactoryData data) {
Expand Down Expand Up @@ -51,4 +52,9 @@ public String getLatField() {
public String getLonField() {
return LONG_GEO;
}

@Override
public String getPointField() {
return Point;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class IconIdCoordinateFactory extends IdCoordinateFactory {
private static final String LONG = "longitude";
private static final String LAT = "latitude";
private static final String TYPE = "coordinatetype";
private static final String Point = "point";

@Override
protected Pair<Integer, Point> buildModel(SimpleFactoryData data) {
Expand Down Expand Up @@ -50,4 +51,9 @@ public String getLatField() {
public String getLonField() {
return LONG;
}

@Override
public String getPointField() {
return Point;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ protected IdCoordinateFactory() {

/** @return the field id for the coordinate longitude */
public abstract String getLonField();

/** @return the filed id for the coordinate point */
public abstract String getPointField();
}
98 changes: 94 additions & 4 deletions src/main/java/edu/ie3/datamodel/io/source/IdCoordinateSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import edu.ie3.util.geo.CoordinateDistance;
import edu.ie3.util.geo.GeoUtils;
import java.util.*;
import javax.measure.quantity.Length;
import org.locationtech.jts.geom.Point;
import tech.units.indriya.ComparableQuantity;

/**
* This class serves mapping purposes between the ID of a coordinate and the actual coordinate with
* latitude and longitude values, which is especially needed for data source that don't offer
* combined primary or foreign keys.
*/
public interface IdCoordinateSource extends DataSource {
double earthRadius = 6378137.0;

/**
* Get the matching coordinate for the given ID
Expand Down Expand Up @@ -56,9 +59,8 @@ public interface IdCoordinateSource extends DataSource {
* @param n how many neighbours to look up
* @return the n nearest coordinates to the given point
*/
default List<CoordinateDistance> getNearestCoordinates(Point coordinate, int n) {
return getNearestCoordinates(coordinate, n, getAllCoordinates());
}
List<CoordinateDistance> getNearestCoordinates(
Point coordinate, int n, ComparableQuantity<Length> distance);

/**
* Returns the nearest n coordinate points to the given coordinate from a given collection of
Expand All @@ -75,6 +77,94 @@ default List<CoordinateDistance> getNearestCoordinates(
GeoUtils.calcOrderedCoordinateDistances(
coordinate,
(coordinates != null && !coordinates.isEmpty()) ? coordinates : getAllCoordinates());
return sortedDistances.stream().limit(n).toList();
return restrictToBoundingBoxWithSetNumberOfCorner(coordinate, sortedDistances, n);
}

/**
* Method to turn a distance into a latitude and longitude deltas. The methode can be found here:
* <a href="https://math.stackexchange.com/questions/474602/reverse-use-of-haversine-formula">
*
* @param coordinate the coordinate at the center of the bounding box.
* @return x- and y-delta in degree
*/
default double[] calculateXYDelta(Point coordinate, ComparableQuantity<Length> distance) {
// y-degrees are evenly spaced, so we can just divide a distance
// by the earth's radius to get a y-delta in radians
double deltaY = distance.getValue().doubleValue() / earthRadius;

// because the spacing between x-degrees change between the equator
// and the poles, we need to calculate the x-delta using the inverse
// haversine formula
double sinus = Math.sin(deltaY / 2);
double squaredSinus = sinus * sinus;
double cosine = Math.cos(Math.toRadians(coordinate.getY()));
double squaredCosine = cosine * cosine;

double deltaX = 2 * Math.asin(Math.sqrt(squaredSinus / squaredCosine));

// converting the deltas to degree and returning them
return new double[] {Math.toDegrees(deltaX), Math.toDegrees(deltaY)};
}

/**
* Method for evaluating the found points and returning the n corner points of the bounding box.
*
* @param coordinate at the center of the bounding box
* @param distances list of found points with their distances
* @param numberOfPoints that should be returned
* @return list of distances
*/
default List<CoordinateDistance> restrictToBoundingBoxWithSetNumberOfCorner(
Point coordinate, Collection<CoordinateDistance> distances, int numberOfPoints) {
boolean topLeft = false;
boolean topRight = false;
boolean bottomLeft = false;
boolean bottomRight = false;

List<CoordinateDistance> resultingDistances = new ArrayList<>();
List<CoordinateDistance> other = new ArrayList<>();

// search for smallest bounding box
for (CoordinateDistance distance : distances) {
Point point = distance.getCoordinateB();

// check for bounding box
if (!topLeft && (point.getX() < coordinate.getX() && point.getY() > coordinate.getY())) {
resultingDistances.add(distance);
topLeft = true;
} else if (!topRight
&& (point.getX() > coordinate.getX() && point.getY() > coordinate.getY())) {
resultingDistances.add(distance);
topRight = true;
} else if (!bottomLeft
&& (point.getX() < coordinate.getX() && point.getY() < coordinate.getY())) {
resultingDistances.add(distance);
bottomLeft = true;
} else if (!bottomRight
&& (point.getX() > coordinate.getX() && point.getY() < coordinate.getY())) {
resultingDistances.add(distance);
bottomRight = true;
} else if (coordinate.equalsExact(point, 1e-6)) {
// if current point is matching the given coordinate, we need to return only the current
// point

resultingDistances.clear();
resultingDistances.add(distance);
return resultingDistances;
} else {
other.add(distance);
}
}

// check if n distances are found
int diff = numberOfPoints - resultingDistances.size();

if (diff > 0) {
resultingDistances.addAll(other.stream().limit(diff).toList());
} else if (diff < 0) {
return resultingDistances.stream().limit(numberOfPoints).toList();
}

return resultingDistances;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import edu.ie3.datamodel.io.factory.timeseries.IdCoordinateFactory;
import edu.ie3.datamodel.io.naming.FileNamingStrategy;
import edu.ie3.datamodel.io.source.IdCoordinateSource;
import edu.ie3.util.geo.CoordinateDistance;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.measure.quantity.Length;
import org.apache.commons.lang3.tuple.Pair;
import org.locationtech.jts.geom.Point;
import tech.units.indriya.ComparableQuantity;

/**
* Implementation of {@link IdCoordinateSource} to read the mapping between coordinate id and actual
Expand All @@ -38,7 +41,7 @@ public CsvIdCoordinateSource(

this.factory = factory;

/* setup the coordinate id to lat/long mapping */
/* set up the coordinate id to lat/long mapping */
idToCoordinate = setupIdToCoordinateMap();
coordinateToId = invert(idToCoordinate);
}
Expand Down Expand Up @@ -93,6 +96,32 @@ public Collection<Point> getAllCoordinates() {
return coordinateToId.keySet();
}

@Override
public List<CoordinateDistance> getNearestCoordinates(
Point coordinate, int n, ComparableQuantity<Length> distance) {
Set<Point> points = coordinateToId.keySet();

double[] deltas = calculateXYDelta(coordinate, distance);

double xMin = coordinate.getX() - deltas[0];
double xMax = coordinate.getX() + deltas[0];
double yMin = coordinate.getY() - deltas[1];
double yMax = coordinate.getY() + deltas[1];

Set<Point> reducedPoints = new HashSet<>();

for (Point point : points) {
double x = point.getX();
double y = point.getY();

if (xMin <= x && x <= xMax && yMin <= y && y <= yMax) {
reducedPoints.add(point);
}
}

return getNearestCoordinates(coordinate, n, reducedPoints);
}

public int getCoordinateCount() {
return idToCoordinate.keySet().size();
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/edu/ie3/datamodel/io/source/sql/SqlDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ protected static String createBaseQueryString(String schemaName, String tableNam
return "SELECT * FROM " + schemaName + ".\"" + tableName + "\"";
}

/**
* Creates a base query string without closing semicolon of the following pattern: <br>
* {@code SELECT 'idColumnName', ST_X('geomColumnName'), ST_Y('geomColumnName') FROM
* <schema>.<table>}
*
* @param schemaName the name of the database schema
* @param tableName the name of the database table
* @param idColumnName the name of the id column
* @param geomColumnName the name of the geometry column
* @return basic query string without semicolon
*/
protected static String createBaseGisQueryString(
String schemaName, String tableName, String idColumnName, String geomColumnName) {
return "SELECT "
+ idColumnName
+ ", ST_X("
+ geomColumnName
+ "), ST_Y("
+ geomColumnName
+ ")"
+ " FROM "
+ schemaName
+ ".\""
+ tableName
+ "\"";
}

/**
* Determine the corresponding database column name based on the provided factory field parameter
* name. Needed to support camel as well as snake case database column names.
Expand Down
Loading