-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f66a776
commit ad719b3
Showing
13 changed files
with
250 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
core/src/main/java/org/polypheny/db/functions/spatial/GeoTransformFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright 2019-2023 The Polypheny Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.polypheny.db.functions.spatial; | ||
|
||
import org.locationtech.jts.geom.Coordinate; | ||
import org.locationtech.jts.geom.Geometry; | ||
import org.locationtech.jts.geom.GeometryFactory; | ||
import org.locationtech.jts.geom.LineString; | ||
import org.locationtech.jts.geom.Point; | ||
import org.locationtech.jts.geom.Polygon; | ||
import org.locationtech.jts.geom.PrecisionModel; | ||
import org.locationtech.proj4j.CRSFactory; | ||
import org.locationtech.proj4j.CoordinateReferenceSystem; | ||
import org.locationtech.proj4j.CoordinateTransform; | ||
import org.locationtech.proj4j.CoordinateTransformFactory; | ||
import org.locationtech.proj4j.Proj4jException; | ||
import org.locationtech.proj4j.ProjCoordinate; | ||
import org.polypheny.db.type.entity.spatial.InvalidGeometryException; | ||
import org.polypheny.db.type.entity.spatial.PolyGeometry; | ||
import org.polypheny.db.type.entity.spatial.PolyGeometryCollection; | ||
|
||
/** | ||
* Transform coordinates to various SRID | ||
*/ | ||
public class GeoTransformFunctions { | ||
|
||
private static final String SRID_PREFIX = "EPSG:"; | ||
|
||
|
||
private GeoTransformFunctions() { | ||
// empty on purpose | ||
} | ||
|
||
|
||
public static PolyGeometry transform( PolyGeometry geometry, int srid ) throws InvalidGeometryException { | ||
// Create a CRSFactory to manage coordinate reference systems | ||
CRSFactory crsFactory = new CRSFactory(); | ||
// Create the original and target coordinate reference systems | ||
CoordinateReferenceSystem sourceCrs = crsFactory.createFromName( SRID_PREFIX + geometry.getSRID() ); | ||
CoordinateReferenceSystem targetCrs = crsFactory.createFromName( SRID_PREFIX + srid ); | ||
// create Geometry factory with new srid | ||
GeometryFactory geometryFactory = new GeometryFactory( new PrecisionModel( PrecisionModel.FLOATING ), srid ); | ||
|
||
if ( geometry.isGeometryCollection() ) { | ||
PolyGeometryCollection geometryCollection = geometry.asGeometryCollection(); | ||
Geometry[] geometries = new Geometry[geometryCollection.getNumGeometries()]; | ||
for ( int i = 0; i < geometryCollection.getNumGeometries(); i++ ) { | ||
Geometry geom = geometryCollection.getGeometryN( i ).getJtsGeometry(); | ||
// Convert the Geometry to Proj4J coordinates | ||
ProjCoordinate[] originalCoords = convertToProj4JCoordinates( geom ); | ||
// Perform the SRID conversion | ||
ProjCoordinate[] targetCoords = convertCoordinates( originalCoords, sourceCrs, targetCrs ); | ||
// Convert Proj4J coordinates back to Geometry | ||
geometries[i] = convertToGeometry( targetCoords, geom, geometryFactory ); | ||
} | ||
switch ( geometry.getGeometryType() ) { | ||
case GEOMETRYCOLLECTION: | ||
return PolyGeometry.of( geometryFactory.createGeometryCollection( geometries ) ); | ||
case MULTIPOINT: | ||
Point[] points = new Point[geometries.length]; | ||
for ( int i = 0; i < geometries.length; i++ ) { | ||
points[i] = geometryFactory.createPoint( geometries[i].getCoordinate() ); | ||
} | ||
return PolyGeometry.of( geometryFactory.createMultiPoint( points ) ); | ||
case MULTILINESTRING: | ||
LineString[] lineStrings = new LineString[geometries.length]; | ||
for ( int i = 0; i < geometries.length; i++ ) { | ||
lineStrings[i] = geometryFactory.createLineString( geometries[i].getCoordinates() ); | ||
} | ||
return PolyGeometry.of( geometryFactory.createMultiLineString( lineStrings ) ); | ||
case MULTIPOLYGON: | ||
Polygon[] polygons = new Polygon[geometries.length]; | ||
for ( int i = 0; i < geometries.length; i++ ) { | ||
polygons[i] = geometryFactory.createPolygon( geometries[i].getCoordinates() ); | ||
} | ||
return PolyGeometry.of( geometryFactory.createMultiPolygon( polygons ) ); | ||
default: | ||
throw new InvalidGeometryException( "Cannot convert back to GeometryCollection" ); | ||
} | ||
} else { | ||
ProjCoordinate[] originalCoords = convertToProj4JCoordinates( geometry.getJtsGeometry() ); | ||
ProjCoordinate[] targetCoords = convertCoordinates( originalCoords, sourceCrs, targetCrs ); | ||
return PolyGeometry.of( convertToGeometry( targetCoords, geometry.getJtsGeometry(), geometryFactory ) ); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
// Convert Geometry coordinates to Proj4J coordinates | ||
private static ProjCoordinate[] convertToProj4JCoordinates( Geometry geometry ) { | ||
ProjCoordinate[] projCoords = new ProjCoordinate[geometry.getNumPoints()]; | ||
for ( int i = 0; i < geometry.getNumPoints(); i++ ) { | ||
projCoords[i] = new ProjCoordinate( geometry.getCoordinates()[i].getX(), geometry.getCoordinates()[i].getY() ); | ||
} | ||
return projCoords; | ||
} | ||
|
||
|
||
// Convert Proj4J coordinates back to Geometry | ||
private static Geometry convertToGeometry( ProjCoordinate[] projCoords, Geometry geometry, GeometryFactory geometryFactory ) throws InvalidGeometryException { | ||
Coordinate[] originalCoordinates = geometry.getCoordinates(); | ||
Coordinate[] coordinates = new Coordinate[projCoords.length]; | ||
|
||
for ( int i = 0; i < projCoords.length; i++ ) { | ||
// X and Y are converted, Z is original | ||
coordinates[i] = new Coordinate( projCoords[i].x, projCoords[i].y, originalCoordinates[i].getZ() ); | ||
} | ||
switch ( geometry.getGeometryType() ) { | ||
case Geometry.TYPENAME_POINT: | ||
return geometryFactory.createPoint( coordinates[0] ); | ||
case Geometry.TYPENAME_LINESTRING: | ||
return geometryFactory.createLineString( coordinates ); | ||
case Geometry.TYPENAME_LINEARRING: | ||
return geometryFactory.createLinearRing( coordinates ); | ||
case Geometry.TYPENAME_POLYGON: | ||
return geometryFactory.createPolygon( coordinates ); | ||
default: | ||
throw new InvalidGeometryException( "Cannot convert back to Geometry" ); | ||
} | ||
|
||
} | ||
|
||
|
||
// Helper method to perform the SRID conversion | ||
private static ProjCoordinate[] convertCoordinates( | ||
ProjCoordinate[] originalCoords, CoordinateReferenceSystem sourceCrs, CoordinateReferenceSystem targetCrs ) { | ||
try { | ||
CoordinateTransform trans = new CoordinateTransformFactory().createTransform( sourceCrs, targetCrs ); | ||
ProjCoordinate[] targetCoords = new ProjCoordinate[originalCoords.length]; | ||
for ( int i = 0; i < originalCoords.length; i++ ) { | ||
targetCoords[i] = new ProjCoordinate(); | ||
trans.transform( originalCoords[i], targetCoords[i] ); | ||
} | ||
return targetCoords; | ||
} catch ( Proj4jException e ) { | ||
throw new RuntimeException( "Error in coordinate transformation.", e ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.