Search in sources :

Example 66 with Point

use of org.locationtech.spatial4j.shape.Point in project crate by crate.

the class WherePKIntegrationTest method testEmptyClusteredByExplicit.

@Test
public void testEmptyClusteredByExplicit() throws Exception {
    // regression test that empty routing executes correctly
    execute("create table explicit_routing (" + "  location geo_point, " + "  name string " + ") clustered by (name) with (number_of_replicas=0)");
    ensureYellow();
    execute("insert into explicit_routing (name, location) values (',', [36.567, 52.998]), ('Dornbirn', [54.45, 4.567])");
    execute("refresh table explicit_routing");
    execute("select * from explicit_routing where name=''");
    assertThat(response.cols(), is(arrayContaining("location", "name")));
    assertThat(response.rowCount(), is(0L));
    execute("select * from explicit_routing where name=','");
    assertThat(response.cols(), is(arrayContaining("location", "name")));
    assertThat(response.rowCount(), is(1L));
    Point point = (Point) response.rows()[0][0];
    assertThat(point.getX(), Matchers.closeTo(36.567d, 0.01));
    assertThat(point.getY(), Matchers.closeTo(52.998d, 0.01));
}
Also used : Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Example 67 with Point

use of org.locationtech.spatial4j.shape.Point in project crate by crate.

the class ServerXContentExtension method getXContentWriters.

@Override
public Map<Class<?>, XContentBuilder.Writer> getXContentWriters() {
    Map<Class<?>, XContentBuilder.Writer> writers = new HashMap<>();
    // Fully-qualified here to reduce ambiguity around our (ES') Version class
    writers.put(org.apache.lucene.util.Version.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(DateTimeZone.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(CachedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(FixedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v)));
    writers.put(MutableDateTime.class, XContentBuilder::timeValue);
    writers.put(DateTime.class, XContentBuilder::timeValue);
    writers.put(TimeValue.class, (b, v) -> b.value(v.toString()));
    writers.put(ZonedDateTime.class, XContentBuilder::timeValue);
    writers.put(OffsetDateTime.class, XContentBuilder::timeValue);
    writers.put(OffsetTime.class, XContentBuilder::timeValue);
    writers.put(java.time.Instant.class, XContentBuilder::timeValue);
    writers.put(LocalDateTime.class, XContentBuilder::timeValue);
    writers.put(LocalDate.class, XContentBuilder::timeValue);
    writers.put(LocalTime.class, XContentBuilder::timeValue);
    writers.put(DayOfWeek.class, (b, v) -> b.value(v.toString()));
    writers.put(Month.class, (b, v) -> b.value(v.toString()));
    writers.put(MonthDay.class, (b, v) -> b.value(v.toString()));
    writers.put(Year.class, (b, v) -> b.value(v.toString()));
    writers.put(Duration.class, (b, v) -> b.value(v.toString()));
    writers.put(Period.class, (b, v) -> b.value(v.toString()));
    writers.put(org.joda.time.Period.class, (b, v) -> {
        org.joda.time.Period period = (org.joda.time.Period) v;
        b.value(IntervalType.PERIOD_FORMATTER.print(period));
    });
    writers.put(BytesReference.class, (b, v) -> {
        if (v == null) {
            b.nullValue();
        } else {
            BytesRef bytes = ((BytesReference) v).toBytesRef();
            b.value(bytes.bytes, bytes.offset, bytes.length);
        }
    });
    writers.put(BytesRef.class, (b, v) -> {
        if (v == null) {
            b.nullValue();
        } else {
            BytesRef bytes = (BytesRef) v;
            b.value(bytes.bytes, bytes.offset, bytes.length);
        }
    });
    writers.put(Regproc.class, (b, v) -> b.value(((Regproc) v).name()));
    writers.put(Regclass.class, (b, v) -> b.value(((Regclass) v).oid()));
    writers.put(PointImpl.class, (b, v) -> {
        Point point = (Point) v;
        b.startArray();
        b.value(point.getX());
        b.value(point.getY());
        b.endArray();
    });
    writers.put(JtsPoint.class, (b, v) -> {
        Point point = (Point) v;
        b.startArray();
        b.value(point.getX());
        b.value(point.getY());
        b.endArray();
    });
    writers.put(RowN.class, (b, v) -> {
        RowN row = (RowN) v;
        b.startArray();
        for (int i = 0; i < row.numColumns(); i++) {
            b.value(row.get(i));
        }
        b.endArray();
    });
    writers.put(TimeTZ.class, (b, v) -> {
        TimeTZ timetz = (TimeTZ) v;
        b.startArray();
        b.value(timetz.getMicrosFromMidnight());
        b.value(timetz.getSecondsFromUTC());
        b.endArray();
    });
    writers.put(BitString.class, (b, v) -> {
        BitString bitString = (BitString) v;
        b.value(bitString.asBitString());
    });
    return writers;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) TimeTZ(io.crate.types.TimeTZ) HashMap(java.util.HashMap) Period(java.time.Period) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.spatial4j.shape.Point) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.spatial4j.shape.Point) Regproc(io.crate.types.Regproc) RowN(io.crate.data.RowN) BitString(io.crate.sql.tree.BitString) Regclass(io.crate.types.Regclass) BytesRef(org.apache.lucene.util.BytesRef)

Example 68 with Point

use of org.locationtech.spatial4j.shape.Point 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)

Example 69 with Point

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

the class GeoNamesWebService method createPointFromWkt.

Point createPointFromWkt(String wkt) {
    try {
        JtsSpatialContextFactory contextFactory = new JtsSpatialContextFactory();
        contextFactory.allowMultiOverlap = true;
        SpatialContext spatialContext = contextFactory.newSpatialContext();
        Shape shape = (Shape) spatialContext.readShapeFromWkt(wkt);
        Point center = shape.getCenter();
        return center;
    } catch (java.text.ParseException parseException) {
        LOGGER.debug(parseException.getMessage(), parseException);
    }
    return null;
}
Also used : SpatialContext(org.locationtech.spatial4j.context.SpatialContext) Shape(org.locationtech.spatial4j.shape.Shape) Point(org.locationtech.spatial4j.shape.Point) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory)

Example 70 with Point

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

the class GeoNamesWebServiceTest method testCreateGeoPointFromWkt.

@Test
public void testCreateGeoPointFromWkt() {
    String pointWkt = CREATE_POINT_FROM_WKT_POINT;
    Point p = webService.createPointFromWkt(pointWkt);
    assertThat(p, notNullValue());
    assertThat(p.getX(), equalTo(-1.0));
    assertThat(p.getY(), equalTo(22.0));
    String polygonWkt = CREATE_POINT_FROM_WKT_POLYGON;
    p = webService.createPointFromWkt(polygonWkt);
    assertThat(p, notNullValue());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Aggregations

Point (org.locationtech.spatial4j.shape.Point)71 Test (org.junit.Test)21 Shape (org.locationtech.spatial4j.shape.Shape)15 Query (org.apache.lucene.search.Query)9 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)9 Rectangle (org.locationtech.spatial4j.shape.Rectangle)9 ArrayList (java.util.ArrayList)7 Field (org.apache.lucene.document.Field)6 PointImpl (org.locationtech.spatial4j.shape.impl.PointImpl)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 TopDocs (org.apache.lucene.search.TopDocs)5 IOException (java.io.IOException)4 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)4 Document (org.apache.lucene.document.Document)3 StoredField (org.apache.lucene.document.StoredField)3 IndexReader (org.apache.lucene.index.IndexReader)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 Cell (org.apache.lucene.spatial.prefix.tree.Cell)3 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)3