Commit b75d487d authored by Noel Alonso's avatar Noel Alonso
Browse files

Renombra y añade nuevas utilidades

Extrae la geometría de un record de avro sin conocer el tipo ni formato

Añade métodos para pasar de un tipo de geometría a otro
parent 5f9bb76d
Loading
Loading
Loading
Loading
+121 −0
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.avro.Schema;
import org.apache.avro.specific.SpecificRecord;
import org.apache.lucene.spatial.prefix.tree.CellIterator;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;
import org.locationtech.spatial4j.context.SpatialContext;
@@ -12,10 +14,11 @@ import org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory;
import org.locationtech.spatial4j.exception.InvalidShapeException;
import org.locationtech.spatial4j.io.GeohashUtils;
import org.locationtech.spatial4j.io.WKTReader;
import org.locationtech.spatial4j.io.jts.JtsWKTWriter;
import org.locationtech.spatial4j.shape.Point;
import org.locationtech.spatial4j.shape.Shape;

public class GeoHashUtils {
public class GeoUtils {

	private static final int MAX_LEVELS = 4;

@@ -23,6 +26,8 @@ public class GeoHashUtils {

	private static WKTReader reader = new WKTReader(JtsSpatialContext.GEO, new JtsSpatialContextFactory());

	private static JtsWKTWriter writer = new JtsWKTWriter(JtsSpatialContext.GEO, new JtsSpatialContextFactory());

	private static GeohashPrefixTree geohashPrefixTree = new GeohashPrefixTree(ctx, MAX_LEVELS);

	public static List<String> getGeoHash(final String geometry_wkt, int precision)
@@ -30,7 +35,7 @@ public class GeoHashUtils {

		assert precision <= MAX_LEVELS;

		Shape shape = reader.parse(geometry_wkt);
		Shape shape = getShapeFromWKT(geometry_wkt);

		return getGeoHash(shape, precision);
	}
@@ -39,11 +44,29 @@ public class GeoHashUtils {

		assert precision <= MAX_LEVELS;

		Point point = ctx.getShapeFactory().pointXY(lon, lat);
		Point point = getPointFromLatLon(lat, lon);

		return GeohashUtils.encodeLatLon(point.getY(), point.getX(), precision);
	}

	public static Shape getShapeFromWKT(String geometry_wkt) throws InvalidShapeException, ParseException {
		return reader.parse(geometry_wkt);
	}

	public static String getWKTFromLatLon(final double lat, final double lon)
			throws InvalidShapeException, ParseException {

		return GeoUtils.getWKTFromShape(GeoUtils.getPointFromLatLon(lat, lon));
	}

	public static String getWKTFromShape(Shape shape) throws InvalidShapeException, ParseException {
		return writer.toString(shape);
	}

	public static Point getPointFromLatLon(final double lat, final double lon) {
		return ctx.getShapeFactory().pointXY(lon, lat);
	}

	private static List<String> getGeoHash(Shape shape, int precision) {

		List<String> geoHashList = new ArrayList<>();
@@ -61,4 +84,38 @@ public class GeoHashUtils {
		return geoHashList;
	}

	/**
	 * 
	 * @param value
	 *            Registro avro de donde extraer la geometría
	 * @return Geometría en formato WKT
	 * @throws InvalidShapeException
	 * @throws ParseException
	 */
	public static String getWKTGeometry(SpecificRecord value) throws InvalidShapeException, ParseException {

		// Comprueba si la geometría viene en los diferentes formatos soportados.

		Schema schema = value.getSchema();

		if (schema.getField("longitude") != null && schema.getField("latitude") != null) {

			Object lon = AvroUtils.getSpecificRecordProperty(value, "longitude");
			Object lat = AvroUtils.getSpecificRecordProperty(value, "latitude");

			return getWKTFromLatLon((double) lat, (double) lon);
		}
		if (schema.getField("x") != null && schema.getField("y") != null) {

			Object lon = AvroUtils.getSpecificRecordProperty(value, "x");
			Object lat = AvroUtils.getSpecificRecordProperty(value, "y");

			return getWKTFromLatLon((double) lat, (double) lon);
		}
		if (schema.getField("geometry") != null) {

			return (String) AvroUtils.getSpecificRecordProperty(value, "geometry");
		}
		return null;
	}
}
+10 −10
Original line number Diff line number Diff line
@@ -10,16 +10,16 @@ import java.util.List;
import org.junit.Test;
import org.locationtech.spatial4j.exception.InvalidShapeException;

import es.redmic.vesselrestrictionchecker.utils.GeoHashUtils;
import es.redmic.vesselrestrictionchecker.utils.GeoUtils;

public class GeoHashUtilsTest {
public class GeoUtilsTest {

	@Test
	public void getGeoHashFromPoint_returnStringCode_whenPointIsValid() {

		String codeExpected = "etj";

		String result = GeoHashUtils.getGeoHash(28.554224326054886, -14.75341796875, 3);
		String result = GeoUtils.getGeoHash(28.554224326054886, -14.75341796875, 3);

		assertEquals(codeExpected, result);
	}
@@ -27,13 +27,13 @@ public class GeoHashUtilsTest {
	@Test(expected = InvalidShapeException.class)
	public void getGeoHashFromPoint_throwException_whenPointIsNotValid() {

		GeoHashUtils.getGeoHash(91.554224326054886, 190.75341796875, 3);
		GeoUtils.getGeoHash(91.554224326054886, 190.75341796875, 3);
	}
	
	@Test(expected = AssertionError.class)
	public void getGeoHashFromPoint_throwException_whenPrecisionIsGreaterThanMaxLevels() throws InvalidShapeException, ParseException {

		GeoHashUtils.getGeoHash(91.554224326054886, 190.75341796875, 6);
		GeoUtils.getGeoHash(91.554224326054886, 190.75341796875, 6);
	}

	@Test
@@ -52,7 +52,7 @@ public class GeoHashUtilsTest {

		// @formatter:on

		List<String> result = GeoHashUtils.getGeoHash(geometry_wkt, 3);
		List<String> result = GeoUtils.getGeoHash(geometry_wkt, 3);

		assertEquals(codeListExpected, result);
	}
@@ -60,21 +60,21 @@ public class GeoHashUtilsTest {
	@Test(expected = InvalidShapeException.class)
	public void getGeoHashFromShape_throwException_whenShapeIsNotValid() throws InvalidShapeException, ParseException {

		GeoHashUtils.getGeoHash("POINT(190.75341796875 91.554224326054886)", 3);
		GeoUtils.getGeoHash("POINT(190.75341796875 91.554224326054886)", 3);
	}
	
	@Test(expected = AssertionError.class)
	public void getGeoHashFromShape_throwException_whenPrecisionIsGreaterThanMaxLevels() throws InvalidShapeException, ParseException {

		GeoHashUtils.getGeoHash("POINT(190.75341796875 91.554224326054886)", 6);
		GeoUtils.getGeoHash("POINT(190.75341796875 91.554224326054886)", 6);
	}

	@Test
	public void getGeoHashFromPointAndgetGeoHashFromShape_returnEqualStringCode_whenPointAndShapeAreEquals() throws InvalidShapeException, ParseException {

		String geoHashFromPoint = GeoHashUtils.getGeoHash(28.554224326054886, -14.75341796875, 3);
		String geoHashFromPoint = GeoUtils.getGeoHash(28.554224326054886, -14.75341796875, 3);

		List<String> geoHashFromShape = GeoHashUtils.getGeoHash("POINT(-14.75341796875 28.554224326054886)", 3);
		List<String> geoHashFromShape = GeoUtils.getGeoHash("POINT(-14.75341796875 28.554224326054886)", 3);

		assertEquals(1, geoHashFromShape.size());
		assertEquals(geoHashFromPoint, geoHashFromShape.get(0));