In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

Commit 46dcfdea authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck

add conversion from and to geodesic cooridnate systems based on *EPSG projection codes*

parent 22941d1e
package org.vadere.manager.traci.commands.get;
import org.vadere.manager.TraCIException;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.commands.TraCIGetCommand;
import org.vadere.manager.traci.compound.CompoundObject;
public class TraCIGetCompoundPayload extends TraCIGetCommand {
private CompoundObject data;
public TraCIGetCompoundPayload(TraCICmd traCICmd, int variableIdentifier, String elementIdentifier) {
super(traCICmd, variableIdentifier, elementIdentifier);
data = null;
}
public TraCIGetCompoundPayload(TraCIGetCommand c) {
super(c.getTraCICmd(), c.getVariableIdentifier(), c.getElementIdentifier());
// expecting a CompoundObject thus check if DataType Byte is present.
c.getCmdBuffer().ensureBytes(1);
TraCIDataType dType = TraCIDataType.fromId(c.getCmdBuffer().readUnsignedByte());
if (!dType.equals(TraCIDataType.COMPOUND_OBJECT)){
throw new TraCIException("expected Compound Object in GetCommand.");
}
this.data = c.getCmdBuffer().readCompoundObject();
}
public CompoundObject getData() {
return data;
}
public void setData(CompoundObject data) {
this.data = data;
}
}
package org.vadere.manager.traci.compound.object;
import org.apache.commons.math3.util.Pair;
import org.vadere.manager.TraCIException;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.compound.CompoundObject;
import org.vadere.state.scenario.ReferenceCoordinateSystem;
import org.vadere.util.geometry.shapes.VPoint;
public class PointConverter {
enum PointType {
POSITION_LON_LAT,
POSITION_2D,
}
private VPoint p; // (lon, lat) Order
private PointType conversionType;
public PointConverter(CompoundObject obj){
if (obj.size() != 2)
throw new TraCIException("Expected 3 elements for PointConverter");
if (obj.hasIndex(0, TraCIDataType.POS_2D)){
p = (VPoint) obj.getData(0, TraCIDataType.POS_2D);
} else if (obj.hasIndex(0, TraCIDataType.POS_LON_LAT)){
p = (VPoint) obj.getData(0, TraCIDataType.POS_LON_LAT);
} else {
throw new TraCIException("Unknown PointType");
}
int type = (Integer) obj.getData(1, TraCIDataType.U_BYTE);
if (type == 0x00){
conversionType = PointType.POSITION_LON_LAT;
} else if (type == 0x01){
conversionType = PointType.POSITION_2D;
} else {
throw new TraCIException("Unknown ConversionType");
}
}
public Pair<TraCIDataType, VPoint> convert(ReferenceCoordinateSystem coord){
Pair<TraCIDataType, VPoint>ret;
switch (conversionType){
case POSITION_2D:
ret = Pair.create(TraCIDataType.POS_2D, coord.convertToCartesian(p.y, p.x)); // LAT LONG axis order!!!
break;
case POSITION_LON_LAT:
ret = Pair.create(TraCIDataType.POS_LON_LAT, coord.convertToGeo(new VPoint(p.x, p.y)));
break;
default:
throw new TraCIException("Unkown ConversionType");
}
return ret;
}
public VPoint getP() {
return p;
}
public void setP(VPoint p) {
this.p = p;
}
public PointType getConversionType() {
return conversionType;
}
public void setConversionType(PointType conversionType) {
this.conversionType = conversionType;
}
}
package org.vadere.state.scenario;
import org.apache.sis.geometry.DirectPosition2D;
import org.apache.sis.referencing.CRS;
import org.apache.sis.referencing.CommonCRS;
import org.apache.sis.referencing.crs.DefaultProjectedCRS;
import org.opengis.geometry.DirectPosition;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.CoordinateOperation;
import org.opengis.referencing.operation.TransformException;
import org.opengis.util.FactoryException;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.logging.Logger;
import java.util.Objects;
public class ReferenceCoordinateSystem {
private static Logger logger = Logger.getLogger(ReferenceCoordinateSystem.class);
/**
* international identifier for the base coordinate reference system (CRS) which was used
* to build this topography.
......@@ -25,18 +37,43 @@ public class ReferenceCoordinateSystem {
*/
private String description = "";
transient private CoordinateReferenceSystem geoCRS;
transient private CoordinateReferenceSystem utmCRS;
transient private CoordinateOperation geoToUtm;
transient private CoordinateOperation utmToGeo;
transient private boolean initialized;
/**
* Translation vector used to move topography to origin (0,0)
*/
private VPoint translation = new VPoint(0.0,0.0);
public ReferenceCoordinateSystem() {
this.initialized = false;
}
public ReferenceCoordinateSystem(String epsgCode, String description, VPoint translation) {
this.epsgCode = epsgCode;
this.description = description;
this.translation = translation;
this.initialized = false;
}
public void initialize(){
if (initialized)
return;
try {
utmCRS = CRS.forCode(epsgCode);
geoCRS = ((DefaultProjectedCRS) utmCRS).getBaseCRS();
geoToUtm = CRS.findOperation(geoCRS, utmCRS, null);
utmToGeo = CRS.findOperation(utmCRS, geoCRS, null);
} catch (FactoryException e) {
logger.errorf(e.getMessage());
throw new RuntimeException(e);
}
initialized = true;
}
public String getEpsgCode() {
......@@ -63,6 +100,40 @@ public class ReferenceCoordinateSystem {
this.translation = translation;
}
public boolean supportsConversion(){
return geoToUtm != null && utmToGeo != null;
}
public VPoint convertToGeo(VPoint cartesian){
if (!initialized)
initialize();
VPoint translated = cartesian.add(translation);
DirectPosition ptSrc = new DirectPosition2D(translated.x, translated.y);
try {
DirectPosition ptDst = utmToGeo.getMathTransform().transform(ptSrc, null);
return new VPoint(ptDst.getCoordinate()[0], ptDst.getCoordinate()[1]);
} catch (TransformException e) {
logger.errorf(e.getMessage());
throw new RuntimeException(e);
}
}
public VPoint convertToCartesian(double latitude, double longitude ){
if (!initialized)
initialize();
// geographic CRS, the (latitude, longitude) axis order!
DirectPosition ptSrc = new DirectPosition2D(latitude, longitude);
try {
DirectPosition ptDst = geoToUtm.getMathTransform().transform(ptSrc, null);
VPoint ret = new VPoint(ptDst.getCoordinate()[0], ptDst.getCoordinate()[1]);
return ret.subtract(this.translation);
} catch (TransformException e) {
logger.errorf(e.getMessage());
throw new RuntimeException(e);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......
package org.vadere.state.scenario;
import org.junit.Test;
import org.vadere.util.geometry.shapes.VPoint;
import static org.junit.Assert.*;
public class ReferenceCoordinateSystemTest {
//EPSG:25832 ETRS89 / UTM zone 32N
@Test
public void convertToGeo1() {
ReferenceCoordinateSystem c = new ReferenceCoordinateSystem("EPSG:25832", "", new VPoint(0.0,0.0));
VPoint p = c.convertToCartesian(0.9029408, 5.4072291);
assertEquals(100000, p.x, 0.01);
assertEquals(100000, p.y, 0.01);
}
@Test
public void convertToCartesian1() {
ReferenceCoordinateSystem c = new ReferenceCoordinateSystem("EPSG:25832", "", new VPoint(0.0,0.0));
VPoint p = c.convertToGeo(new VPoint(100000, 100000));
assertEquals(0.9029408, p.x, 0.00001);
assertEquals(5.4072291, p.y, 0.00001);
}
// WGS84 (OpenStreetMaps)
@Test
public void convertToGeo() {
ReferenceCoordinateSystem c = new ReferenceCoordinateSystem("EPSG:32632", "", new VPoint(692000.0,5337000.0));
VPoint p = c.convertToCartesian(48.16219, 11.58645);
assertEquals(324.886638718, p.x, 0.00001);
assertEquals(562.448443838, p.y, 0.00001);
}
@Test
public void convertToCartesian() {
ReferenceCoordinateSystem c = new ReferenceCoordinateSystem("EPSG:32632", "", new VPoint(692000.0,5337000.0));
VPoint p = c.convertToGeo(new VPoint(324.886638718, 562.448443838));
assertEquals(48.16219, p.x, 0.00001);
assertEquals(11.58645, p.y, 0.00001);
}
}
\ No newline at end of file
......@@ -148,6 +148,20 @@
<artifactId>commons-configuration2</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.sis.core/sis-referencing -->
<dependency>
<groupId>org.apache.sis.core</groupId>
<artifactId>sis-referencing</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
......
Markdown is supported
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