Skip to content

Commit

Permalink
Updating the serializers for Points to work with GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
computate committed Dec 13, 2023
1 parent 984fcda commit 24d16c8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.io.IOException;
import java.util.stream.Collectors;

import org.computate.vertx.tool.VertxTool;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand All @@ -31,8 +33,6 @@ public class PgClientPathSerializer extends JsonSerializer<Path> {

@Override()
public void serialize(Path o, JsonGenerator generator, SerializerProvider provider) throws IOException, IOException {
JsonArray pointsArray = new JsonArray();
o.getPoints().stream().map(point -> new JsonArray().add(Double.valueOf(point.getX())).add(Double.valueOf(point.getY()))).collect(Collectors.toList()).forEach(pointArray -> pointsArray.add(pointArray));
generator.writeObject(new JsonObject().put("type", "LineString").put("coordinates", pointsArray));
generator.writeObject(VertxTool.toGeoJson(o));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.jknack.handlebars.internal.lang3.StringUtils;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.pgclient.data.Point;

/**
Expand All @@ -32,9 +35,25 @@ public class PgClientPointDeserializer extends JsonDeserializer<Point> {
public Point deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {

String[] vals = jsonParser.getText().split(",");
if(vals.length == 2 && NumberUtils.isParsable(vals[0]) && NumberUtils.isParsable(vals[1]))
return new Point(Double.parseDouble(vals[0]), Double.parseDouble(vals[1]));
return null;

String text = jsonParser.getText().strip();

if(StringUtils.startsWith(text, "{")) {
JsonObject json = new JsonObject(text);
Point point = new Point();
JsonArray points = json.getJsonArray("coordinates");

if(points.size() == 2) {
point.setX(Double.parseDouble(points.getString(0)));
point.setY(Double.parseDouble(points.getString(1)));
return point;
}
return null;
} else {
String[] vals = jsonParser.getText().split(",");
if(vals.length == 2 && NumberUtils.isParsable(vals[0]) && NumberUtils.isParsable(vals[1]))
return new Point(Double.parseDouble(vals[0]), Double.parseDouble(vals[1]));
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
package org.computate.vertx.serialize.pgclient;

import java.io.IOException;
import java.time.LocalDate;

import org.computate.vertx.tool.VertxTool;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand All @@ -31,6 +32,6 @@ public class PgClientPointSerializer extends JsonSerializer<Point> {

@Override()
public void serialize(Point o, JsonGenerator generator, SerializerProvider provider) throws IOException, IOException {
generator.writeString(String.format(POINT_FORMAT, o.getX(), o.getY()));
generator.writeObject(VertxTool.toGeoJson(o));
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/computate/vertx/tool/VertxTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.pgclient.data.Path;
import io.vertx.pgclient.data.Point;

public class VertxTool {
protected static final Logger LOG = LoggerFactory.getLogger(VertxTool.class);
Expand Down Expand Up @@ -63,6 +64,20 @@ public String toId(String s) {
return s;
}

/**
* Description: A helper method to convert a Postgres Point to GeoJSON.
*/
public static JsonObject toGeoJson(Point point) {
JsonObject result = null;
if(point != null) {
JsonArray pointsArray = new JsonArray();
pointsArray.add(Double.valueOf(point.getX()));
pointsArray.add(Double.valueOf(point.getY()));
result = new JsonObject().put("type", "Point").put("coordinates", pointsArray);
}
return result;
}

/**
* Description: A helper method to convert a Postgres Path to GeoJSON.
*/
Expand Down

0 comments on commit 24d16c8

Please sign in to comment.