Skip to content

Instantly share code, notes, and snippets.

@RanKey1496
Created May 6, 2019 04:16
Show Gist options
  • Save RanKey1496/1c988fda1f1b00cbe86822e6c99083f8 to your computer and use it in GitHub Desktop.
Save RanKey1496/1c988fda1f1b00cbe86822e6c99083f8 to your computer and use it in GitHub Desktop.
Convert String to LineString or Point written in Java
public class Utils {
private static final int GEOMETRY_PRECISION = 4326;
public static Point convertStringToPoint(String encode) {
try {
if (encode == null || encode.length() <= 0)
return null;
String[] coordinates = encode.replaceFirst("POINT ?\\(", "").replaceFirst("\\)", "").split(" ");
double lon = Double.parseDouble(coordinates[0]);
double lat = Double.parseDouble(coordinates[1]);
Point point = new GeometryFactory(new PrecisionModel(), GEOMETRY_PRECISION)
.createPoint(new Coordinate(lon, lat));
return point;
} catch (Exception e) {
return null;
}
}
public static LineString convertStringToLineString(String encode) {
try {
if (encode == null || encode.length() <= 0)
return null;
String[] split = encode.replaceFirst("LINESTRING ?\\(", "").replaceFirst("\\)", "").split(",");
Coordinate[] coordinates = new Coordinate[split.length];
int index = 0;
for (int i = 0; i < split.length; i++) {
String coor[] = split[i].split(" ");
coordinates[index] = new Coordinate(Double.parseDouble(coor[0]), Double.parseDouble(coor[1]));
index++;
}
LineString lineString = new GeometryFactory(new PrecisionModel(), GEOMETRY_PRECISION)
.createLineString(coordinates);
return lineString;
} catch (Exception e) {
System.out.println("Error convirtiendo: " + e.getMessage());
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment