Search in sources :

Example 1 with SupportedFormats

use of org.locationtech.spatial4j.io.SupportedFormats in project lucene-solr by apache.

the class TestGeoJSONResponseWriter method testTransformToAllFormats.

@Test
public void testTransformToAllFormats() throws Exception {
    String wkt = "POINT( 1 2 )";
    SupportedFormats fmts = SpatialContext.GEO.getFormats();
    Shape shape = fmts.read(wkt);
    String[] check = new String[] { "srpt_geohash", "srpt_geohash", "srpt_quad", "srpt_packedquad", "srptgeom" };
    String[] checkFormats = new String[] { "GeoJSON", "WKT", "POLY" };
    for (String field : check) {
        // Add a document with the given field
        assertU(adoc("id", "test", field, wkt));
        assertU(commit());
        for (String fmt : checkFormats) {
            String json = h.query(req("q", "id:test", "wt", "json", "indent", "true", "fl", "xxx:[geo f=" + field + " w=" + fmt + "]"));
            Map<String, Object> doc = readFirstDoc(json);
            Object v = doc.get("xxx");
            String expect = fmts.getWriter(fmt).toString(shape);
            if (!(v instanceof String)) {
                v = normalizeMapToJSON(v.toString());
                expect = normalizeMapToJSON(expect);
            }
            assertEquals("Bad result: " + field + "/" + fmt, expect, v.toString());
        }
    }
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) SupportedFormats(org.locationtech.spatial4j.io.SupportedFormats) Test(org.junit.Test)

Example 2 with SupportedFormats

use of org.locationtech.spatial4j.io.SupportedFormats in project lucene-solr by apache.

the class GeoJSONWriter method write.

@Override
public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
    String geofield = req.getParams().get(FIELD, null);
    if (geofield == null || geofield.length() == 0) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON.  Missing parameter: '" + FIELD + "'");
    }
    SchemaField sf = req.getSchema().getFieldOrNull(geofield);
    if (sf == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON.  Unknown field: '" + FIELD + "'=" + geofield);
    }
    SupportedFormats formats = null;
    if (sf.getType() instanceof AbstractSpatialFieldType) {
        SpatialContext ctx = ((AbstractSpatialFieldType) sf.getType()).getSpatialContext();
        formats = ctx.getFormats();
    }
    JSONWriter w = new GeoJSONWriter(writer, req, rsp, geofield, formats);
    try {
        w.writeResponse();
    } finally {
        w.close();
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) SolrException(org.apache.solr.common.SolrException) SupportedFormats(org.locationtech.spatial4j.io.SupportedFormats)

Example 3 with SupportedFormats

use of org.locationtech.spatial4j.io.SupportedFormats in project lucene-solr by apache.

the class AbstractSpatialFieldType method init.

@Override
protected void init(IndexSchema schema, Map<String, String> args) {
    super.init(schema, args);
    if (ctx == null) {
        // subclass can set this directly
        final String CTX_PARAM = "spatialContextFactory";
        final String OLD_SPATIAL4J_PREFIX = "com.spatial4j.core";
        final String NEW_SPATIAL4J_PREFIX = "org.locationtech.spatial4j";
        for (Map.Entry<String, String> argEntry : args.entrySet()) {
            // "JTS" is a convenience alias
            if (argEntry.getKey().equals(CTX_PARAM) && argEntry.getValue().equals("JTS")) {
                argEntry.setValue("org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory");
                continue;
            }
            // Warn about using old Spatial4j class names
            if (argEntry.getValue().contains(OLD_SPATIAL4J_PREFIX)) {
                log.warn("Replace '" + OLD_SPATIAL4J_PREFIX + "' with '" + NEW_SPATIAL4J_PREFIX + "' in your schema.");
                argEntry.setValue(argEntry.getValue().replace(OLD_SPATIAL4J_PREFIX, NEW_SPATIAL4J_PREFIX));
            }
        }
        //Solr expects us to remove the parameters we've used.
        MapListener<String, String> argsWrap = new MapListener<>(args);
        ctx = SpatialContextFactory.makeSpatialContext(argsWrap, schema.getResourceLoader().getClassLoader());
        args.keySet().removeAll(argsWrap.getSeenKeys());
    }
    final String distanceUnitsStr = args.remove("distanceUnits");
    if (distanceUnitsStr == null) {
        this.distanceUnits = ctx.isGeo() ? DistanceUnits.KILOMETERS : DistanceUnits.DEGREES;
    } else {
        this.distanceUnits = parseDistanceUnits(distanceUnitsStr);
        if (this.distanceUnits == null)
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Must specify distanceUnits as one of " + DistanceUnits.getSupportedUnits() + " on field types with class " + getClass().getSimpleName());
    }
    final SupportedFormats fmts = ctx.getFormats();
    String format = args.remove(FORMAT);
    if (format == null) {
        format = "WKT";
    }
    shapeWriter = fmts.getWriter(format);
    shapeReader = fmts.getReader(format);
    if (shapeWriter == null) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Shape Format: " + format);
    }
    if (shapeReader == null) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Shape Format: " + format);
    }
    argsParser = newSpatialArgsParser();
}
Also used : MapListener(org.apache.solr.util.MapListener) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException) SupportedFormats(org.locationtech.spatial4j.io.SupportedFormats)

Aggregations

SupportedFormats (org.locationtech.spatial4j.io.SupportedFormats)3 SolrException (org.apache.solr.common.SolrException)2 Map (java.util.Map)1 AbstractSpatialFieldType (org.apache.solr.schema.AbstractSpatialFieldType)1 SchemaField (org.apache.solr.schema.SchemaField)1 MapListener (org.apache.solr.util.MapListener)1 Test (org.junit.Test)1 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)1 Shape (org.locationtech.spatial4j.shape.Shape)1