Search in sources :

Example 6 with PointImpl

use of org.locationtech.spatial4j.shape.impl.PointImpl in project crate by crate.

the class SQLTransportExecutor method getObject.

/**
 * retrieve the same type of object from the resultSet as the CrateClient would return
 */
private static Object getObject(ResultSet resultSet, int i, String typeName) throws SQLException {
    Object value;
    int columnIndex = i + 1;
    switch(typeName) {
        // E.g. int2 would return Integer instead of short.
        case "int2":
            Integer intValue = (Integer) resultSet.getObject(columnIndex);
            if (intValue == null) {
                return null;
            }
            value = intValue.shortValue();
            break;
        case "_char":
            {
                Array array = resultSet.getArray(columnIndex);
                if (array == null) {
                    return null;
                }
                ArrayList<Byte> elements = new ArrayList<>();
                for (Object o : ((Object[]) array.getArray())) {
                    elements.add(Byte.parseByte((String) o));
                }
                return elements;
            }
        case "oidvector":
            {
                String textval = resultSet.getString(columnIndex);
                if (textval == null) {
                    return null;
                }
                return PgOidVectorType.listFromOidVectorString(textval);
            }
        case "char":
            String strValue = resultSet.getString(columnIndex);
            if (strValue == null) {
                return null;
            }
            return Byte.valueOf(strValue);
        case "byte":
            value = resultSet.getByte(columnIndex);
            break;
        case "_json":
            {
                Array array = resultSet.getArray(columnIndex);
                if (array == null) {
                    return null;
                }
                ArrayList<Object> jsonObjects = new ArrayList<>();
                for (Object item : (Object[]) array.getArray()) {
                    jsonObjects.add(jsonToObject((String) item));
                }
                value = jsonObjects;
                break;
            }
        case "json":
            String json = resultSet.getString(columnIndex);
            value = jsonToObject(json);
            break;
        case "point":
            PGpoint pGpoint = resultSet.getObject(columnIndex, PGpoint.class);
            value = new PointImpl(pGpoint.x, pGpoint.y, JtsSpatialContext.GEO);
            break;
        case "record":
            value = resultSet.getObject(columnIndex, PGobject.class).getValue();
            break;
        case "_bit":
            String pgBitStringArray = resultSet.getString(columnIndex);
            if (pgBitStringArray == null) {
                return null;
            }
            byte[] bytes = pgBitStringArray.getBytes(StandardCharsets.UTF_8);
            ByteBuf buf = Unpooled.wrappedBuffer(bytes);
            value = PGArray.BIT_ARRAY.readTextValue(buf, bytes.length);
            buf.release();
            break;
        default:
            value = resultSet.getObject(columnIndex);
            break;
    }
    if (value instanceof Timestamp) {
        value = ((Timestamp) value).getTime();
    } else if (value instanceof Array) {
        value = Arrays.asList(((Object[]) ((Array) value).getArray()));
    }
    return value;
}
Also used : Array(java.sql.Array) PGArray(io.crate.protocols.postgres.types.PGArray) PGpoint(org.postgresql.geometric.PGpoint) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Timestamp(java.sql.Timestamp) PGpoint(org.postgresql.geometric.PGpoint) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 7 with PointImpl

use of org.locationtech.spatial4j.shape.impl.PointImpl in project crate by crate.

the class InsertIntoIntegrationTest method testInsertIntoGeoPointArray.

@Test
// inserting geo-point array requires special treatment for PostgreSQL
@UseJdbc(0)
public void testInsertIntoGeoPointArray() throws Exception {
    execute("create table t (id int, points array(geo_point)) clustered into 1 shards with (number_of_replicas=0)");
    ensureYellow();
    execute("insert into t (id, points) values (1, [[1.1, 2.2],[3.3, 4.4]])");
    execute("insert into t (id, points) values (2, ['POINT(5.5 6.6)','POINT(7.7 8.8)'])");
    execute("insert into t (id, points) values (?, ?)", new Object[] { 3, new Double[][] { new Double[] { 9.9, 10.10 }, new Double[] { 11.11, 12.12 } } });
    execute("refresh table t");
    execute("select points from t order by id");
    assertThat(response.rowCount(), is(3L));
    assertThat((List<Object>) response.rows()[0][0], contains(is(new PointImpl(1.1, 2.2, JtsSpatialContext.GEO)), is(new PointImpl(3.3, 4.4, JtsSpatialContext.GEO))));
    assertThat((List<Object>) response.rows()[1][0], contains(is(new PointImpl(5.5, 6.6, JtsSpatialContext.GEO)), is(new PointImpl(7.7, 8.8, JtsSpatialContext.GEO))));
    assertThat((List<Object>) response.rows()[2][0], contains(is(new PointImpl(9.9, 10.10, JtsSpatialContext.GEO)), is(new PointImpl(11.11, 12.12, JtsSpatialContext.GEO))));
}
Also used : PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test)

Example 8 with PointImpl

use of org.locationtech.spatial4j.shape.impl.PointImpl in project ddf by codice.

the class NearbyLocationImplTest method testNearbyLocationImpl.

@Test
public void testNearbyLocationImpl() {
    final Point source = new PointImpl(50, 50, SpatialContext.GEO);
    final Point nearby = new PointImpl(50.5, 50.5, SpatialContext.GEO);
    final NearbyLocation nearbyLocation = new NearbyLocationImpl(source, nearby, "Nearby");
    assertThat(nearbyLocation.getCardinalDirection(), is("SW"));
    // This distance value was obtained from http://www.movable-type.co.uk/scripts/latlong.html
    String expected = NumberFormat.getNumberInstance(Locale.getDefault()).format(65.99);
    assertThat(String.format("%.2f", nearbyLocation.getDistance()), is(expected));
    assertThat(nearbyLocation.getName(), is("Nearby"));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) Test(org.junit.Test)

Example 9 with PointImpl

use of org.locationtech.spatial4j.shape.impl.PointImpl in project elasticsearch by elastic.

the class ShapeBuilderTests method testGeoCircle.

public void testGeoCircle() {
    double earthCircumference = 40075016.69;
    Circle circle = ShapeBuilders.newCircleBuilder().center(0, 0).radius("100m").build();
    assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
    assertEquals(new PointImpl(0, 0, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
    circle = ShapeBuilders.newCircleBuilder().center(+180, 0).radius("100m").build();
    assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
    assertEquals(new PointImpl(180, 0, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
    circle = ShapeBuilders.newCircleBuilder().center(-180, 0).radius("100m").build();
    assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
    assertEquals(new PointImpl(-180, 0, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
    circle = ShapeBuilders.newCircleBuilder().center(0, 90).radius("100m").build();
    assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
    assertEquals(new PointImpl(0, 90, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
    circle = ShapeBuilders.newCircleBuilder().center(0, -90).radius("100m").build();
    assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
    assertEquals(new PointImpl(0, -90, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
    double randomLat = (randomDouble() * 180) - 90;
    double randomLon = (randomDouble() * 360) - 180;
    double randomRadius = randomIntBetween(1, (int) earthCircumference / 4);
    circle = ShapeBuilders.newCircleBuilder().center(randomLon, randomLat).radius(randomRadius + "m").build();
    assertEquals((360 * randomRadius) / earthCircumference, circle.getRadius(), 0.00000001);
    assertEquals(new PointImpl(randomLon, randomLat, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
}
Also used : Circle(org.locationtech.spatial4j.shape.Circle) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 10 with PointImpl

use of org.locationtech.spatial4j.shape.impl.PointImpl in project ddf by codice.

the class GeoNamesWebService method getNearestCities.

@Override
public List<NearbyLocation> getNearestCities(String locationWkt, int radiusInKm, int maxResults) throws java.text.ParseException, GeoEntryQueryException {
    notNull(locationWkt, "argument locationWkt may not be null");
    Point wktCenterPoint = createPointFromWkt(locationWkt);
    String urlStr = String.format("%s://%s/findNearbyPlaceNameJSON?lat=%f&lng=%f&maxRows=%d&radius=%d&username=%s&cities=cities5000", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, wktCenterPoint.getY(), wktCenterPoint.getX(), limitMaxRows(maxResults), radiusInKm, USERNAME);
    Object result = webQuery(urlStr);
    if (result instanceof JSONObject) {
        JSONObject jsonResult = (JSONObject) result;
        JSONArray geoNames = (JSONArray) jsonResult.get(GEONAMES_KEY);
        if (geoNames != null) {
            return geoNames.stream().map(JSONObject.class::cast).map(obj -> {
                double lat = Double.parseDouble((String) obj.get(LAT_KEY));
                double lon = Double.parseDouble((String) obj.get(LON_KEY));
                String cityName = (String) obj.get(PLACENAME_KEY);
                Point cityPoint = new PointImpl(lon, lat, SpatialContext.GEO);
                return new NearbyLocationImpl(wktCenterPoint, cityPoint, cityName);
            }).collect(toList());
        }
    }
    return Collections.emptyList();
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) LoggerFactory(org.slf4j.LoggerFactory) Validate.notNull(org.apache.commons.lang.Validate.notNull) JSONParser(net.minidev.json.parser.JSONParser) MediaType(javax.ws.rs.core.MediaType) Locale(java.util.Locale) ParseException(net.minidev.json.parser.ParseException) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryQueryable(org.codice.ddf.spatial.geocoding.GeoEntryQueryable) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory) Logger(org.slf4j.Logger) WebClient(org.apache.cxf.jaxrs.client.WebClient) MissingResourceException(java.util.MissingResourceException) Shape(org.locationtech.spatial4j.shape.Shape) StandardCharsets(java.nio.charset.StandardCharsets) Suggestion(org.codice.ddf.spatial.geocoding.Suggestion) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) Collectors.toList(java.util.stream.Collectors.toList) URLEncoder(java.net.URLEncoder) List(java.util.List) Point(org.locationtech.spatial4j.shape.Point) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Optional(java.util.Optional) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Aggregations

PointImpl (org.locationtech.spatial4j.shape.impl.PointImpl)13 Point (org.locationtech.spatial4j.shape.Point)6 Test (org.junit.Test)5 NearbyLocationImpl (org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl)4 NearbyLocation (org.codice.ddf.spatial.geocoding.context.NearbyLocation)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JSONArray (net.minidev.json.JSONArray)2 JSONObject (net.minidev.json.JSONObject)2 GeoEntryQueryException (org.codice.ddf.spatial.geocoding.GeoEntryQueryException)2 PGArray (io.crate.protocols.postgres.types.PGArray)1 UseJdbc (io.crate.testing.UseJdbc)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLEncoder (java.net.URLEncoder)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Array (java.sql.Array)1 Timestamp (java.sql.Timestamp)1 Collections (java.util.Collections)1