forked from C0urante/avro-random-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add location based logical types FPF-10479 (#32)
* Add geometry, feature and feature-collection logical types * Add Coordinates Reference System classes * fix: geojson random generators * chore: freeze sdk --------- Co-authored-by: Carlos del Prado <[email protected]> Co-authored-by: Carlos del Prado Mota <[email protected]>
- Loading branch information
1 parent
46591c4
commit f68665f
Showing
17 changed files
with
518 additions
and
1 deletion.
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
47 changes: 47 additions & 0 deletions
47
app/src/main/java/io/confluent/avro/random/generator/geojson/Feature.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,47 @@ | ||
package io.confluent.avro.random.generator.geojson; | ||
|
||
import io.confluent.avro.random.generator.geojson.crs.CRS; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Use this class in order to create Feature objects according to GeoJson standard that can be stringified. | ||
*/ | ||
public class Feature { | ||
public final Geometry geometry; | ||
public final Properties properties; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param geometry Geometry object. | ||
*/ | ||
public Feature( | ||
Geometry geometry | ||
) { | ||
this.geometry = geometry; | ||
this.properties = new Properties(geometry.crs); | ||
} | ||
|
||
/** | ||
* Stringifies this object. | ||
* | ||
* @return String version of this object. | ||
*/ | ||
public String toString() { | ||
return "{\"type\": \"Feature\", \"geometry\": " + geometry + ", \"properties\": " + properties + "}"; | ||
} | ||
|
||
private static class Properties extends HashMap<String, String> { | ||
public Properties(CRS crs) { | ||
super(); | ||
this.put("\"crs\"", "\"" + crs.toString() + "\""); | ||
this.put("\"date_time\"", "\"" + new java.util.Date() + "\""); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString().replaceAll("\"=\"", "\": \""); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/io/confluent/avro/random/generator/geojson/FeatureCollection.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,29 @@ | ||
package io.confluent.avro.random.generator.geojson; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Use this class in order to create FeatureCollection objects according to GeoJson standard that can be | ||
* stringified. | ||
*/ | ||
public class FeatureCollection { | ||
public ArrayList<Feature> features; | ||
|
||
/** | ||
* Constructor. | ||
* @param features Features list. | ||
*/ | ||
public FeatureCollection( | ||
ArrayList<Feature> features | ||
) { | ||
this.features = features; | ||
} | ||
|
||
/** | ||
* Stringifies this object. | ||
* @return String version of this object. | ||
*/ | ||
public String toString() { | ||
return "{\"type\": \"FeatureCollection\", \"features\":" + features + "}"; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
app/src/main/java/io/confluent/avro/random/generator/geojson/Geometry.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,120 @@ | ||
package io.confluent.avro.random.generator.geojson; | ||
|
||
import io.confluent.avro.random.generator.geojson.crs.CRS; | ||
import io.confluent.avro.random.generator.geojson.crs.EPSG4326; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Use this class in order to create Geometry objects according to GeoJson standard that can be stringified. | ||
*/ | ||
public class Geometry { | ||
public final GeometryType geometryType; | ||
public final CRS crs; | ||
public final String coordinates; | ||
|
||
/** | ||
* Constructor. | ||
* @param geometryType Geometry type as can be found in GeometryType.java . Random type by default. | ||
* @param crs Coordinates Reference System. EPSG4326 by default. | ||
*/ | ||
public Geometry( | ||
GeometryType geometryType, | ||
CRS crs | ||
) { | ||
this.geometryType = geometryType == null ? GeometryType.randomGeometryType() : geometryType; | ||
this.crs = crs == null ? new EPSG4326() : crs; | ||
coordinates = randomCoordinates(); | ||
} | ||
|
||
/** | ||
* Stringifies this object. | ||
* @return String version of this object. | ||
*/ | ||
public String toString() { | ||
return "{\"type\": \"" + geometryType + "\", \"coordinates\": " + coordinates + "}"; | ||
} | ||
|
||
private String randomCoordinates() { | ||
switch (geometryType) { | ||
case Point: | ||
return new Point(null, null, crs).toString(); | ||
case MultiPoint: | ||
return Arrays.asList( | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString() | ||
).toString(); | ||
case LineString: | ||
return Arrays.asList( | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString() | ||
).toString(); | ||
case MultiLineString: | ||
return Arrays.asList( | ||
Arrays.asList( | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString() | ||
), | ||
Arrays.asList( | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString() | ||
) | ||
).toString(); | ||
case Polygon: | ||
String first = new Point(null, null, crs).toString(); | ||
|
||
return List.of( | ||
Arrays.asList( | ||
first, | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString(), | ||
first | ||
) | ||
).toString(); | ||
case MultiPolygon: | ||
String first1 = new Point(null, null, crs).toString(); | ||
String first2 = new Point(null, null, crs).toString(); | ||
|
||
return Arrays.asList( | ||
List.of( | ||
Arrays.asList( | ||
first1, | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString(), | ||
first1 | ||
) | ||
), | ||
List.of( | ||
Arrays.asList( | ||
first2, | ||
new Point(null, null, crs).toString(), | ||
new Point(null, null, crs).toString(), | ||
first2 | ||
) | ||
) | ||
).toString(); | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
private static class Point { | ||
private final double latitude; | ||
private final double longitude; | ||
|
||
public Point( | ||
Double latitude, | ||
Double longitude, | ||
CRS referenceSystemAndProjection | ||
) { | ||
this.latitude = latitude == null ? referenceSystemAndProjection.randomLatitude() : latitude; | ||
this.longitude = longitude == null ? referenceSystemAndProjection.randomLongitude() : longitude; | ||
} | ||
|
||
public String toString() { | ||
return "[" + latitude + ", " + longitude + "]"; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/io/confluent/avro/random/generator/geojson/GeometryType.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,19 @@ | ||
package io.confluent.avro.random.generator.geojson; | ||
|
||
import java.util.Random; | ||
|
||
enum GeometryType { | ||
Point, | ||
MultiPoint, | ||
LineString, | ||
MultiLineString, | ||
Polygon, | ||
MultiPolygon, | ||
//GeometryCollection | ||
; | ||
|
||
public static GeometryType randomGeometryType() { | ||
Random r = new Random(); | ||
return GeometryType.values()[r.nextInt(GeometryType.values().length)]; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/io/confluent/avro/random/generator/geojson/Projection.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,5 @@ | ||
package io.confluent.avro.random.generator.geojson; | ||
|
||
public enum Projection { | ||
UTM | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/io/confluent/avro/random/generator/geojson/ReferenceSystem.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,6 @@ | ||
package io.confluent.avro.random.generator.geojson; | ||
|
||
public enum ReferenceSystem { | ||
ETRS89, | ||
WGS84 | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/io/confluent/avro/random/generator/geojson/crs/CRS.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,34 @@ | ||
package io.confluent.avro.random.generator.geojson.crs; | ||
|
||
import java.util.Random; | ||
|
||
public abstract class CRS { | ||
protected double leftBound; | ||
protected double rightBound; | ||
protected double bottomBound; | ||
protected double upperBound; | ||
|
||
protected void setBounds( | ||
double leftBound, | ||
double rightBound, | ||
double bottomBound, | ||
double upperBound | ||
) { | ||
this.leftBound = leftBound; | ||
this.rightBound = rightBound; | ||
this.bottomBound = bottomBound; | ||
this.upperBound = upperBound; | ||
} | ||
|
||
public double randomLatitude() { | ||
Random r = new Random(); | ||
return bottomBound + (upperBound - bottomBound) * r.nextDouble(); | ||
} | ||
|
||
public double randomLongitude() { | ||
Random r = new Random(); | ||
return leftBound + (rightBound - leftBound) * r.nextDouble(); | ||
} | ||
|
||
public abstract String toString(); | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/io/confluent/avro/random/generator/geojson/crs/EPSG4326.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,17 @@ | ||
package io.confluent.avro.random.generator.geojson.crs; | ||
|
||
public class EPSG4326 extends CRS { | ||
public EPSG4326() { | ||
super(); | ||
double leftBound = -180.0; | ||
double rightBound = 180.0; | ||
double bottomBound = 90.0; | ||
double upperBound = -90.0; | ||
this.setBounds(leftBound, rightBound, bottomBound, upperBound); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "EPSG4326"; | ||
} | ||
} |
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.