PolygonEncoder implements LocationReferenceEncoder { @Override public LocationReference encode(RawLocationReference rawLocationReference) throws OpenLRProtoException { if (rawLocationReference.getLocationType() != LocationType.POLYGON) { throw new OpenLRProtoException(OpenLRProtoStatusCode.INVALID_LOCATION_REFERENCE); } List<Coordinates> coordinates = new ArrayList<>(); for (GeoCoordinates cornerPoint : rawLocationReference.getCornerPoints()) { Coordinates c = Coordinates.newBuilder() .setLongitude(cornerPoint.getLongitudeDeg()) .setLatitude(cornerPoint.getLatitudeDeg()) .build(); coordinates.add(c); } PolygonLocationReference polygonLocationReference = PolygonLocationReference.newBuilder() .addAllCoordinates(coordinates) .build(); LocationReferenceData locationReferenceData = LocationReferenceData.newBuilder() .setPolygonLocationReference(polygonLocationReference) .build(); return new LocationReferenceProtoImpl(rawLocationReference.getID(), LocationType.POLYGON, locationReferenceData); }  @Override LocationReference encode(RawLocationReference rawLocationReference);  }
@Test public void testEncodePolygon() throws OpenLRProtoException { List<GeoCoordinates> geoCoordinates = new ArrayList<>(); geoCoordinates.add(new GeoCoordinatesProtoImpl(-122.915, 39.045)); geoCoordinates.add(new GeoCoordinatesProtoImpl(-122.914, 39.046)); geoCoordinates.add(new GeoCoordinatesProtoImpl(-122.913, 39.047)); RawPolygonLocRef rawLocationReference = new RawPolygonLocRef("1", geoCoordinates); LocationReference locationReference = polygonEncoder.encode(rawLocationReference); assertNotNull(locationReference); assertEquals(locationReference.getID(), "1"); assertEquals(locationReference.getLocationType(), LocationType.POLYGON); assertEquals(locationReference.getDataIdentifier(), "proto"); assertEquals(locationReference.getDataClass(), LocationReferenceData.class); Object locationReferenceData = locationReference.getLocationReferenceData(); assertTrue(locationReferenceData instanceof LocationReferenceData); LocationReferenceData data = (LocationReferenceData) locationReferenceData; assertTrue(data.hasPolygonLocationReference()); PolygonLocationReference polygonLocationReference = data.getPolygonLocationReference(); List<Coordinates> coordinatesList = polygonLocationReference.getCoordinatesList(); assertEquals(coordinatesList.size(), 3); Coordinates first = coordinatesList.get(0); assertNotNull(first); assertEquals(first.getLongitude(), -122.915); assertEquals(first.getLatitude(), 39.045); Coordinates second = coordinatesList.get(1); assertNotNull(second); assertEquals(second.getLongitude(), -122.914); assertEquals(second.getLatitude(), 39.046); Coordinates third = coordinatesList.get(2); assertNotNull(third); assertEquals(third.getLongitude(), -122.913); assertEquals(third.getLatitude(), 39.047); }