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;
}
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))));
}
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"));
}
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());
}
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();
}
Aggregations